summaryrefslogtreecommitdiff
path: root/src/ast.ml
blob: f815c83fb8644d1a513f12888ca3f58e3f6a9138 (plain) (blame)
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
(*
	Langages de Programmation et Compilation (J.-C. Filliatre)
	2013-2014
	Alex AUVOLAT

	AST for Mini-C++
*)

module Sset = Set.Make(String)
let type_names = ref Sset.empty

type ident = string
type tident = string

type binop =
	| Equal | NotEqual
	| Lt | Le | Gt | Ge
	| Add | Sub | Mul | Div | Modulo
	| Land | Lor

type unop =
	| PreIncr | PostIncr | PreDecr | PostDecr
	| Ref | Deref
	| Not
	| Minus | Plus

type var_type =
	| TVoid
	| TInt
	| TPtr of var_type
	| TRef of var_type
	| TIdent of tident

type expression =
	| EInt of int
	| EBool of bool
	| ENull
	| EThis
	| EIdent of ident
	| EAssign of expression * expression
	| ECall of expression * expression list
	| EUnary of unop * expression
	| EBinary of expression * binop * expression
	| EMember of expression * ident
	| ENew of ident * expression list

type str_expression =
	| SEExpr of expression
	| SEStr of string

type statement =
	| SEmpty
	| SExpr of expression
	| SIf of expression  * statement * statement
	| SWhile of expression * statement
	| SFor of expression list * expression option * expression list * statement
	| SBlock of block
	| SReturn of expression option
	| SDeclare of var_type * ident
	| SDeclareAssignExpr of var_type * ident * expression
	| SDeclareAssignConstructor of var_type * ident * ident * expression list
			(* Type of variable, variable name, constructor class name, constructor arguments *)
	| SWriteCout of str_expression list
and block = statement list

type proto = {
	p_name : ident;	
	p_class : ident option; (* p_class = none : standalone function *)
	p_ret_type : var_type option; (* p_class = some and p_ret_type = none : constructor *)
	p_args : (var_type * ident) list;
}

type cls_mem =
	| CVar of var_type * ident
	| CMethod of proto
	| CVirtualMethod of proto

type cls = {
	c_name : ident;
	c_supers : ident list option;
	c_members : cls_mem list;
}

type declaration =
	| DGlobal of (var_type * ident)
	| DFunction of (proto * block)
	| DClass of cls

type program = declaration list