Mercurial > repos > tabletprog
annotate parser.js @ 299:c0561cb06ee3
Add readAll method to file objects
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 25 Jul 2014 08:24:39 -0700 |
parents | f73ebc146af9 |
children |
rev | line source |
---|---|
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
1 |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
2 function op(left, op, right) |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
3 { |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
4 this.left = left; |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
5 if (op instanceof Array) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
6 op = op[0]; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
7 } |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
8 this.op = op; |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
9 this.right = right; |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
10 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
11 op.prototype.valueOf = function(indent) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
12 return this.left.valueOf(indent) + ' ' + this.op.valueOf(indent) + ' ' + this.right.valueOf(indent); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
13 }; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
14 |
19
132c7756860e
Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
15 function symbol(name, symbols) |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
16 { |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
17 this.name = name; |
19
132c7756860e
Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
18 this.symbols = symbols; |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
19 this.dirty = false; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
20 } |
6
554602d4cbc6
Javascript compiler backend
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
21 symbol.prototype.cleanName = function() { |
554602d4cbc6
Javascript compiler backend
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
22 return this.name[0] == ':' ? this.name.substr(1) : this.name; |
554602d4cbc6
Javascript compiler backend
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
23 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
24 symbol.prototype.valueOf = function() { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
25 return this.name; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
26 }; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
27 |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
138
diff
changeset
|
28 function intlit(val, bits, unsigned) |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
29 { |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
30 if (!bits) { |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
31 bits = 32; |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
32 } |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
138
diff
changeset
|
33 if (unsigned === undefined) { |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
138
diff
changeset
|
34 unsigned = false; |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
138
diff
changeset
|
35 } |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
138
diff
changeset
|
36 this.unsigned = unsigned; |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
37 this.bits = bits; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
38 this.val = val; |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
39 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
40 intlit.prototype.valueOf = function() { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
41 var val = '' + this.val; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
42 if (this.bits != 32 || this.unsigned) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
43 val += this.unsigned ? 'u' : 'i' |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
44 val += this.bits; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
45 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
46 return val; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
47 }; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
48 |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
233
diff
changeset
|
49 function floatlit(val, bits) |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
50 { |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
51 this.val = val; |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
233
diff
changeset
|
52 this.bits = bits; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
53 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
54 floatlit.prototype.valueOf = function() { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
55 return '' + val; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
56 }; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
57 |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
58 function strlit(val) |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
59 { |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
60 this.val = val; |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
61 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
62 strlit.prototype.valueOf = function() { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
63 return '"'+this.val+'"'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
64 }; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
65 |
25
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
66 function listlit(val) |
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
67 { |
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
68 this.val = val; |
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
69 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
70 listlit.prototype.valueOf = function(indent) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
71 if (indent === undefined) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
72 indent = ''; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
73 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
74 var nextindent = indent + '\t'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
75 var val = '[' |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
76 for (var i = 0; i < this.val.length; i++) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
77 val += '\n' + nextindent + this.val[i].valueOf(nextindent); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
78 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
79 if (this.val.length) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
80 val += '\n' + indent; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
81 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
82 val += ']'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
83 return val; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
84 }; |
25
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
85 |
38 | 86 function arraylit(val) |
87 { | |
88 this.val = val; | |
89 } | |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
90 arraylit.prototype.valueOf = function(indent) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
91 if (indent === undefined) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
92 indent = ''; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
93 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
94 var nextindent = indent + '\t'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
95 var val = '#[' |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
96 for (var i = 0; i < this.val.length; i++) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
97 val += '\n' + nextindent + this.val[i].valueOf(nextindent); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
98 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
99 if (this.val.length) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
100 val += '\n' + indent; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
101 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
102 val += ']'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
103 return val; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
104 }; |
38 | 105 |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
106 function funcall(name, args) |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
107 { |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
108 this.name = name; |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
109 this.args = args; |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
110 this.receiver = null; |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
111 this.dirty = false; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
112 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
113 funcall.prototype.valueOf = function(indent) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
114 var parts = this.name.split(':'); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
115 var val = ''; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
116 if (this.receiver) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
117 val += this.receiver.valueOf(indent); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
118 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
119 var curarg = 0; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
120 for (var i = 0; i < parts.length; i++) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
121 if (val) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
122 val += ' '; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
123 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
124 if (parts[i]) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
125 val += parts[i] + ': '; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
126 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
127 if (curarg < this.args.length) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
128 val += this.args[curarg++].valueOf(indent); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
129 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
130 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
131 while (curarg < this.args.length) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
132 val += ' ' + this.args[curarg++].valueOf(indent); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
133 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
134 return val; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
135 }; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
136 |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
137 function object(messages) |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
138 { |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
139 this.messages = messages; |
83
fdb9785d2c93
Add logical operators to grammar. Allow vertical whitespace after funcall expression in object.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
140 this.name = null; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
141 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
142 object.prototype.valueOf = function(indent) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
143 if (indent === undefined) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
144 indent = ''; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
145 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
146 var nextindent = indent + '\t'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
147 var val = '#{'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
148 for (var i = 0; i < this.messages.length; i++) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
149 val += '\n' + nextindent + this.messages[i].valueOf(nextindent); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
150 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
151 if (val.length > 2) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
152 val += '\n' + indent; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
153 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
154 val += '}'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
155 return val; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
156 }; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
157 |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
158 function lambda(args, expressions) |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
159 { |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
38
diff
changeset
|
160 this.args = args ? args : []; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
161 this.expressions = expressions; |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
162 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
163 lambda.prototype.valueOf = function(indent) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
164 if (indent === undefined) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
165 indent = ''; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
166 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
167 var nextindent = indent + '\t'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
168 var val = ''; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
169 for (var i = 0; i < this.args.length; i++) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
170 val += this.args[i].valueOf(indent) + ' '; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
171 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
172 val += '{'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
173 for (var i = 0; i < this.expressions.length; i++) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
174 val += '\n' + nextindent + this.expressions[i].valueOf(nextindent); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
175 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
176 if (this.expressions.length) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
177 val += '\n' + indent; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
178 } |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
179 val += '}'; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
180 return val; |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
181 }; |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
182 |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
183 function assignment(sym, expr) |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
184 { |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
185 this.symbol = sym; |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
186 this.expression = expr; |
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
187 } |
217
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
188 assignment.prototype.valueOf = function(indent) { |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
189 return this.symbol.valueOf(indent) + ' <- ' + this.expression.valueOf(indent); |
adad61ea2f3a
Switched to a less hacky implementation of hygiene and exposed more AST properties to macros
Michael Pavone <pavone@retrodev.com>
parents:
214
diff
changeset
|
190 }; |
1 | 191 |
25
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
192 function isLambda(node) |
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
193 { |
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
194 return node instanceof lambda; |
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
195 } |
4d87c38404d6
List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents:
22
diff
changeset
|
196 |
122
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
104
diff
changeset
|
197 var grammar = |
1 | 198 'start = ws module:(object / lambda) ws { return module; };' + |
292
f73ebc146af9
Fix ws rule to include block comments
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
199 'ws = ([ \\t\\n\\r] / "//" [^\\n]* "\\n" / "/*" ([^*] / "*" ! "/")* "*/")*;' + |
4 | 200 'hws = ([ \\t] / "/*" ([^*] / "*" ! "/")* "*/" )*;' + |
214
e01137a97654
Make assignments an expression in the grammar
Michael Pavone <pavone@retrodev.com>
parents:
187
diff
changeset
|
201 'expr = e:(funcall / methcall / assignment / opexpr) ws { return e; };' + |
229
7435367a932a
Allow newlines between binary operator and the right argument to the operator
Michael Pavone <pavone@retrodev.com>
parents:
217
diff
changeset
|
202 'opexpr = left:compareop pieces:(hws ("&&" / "||") ws compareop)* { if (pieces.length) { var cur = new op(left, pieces[0][1], pieces[0][3]); for (var i = 1; i < pieces.length; i++) { cur = new op(cur, pieces[i][1], pieces[i][3]); } return cur; } else { return left; } };'+ |
7435367a932a
Allow newlines between binary operator and the right argument to the operator
Michael Pavone <pavone@retrodev.com>
parents:
217
diff
changeset
|
203 'compareop = left:maybecons pieces:(hws ("<=" / ">=" / "<" / ">" / "=" / "!=") ws maybecons)* { if (pieces.length) { var cur = new op(left, pieces[0][1], pieces[0][3]); for (var i = 1; i < pieces.length; i++) { cur = new op(cur, pieces[i][1], pieces[i][3]); } return cur; } else { return left; } };'+ |
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
138
diff
changeset
|
204 'maybecons = consop / addsub;' + |
229
7435367a932a
Allow newlines between binary operator and the right argument to the operator
Michael Pavone <pavone@retrodev.com>
parents:
217
diff
changeset
|
205 'consop = left:addsub hws "|" ws right:maybecons { return new op(left, "|", right); };'+ |
7435367a932a
Allow newlines between binary operator and the right argument to the operator
Michael Pavone <pavone@retrodev.com>
parents:
217
diff
changeset
|
206 'addsub = left:muldiv pieces:(hws ((("xor"/"and"/"or") ! [a-zA-Z_!?@0-9])/("+"/"-"/".")) ws muldiv)* { if (pieces.length) { var cur = new op(left, pieces[0][1], pieces[0][3]); for (var i = 1; i < pieces.length; i++) { cur = new op(cur, pieces[i][1], pieces[i][3]); } return cur; } else { return left; } };'+ |
7435367a932a
Allow newlines between binary operator and the right argument to the operator
Michael Pavone <pavone@retrodev.com>
parents:
217
diff
changeset
|
207 'muldiv = left:primlitsym pieces:(hws ("*"/"/"/"%") ws primlitsym)* { if (pieces.length) { var cur = new op(left, pieces[0][1], pieces[0][3]); for (var i = 1; i < pieces.length; i++) { cur = new op(cur, pieces[i][1], pieces[i][3]); } return cur; } else { return left; } };'+ |
53
9482a0afe07c
Fix whitespace for parens in grammar
Mike Pavone <pavone@retrodev.com>
parents:
49
diff
changeset
|
208 'primlitsym = hws val:(float / hex / binary / int / string / symbol / object / array / list / lambda / "(" ws expr:expr hws ")" { return expr; }) { return val; };' + |
104 | 209 'symbol = chars:[a-zA-Z_!?@]+ trailing:(":"? [a-zA-Z_!?@0-9])* ! ":" { for (var i = 0; i < trailing.length; i++) { trailing[i] = trailing[i].join(""); } return new symbol(chars.join("") + trailing.join("")); };' + |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
233
diff
changeset
|
210 'float = digits:[0-9]+ "." decimals:[0-9]+ size:("f" ("32" / "64"))? { var bits = size ? parseInt(size[1], 10) : 64; return new floatlit(parseFloat(digits.join("") + "." + decimals.join("")), bits); };' + |
49
f2cda2e6f70e
Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents:
44
diff
changeset
|
211 'binary = "0b" digits:[01]+ { return new intlit(parseInt(digits.join(""), 2)); };' + |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
138
diff
changeset
|
212 'hex = "0x" digits:[0-9a-fA-F]+ size:([iu] ("8" / "16" / "32" / "64"))? { var bits = size ? parseInt(size[1], 10) : 0; return new intlit(parseInt(digits.join(""), 16), bits, size[0] == "u"); };' + |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
138
diff
changeset
|
213 'int = sign:"-"? digits:[0-9]+ size:([iu] ("8" / "16" / "32" / "64"))? { var bits = size ? parseInt(size[1], 10) : 0; return new intlit(parseInt(sign + digits.join(""), 10), bits, size[0] == "u"); };' + |
44
9dd370530f69
Fix escape codes in string literals. Don't print out the return value of main method. Fixup fib example to use print: method. Cleanup error handling in compiler slightly
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
214 'string = "\\"" text:(strpart/escape)* "\\"" { return new strlit(text.join("")); };' + |
122
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
104
diff
changeset
|
215 'strpart = text:[^\\"\\\\]+ { return text.join(""); };' + |
233
c9cacb06964a
Fix tab escapes in old parser
Michael Pavone <pavone@retrodev.com>
parents:
229
diff
changeset
|
216 'escape = "\\\\" char:[nt\\"r\\\\] { if (char == "n") { return "\\n"; } if (char == "r") { return "\\r"; } if (char == "t") { return "\\t"; } return char; };' + |
83
fdb9785d2c93
Add logical operators to grammar. Allow vertical whitespace after funcall expression in object.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
217 'object = "#{" ws messages:(assignment / funexpr)* "}" { return new object(messages); };' + |
176
ab204751d1e7
Allow all expressions in array and list literals
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
218 'array = "#[" ws els:expr* "]" { return new arraylit(els); };' + |
ab204751d1e7
Allow all expressions in array and list literals
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
219 'list = "[" ws els:expr* "]" { return new listlit(els); };' + |
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
138
diff
changeset
|
220 'opsym = name:("&&" / "||" / "<=" / ">=" / "<" / ">" / "=" / "!=" / "+" / "-" / "." / "*" / "/" / "%" / "|") { return new symbol(name); };' + |
187
810edf474f86
Fix parse precedence problem between opsyms and symbols in assignments
Mike Pavone <pavone@retrodev.com>
parents:
176
diff
changeset
|
221 'assignment = ws sym:(opsym / symbol) hws "<-" expr:expr ws { return new assignment(sym, expr); }' + |
214
e01137a97654
Make assignments an expression in the grammar
Michael Pavone <pavone@retrodev.com>
parents:
187
diff
changeset
|
222 'lambda = args:((& ":") argname+ )? "{" ws exprs:expr* "}" { return new lambda(args[1], exprs); };' + |
5
ed5b563147ec
Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
223 'argname = init:":"? chars:[a-zA-Z_!?@]+ trailing:[a-zA-Z_!?@0-9]* hws { return new symbol(init + chars.join("") + trailing.join("")); };' + |
83
fdb9785d2c93
Add logical operators to grammar. Allow vertical whitespace after funcall expression in object.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
224 'funexpr = f: funcall ws { return f; };' + |
104 | 225 'funcall = hws parts: funcallpart+ { var fun = ""; var args = []; for (var i = 0; i < parts.length; i++) { fun += parts[i].name; args = args.concat(parts[i].args); } return new funcall(fun, args); };' + |
2
454c0346f357
Added method call syntax and comparison operators
Mike Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
226 'funcallpart = fun:funpart args:opexpr* hws { return { name: fun, args: args}; };' + |
454c0346f357
Added method call syntax and comparison operators
Mike Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
227 'funpart = chars:[a-zA-Z_!?@]+ middle:[a-zA-Z_!?@0-9]* ":" & [ \\t\\n\\r] { return chars.join("") + middle.join("") + ":"; };' + |
454c0346f357
Added method call syntax and comparison operators
Mike Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
228 'methcall = receiver:opexpr hws info:methcallrest { info.receiver = receiver; return info; };' + |
454c0346f357
Added method call syntax and comparison operators
Mike Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
229 'methcallrest = funcall / unarymeth;' + |
6
554602d4cbc6
Javascript compiler backend
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
230 'unarymeth = name:symbol { return new funcall(name.name, []); };'; |
0 | 231 var parser = PEG.buildParser(grammar); |
232 | |
233 |