-
Notifications
You must be signed in to change notification settings - Fork 0
/
iddish_compiler.py
352 lines (279 loc) · 11.3 KB
/
iddish_compiler.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""This translates the "Iddish" intermediate language to C."""
try:
import psyco
psyco.full()
except:
pass
from pymeta.grammar import OMeta
from pymeta.runtime import ParseError
from python_rewriter.base import strip_comments
import sys
sys.setrecursionlimit(1000000)
def function_writer(name, args, code):
# ALL of our functions have the following signature:
# pointer-to-object function_<name>(pointer-to-closure closure, pointer-to-object self, ARGS...)
func = """
struct object *function_"""+name+"""(struct closure *closure, struct object *self"""
if len(args) > 0:
func = func + ', '
for arg in args:
if ' ' in arg:
func = func + arg + ', '
else:
func = func + 'struct object *'+arg+', '
func = func[:-2]
func = func + """) {
"""+code+"""
}"""
#struct object *s_"""+name+' = send(current_namespace, s_intern, '+name+""";
#"""
return func
def method_writer(class_name, method_name, args, code):
# ALL of our methods have the following signature:
# pointer-to-object function_<name>(pointer-to-closure closure, pointer-to-object self, ARGS...)
meth = """
struct object* functionOF"""+class_name+'_'+method_name+"""(struct closure *closure, struct object *self"""
if len(args) > 0:
meth = meth + ', '
for arg in args:
if ' ' in arg:
meth = meth + arg + ', '
else:
meth = meth + 'struct object *'+arg+', '
meth = meth[:-2]
meth = meth + """) {
"""+code+"""
}"""
#struct object *sOF"""+class_name+'_'+method_name+' = send(current_namespace, s_intern, '+class_name+'METHOD'+method_name+""");
#send(vtable_"""+class_name+', s_addMethod, sOF'+class_name+'_'+method_name+', functionOF'+class_name+'_'+method_name+""");
#"""
return meth
def message_sender(object, messagename, arguments):
# All of our objects accept messages sent via:
# send(object's VTable, message symbol, ARGS...)
call = """
current_object = send(current_namespace, s_lookup, """+object+""");
send(current_object, s_"""+messagename
if len(arguments) > 0:
call = call + ', '+', '.join(arguments)
call = call+');'
return call
def function_caller(messagename, arguments):
code = 'send(current_namespace, symbol_'+messagename
if len(arguments) > 0:
code = code + ', '+', '.join(arguments)
code = code + ');'
return code
grammar_def = """
# A program is a series of statements
program ::= <statement>+:code => \"""\n\""".join(code)
# A space is a space (ie. ' '), a tab or a newline character
space ::= <anything>:a ?(a == ' ' or a == \"""\n\""" or a == '\t') => ''
# Whitespace is an arbitrary amount of spaces
whitespace ::= (<space>)* => ''
# A statement is a single line of code (where lines end in a semicolon)
statement ::= <whitespace> (<comment>
| <assignment>
| <embedded_c>
| <function_def>
| <class_def>
| <message_send>
| <raw_call>
| <declaration>):stmt => stmt
# Comments begin with '//' and end at a newline (careful when using
# character codes in comments such as backslash-n!)
comment ::= '/' '/' <commentbody>:body => '// '+body
# This catches everything up to and including a newline
commentbody ::= <anything>:newline ?(newline == \"""\n\""") => ''
| <anything>:start <commentbody>:rest => start+rest
# Assignments are the binding of a statement's value to a variable
assignment ::= <name>:variable <whitespace> '=' <whitespace>
<assignment_value>:value => variable+' = '+value
assignment_value ::= '(' <name>:n ')' => n+';'
| <statement>:s => s
# Embedded C will simply be inserted into the output unchanged
embedded_c ::= <token 'EMBEDDED_C{{{'> <embedded_c_body>:body => body
# This returns everything up to and excluding "}}}END_EMBEDDED_C"
embedded_c_body ::= '}' '}' '}' 'E' 'N' 'D' '_' 'E' 'M' 'B' 'E' 'D' 'D'
'E' 'D' '_' 'C' => ''
| <anything>:start <embedded_c_body>:rest => start+rest
# function_def is the definition of a function
function_def ::= <token 'def'> ' ' <name>:function_name '('
<arg>*:arguments ')' <token '{'> <statement>*:code
<token '}'> <whitespace> => function_writer(function_name, arguments, \"""\n\""".join(code))
# method_def is the definition of a function belonging to an object
method_def ::= <token 'def'> <whitespace> <name>:class_name '.'
<name>:method_name '(' <arg>*:arguments ')' <token '{'>
<statement>*:code <token '}'> <whitespace> => method_writer(class_name, method_name, arguments, code)
# Class definitions simply declare that a class exists, methods for
# classes are defined using the method_def syntax foo.bar(args){code}
class_def ::= <token 'class '> <name>:classname '(' <name>:parent ')' => 'struct object *vtable_'+classname+' = send(vtable_'+parent+', s_delegated);'
| <token 'class '> <name> => 'struct object *vtable_'+classname+' = send(vtable_object, s_delegated);'
# Message send is the calling of an object's function, ie. a method call
message_send ::= <name>:o '.' <name>:m '(' <arg>*:a ')' => message_sender(o, m, a)
# A raw call is the calling of a function which isn't owned by an object
raw_call ::= <name>:m '(' <arg>*:a ')' => function_caller(m, a)
# A declaration declares a new object variable
declaration ::= <name>:n => 'struct object *'+n+';'
# Arg is an argument to a function
arg ::= <name>:type ' ' <name>:binding ','* => type+'* '+binding
| <name>:variable ','* => variable
# A name is a valid variable, function or class name
name ::= <startchar>:start <namechar>*:rest => start+''.join(rest)
# These are valid at the start of a name
startchar ::= 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J'
| 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T'
| 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' | 'a' | 'b' | 'c' | 'd'
| 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n'
| 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x'
| 'y' | 'z' | '_'
# These are valid anywhere after the start of a name
namechar ::= <startchar> | '0' | '1' | '2' | '3' | '4' | '5' | '6'
| '7' | '8' | '9'
"""
initialiser = """
EMBEDDED_C{{{
// Import the Id object model
#include "obj.c"
// Now define the namespace type
struct namespace
{
struct vtable *_vt[0];
int size;
int tally;
struct object **keys;
struct object **values;
struct vtable *parent;
struct namespace *parent_namespace;
};
}}}END_EMBEDDED_C
//// Define our functions first
// This needs to exist now, although it won't be assigned until later
s_namespace_lookup
// This function searches its object (a namespace) for a symbol
// representing the given string. If found then it is returned.
def namespace_lookup(char string)
{
symbol
EMBEDDED_C{{{
int i;
for (i = 0; i < ((struct vtable *)self)->tally; ++i)
{
symbol = ((struct vtable *)self)->keys[i];
if (!strcmp(string, ((struct symbol *)symbol)->string))
return symbol;
}
symbol = 0;
symbol = send(((struct namespace *)self)->parent_namespace, s_namespace_lookup, string);
return symbol;
}}}END_EMBEDDED_C
}
def toplevel_namespace_lookup(char string)
{
symbol // This will store our symbol if we find it
EMBEDDED_C{{{
// Loop through every key in this vtable (which we've hijacked to
// implement our namespace dictionary)
int i;
for (i = 0; i < ((struct vtable *)self)->tally; ++i)
{
symbol = ((struct vtable *)self)->keys[i];
// Compare the string which this symbol represents with the
// string we've been given.
if (!strcmp(string, ((struct symbol *)symbol)->string)) {
// Return if we've found a match
return symbol;
}
}
// If we've got this far without returning then the toplevel
// namespace doesn't contain such a symbol, and since we can't look
// any higher up all we can do is return a null pointer
symbol = 0;
return symbol;
}}}END_EMBEDDED_C
}
// This looks for a symbol matching the given string in the current
// object (a namespace) and its direct ancestors and returns it. If it
// is not found then a new symbol is made for it in the current object.
def namespace_intern(char string)
{
symbol // This will eventually store the symbol
EMBEDDED_C{{{
symbol = send(self, s_namespace_lookup, string);
if (symbol == 0) {
symbol = symbol_new(string);
send(self, s_addMethod, symbol, 0);
}
return symbol;
}}}END_EMBEDDED_C
}
EMBEDDED_C{{{
int main(void) {
//// Begin with some initialisation stuff
// Initialise Id
init();
// Rename the root object's vtable to fit our naming scheme
struct vtable *vtable_object = object_vt;
}}}END_EMBEDDED_C
// This will store objects we are about to do things to, once they've
// been extracted from the namespace dictionary
current_object
// We need to use namespaces in order to prevent headaches, which we can
// do using the symbols and vtables of Id
EMBEDDED_C{{{
// The namespace we are in will always be referenced by this name
struct namespace *current_namespace;
}}}END_EMBEDDED_C
// This encapsulates the behaviour of namespaces
class namespace(vt)
// This is a special-case for the highest namespace (since it can't
// defer to parents)
class toplevel_namespace(namespace)
// We now need to tell the namespace that it should live below the
// toplevel namespace
EMBEDDED_C{{{
((struct namespace *)vtable_namespace)->parent_namespace = (struct namespace *)vtable_toplevel_namespace;
}}}END_EMBEDDED_C
// Make some global symbols so we can bootstrap the namespaces
EMBEDDED_C{{{
struct object *s_namespace_lookup = symbol_intern(0, 0, "namespace_lookup");
send(vtable_namespace, s_addMethod, s_namespace_lookup, function_namespace_lookup);
}}}END_EMBEDDED_C
// Overwrite lookups for the toplevel namespace using its own function
EMBEDDED_C{{{
send(vtable_toplevel_namespace, s_addMethod, s_namespace_lookup, function_toplevel_namespace_lookup);
}}}END_EMBEDDED_C
// Make some global symbols so we can bootstrap the namespaces
EMBEDDED_C{{{
struct object *s_namespace_intern = symbol_intern(0, 0, "namespace_intern");
send(vtable_namespace, s_addMethod, s_namespace_intern, function_namespace_intern);
}}}END_EMBEDDED_C
EMBEDDED_C{{{
// Set the current namespace
current_namespace = (struct namespace *)vtable_toplevel_namespace;
}}}END_EMBEDDED_C
EMBEDDED_C{{{
return 0;
}
}}}END_EMBEDDED_C
"""
params = globals()
for key in locals().keys():
params[key] = locals()[key]
grammar = OMeta.makeGrammar(strip_comments(grammar_def), params)
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: iddish_compiler.py input.id [output.c]"
sys.exit()
in_name = sys.argv[1]
if len(sys.argv) > 2:
out_name = sys.argv[2]
else:
out_name = in_name.rsplit('.', 1)[0]+'.c'
in_file = open(in_name, 'r')
in_lines = ''.join([l for l in in_file.readlines()])
in_file.close()
matcher = grammar(initialiser+in_lines)
out_file = open(out_name, 'w')
out_file.write(matcher.apply('program'))
out_file.close()