Last active
September 30, 2016 10:51
-
-
Save hsk/3c6998d82b26598ed63b07e032c651b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- initialization(main). | |
eval1(Ctx,L,Ctx_,V) :- print_term(L,[]),nl,eval(Ctx,L,Ctx_,V),!. | |
eval(Ctx,["set",X,E],[X=V|Ctx_],null) :- eval1(Ctx, E, Ctx_, V). | |
eval(Ctx,["get",X],Ctx,V) :- member(X=V, Ctx). | |
eval(Ctx,["+",E1,E2],Ctx2,V) :- eval1(Ctx,E1,Ctx1,V1),eval1(Ctx1,E2,Ctx2,V2), V is V1 + V2. | |
eval(Ctx,["=",E1,E2],Ctx2,V) :- eval1(Ctx,E1,Ctx1,V1),eval1(Ctx1,E2,Ctx2,V2), (V1 = V2,V=true;V=false). | |
eval(Ctx,["until",E1,E2],Ctx3,null) :- | |
eval1(Ctx,E1,Ctx1,V1), | |
(V1=true,Ctx3=Ctx1; eval1(Ctx1,E2,Ctx2,_),eval1(Ctx2,["until",E1,E2],Ctx3,_)). | |
eval(Ctx,["step"|[]],Ctx,null). | |
eval(Ctx,["step"|[E1]],Ctx1,V) :- eval1(Ctx,E1,Ctx1,V). | |
eval(Ctx,["step"|[E1|Es]],Ctx2,V) :- eval1(Ctx,E1,Ctx1,_),eval1(Ctx1,["step"|Es],Ctx2,V). | |
eval(Ctx,V,Ctx,V). | |
main :- | |
Source = ["step", | |
["set", "i", 10], | |
["set", "sum", 0], | |
["until", ["=", ["get", "i"], 0], [ | |
"step", | |
["set", "sum", ["+", ["get", "sum"], ["get", "i"]]], | |
["set", "i", ["+", ["get", "i"], -1]] | |
]], | |
["get", "sum"] | |
], | |
eval1([],Source,Ctx,V),writeln(V),halt. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment