Skip to content

Commit

Permalink
Add skip statement
Browse files Browse the repository at this point in the history
  • Loading branch information
epatrizio committed Mar 18, 2022
1 parent a222946 commit f53e2c9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and stmt =
| Sfor of stmt * expr * stmt * block
| Sprint of expr
| Sexit
| Sskip

and block =
| Bstmt of stmt
Expand Down
3 changes: 2 additions & 1 deletion compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ let rec compile_stmt ?(label = "") s env li =
compile_expr e env 0 li @ labeled_inst ~label:("wcond"^sct) ("BRANCHIFNOT wdone"^sct) @ compile_block b env li @ compile_expr e env 0 li @ ["BRANCH wcond" ^ sct] @ labeled_inst ~label:("wdone"^sct) ""
| Sfor (s1,e,s2,b) -> compile_stmt s1 env li @ compile_stmt (Swhile (e, Bseq_r (b,s2))) env li
| Sprint e -> (compile_expr ~label:label e env 0 li) @ ["PRIM print"]
| Sexit -> ["STOP"] @ li
| Sexit -> labeled_inst ~label:label ("STOP") @ li
| Sskip -> labeled_inst ~label:label ("CONST 0") @ li

and compile_block ?(label = "") b env li =
match b with
Expand Down
1 change: 1 addition & 0 deletions lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ rule token = parse
| "print" { PRINT }
| "array_size" { ARRAY_SIZE }
| "exit" { EXIT }
| "skip" { SKIP }
| "(*" { comment lexbuf }
| boolean as b { CST (Cbool (bool_of_string b)) }
| integer as s { CST (Cint (int_of_string s)) }
Expand Down
3 changes: 2 additions & 1 deletion parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

%token <Ast.constant> CST
%token LET IN REF BEGIN END IF THEN ELSE WHILE DO DONE FOR AND OR NOT
%token PRINT ARRAY_SIZE EXIT
%token PRINT ARRAY_SIZE EXIT SKIP
%token EOF
%token COMMA SEMICOLON EXCL LP RP LSQ RSQ LCU RCU
%token EQUAL REF_EQUAL CMP_EQ CMP_NEQ CMP_LT CMP_LE CMP_GT CMP_GE
Expand Down Expand Up @@ -45,6 +45,7 @@ stmt :
| FOR s1=stmt SEMICOLON e=expr SEMICOLON s2=stmt DO b=block DONE { Ast.Sfor (s1, e, s2, b) }
| PRINT e=expr { Ast.Sprint e }
| EXIT { Ast.Sexit }
| SKIP { Ast.Sskip }
;

block :
Expand Down
13 changes: 13 additions & 0 deletions tests/t12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let i = (ref ()) in
begin
print 42;
skip;
print 27;
for i:=1 ; ((!i)<=10) ; i:=((!i)+1) do
if ((!i) < 5) then
print (!i)
else
skip
done;
print 42
end

0 comments on commit f53e2c9

Please sign in to comment.