-
Notifications
You must be signed in to change notification settings - Fork 4
/
expr.ml
73 lines (59 loc) · 2.25 KB
/
expr.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
(*
CS 51 Final Project
MiniML -- Expressions
Spring 2017
*)
(* Abstract syntax of MiniML expressions *)
type unop =
| Negate
;;
type binop =
| Plus
| Minus
| Times
| Equals
| LessThan
;;
type expr =
| Var of varid (* variables *)
| Num of int (* integers *)
| Bool of bool (* booleans *)
| Unop of unop * expr (* unary operators *)
| Binop of binop * expr * expr (* binary operators *)
| Conditional of expr * expr * expr (* if then else *)
| Fun of varid * expr (* function definitions *)
| Let of varid * expr * expr (* local naming *)
| Letrec of varid * expr * expr (* recursive local naming *)
| Raise (* exceptions *)
| Unassigned (* (temporarily) unassigned *)
| App of expr * expr (* function applications *)
and varid = string ;;
(* Sets of varids *)
module SS = Set.Make (struct
type t = varid
let compare = String.compare
end ) ;;
type varidset = SS.t ;;
(* Test to see if two sets have the same elements (for
testing purposes) *)
let same_vars = SS.equal;;
(* Generate a set of variable names from a list of strings (for
testing purposes) *)
let vars_of_list = SS.of_list ;;
(* Return a set of the variable names free in [exp] *)
let rec free_vars (exp : expr) : varidset =
failwith "free_vars not implemented" ;;
(* Return a fresh variable, constructed with a running counter a la
gensym. Assumes no variable names use the prefix "var". *)
let new_varname () : varid =
failwith "new_varname not implemented" ;;
(* Substitute [repl] for free occurrences of [var_name] in [exp] *)
let rec subst (var_name : varid) (repl : expr) (exp : expr) : expr =
failwith "subst not implemented" ;;
(* exp_to_string -- Returns a string representation of the expr *)
let rec exp_to_string (exp : expr) : string =
failwith "exp_to_string not implemented" ;;
(* exp_to_abstract_string: Returns a string representation of the abstract
syntax of the expr *)
let rec exp_to_abstract_string (exp : expr) : string =
failwith "exp_to_abstract_string not implemented" ;;