forked from ManuelCPinto/mini-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.ml
More file actions
97 lines (77 loc) · 2.29 KB
/
Copy pathast.ml
File metadata and controls
97 lines (77 loc) · 2.29 KB
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
(** {2 Abstract Syntax of Mini-Python} *)
(** {3 Parsed trees}
This is the output of the parser and the input of the type checker. *)
type location = Lexing.position * Lexing.position
type ident = { loc: location; id: string; }
type unop =
| Uneg (** -e *)
| Unot (** not e *)
type binop =
| Badd | Bsub | Bmul | Bdiv | Bmod (** + - * // % *)
| Beq | Bneq | Blt | Ble | Bgt | Bge (** == != < <= > >= *)
| Band | Bor (** and or *)
type constant =
| Cnone
| Cbool of bool
| Cstring of string
| Cint of int64
type expr =
| Ecst of constant
| Eident of ident
| Ebinop of binop * expr * expr
| Eunop of unop * expr
| Ecall of expr * expr list
| Elist of expr list (** {[ [e1,e2,...] ]} *)
| Eget of expr * expr (** {[ e1[e2] ]} *)
| Elambda of ident list * expr
and stmt =
| Sif of expr * stmt * stmt
| Sreturn of expr
| Sassign of ident * expr
| Sprint of expr
| Sblock of stmt list
| Swhile of expr * stmt
| Sfor of ident * expr * stmt
| Seval of expr
| Sset of expr * expr * expr (** {[ e1[e2] = e3 ]} *)
and def = ident * ident list * stmt
and file = def list * stmt
(** {3 Typed trees}
This is the output of the type checker and the input of the code
generation. *)
(** In the typed trees, all the occurrences of the same variable
point to a single record of the following type. *)
type var = {
v_name: string;
mutable v_ofs: int; (** position wrt %rbp *)
}
(** Similarly, all the occurrences of a given function all point
to a single record of the following type. *)
type fn = {
fn_name: string;
fn_params: var list;
fn_anoymous: bool;
}
type texpr =
| TEcst of constant
| TEvar of var
| TEfn of fn
| TEbinop of binop * texpr * texpr
| TEunop of unop * texpr
| TEcall of texpr * texpr list
| TElist of texpr list
| TErange of texpr (** list(range(e1)) *)
| TEget of texpr * texpr (** {[ e1[e2] ]} *)
type tstmt =
| TSif of texpr * tstmt * tstmt
| TSreturn of texpr
| TSassign of var * texpr
| TSprint of texpr
| TSblock of tstmt list
| TSfor of var * texpr * tstmt
| TSwhile of texpr * tstmt
| TSeval of texpr
| TSset of texpr * texpr * texpr (** {[ e1[e2] = e3 ]} *)
and tdef = fn * tstmt
and tfile = tdef list
(** the block of global statements is now a `main` function *)