📄 compiler.pl
字号:
/* Compiler fuer mini-Pascal */
?- consult(scanner).
compile_programm(Datei):-
parse_programm(Datei, Parsebaum),
retractall(marke(_)),
asserta(marke(1)),
compile_anweisungen(Parsebaum).
gib_marke(Marke):-
retract(marke(Marke)),
Marke2 is Marke + 1,
asserta(marke(Marke2)).
compile_anweisungen([Anweisung|Anweisungen]):-
compile_anweisung(Anweisung), !,
compile_anweisungen(Anweisungen).
compile_anweisungen([]).
compile_anweisung(zuweisung(Variable, Ausdruck)):-
compile_ausdruck(Ausdruck),
schreibnl('mov ', Variable, ', ax').
compile_anweisung(while(Ausdruck, Anweisung)):-
gib_marke(Marke1),
schreib_marke(Marke1),
compile_ausdruck(Ausdruck),
schreibnl('cmp ax, True'),
gib_marke(Marke2),
schreibnl('jnz M', Marke2),
compile_anweisung(Anweisung),
schreibnl('jmp M', Marke1),
schreib_marke(Marke2).
compile_anweisung(if(Ausdruck, Anweisung1, Anweisung2)):-
compile_ausdruck(Ausdruck),
gib_marke(Marke1),
schreibnl('cmp ax, True'),
schreibnl('jnz M', Marke1),
compile_anweisung(Anweisung1),
gib_marke(Marke2),
schreibnl('jmp M', Marke2),
schreib_marke(Marke1),
compile_anweisung(Anweisung2),
schreib_marke(Marke2).
compile_anweisung(begin(Anweisungen)):-
compile_anweisungen(Anweisungen).
compile_anweisung(write(Ausdruck)):-
compile_ausdruck(Ausdruck),
schreibnl('out ax').
compile_anweisung(nil).
compile_ausdruck(Ausdruck1 < Ausdruck2):-
compile_ausdruck(Ausdruck1),
schreibnl('push ax'),
compile_ausdruck(Ausdruck2),
schreibnl('pop cx'),
schreibnl('cmp ax, cx'),
schreibnl('mov ax, True'),
gib_marke(Marke),
schreibnl('jl M', Marke),
schreibnl('mov ax, False'),
schreib_marke(Marke).
compile_ausdruck(Ausdruck1 + Ausdruck2):-
compile_ausdruck(Ausdruck2),
schreibnl('push ax'),
compile_ausdruck(Ausdruck1),
schreibnl('pop cx'),
schreibnl('add ax, cx').
compile_ausdruck(Ausdruck1 - Ausdruck2):-
compile_ausdruck(Ausdruck2),
schreibnl('push ax'),
compile_ausdruck(Ausdruck1),
schreibnl('pop cx'),
schreibnl('sub ax, cx').
compile_ausdruck(Ausdruck1 * Ausdruck2):-
compile_ausdruck(Ausdruck2),
schreibnl('push ax'),
compile_ausdruck(Ausdruck1),
schreibnl('pop cx'),
schreibnl('imul ax, cx').
compile_ausdruck(Ausdruck1 / Ausdruck2):-
compile_ausdruck(Ausdruck2),
schreibnl('push ax'),
compile_ausdruck(Ausdruck1),
schreibnl('pop cx'),
schreibnl('idiv ax, cx').
compile_ausdruck(- Ausdruck):-
compile_ausdruck(Ausdruck), !,
schreibnl('neg ax').
compile_ausdruck(Variable):-
schreibnl('mov ax, ', Variable).
schreibnl(Ausgabe):-
tab(4), write(Ausgabe), nl.
schreibnl(Ausgabe1, Ausgabe2):-
tab(4), write(Ausgabe1), write(Ausgabe2), nl.
schreibnl(Ausgabe1, Ausgabe2, Ausgabe3):-
tab(4), write(Ausgabe1), write(Ausgabe2), write(Ausgabe3), nl.
schreib_marke(Marke):-
write('M'), write(Marke), write(':'), nl.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -