Commits

Anonymous committed 238cc2b

version 1.0

Comments (0)

Files changed (4)

   List.iter (fun (v_names, ty) -> List.iter (gen_data ty) v_names) decls
     
 let add out = function
-  | A_Plus -> out "\tadd ebx"
-  | A_Minus -> out "TODO minus"
+  | A_Plus -> out "\tadd eax,ebx"
+  | A_Minus -> out "\tsub eax,ebx"
 let mul out = function
-  | M_Mult -> out "\tmul ebx"
-  | M_Div -> out "\tdiv ebx"
+  | M_Mult -> out "\tmul ?eax,ebx"
+  | M_Div -> out "\tmov edx,0"; out "\tdiv ebx"
 let rec push_expr out = function
   | E_Tm tm -> push_term out tm
   | Ex (add_op, tm, ex) ->
+      push_expr out ex;
       push_term out tm;
-      push_expr out ex;
-      out @@ "pop ebx"; out @@ "pop eax";
-      add out add_op
+      out "\tpop ebx"; out "\tpop eax";
+      add out add_op;
+      out "ADD\tpush eax"
 and push_term out = function
   | T_Fc f -> push_factor out f
   | Tm (mul_op, fc, tm) ->
+      push_term out tm;
       push_factor out fc;
-      push_term out tm;
-      out @@ "pop ebx"; out @@ "pop eax";
-      mul out mul_op
+      out "\tpop ebx"; out "\tpop eax";
+      mul out mul_op;
+      out "\tpush eax"
 and push_factor out = function
   | F_Li (Ident x) -> out @@ "\tpush " ^ x
   | F_Li (Number x) -> out @@ "\tmov edx," ^ x; out "\tpush edx"
 		| pos', l, "STRING" -> (pos, Type STRING) :: loop pos' l
 		| pos', l, w -> (pos, Literal(Ident w)) :: loop pos' l
               end
-	  | _ -> raise @@ LexError(pos, "unknown token: '" ^ string1 c ^ "..'")
+	  | _ -> raise @@ LexError(pos, "unknown token: '" ^ string1 c ^ "'")
 	end
     with
     | End_of_file -> []
     let ast = Parser.parse tokens in
     Gencode.gen ast
   with
-  | Invalid_argument _ ->
-      prerr_endline @@ "no input files"
-  | Lexer.LexError(pos, msg) ->
-      prerr_endline @@ "Lex error: " ^ spos pos ^ ": " ^ msg
-  | Parser.ParseError(Some pos, msg) ->
-      prerr_endline @@ "Parse error: " ^ spos pos ^ ": " ^ msg
-  | Parser.ParseError(None, msg) ->
-      prerr_endline @@ "Parse error: last" ^ ": " ^ msg
-  | err -> prerr_endline @@ "mistake of compiler " ^ Printexc.to_string err
+  | Invalid_argument _ -> prerr_endline @@ "no input files"
+  | Lexer.LexError(pos, msg) -> prerr_endline @@ "Lex error: " ^ spos pos ^ ": " ^ msg
+  | Parser.ParseError(Some pos, msg) -> prerr_endline @@ "Parse error: " ^ spos pos ^ ": " ^ msg
+  | Parser.ParseError(None, msg) -> prerr_endline @@ "Parse error: last" ^ ": " ^ msg
+  | other_err -> prerr_endline @@ "mistake of compiler " ^ Printexc.to_string other_err
 (*  | Neq | Lt | Le | Eq | Gt | Ge*)
     | _ -> "_"
 
-let spos pos = "line " ^ ~~(pos.pline) ^ ", characters " ^ ~~(pos.pmin) ^ "-"
+let spos pos = "line " ^ ~~(pos.pline) ^ ", characters " ^ ~~(pos.pmin-1) ^ "-"
 let stoken (pos,t) = "(" ^ spos pos ^ ":" ^ st t ^ ")"