-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp.ml
171 lines (135 loc) · 6.06 KB
/
lisp.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(*
History
Lisp
LCF
OCaml *)
(* Lisp: List Processor *)
(* lambda calculus model of function application *)
(* Modern programming languages consider a source file as a series of tokens not characters *)
(* let add x y = x + y;; *)
(* token: significant series of chars, ignores blanks, new lines, comments*)
(* scanner, tokenizer, lexer, lexical scanner: *)
(* Turns sequence of chars to sequence of tokens*)
(* parser *)
(* turns a sequence of tokens into some internal representation*)
(* hello 31 (big dogs) *)
module Scanner =
struct type token =
CloseParenToken|
OpenParenToken|
EndToken|
NumberToekn of int|
SymbolToken of string;;
let makeScanner path =
let input = open_in path in
let ch = ref ' ' (* character constant*)in
(* keep reading next token *)
let rec nextToken() =
let let nextChar () = (* increase ch variable to the next one*)
try ch:= input_char input
with End_of_File -> ch:='\000'
in
let nextBloseParen() =
nextChar();CloseParenToken
in let rec nextComment () =
match !ch with
|'\000' -> ()
|'\n' -> nextChar()
|_ -> nextChar();nextComment()
let nextEndToken () = EndToken
in let nextOpenParenToken () =
nextChar();CloseParenToken
in let nextSymbolToken prefix =
let rec nextSymboling chars =
match !ch with
| '\000'|'\n'|'('|')'->
SymbolToken chars
| _ -> let otherChars = Char.escaped !ch
in nextChar ();
nextSymboling (chars ^ otherChars)
in nextSymboling prefix
in let nextNumberToken prefix =
let rec nextNumbering chars =
match !ch with
|'\000'|'\n'|' '|'('|')'->
NumberToekn (int_of_string chars)
|_ ->
let otherChars = Char.escaped !ch in
nextChar ();
nextNumbering (chars ^ otherChars)
in nextNumbering prefix
in let nextNumberOrSymbolToken prefix =
nextChar ();
match !ch with
|'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' -> nextNumberToken "-"
|_ -> nextSymbolToken "-" in
match !ch with
'\000' ->
nextEndToken ()(* character at the end of the file *)|
' '|'\n' -> nextChar ();nextToken ()|
'(' -> nextOpenParenToken ()|
')' -> nextCloseParenToken ()|
';' -> nextComment();nextToken ()|
'-' -> nextNumberOrSymbolToken ()|
'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' -> nextNumberToken()|
_ -> (*symbol*) nextSymbolToken prefix
in nextChar();nextToken;;
(* Grammer - formal description of a set of strings. strings may be of tokens, chars.
CFG Context-free grammer; Roles, productions
Syntax diagram Nicholos Wirth*)
(* e.g. *)
(append ((quote))quo(te()));;
let envMake() = [];;
let global = ref envMake();;
let envPut name value env =
(name,value)::env;;
global := envPut "nil" Nil (!global);;
let tee = Symbol "t";;
global := envPut "t" tee (!global);;
type thing =
|Closure of thing * thing * environment
|Cons of thing * thing
|Nil
|Number of integer
|Symbol of string
and
environment = (string*thing) list;;
module type Evaluators =
sig
val evaluate: thing -> thing;;
exception EvaluatorError of string;;
end;;
module Evaluator:Evaluators =
struct
exception EvaluatorError of string;;
let oops message = raise (EvaluatorError message);;
let envGet env name etc =
let envGetting env =
match env with
|[] -> etc ()
|(otherName, otherValue)::otherEnv ->
if name=otherName then otherValue
else envGetting otherEnv
in envGetting env;;
let lookup env name =
envGet env name (fun () ->
envGet (!global) name (fun () ->
oops "Unbounded name " ^ name));;
let rec evaluating thing env =
match thing with
|Cons (func,args) ->
(match (evaluating func env) with
|Closure (pars,body,bodyEnv) -> apply pars args body bodyEnv
|Primitive howTo -> howTo args env
|_ -> oops "Closure or primitive expected")
|Symbol name ->
lookup env name|
_ -> thing;;
let evalute thing =
evaluating thing (envMake ());;
let primitive name howTo =
global := envPut name (Primitive howTo) (!global);;
let primitive quote (fun args _ ->
match args with
|Cons(arg,_)->arg
|_ -> oops "quote excepted one argument");;