Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 4.33 KB | None | 0 0
  1. (* Abstract Syntax Tree (AST) Definitions for AP++ compiler *)
  2.  
  3. type op = Add | Sub | Mult | Div | Mod | Equal | Neq | Less | Leq | Greater | Geq
  4.           | And | Or
  5.  
  6. type uop = PlusPlusPre | PlusPlusPost | MinusMinusPre | MinusMinusPost | Neg | Not
  7.  
  8. type typ = Int | Bool | Float | String | Void | List of typ
  9.  
  10. type bind = typ * string
  11.  
  12. type expr =
  13.     ILiteral of int
  14.   | BLiteral of bool
  15.   | SLiteral of string
  16.   | FLiteral of float
  17.   | Id of string
  18.   | Binop of expr * op * expr
  19.   | Unop of uop * expr
  20.   | Assign of string * expr
  21.   | Call of string * expr list
  22.   | ListGet of string * expr
  23.   | ListPop of string
  24.   | ListSize of string
  25.   | ListSlice of string * expr * expr
  26.   | ListFind of string * expr
  27.   | ListLiteral of expr list
  28.   | Noexpr
  29.  
  30. type stmt =
  31.   Block of stmt list
  32. | Expr of expr
  33. | Return of expr
  34. | If of expr * stmt * stmt
  35. | For of expr * expr * expr * stmt
  36. | While of expr * stmt
  37. | ListPush of string * expr
  38. | ListSet of string * expr * expr
  39. | ListClear of string
  40. | ListRemove of string * expr
  41. | ListInsert of string * expr * expr
  42. | ListReverse of string
  43.  
  44. type func_decl = {
  45.     typ : typ;
  46.     fname : string;
  47.     formals : bind list;
  48.     locals : bind list;
  49.     body : stmt list;
  50.   }
  51.  
  52. type program = bind list * func_decl list
  53.  
  54. (* Pretty-printing functions *)
  55.  
  56. let string_of_op = function
  57.     Add -> "+"
  58.   | Sub -> "-"
  59.   | Mult -> "*"
  60.   | Div -> "/"
  61.   | Equal -> "=="
  62.   | Neq -> "!="
  63.   | Less -> "<"
  64.   | Leq -> "<="
  65.   | Greater -> ">"
  66.   | Geq -> ">="
  67.   | And -> "&&"
  68.   | Or -> "||"
  69.   | _ -> "?"
  70.  
  71. let string_of_uop = function
  72.     Neg -> "-"
  73.   | Not -> "!"
  74.   | PlusPlusPre -> "++x"
  75.   | PlusPlusPost -> "x++"
  76.   | MinusMinusPre -> "--x"
  77.   | MinusMinusPost -> "x++"
  78.  
  79. let rec string_of_expr = function
  80.     ILiteral(l) -> string_of_int l
  81.   | FLiteral(l) -> string_of_float l
  82.   | BLiteral(true) -> "true"
  83.   | BLiteral(false) -> "false"
  84.   | SLiteral(l) -> l
  85.   | ListGet(id, e) -> "list_get " ^ id ^ ", " ^ (string_of_expr e)
  86.   | ListPop (id) -> "list_pop " ^ id
  87.   | ListSize(id) -> "list_size " ^ id
  88.   | ListSlice(id, e1, e2) -> "list_slice " ^ id ^ ", " ^ (string_of_expr e1) ^ ", " ^ (string_of_expr e2)
  89.   | ListFind(id, e) -> "list_find " ^ id ^ ", " ^ (string_of_expr e)
  90.   | ListLiteral(_) -> "list_literal"
  91.   | Id(s) -> s
  92.   | Binop(e1, o, e2) ->
  93.       string_of_expr e1 ^ " " ^ string_of_op o ^ " " ^ string_of_expr e2
  94.   | Unop(o, e) -> string_of_uop o ^ string_of_expr e
  95.   | Assign(v, e) -> v ^ " = " ^ string_of_expr e
  96.   | Call(f, el) ->
  97.       f ^ "(" ^ String.concat ", " (List.map string_of_expr el) ^ ")"
  98.   | Noexpr -> ""
  99.  
  100. let rec string_of_stmt = function
  101.     Block(stmts) ->
  102.       "{\n" ^ String.concat "" (List.map string_of_stmt stmts) ^ "}\n"
  103.   | Expr(expr) -> string_of_expr expr ^ ";\n";
  104.   | Return(expr) -> "return " ^ string_of_expr expr ^ ";\n";
  105.   | If(e, s, Block([])) -> "if (" ^ string_of_expr e ^ ")\n" ^ string_of_stmt s
  106.   | If(e, s1, s2) ->  "if (" ^ string_of_expr e ^ ")\n" ^
  107.       string_of_stmt s1 ^ "else\n" ^ string_of_stmt s2
  108.   | For(e1, e2, e3, s) ->
  109.       "for (" ^ string_of_expr e1  ^ " ; " ^ string_of_expr e2 ^ " ; " ^
  110.       string_of_expr e3  ^ ") " ^ string_of_stmt s
  111.   | While(e, s) -> "while (" ^ string_of_expr e ^ ") " ^ string_of_stmt s
  112.   | ListPush(id, e) -> "list_push " ^ id ^ ", " ^ string_of_expr e
  113.   | ListSet(id, e1, e2) -> "list_set " ^ id ^ ", " ^ (string_of_expr e1) ^ ", " ^ (string_of_expr e2)
  114.   | ListClear(id) -> "list_clear " ^ id
  115.   | ListRemove(id, e) -> "list remove " ^ id ^ ", " ^ (string_of_expr e)
  116.   | ListInsert(id, e1, e2) -> "list_insert " ^ id ^ ", " ^ (string_of_expr e1) ^ ", " ^ (string_of_expr e2)
  117.   | ListReverse(id) -> "list_rev " ^ id
  118.  
  119. let rec string_of_typ = function
  120.     Int -> "int"
  121.   | Bool -> "bool"
  122.   | Float -> "float"
  123.   | Void -> "void"
  124.   | String -> "string"
  125.   | List x -> "list<" ^ (string_of_typ x) ^ ">"
  126.  
  127. let string_of_vdecl (t, id) = string_of_typ t ^ " " ^ id ^ ";\n"
  128.  
  129. let string_of_fdecl fdecl =
  130.   string_of_typ fdecl.typ ^ " " ^
  131.   fdecl.fname ^ "(" ^ String.concat ", " (List.map snd fdecl.formals) ^
  132.   ")\n{\n" ^
  133.   String.concat "" (List.map string_of_vdecl fdecl.locals) ^
  134.   String.concat "" (List.map string_of_stmt fdecl.body) ^
  135.   "}\n"
  136.  
  137. let string_of_program (vars, funcs) =
  138.   String.concat "" (List.map string_of_vdecl vars) ^ "\n" ^
  139.   String.concat "\n" (List.map string_of_fdecl funcs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement