-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjlang.ml
115 lines (89 loc) · 2.74 KB
/
objlang.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(** Annotated abstract syntax for the OBJEling language. *)
(* Types of SIMP values *)
type typ =
| TInt
| TBool
| TClass of string (* class type, identified by its name *)
| TArray of typ (* array containing elements of the specified type *)
| TVoid
(* not an actual type in the source language, but having it in
the AST makes the code more uniform *)
let rec string_of_typ t =
match t with
| TInt ->
"int"
| TBool ->
"bool"
| TClass name ->
name
| TArray typ ->
"Array<" ^ string_of_typ typ ^ ">"
| TVoid ->
"void"
type binop = Add | Mul | Lt
type 'a expression = {annot: 'a; expr: 'a expr}
and 'a expr =
| Cst of int
| Bool of bool
| Var of string
| Binop of binop * 'a expression * 'a expression
| Call of string * 'a expression list
| MCall of 'a expression * string * 'a expression list
| New of
string
* 'a expression list (* create an instance and call the constructor *)
| NewTab of
typ * 'a expression (* create an array of the given type and size *)
| Read of 'a mem (* read in memory *)
| This (* current object *)
and 'a mem =
| Arr of 'a expression * 'a expression (* array access e1[e2] *)
| Atr of 'a expression * string (* attribute access o.x *)
let mk_expr a e = {annot= a; expr= e}
(** literal varilable name as local varilable when injecting this as a local
varilable *)
let this_variable_name = "this"
type 'a instruction =
| Putchar of 'a expression
| Set of string * 'a expression
| If of 'a expression * 'a sequence * 'a sequence
| While of 'a expression * 'a sequence
| Return of 'a expression
| Expr of 'a expression
| Write of 'a mem * 'a expression (* m = e; *)
and 'a sequence = 'a instruction list
(* Function definition *)
type 'a function_def =
{ name: string
; params: (string * typ) list
; locals: (string * typ) list
; code: 'a sequence
; return: typ }
(* Class definition *)
type 'a class_def =
{ name: string
; fields: (string * typ) list
; methods: 'a function_def list
; parent: string option }
(* Program as in IMP + types + user-defined *)
type 'a program =
{ globals: (string * typ) list
; functions: 'a function_def list
; classes: 'a class_def list }
(* expression *)
type untyped_expression = unit expression
type typed_expression = typ expression
(* instruction *)
type untyped_instruction = unit instruction
type typed_instruction = typ instruction
type untyped_sequence = unit sequence
type typed_sequence = typ sequence
(* function *)
type untyped_function = unit function_def
type typed_function = typ function_def
(* class *)
type untyped_class = unit class_def
type typed_class = typ class_def
(* program *)
type untyped_program = unit program
type typed_program = typ program