diff --git a/ast.ml b/ast.ml index 0f34389..4345834 100644 --- a/ast.ml +++ b/ast.ml @@ -36,6 +36,7 @@ and stmt = | Sfor of stmt * expr * stmt * block | Sprint of expr | Sexit + | Sskip and block = | Bstmt of stmt diff --git a/compiler.ml b/compiler.ml index c937630..a9aad8d 100644 --- a/compiler.ml +++ b/compiler.ml @@ -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 diff --git a/lexer.mll b/lexer.mll index a2f5679..1ca4833 100644 --- a/lexer.mll +++ b/lexer.mll @@ -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)) } diff --git a/parser.mly b/parser.mly index a794bfa..dcec52a 100644 --- a/parser.mly +++ b/parser.mly @@ -11,7 +11,7 @@ %token 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 @@ -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 : diff --git a/tests/t12.txt b/tests/t12.txt new file mode 100644 index 0000000..1c41c48 --- /dev/null +++ b/tests/t12.txt @@ -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