Skip to content

Commit

Permalink
completed (modulo the parser)
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@700 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Nov 6, 2009
1 parent 3abaf04 commit e2963ee
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
17 changes: 16 additions & 1 deletion topics/exercises/while3/bigStm.pro
@@ -1,15 +1,19 @@
% Big-step transition relation for statement execution
%% Big-step transition relation for statement execution

% Skip statement
execute(skip,M,M).

% Sequential composition
execute(slist(S1,S2),M1,M3) :-
execute(S1,M1,M2),
execute(S2,M2,M3).

% Assignment
execute(assign(identifier(X),A),M1,M2) :-
evala(A,M1,Y),
update(M1,X,Y,M2).

% Conditional statement
execute(ifthenelse(B,S1,S2),M1,M2) :-
evalb(B,M1,X),
(
Expand All @@ -20,4 +24,15 @@ execute(ifthenelse(B,S1,S2),M1,M2) :-
execute(S2,M1,M2)
).

% Loop statement
execute(while(B,S),M1,M3) :-
evalb(B,M1,X),
(
X == tt,
execute(S,M1,M2),
execute(while(B,S),M2,M3)
;
X == ff,
M3 = M1
).

13 changes: 10 additions & 3 deletions topics/exercises/while3/compExpr.pro
@@ -1,5 +1,4 @@
%% Evaluate arithmetic expressions

% Number is evaluated to its value
evala(number(V),_,V).

Expand All @@ -25,7 +24,6 @@ evala(mul(A1,A2),M,V) :-
V is V1 * V2.

%% Evaluate Boolean expressions

% True is tt
evalb(true,_,tt).

Expand All @@ -37,13 +35,19 @@ evalb(not(B),M,V) :-
evalb(B,M,V1),
not(V1,V).

% Conjunction
evalb(and(B1,B2),M,V) :-
evalb(B1,M,V1),
evalb(B2,M,V2),
and(V1,V2,V).

% Test for equality
evalb(equals(A1,A2),M,V) :-
evala(A1,M,V1),
evala(A2,M,V2),
equals(V1,V2,V).

% Test for equality
% Test for being less than or equal
evalb(lte(A1,A2),M,V) :-
evala(A1,M,V1),
evala(A2,M,V2),
Expand All @@ -56,4 +60,7 @@ lte(V1,V2,tt) :- V1 =< V2.
lte(V1,V2,ff) :- \+ V1 =< V2.
not(tt,ff).
not(ff,tt).
and(tt,tt,tt).
and(ff,_,ff).
and(_,ff,ff).

9 changes: 8 additions & 1 deletion topics/exercises/while3/input.txt
@@ -1 +1,8 @@
y:=2; if y = 3 then z := y else z := 1000
y:=2;
x:=(y+4);
if (y = 2 ^ x = 6)
then
z := y
else
z := 1000;
while x≤10 do x := (x + 1)
2 changes: 1 addition & 1 deletion topics/exercises/while3/mappings.pro
Expand Up @@ -4,6 +4,6 @@ lookup(M,X,Y) :- append(_,[(X,Y)|_],M).
% Function update in one position
update([],X,Y,[(X,Y)]).
update([(X,_)|M],X,Y,[(X,Y)|M]).
update([(X1,_)|M1],X2,Y2,[(X1,Y2)|M2]) :- \+ X1 = X2, update(M1,X2,Y2,M2).
update([(X1,Y1)|M1],X2,Y2,[(X1,Y1)|M2]) :- \+ X1 = X2, update(M1,X2,Y2,M2).


0 comments on commit e2963ee

Please sign in to comment.