diff --git a/baselib.ml b/baselib.ml index 2110b03..610e304 100644 --- a/baselib.ml +++ b/baselib.ml @@ -2,11 +2,19 @@ open Ast open Mips module Env = Map.Make (String) -let _types_ = Env.of_seq (List.to_seq [ "%add", Func_t (Int_t, [ Int_t; Int_t ]) ]) +let _types_ = + Env.of_seq + (List.to_seq + [ "%add", Func_t (Int_t, [ Int_t; Int_t ]) + ; "%mul", Func_t (Int_t, [ Int_t; Int_t ]) + ]) +;; let builtins = List.fold_left (fun env (fn, impl) -> Env.add fn impl env) Env.empty - [ "%add", [ Lw (T0, Mem (SP, 0)); Lw (T1, Mem (SP, 4)); Add (V0, T0, T1) ] ] + [ "%add", [ Lw (T0, Mem (SP, 0)); Lw (T1, Mem (SP, 4)); Add (V0, T0, T1) ] + ; "%mul", [ Lw (T0, Mem (SP, 0)); Lw (T1, Mem (SP, 4)); Mul (V0, T0, T1) ] + ] ;; diff --git a/lexer.mll b/lexer.mll index cd3c9d8..5294e76 100644 --- a/lexer.mll +++ b/lexer.mll @@ -21,7 +21,8 @@ rule token = parse | bool as b { Lbool (bool_of_string b) } | '=' { Lassign } | ';' { Lsc } -| '+' { Ladd} +| '+' { Ladd } +| '*' { Lmul } | ident as i { Lvar i } | '#' { comment lexbuf } | _ as c { raise (Error c) } diff --git a/parser.mly b/parser.mly index 9848b7b..74df4af 100644 --- a/parser.mly +++ b/parser.mly @@ -8,9 +8,9 @@ %token Ltype %token Lvar %token Lend Lassign Lsc Lreturn -%token Ladd +%token Ladd Lmul -%left Ladd +%left Ladd Lmul %start prog @@ -49,4 +49,7 @@ expr: | a = expr ; Ladd ; b = expr { Call { func = "%add" ; args = [ a ; b ] ; pos = $startpos($2) } } + | a = expr ; Lmul ; b = expr { + Call { func = "%mul" ; args = [ a ; b ] ; pos = $startpos($2) } + } ; diff --git a/tests/10_add-mul.test b/tests/10_add-mul.test new file mode 100644 index 0000000..1a7bbe7 --- /dev/null +++ b/tests/10_add-mul.test @@ -0,0 +1 @@ +int res = 13 * 100 + 12; diff --git a/tests/10_add.test b/tests/10_add.test deleted file mode 100644 index cffa17b..0000000 --- a/tests/10_add.test +++ /dev/null @@ -1 +0,0 @@ -int res = 1 + 1;