annotate parser.js @ 234:370e03964239

Add string method to unarymeth AST node object
author Michael Pavone <pavone@retrodev.com>
date Fri, 03 Jan 2014 19:23:47 -0800
parents c9cacb06964a
children ddf38b66b2e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
49 function floatlit(val)
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;
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
52 }
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
53 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
54 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
55 };
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
56
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
57 function strlit(val)
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
58 {
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
59 this.val = val;
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
60 }
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
61 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
62 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
63 };
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
64
25
4d87c38404d6 List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents: 22
diff changeset
65 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
66 {
4d87c38404d6 List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents: 22
diff changeset
67 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
68 }
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
69 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
70 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
71 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
72 }
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 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
74 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
75 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
76 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
77 }
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 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
79 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
80 }
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 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
82 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
83 };
25
4d87c38404d6 List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents: 22
diff changeset
84
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 25
diff changeset
85 function arraylit(val)
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 25
diff changeset
86 {
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 25
diff changeset
87 this.val = val;
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 25
diff changeset
88 }
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
89 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
90 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
91 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
92 }
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 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
94 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
95 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
96 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
97 }
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 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
99 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
100 }
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 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
102 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
103 };
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 25
diff changeset
104
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
105 function funcall(name, args)
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
106 {
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
107 this.name = name;
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
108 this.args = args;
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
109 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
110 this.dirty = false;
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
111 }
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
112 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
113 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
114 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
115 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
116 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
117 }
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 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
119 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
120 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
121 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 }
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 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
124 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
125 }
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 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
127 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
128 }
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 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
131 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
132 }
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 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
134 };
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
135
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
136 function object(messages)
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
137 {
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
138 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
139 this.name = null;
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
140 }
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
141 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
142 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
143 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
144 }
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 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
146 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
147 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
148 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
149 }
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 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
151 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
152 }
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 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
154 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
155 };
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
156
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
157 function lambda(args, expressions)
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
158 {
42
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 38
diff changeset
159 this.args = args ? args : [];
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
160 this.expressions = expressions;
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
161 }
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
162 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
163 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
164 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
165 }
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 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
167 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
168 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
169 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
170 }
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 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
172 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
173 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
174 }
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 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
176 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
177 }
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 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
179 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
180 };
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
181
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
182 function assignment(sym, expr)
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
183 {
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
184 this.symbol = sym;
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
185 this.expression = expr;
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
186 }
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
187 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
188 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
189 };
1
a57a12bdae96 Added funcall syntax
Mike Pavone <pavone@retrodev.com>
parents: 0
diff changeset
190
25
4d87c38404d6 List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents: 22
diff changeset
191 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
192 {
4d87c38404d6 List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents: 22
diff changeset
193 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
194 }
4d87c38404d6 List literals, fixes to implicit self property lookup, import statement and editor improvements
Mike Pavone <pavone@retrodev.com>
parents: 22
diff changeset
195
122
9820ecd4eed4 Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 104
diff changeset
196 var grammar =
1
a57a12bdae96 Added funcall syntax
Mike Pavone <pavone@retrodev.com>
parents: 0
diff changeset
197 'start = ws module:(object / lambda) ws { return module; };' +
4
a2c3ac7fafa2 Added support for comments
Mike Pavone <pavone@retrodev.com>
parents: 2
diff changeset
198 'ws = ([ \\t\\n\\r] / "//" [^\\n]* "\\n")*;' +
a2c3ac7fafa2 Added support for comments
Mike Pavone <pavone@retrodev.com>
parents: 2
diff changeset
199 'hws = ([ \\t] / "/*" ([^*] / "*" ! "/")* "*/" )*;' +
214
e01137a97654 Make assignments an expression in the grammar
Michael Pavone <pavone@retrodev.com>
parents: 187
diff changeset
200 '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
201 '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
202 '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
203 '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
204 '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
205 '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
206 '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
207 'primlitsym = hws val:(float / hex / binary / int / string / symbol / object / array / list / lambda / "(" ws expr:expr hws ")" { return expr; }) { return val; };' +
104
648659961e0e Get editor working again
Mike Pavone <pavone@retrodev.com>
parents: 86
diff changeset
208 '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("")); };' +
5
ed5b563147ec Wrote constructors for AST objects
Mike Pavone <pavone@retrodev.com>
parents: 4
diff changeset
209 'float = digits:[0-9]+ "." decimals:[0-9]+ { return new floatlit(parseFloat(digits.join("") + "." + decimals.join(""))); };' +
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
210 '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
211 '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
212 '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
213 '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
214 'strpart = text:[^\\"\\\\]+ { return text.join(""); };' +
233
c9cacb06964a Fix tab escapes in old parser
Michael Pavone <pavone@retrodev.com>
parents: 229
diff changeset
215 '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
216 '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
217 '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
218 '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
219 '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
220 '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
221 '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
222 '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
223 'funexpr = f: funcall ws { return f; };' +
104
648659961e0e Get editor working again
Mike Pavone <pavone@retrodev.com>
parents: 86
diff changeset
224 '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
225 '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
226 '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
227 '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
228 'methcallrest = funcall / unarymeth;' +
6
554602d4cbc6 Javascript compiler backend
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
229 'unarymeth = name:symbol { return new funcall(name.name, []); };';
0
3d1b8e96f5dc Initial commit
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
230 var parser = PEG.buildParser(grammar);
3d1b8e96f5dc Initial commit
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
231
3d1b8e96f5dc Initial commit
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
232