-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantics.js
184 lines (168 loc) · 3.6 KB
/
semantics.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function FreshName(name)
{
this.name = name;
this.names = {};
this.names[name] = true;
this.probable_type = undefined;
}
function Definition(name, value)
{
this.name = name;
this.value = value;
this.probable_type = Boolean;
}
function Undefined(names, probable_type)
{
this.names = names;
this.probable_type = probable_type;
}
function TypeError()
{
this.names = {};
this.probable_type = undefined;
}
function UndefinedSyntax(msg)
{
this.names = {};
this.probable_type = undefined;
this.msg = msg;
}
function IntroducedName(name)
{
this.names = {};
this.probable_type = undefined;
}
function Special()
{
}
function Operator(probable_type, typecheck, eval)
{
this.probable_type = probable_type;
this.typecheck = typecheck;
this.eval = eval;
}
function is_defined(obj)
{
return !(obj instanceof FreshName) &&
!(obj instanceof IntroducedName) &&
!(obj instanceof Undefined) &&
!(obj instanceof TypeError) &&
!(obj instanceof Definition) &&
!(obj instanceof UndefinedSyntax);
}
function is_malformed(obj)
{
return obj instanceof TypeError || obj instanceof UndefinedSyntax;
}
function is_fresh_name(obj)
{
return obj instanceof FreshName;
}
function is_definition(obj)
{
return obj instanceof Definition;
}
function is_number(obj)
{
if (is_defined(obj))
return obj instanceof Number;
else
return obj.probable_type == Number;
}
function is_boolean(obj)
{
if (is_defined(obj))
return obj instanceof Boolean;
else
return obj.probable_type == Boolean;
}
function is_definitely_true(obj)
{
return is_defined(obj) && is_boolean(obj) && obj.valueOf();
}
function is_definitely_false(obj)
{
return is_defined(obj) && is_boolean(obj) && !obj.valueOf();
}
function probableOptype(env, op)
{
if (!(op in env)) return undefined;
return env[op].probable_type;
}
function perform_op_defined(env, op, lhs, rhs)
{
if (!is_defined(lhs)) throw 'lhs not defined';
if (!is_defined(rhs)) throw 'rhs not defined';
if (!(op in env)) {
var names = {};
names[op] = true;
return new Undefined(names,undefined);
}
if (!env[op].typecheck(lhs, rhs))
return new TypeError();
return env[op].eval(lhs, rhs);
}
function join_undefined(lhs, rhs, probable_type)
{
var lhs_names = {};
var rhs_names = {};
var names = {};
if (is_malformed(lhs)) return lhs;
if (is_malformed(rhs)) return rhs;
if (!is_defined(lhs)) lhs_names = lhs.names;
if (!is_defined(rhs)) rhs_names = rhs.names;
for (var name in lhs_names)
names[name] = true;
for (var name in rhs_names)
names[name] = true;
return new Undefined(names, probable_type);
}
function perform_op(env, op, lhs, rhs)
{
if (is_defined(lhs) && is_defined(rhs))
{
return perform_op_defined(env, op, lhs, rhs);
}
else if (op == '=' && is_fresh_name(lhs) && is_defined(rhs))
{
return new Definition(lhs.name, rhs);
}
else
{
return join_undefined(lhs, rhs, probableOptype(env, op));
}
}
function semantics(env, ast)
{
if (ast.type == 'name')
{
if (ast.name in env)
return env[ast.name];
else
return new FreshName(ast.name);
}
else if (ast.type == 'number')
{
if ('<number>' in env)
return new Number(ast.number);
else
return new UndefinedSyntax('<number>');
}
else if (ast.type == 'quoted')
{
if ('<quoted>' in env)
return ast.quoted;
else
return new UndefinedSyntax('<quoted>');
}
else if (ast.type == 'tree')
{
var lhs = semantics(env, ast.lhs);
var rhs = semantics(env, ast.rhs);
return perform_op(env, ast.op, lhs, rhs);
}
else
{
throw 'Unrecognized AST node: ' + ast.type;
}
}