Просмотр исходного кода

Added C preprocessor to parser + quickfix to make code work

Taddeus Kroes 12 лет назад
Родитель
Сommit
3747dc9846
8 измененных файлов с 48 добавлено и 45 удалено
  1. 1 0
      .gitignore
  2. 5 0
      Makefile
  3. 28 28
      ast.ml
  4. 3 6
      parser.cpp.mly
  5. 3 3
      phases/desug.ml
  6. 5 3
      phases/parse.ml
  7. 1 1
      stringify.ml
  8. 2 4
      util.ml

+ 1 - 0
.gitignore

@@ -9,3 +9,4 @@ civicc
 lexer.ml
 parser.ml
 parser.mli
+parser.mly

+ 5 - 0
Makefile

@@ -10,6 +10,11 @@ YFLAGS := --infer
 
 all: native-code
 
+parser.mly: parser.cpp.mly
+	cpp -o $@ $<
+	line=`grep -m 1 -n '%{' $@ | head -c -4`; \
+	sed -i "1,$$(($$line - 1))d" $@
+
 clean:: myclean
 
 .PHONY: myclean

+ 28 - 28
ast.ml

@@ -10,38 +10,38 @@ type ctype = Void | Bool | Int | Float
 and node =
     (* global *)
     | Program of node list * loc
-    | Param of ctype * string * loc
-    | FunDec of ctype * string * node list * loc
-    | FunDef of bool * ctype * string * node list * node list * loc
-    | GlobalDec of ctype * string * loc
-    | GlobalDef of bool * ctype * string * node option * loc
+    | Param of ctype * string
+    | FunDec of ctype * string * node list
+    | FunDef of bool * ctype * string * node list * node list
+    | GlobalDec of ctype * string
+    | GlobalDef of bool * ctype * string * node option
 
     (* statements *)
-    | VarDec of ctype * string * node option * loc
-    | Assign of string * node * loc
-    | Return of node * loc
-    | If of node * node list * loc
-    | IfElse of node * node list * node list * loc
-    | While of node * node list * loc
-    | DoWhile of node * node list * loc
-    | For of string * node * node * node * node list * loc
-    | Allocate of string * node list * loc
-    | Expr of node * loc
-    | Statements of node list * loc
+    | VarDec of ctype * string * node option
+    | Assign of string * node
+    | Return of node
+    | If of node * node list
+    | IfElse of node * node list * node list
+    | While of node * node list
+    | DoWhile of node * node list
+    | For of string * node * node * node * node list
+    | Allocate of string * node list
+    | Expr of node
+    | Statements of node list
 
     (* expressions *)
-    | BoolConst of bool * loc
-    | IntConst of int * loc
-    | FloatConst of float * loc
-    | ArrayConst of node list * loc
-    | ArrayScalar of node * loc
-    | Var of string * loc
-    | Deref of string * node list * loc
-    | Monop of monop * node * loc
-    | Binop of binop * node * node * loc
-    | Cond of node * node * node * loc
-    | TypeCast of ctype * node * loc
-    | FunCall of string * node list * loc
+    | BoolConst of bool
+    | IntConst of int
+    | FloatConst of float
+    | ArrayConst of node list
+    | ArrayScalar of node
+    | Var of string
+    | Deref of string * node list
+    | Monop of monop * node
+    | Binop of binop * node * node
+    | Cond of node * node * node
+    | TypeCast of ctype * node
+    | FunCall of string * node list
 
 (* intermediate representations between phases *)
 type repr =

+ 3 - 6
parser.mly → parser.cpp.mly

@@ -1,10 +1,7 @@
+#define LOC ("", 0, 0, 0, 0)
+
 %{
 open Ast
-
-let loc lexbuf =
-    (0, 0, 0, 0)
-    sprintf "%s:%d:%d" pos.pos_fname pos.pos_lnum
-                       (pos.pos_cnum - pos.pos_bol + 1)
 %}
 
 /* Tokens */
@@ -48,7 +45,7 @@ basic_type : FLOAT      { Float }
            | BOOL       { Bool }
 
 program : decl*; EOF
-          { Program $1 }
+          { Program ($1, LOC) }
 
 decl : EXTERN; fun_header; SEMICOL
        { let (t, n, p) = $2 in FunDec(t, n, p) }

+ 3 - 3
phases/desug.ml

@@ -45,7 +45,7 @@ let rec var_init = function
     | GlobalDef (export, ctype, name, Some init) ->
         Statements [GlobalDef (export, ctype, name, None); Assign (name, init)]
 
-    | Program decls ->
+    | Program (decls, l) ->
         let decls = flatten (List.map var_init decls) in
         let rec trav assigns = function
             | [] -> (assigns, [])
@@ -56,10 +56,10 @@ let rec var_init = function
         in
         let (assigns, decls) = trav [] decls in
         (match assigns with
-            | [] -> Program decls
+            | [] -> Program (decls, l)
             | assigns ->
                 let init_func = FunDef (true, Void, "__init", [], assigns) in
-                Program (init_func :: decls)
+                Program (init_func :: decls, l)
             )
 
     | node -> transform var_init node

+ 5 - 3
phases/parse.ml

@@ -7,19 +7,21 @@ let get_position lexbuf =
     sprintf "%s:%d:%d" pos.pos_fname pos.pos_lnum
                        (pos.pos_cnum - pos.pos_bol + 1)
 
+(*
 let get_loc lexbuf =
     let pos = lexbuf.lex_curr_p in
     let colnum = (pos.pos_cnum - pos.pos_bol + 1) in
-    Loc (pos.pos_fname, pos.pos_lnum, , colnum, )
     sprintf "%s:%d:%d" pos.pos_fname pos.pos_lnum
                        (pos.pos_cnum - pos.pos_bol + 1)
+*)
 
 let parse_with_error lexbuf =
     try Some (Parser.program Lexer.token lexbuf) with
     | Lexer.SyntaxError msg ->
-        raise (CompileError (sprintf "%s: %s" (get_loc lexbuf) msg))
+        raise (CompileError (sprintf "%s: %s" (get_position lexbuf) msg))
     | Parser.Error ->
-        raise (LocError ("syntax error" (get_loc lexbuf)))
+        raise (CompileError (sprintf "%s: syntax error" (get_position lexbuf)))
+        (*raise (LocError ("syntax error" (get_loc lexbuf)))*)
 
 let phase repr =
     print_endline "- Parse input";

+ 1 - 1
stringify.ml

@@ -121,7 +121,7 @@ and node2str node =
     let concat sep nodes = String.concat sep (List.map node2str nodes) in
     match node with
     (* Global *)
-    | Program decls ->
+    | Program (decls, _) ->
         let decl2str decl = String.concat "\n" (node2lines decl) in
         String.concat "\n\n" (List.map decl2str decls)
     | Param (param_type, name) -> (type2str param_type) ^ " " ^ name

+ 2 - 4
util.ml

@@ -12,8 +12,8 @@ let rec transform visitor node =
     let trav = visitor in
     let trav_all nodes = List.map trav nodes in
     match node with
-    | Program (decls) ->
-        Program (trav_all decls)
+    | Program (decls, l) ->
+        Program (trav_all decls, l)
     | FunDec (ret_type, name, params) ->
         FunDec (ret_type, name, trav_all params)
     | FunDef (export, ret_type, name, params, body) ->
@@ -55,7 +55,5 @@ let rec transform visitor node =
 
     | Statements (stats) ->
         Statements (trav_all stats)
-    | Loc (node, loc) ->
-        Loc (trav node, loc)
 
     | _ -> node