This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast.ml
105 lines (97 loc) · 2.26 KB
/
ast.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
(*
A faire:
Floats, assignation,
Ajouter les positions dans le fichier source.
*)
type pos = int
type id = string
and expr =
| Unit
| False
| True
(*| Float of float *)
| Int of int
| Id of id
| Op of (op * expr * expr)
| Apply of (id * expr list)
| If of (expr * expr * expr)
| Let of (id * expr * expr)
| Seq of (expr * expr)
| Fun of (id * id list * expr)
| Lambda of (id list * expr)
(* Statements/Instruction.
* Return ()
* like assignation
* Other design ML-style.
* let <ID> = <Expr> in <Expr>; <- Mandatory
* ^ ^
* | Explicit block
* Optional mut?
*)
and op =
| Mult
| Sub
| Add
| Div
| Mod
| Le
| Ge
| Gt
| Lt
| Neq
| Eq
let string_of_op = function
| Mult -> "*"
| Sub -> "-"
| Add -> "+"
| Div -> "/"
| Mod -> "mod"
| Lt -> "<"
| Gt -> ">"
| Ge -> ">="
| Le -> "<="
| Neq -> "!="
| Eq -> "="
let string_of_id (identif: id) : string = identif
let rec string_of_expr = function
| Unit -> "()"
| False -> "False"
| True -> "True"
| Int i -> string_of_int i
(*| Float f -> string_of_float f *)
| Id id -> string_of_id id
| Op (op, left, right) ->
(Printf.sprintf "(%s %s %s)"
(string_of_expr left)
(string_of_op op)
(string_of_expr right))
| Apply (ident, args) ->
(Printf.sprintf "%s %s"
(string_of_id ident)
(List.map string_of_expr args
|> String.concat " "))
| If (pred, then', else') ->
(Printf.sprintf "if %s { %s } else { %s }"
(string_of_expr pred)
(string_of_expr then')
(string_of_expr else'))
| Let (id, init, body) ->
(Printf.sprintf "let %s = %s in %s end"
id
(string_of_expr init)
(string_of_expr body))
| Seq (left, right) ->
(Printf.sprintf "%s ; %s"
(string_of_expr left)
(string_of_expr right))
| Fun (id, args, body) ->
(Printf.sprintf "fun %s %s {\n%s\n}"
(string_of_id id)
(List.map string_of_id args |> String.concat " ")
(string_of_expr body))
| Lambda (args, body) ->
(Printf.sprintf "fn %s {\n%s\n}"
(List.map string_of_id args |> String.concat " ")
(string_of_expr body))
(* TODO: implem it really. *)
let string_of_program = string_of_expr