Mercurial > repos > tabletprog
annotate cbackend.js @ 142:833624457b81
Merge
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 09 Aug 2013 01:37:09 -0700 |
parents | bf8f75b69048 101fa04ee9e1 |
children | 282b8056b702 |
rev | line source |
---|---|
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 var mainModule; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 var modules = {}; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 var nextmethodId = 0; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 var methodIds = {}; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 function getMethodId(methodName) |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 if (!(methodName in methodIds)) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 methodIds[methodName] = nextmethodId++; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
10 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 return methodIds[methodName]; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 |
122
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
15 function getOpMethodName(opname) |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
16 { |
97
59a94f3ad56f
Added short-circuit && and || operators
Mike Pavone <pavone@retrodev.com>
parents:
96
diff
changeset
|
17 var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_', '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_', '.': 'CAT_', '&&':'if', '||':'ifnot'}; |
122
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
18 if (opname in optoMeth) { |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
19 return optoMeth[opname]; |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
20 } else { |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
21 return opname; |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
22 } |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
23 } |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
24 |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
25 op.prototype.toC = function(isReceiver) { |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
26 var method = getOpMethodName(this.op); |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
27 return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.left.toC() + ', ' + this.right.toC() + ')\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 }; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
29 op.prototype.toCLLExpr = function(vars) { |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
30 var opmap = {'=': '==', 'xor': '^'}; |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
31 return this.left.toCLLExpr(vars) + (this.op in opmap ? opmap[this.op] : this.op) + this.right.toCLLExpr(vars); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
32 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
33 op.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
34 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) + ';']; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
35 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
36 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 function escapeCName(name) |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 { |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
40 if (name == 'self') { |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
41 return name; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
42 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 name = name.replace("_", "UN_").replace(":", "CN_").replace("!", "EX_").replace('?', 'QS_').replace('@', 'AT_'); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 name = 'tp_' + name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 return name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 |
54 | 48 function getSymbolPrefix(info, symbols) |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
49 { |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 var pre = ''; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
51 switch(info.type) { |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
52 case 'self': |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
53 |
54 | 54 pre = (new symbol('self', symbols)).toC() + '->'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
55 break; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
56 case 'parent': |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
57 pre = (new symbol('self', symbols)).toC() + '->header.'; |
32
64f1d516fbfd
Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents:
31
diff
changeset
|
58 for (var i = 0; i < info.depth; ++i) { |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
59 pre += (i ? '->' : '') + 'parent'; |
32
64f1d516fbfd
Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents:
31
diff
changeset
|
60 } |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
61 pre = '((' + info.selftype + ' *)' + pre + ')->'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
62 break; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
63 case 'upvar': |
32
64f1d516fbfd
Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents:
31
diff
changeset
|
64 pre = 'env->'; |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
65 for (var i = info.startdepth; i < info.depth; ++i) { |
32
64f1d516fbfd
Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents:
31
diff
changeset
|
66 pre += 'parent->'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 } |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
68 break; |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
69 case 'recupvar': |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
70 if (info.subtype == 'object') { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
71 pre = 'self->env->'; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
72 } else { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
73 //TODO: fill this case in if necessary |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
74 } |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
75 pre += getSymbolPrefix(info.parent); |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
76 case 'closedover': |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
77 pre = 'myenv->'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 } |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
79 return pre; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
80 } |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
81 |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
82 symbol.prototype.toC = function() { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
83 var name = this.cleanName(); |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
84 var info = this.symbols.find(name); |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
85 if (!info) { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
86 throw new Error('symbol ' + name + ' not found'); |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
87 } |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
88 if (info.type == 'toplevel') { |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
89 return toplevel.moduleVar(name); |
69
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
90 } else if (info.type == 'self' && info.def instanceof lambda) { |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
91 return 'mcall(' + getMethodId(name) + '/* ' + name + ' */, 1, ' + (new symbol('self', this.symbols)).toC() + ')'; |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
92 } else if (info.type == 'parent' && info.def instanceof lambda) { |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
93 var obj = (new symbol('self', this.symbols)).toC() + '->header.'; |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
94 for (var i = 0; i < info.depth; ++i) { |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
95 obj += (i ? '->' : '') + 'parent'; |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
96 } |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
97 return 'mcall(' + getMethodId(name) + '/* ' + name + ' */, 1, ' + obj + ')'; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
98 } |
54 | 99 return getSymbolPrefix(info, this.symbols) + escapeCName(name); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
101 symbol.prototype.toCTypeName = function() { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
102 return this.cleanName(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
103 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
104 symbol.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
105 var name = this.cleanName(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
106 if (name in vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
107 return name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
108 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
109 if (name == 'self') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
110 return 'self'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
111 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
112 var info = this.symbols.find(name, false, true); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
113 var symbols = this.symbols; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
114 while (info && info.type == 'local') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
115 symbols = symbols.parent; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
116 info = symbols.find(name, false, true); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
117 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
118 if (!info) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
119 return name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
120 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
121 if (info.type == 'toplevel') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
122 return toplevel.moduleVar(name); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
123 } else if (info.type == 'self') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
124 if (info.isll || !(info.def instanceof lambda)) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
125 return 'self->' + name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
126 } else { |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
127 return 'mcall(' + getMethodId(name) + '/* ' + name + ' */, 1, self)'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
128 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
129 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
130 throw new Error('Unsupported reference type ' + info.type + ' for variable ' + name); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
131 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
132 symbol.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
133 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) + ';' ]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
134 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
136 var declaredInts = {}; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
137 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 intlit.prototype.toC = function() { |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
139 var intType = 'int' + this.bits; |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
140 var str = intType + '_' + (this.val < 0 ? 'neg_' + (0-this.val).toString() : this.val.toString()); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
141 if (!(str in declaredInts)) { |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
142 toplevelcode += 'obj_' + intType + ' ' + str + ' = {{&obj_' + intType + '_meta, NULL}, ' + this.val.toString() + '};\n'; |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
143 declaredInts[str] = true; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
144 } |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
145 return '((object *)&' + str + ')'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
147 intlit.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
148 return this.val.toString(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
149 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
150 intlit.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
151 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) + ';' ]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
152 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
153 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
154 floatlit.prototype.toC = function() { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 return 'make_float(' + this.val.toString() + ')'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
157 floatlit.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
158 return this.val.toString(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
159 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
160 floatlit.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
161 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) + ';' ]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
162 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 |
41 | 164 var declaredStrings = {}; |
165 var nextStringId = 0; | |
166 | |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 strlit.prototype.toC = function() { |
41 | 168 if (!(this.val in declaredStrings)) { |
169 //TODO: get the proper byte length | |
43
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
170 toplevelcode += 'string str_' + nextStringId + ' = {{&string_meta, NULL}, ' + this.val.length + ', ' + this.val.length + ', "' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"};\n'; |
41 | 171 declaredStrings[this.val] = nextStringId++; |
172 } | |
173 return '((object *)&str_' + declaredStrings[this.val] + ')'; | |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
174 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
175 strlit.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
176 return '"' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
177 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
178 strlit.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
179 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) +';' ]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
180 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 listlit.prototype.toC = function() { |
40
927fd7911a01
Add append message to array
Mike Pavone <pavone@retrodev.com>
parents:
39
diff
changeset
|
183 var ret = 'make_list(' + this.val.length; |
38 | 184 for (var i = 0; i < this.val.length; i++) { |
40
927fd7911a01
Add append message to array
Mike Pavone <pavone@retrodev.com>
parents:
39
diff
changeset
|
185 ret += ', ' + this.val[i].toC(); |
38 | 186 } |
187 return ret + ')'; | |
188 } | |
189 | |
190 arraylit.prototype.toC = function() { | |
40
927fd7911a01
Add append message to array
Mike Pavone <pavone@retrodev.com>
parents:
39
diff
changeset
|
191 var ret = 'make_array(' + this.val.length; |
38 | 192 for (var i = 0; i < this.val.length; i++) { |
40
927fd7911a01
Add append message to array
Mike Pavone <pavone@retrodev.com>
parents:
39
diff
changeset
|
193 ret += ', ' + this.val[i].toC(); |
38 | 194 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
195 return ret + ')'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
196 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 funcall.prototype.toC = function() { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
200 if (name == 'foreign') { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
201 if ((this.args[0] instanceof lambda) || (this.args[0] instanceof object)) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
202 return null; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
203 } else if(this.args[0] instanceof symbol) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 return this.args[0].name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
206 throw new Error("Unexpected AST type for foreign:"); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
207 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
208 } else if(name == 'llProperty:withType' || name == 'llProperty:withVars:andCode') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
209 return null; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
210 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
211 var args = this.args.slice(0, this.args.length); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
212 if (this.receiver) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
213 args.splice(0, 0, this.receiver); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
214 } |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
215 var method = false; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
216 var funinfo = this.symbols.find(name); |
72
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
217 var start = 0; |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
218 if (!funinfo || funinfo.def instanceof setter || funinfo.type == 'toplevel') { |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
219 method = true; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
220 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
221 switch(funinfo.type) |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
222 { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
223 case 'self': |
72
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
224 case 'parent': |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
225 var defargs = funinfo.def.args.length; |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
226 if (!defargs || funinfo.def.args[0].name != 'self') { |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
227 defargs ++ |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
228 } |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
229 if (args.length < defargs) { |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
230 if (funinfo.type == 'self') { |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
231 args.splice(0, 0, new symbol('self', this.symbols)); |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
232 } else { |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
233 var obj = (new symbol('self', this.symbols)).toC() + '->header.'; |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
234 for (var i = 0; i < funinfo.depth; ++i) { |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
235 obj += (i ? '->' : '') + 'parent'; |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
236 } |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
237 args.splice(0, 0, ', ' + obj); |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
238 start = 1; |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
239 } |
137
101fa04ee9e1
Fix calling methods on objects that are not self when self implements the method being called
Mike Pavone <pavone@retrodev.com>
parents:
135
diff
changeset
|
240 } else if(!(args[0] instanceof symbol) || args[0].name != 'self') { |
101fa04ee9e1
Fix calling methods on objects that are not self when self implements the method being called
Mike Pavone <pavone@retrodev.com>
parents:
135
diff
changeset
|
241 funinfo = null; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
242 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
243 method = true; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
244 break; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
245 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
246 } |
72
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
247 for (var i = start; i < args.length; i++) { |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
248 args[i] = ', ' + (!i && method ? '(object *)(' : '') + args[i].toC() + (!i && method ? ')' : ''); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
249 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
250 var callpart; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
251 if (method) { |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
252 if (funinfo && funinfo.type == 'self' && funinfo.def.name) { |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
253 callpart = funinfo.def.name + '(' + (funinfo.def.symbols.parent.needsenv ? (new symbol('self', this.symbols)).toC() + '->env' : 'NULL' ); |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
254 } else { |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
255 callpart = 'mcall(' + getMethodId(name) + '/* ' + name + ' */'; |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
256 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
257 } else { |
55
93ddb4ad6fcb
Fix some nested closure bugs
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
258 callpart = 'ccall(' + (new symbol(name, this.symbols)).toC(); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
259 } |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
260 return callpart + ', ' + args.length + args.join('') + ')'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
261 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
262 funcall.prototype.toCTypeName = function() { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
263 switch(this.name) |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
264 { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
265 case 'ptr:': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
266 case 'ptr': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
267 var receiver = this.receiver ? this.receiver : this.args[0]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
268 return receiver.toCTypeName() + ' *'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
269 break; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
270 default: |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
271 throw new Error('invalid use of funcall expression where a C type name is expected'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
272 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
273 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
274 funcall.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
275 var lines = []; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
276 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
277 var args = this.args.slice(0, this.args.length); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
278 if (this.receiver) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
279 args.splice(0, 0, [this.receiver]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
280 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
281 switch(name) |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
282 { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
283 case 'if': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
284 lines.push('if (' + this.args[0].toCLLExpr(vars) + ') {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
285 var blines = this.args[1].toCLines(vars, needsreturn); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
286 for (var i in blines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
287 lines.push('\t' + blines[i]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
288 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
289 if (needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
290 lines.push('} else {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
291 lines.push('\t return module_false;'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
292 lines.push('}'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
293 } else { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
294 lines.push('}'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
295 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
296 break; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
297 case 'if:else': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
298 lines.push('if (' + this.args[0].toCLLExpr(vars) + ') {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
299 var blines = this.args[1].toCLines(vars, needsreturn); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
300 for (var i in blines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
301 lines.push('\t' + blines[i]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
302 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
303 lines.push('} else {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
304 blines = this.args[2].toCLines(vars, needsreturn); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
305 for (var i in blines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
306 lines.push('\t' + blines[i]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
307 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
308 lines.push('}'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
309 break; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
310 case 'while:do': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
311 if (needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
312 throw new Error("while:do can't be last statement in llMessage code block"); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
313 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
314 lines.push('while (' + this.args[0].toCLLExpr(vars) + ') {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
315 var blines = this.args[1].toCLines(vars); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
316 for (var i in blines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
317 lines.push('\t' + blines[i]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
318 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
319 lines.push('}'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
320 break; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
321 default: |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
322 lines.push( (needsreturn ? 'return (object *)' : '') + this.toCLLExpr(vars) + ';'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
323 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
324 return lines; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
325 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
326 |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
327 funcall.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
328 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
329 var args = this.args.slice(0, this.args.length); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
330 if (this.receiver) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
331 if(this.args.length == 0) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
332 return this.receiver.toCLLExpr(vars) + '->' + this.name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
333 } else if (this.args.length == 1 && name[name.length-1] == '!') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
334 return this.receiver.toCLLExpr(vars) + '->' + this.name.substr(0, name.length-1) + ' = ' + args[0].toCLLExpr(vars); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
335 } else { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
336 args.splice(0, 0, this.receiver); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
337 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
338 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
339 switch(name) |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
340 { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
341 case 'if': |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
342 return '((' + args[0].toCLLExpr(vars) + ') ? (' + args[1].toCLLExpr(vars) + ') : 0)'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
343 case 'if:else': |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
344 return '((' + args[0].toCLLExpr(vars) + ') ? (' + args[1].toCLLExpr(vars) + ') : (' + args[2].toCLLExpr(vars) + '))'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
345 case 'while:do': |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
346 throw new Error('while:do not allowed in expression context in llMessage block'); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
347 case 'addr_of': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
348 return '&(' + args[0].toCLLExpr(vars) + ')'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
349 case 'sizeof': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
350 return 'sizeof(' + args[0].toCTypeName() + ')'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
351 case 'get': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
352 return args[0].toCLLExpr(vars) + '[' + args[1].toCLLExpr(vars) + ']'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
353 case 'set': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
354 return args[0].toCLLExpr(vars) + '[' + args[1].toCLLExpr(vars) + '] = ' + args[2].toCLLExpr(vars); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
355 case 'not': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
356 return '!(' + args[0].toCLLExpr(vars) + ')'; |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
357 case 'mcall': |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
358 if (args[0] instanceof symbol) { |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
359 args[0] = new intlit(getMethodId(args[0].name)); |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
360 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
361 default: |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
362 for (var i in args) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
363 args[i] = args[i].toCLLExpr(vars); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
364 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
365 return name + '(' + args.join(', ') + ')'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
366 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
367 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
368 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
369 function cObject(name) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
370 this.name = name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
371 this.slots = {}; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
372 this.properties = []; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
373 this.values = []; |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
374 this.slotvars = {}; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
375 this.includes = {}; |
59 | 376 this.parent = 'NULL'; |
377 this.init = []; | |
378 this.initmsgadded = false; | |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
379 } |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
380 |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
381 cObject.prototype.addInclude = function(includefile) { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
382 this.includes[includefile] = true; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
383 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
384 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
385 cObject.prototype.addMessage = function(msgname, implementation) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
386 var methodid = getMethodId(msgname); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
387 var trunc = methodid & 0xF; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
388 if (!(trunc in this.slots)) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
389 this.slots[trunc] = []; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
390 } |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
391 this.slots[trunc].push([methodid, '\t\t' + implementation.lines.join('\n\t\t') + '\n', msgname]); |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
392 if (!(trunc in this.slotvars)) { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
393 this.slotvars[trunc] = {}; |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
394 } |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
395 for (var varname in implementation.vars) { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
396 this.slotvars[trunc][varname] = implementation.vars[varname]; |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
397 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
398 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
399 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
400 cObject.prototype.addProperty = function(propname, value, type) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
401 if (type != undefined) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
402 this.properties.push([propname, type]); |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
403 if (value !== null) { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
404 this.values.push(value); |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
405 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
406 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
407 var escaped = escapeCName(propname); |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
408 this.addMessage(propname, { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
409 vars: {}, |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
410 lines: [ |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
411 'return self->' + escaped + ';' |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
412 ]}); |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
413 this.addMessage(propname + '!', { |
59 | 414 vars: {}, |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
415 lines: [ |
59 | 416 'self->' + escaped + ' = va_arg(args, object *);', |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
417 'return (object *)self;' |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
418 ]}); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
419 this.properties.push(escaped); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
420 this.values.push(value); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
421 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
422 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
423 |
59 | 424 cObject.prototype.addInit = function(statement) { |
425 this.init.push(statement); | |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
426 }; |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
427 |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
428 cObject.prototype.addImport = function(symbols, source) { |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
429 this.imported.push(source); |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
430 var importNum = imported.length - 1; |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
431 if (symbols) { |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
432 each(symbols, function(i, sym) { |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
433 this.addMessage(sym.name, { |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
434 vars: {}, |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
435 lines: [ |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
436 'return self->import_' + importNum + '->meta->meth_lookup[method_id & 0xF](method_id, num_args, self->import_' + importNum + ', args);' |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
437 ] |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
438 }); |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
439 }); |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
440 } else { |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
441 //TODO: handle proxying for unimplemented methods |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
442 } |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
443 }; |
59 | 444 |
445 cObject.prototype.checkInitMsg = function() { | |
446 if (!this.initmsgadded && this.init.length) { | |
447 var init = this.init.slice(0, this.init.length); | |
448 init.push('return (object *)self;'); | |
449 this.addMessage('_init', { | |
450 vars: {}, | |
451 lines: init | |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
452 }); |
59 | 453 this.initmsgadded = true; |
454 } | |
455 } | |
456 | |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
457 cObject.prototype.populateSymbols = function() {}; |
59 | 458 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
459 cObject.prototype.toEarlyCDef = function() { |
59 | 460 this.checkInitMsg(); |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
461 var includes = ''; |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
462 for (var file in this.includes) { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
463 includes += '#include ' + file + '\n'; |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
464 } |
54 | 465 var objdef = 'typedef struct ' + this.name + ' {\n\tobject header;\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
466 for (var i in this.properties) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
467 if (this.properties[i] instanceof Array) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
468 objdef += '\t' + this.properties[i][1] + ' ' + this.properties[i][0] + ';\n'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
469 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
470 objdef += '\tobject * ' + this.properties[i] + ';\n' |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
471 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
472 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
473 objdef += '} ' + this.name + ';\nobj_meta ' + this.name + '_meta;\n'; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
474 return includes + objdef; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
475 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
476 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
477 cObject.prototype.toCDef = function() { |
59 | 478 this.checkInitMsg(); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
479 var slotdefs = ''; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
480 var metadef = 'obj_meta ' + this.name + '_meta = {sizeof(' + this.name +'), {'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
481 for (var i = 0; i < 16; i++) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
482 if (i) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
483 metadef += ', '; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
484 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
485 if (i in this.slots) { |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
486 slotdefs += 'object * ' + this.name + '_slot_' + i + '(uint32_t method_id, uint32_t num_params, object * oself, va_list args) {\n\t' + |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
487 this.name + ' *self = (' + this.name + ' *)oself;\n'; |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
488 for (var varname in this.slotvars[i]) { |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
489 slotdefs += '\t' + this.slotvars[i][varname] + ' ' + varname + ';\n'; |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
490 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
491 if (this.slots[i].length == 1) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
492 slotdefs += '\tif (method_id == ' + this.slots[i][0][0] + ') { /* ' + this.slots[i][0][2] + '*/\n' + |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
493 '\t\t' + this.slots[i][0][1] + '\n' + |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
494 '\t}\n' + |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
495 '\treturn no_impl(method_id, num_params, (object *)self, args);\n}\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
496 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
497 slotdefs += '\tswitch(method_id) {\n'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
498 for (j in this.slots[i]) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
499 slotdefs += '\t\tcase ' + this.slots[i][j][0] + ': /* ' + this.slots[i][j][2] + '*/\n' + |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
500 '\t\t\t' + this.slots[i][j][1] + '\n'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
501 } |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
502 slotdefs += '\t\tdefault:\n' + |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
503 '\t\t\treturn no_impl(method_id, num_params, (object *)self, args);\n\t}\n}\n'; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
504 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
505 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
506 metadef += this.name + '_slot_' + i; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
507 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
508 metadef += 'no_impl'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
509 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
510 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
511 metadef += '}};\n'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
512 return slotdefs + metadef; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
513 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
514 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
515 cObject.prototype.toCInstance = function() { |
59 | 516 this.checkInitMsg(); |
517 var ret = 'make_object(&' + this.name + '_meta, ' + this.parent + ', ' + this.values.length + (this.values.length ? ', ' : '') + this.values.join(', ') + ')'; | |
518 if (this.initmsgadded) { | |
519 ret = 'mcall(' + getMethodId('_init') + ', 1, ' + ret + ')' | |
520 } | |
521 return ret; | |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
522 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
523 |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
524 cObject.prototype.toC = function() { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
525 forwarddec += this.toEarlyCDef(); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
526 toplevelcode += this.toCDef(); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
527 return this.toCInstance(); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
528 } |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
529 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
530 var nextobject = 0; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
531 |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
532 |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
533 object.prototype.toCObject = function() { |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
534 var messages = this.messages; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
535 if (!this.name) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
536 this.name = 'object_' + nextobject++; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
537 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
538 var me = new cObject(this.name); |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
539 this.symbols.typename = me.name; |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
540 if (this.symbols.needsenv) { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
541 me.addProperty('env', this.symbols.envVar(), 'struct ' + this.symbols.getEnvType() + ' * '); |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
542 me.hasenv = true; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
543 } |
59 | 544 if (this.symbols.needsparent && !(this.symbols.parent instanceof osymbols)) { |
545 me.parent = '(object *)(' + (new symbol('self', this.symbols.parent)).toC() + ')'; | |
546 } | |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
547 for (var i in messages) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
548 if (messages[i] instanceof funcall) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
549 if (messages[i].name == 'import:' && messages[i].args.length == 1) { |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
550 me.addImport(false, messages[i].args[0]); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
551 } else if(messages[i].name == 'import:from:' && messages[i].args.length == 2) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
552 var importsyms = []; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
553 each(messages[i].args[0].val, function(i, el) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
554 if (!(el instanceof symbol)) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
555 throw new Error('Names in import:from statement must be symbols'); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
556 } |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
557 importsyms.push(el); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
558 }); |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
559 me.addImport(importsyms, messages[i].args[1]); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
560 } else if(messages[i].name == 'llProperty:withType:' && messages[i].args.length == 2) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
561 me.addProperty(messages[i].args[0].name, null, messages[i].args[1].toCTypeName()) |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
562 } else if(messages[i].name == 'llMessage:withVars:andCode:' && messages[i].args.length == 3) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
563 var msgname = messages[i].args[0].name |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
564 var rawvars = messages[i].args[1].expressions; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
565 var vars = {}; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
566 for(var v in rawvars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
567 vars[rawvars[v].symbol.name] = rawvars[v].expression.toCTypeName(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
568 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
569 me.addMessage(msgname, { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
570 vars: vars, |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
571 lines: messages[i].args[2].toCLines(vars, true) |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
572 }); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
573 } else { |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
574 |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
575 throw new Error('Only import and import:from calls allowed in object context. ' + messages[i].name + 'with ' + messages[i].args.length + ' arguments found instead.'); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
576 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
577 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
578 messages[i].toCObject(me); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
579 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
580 } |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
581 |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
582 return me; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
583 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
584 |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
585 object.prototype.toC = function() { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
586 return this.toCObject().toC(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
587 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
588 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
589 var toplevelcode; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
590 var forwarddec; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
591 |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
592 function addBinaryOp(cobject, opname, cop, objtype) |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
593 { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
594 cobject.addMessage(opname, { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
595 vars: {ret: objtype + ' *', argb: objtype +' *'}, |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
596 lines: [ |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
597 'argb = va_arg(args, ' + objtype + ' *);', |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
598 'ret = (' + objtype + ' *)make_object(&' + objtype + '_meta, NULL, 0);', |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
599 'ret->num = self->num ' + cop + ' argb->num;', |
52
ab6217b8ae4c
Remove some C compiler warnings due to sloppy native code
Mike Pavone <pavone@retrodev.com>
parents:
49
diff
changeset
|
600 'return &(ret->header);' |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
601 ] |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
602 }); |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
603 } |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
604 |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
605 function addCompOp(cobject, opname, cop, objtype) |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
606 { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
607 cobject.addMessage(opname, { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
608 vars: {argb: objtype + ' *'}, |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
609 lines: [ |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
610 'argb = va_arg(args, ' + objtype + ' *);', |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
611 'if (self->num ' + cop + ' argb->num) {', |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
612 ' return ' + toplevel.moduleVar('true') + ';', |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
613 '}', |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
614 'return ' + toplevel.moduleVar('false') + ';', |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
615 ] |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
616 }); |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
617 } |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
618 |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
619 function makeInt(bits) |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
620 { |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
621 var typename = 'obj_int' + bits; |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
622 var intObj = new cObject(typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
623 intObj.addProperty('num', null, 'int' + bits +'_t'); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
624 addBinaryOp(intObj, 'ADD_', '+', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
625 addBinaryOp(intObj, 'SUB_', '-', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
626 addBinaryOp(intObj, 'MUL_', '*', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
627 addBinaryOp(intObj, 'DIV_', '/', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
628 addBinaryOp(intObj, 'MOD_', '%', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
629 addBinaryOp(intObj, 'or', '|', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
630 addBinaryOp(intObj, 'xor', '^', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
631 addBinaryOp(intObj, 'and', '&', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
632 addBinaryOp(intObj, 'lshift:by', '<<', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
633 addBinaryOp(intObj, 'rshift:by', '>>', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
634 addCompOp(intObj, 'LT_', '<', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
635 addCompOp(intObj, 'GT_', '>', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
636 addCompOp(intObj, 'EQ_', '==', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
637 addCompOp(intObj, 'NEQ_', '!=', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
638 addCompOp(intObj, 'GEQ_', '>=', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
639 addCompOp(intObj, 'LEQ_', '<=', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
640 intObj.addInclude('<string.h>'); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
641 //-9223372036854775808 |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
642 //01234567890123456789 |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
643 intObj.addMessage('string', { |
43
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
644 vars: {str: 'string *'}, |
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
645 lines: [ |
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
646 'str = (string *)make_object(&string_meta, NULL, 0);', |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
647 'str->data = GC_MALLOC(' + (bits == 64 ? 21 : 12) + ');', |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
648 'sprintf(str->data, "%' + (bits == 64 ? 'l' : '') +'d", self->num);', |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
649 'str->len = str->bytes = strlen(str->data);', |
52
ab6217b8ae4c
Remove some C compiler warnings due to sloppy native code
Mike Pavone <pavone@retrodev.com>
parents:
49
diff
changeset
|
650 'return &(str->header);' |
43
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
651 ] |
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
652 }); |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
653 //7FFFFFFFFFFFFFFF |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
654 //01234567890123456789 |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
655 intObj.addMessage('hex', { |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
656 vars: {str: 'string *'}, |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
657 lines: [ |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
658 'str = (string *)make_object(&string_meta, NULL, 0);', |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
659 'str->data = GC_MALLOC(' + (bits == 64 ? 17 : 9) + ');', |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
660 'sprintf(str->data, "%' + (bits == 64 ? 'l' : '') +'X", self->num);', |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
661 'str->len = str->bytes = strlen(str->data);', |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
662 'return &(str->header);' |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
663 ] |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
664 }); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
665 intObj.addMessage('isInteger?', { |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
666 vars: {}, |
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
667 lines: [ |
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
668 'return ' + toplevel.moduleVar('true') + ';' |
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
669 ] |
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
670 }); |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
671 intObj.addMessage('hash', { |
79
7f635666c73d
Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents:
78
diff
changeset
|
672 vars: {}, |
7f635666c73d
Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents:
78
diff
changeset
|
673 lines: [ |
7f635666c73d
Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents:
78
diff
changeset
|
674 'return &(self->header);' |
7f635666c73d
Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents:
78
diff
changeset
|
675 ] |
7f635666c73d
Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents:
78
diff
changeset
|
676 }); |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
677 return intObj; |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
678 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
679 |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
680 function makeArray() |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
681 { |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
682 var arrayfile = toplevel.names['array']; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
683 var ast = parseFile(arrayfile.path + '/' + arrayfile.file); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
684 ast.name = 'array'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
685 ast.populateSymbols(toplevel); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
686 return ast.toCObject(); |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
687 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
688 |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
689 function makeString() |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
690 { |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
691 var arrayfile = toplevel.names['string']; |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
692 var ast = parseFile(arrayfile.path + '/' + arrayfile.file); |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
693 ast.name = 'string'; |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
694 ast.populateSymbols(toplevel); |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
695 return ast.toCObject(); |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
696 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
697 |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
698 function makelambda() |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
699 { |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
700 var clos = new cObject('lambda'); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
701 clos.addProperty('env', null, 'void *'); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
702 clos.addProperty('func', null, 'closure_func'); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
703 clos.addMessage('while:do', { |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
704 vars: {action: 'lambda *', ret: 'object *'}, |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
705 lines: [ |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
706 'action = va_arg(args, lambda *);', |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
707 'ret = ' + toplevel.moduleVar('true') + ';', |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
708 'while(' + toplevel.moduleVar('true') + ' == ccall(self, 0)) {', |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
709 ' ccall(action, 0);', |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
710 '}', |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
711 'return ret;' |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
712 ] |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
713 }); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
714 return clos; |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
715 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
716 |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
717 function builtinTypes() |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
718 { |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
719 return [makeInt(64), makeInt(32), makeInt(16), makeInt(8), makeArray(), makeString(), makelambda()]; |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
720 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
721 |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
722 function addBuiltinModules(toplevel) |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
723 { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
724 var os = new cObject('mod_obj_os'); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
725 os.addInclude('<sys/stat.h>'); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
726 os.addInclude('<fcntl.h>'); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
727 os.addMessage('write', { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
728 vars: {str: 'string *', intret: 'obj_int32 *', filedes: 'obj_int32 *'}, |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
729 lines: [ |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
730 'filedes = va_arg(args, obj_int32 *);', |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
731 'str = va_arg(args, string *);', |
59 | 732 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
733 'intret->num = write(filedes->num, str->data, str->bytes);', |
59 | 734 'return &(intret->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
735 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
736 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
737 os.addMessage('read', { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
738 vars: {str: 'string *', size: 'obj_int32 *', filedes: 'obj_int32 *'}, |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
739 lines: [ |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
740 'filedes = va_arg(args, obj_int32 *);', |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
741 'size = va_arg(args, obj_int32 *);', |
59 | 742 'str = (string *)make_object(&string_meta, NULL, 0);', |
78
abc6f3d644a4
Use Boehm-GC for garbage collection. Also make no_impl print on stderr rather than standard in and return a non-zero error code.
Mike Pavone <pavone@retrodev.com>
parents:
72
diff
changeset
|
743 'str->data = GC_MALLOC_ATOMIC(size->num + 1);', |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
744 'str->length = str->bytes = read(filedes->num, str->data, size->num);', |
58
7b454d100dc8
Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents:
57
diff
changeset
|
745 'if (str->bytes < 0) { str->bytes = str->length = 0; }', |
7b454d100dc8
Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents:
57
diff
changeset
|
746 'str->data[str->bytes] = 0;', |
59 | 747 'return &(str->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
748 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
749 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
750 os.addMessage('open', { |
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:
48
diff
changeset
|
751 vars: {str: 'string *', flags: 'obj_int32 *', filedes: 'obj_int32 *', perm: 'obj_int32 *'}, |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
752 lines: [ |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
753 'str = va_arg(args, string *);', |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
754 'flags = va_arg(args, obj_int32 *);', |
59 | 755 'filedes = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', |
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:
48
diff
changeset
|
756 'if (num_params == 3) {', |
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:
48
diff
changeset
|
757 ' filedes->num = open(str->data, flags->num);', |
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:
48
diff
changeset
|
758 '} else if (num_params == 4) {', |
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:
48
diff
changeset
|
759 ' perm = va_arg(args, obj_int32 *);', |
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:
48
diff
changeset
|
760 ' filedes->num = open(str->data, flags->num, perm->num);', |
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:
48
diff
changeset
|
761 '} else {', |
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:
48
diff
changeset
|
762 ' filedes->num = -1;', |
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:
48
diff
changeset
|
763 '}', |
59 | 764 'return &(filedes->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
765 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
766 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
767 os.addMessage('close', { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
768 vars: {filedes: 'obj_int32 *', intret: 'obj_int32 *'}, |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
769 lines: [ |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
770 'filedes = va_arg(args, obj_int32 *);', |
59 | 771 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
772 'intret->num = close(filedes->num);', |
59 | 773 'return &(intret->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
774 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
775 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
776 os.addMessage('O_RDONLY', { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
777 vars: {intret: 'obj_int32 *'}, |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
778 lines: [ |
59 | 779 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
780 'intret->num = O_RDONLY;', |
59 | 781 'return &(intret->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
782 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
783 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
784 os.addMessage('O_WRONLY', { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
785 vars: {intret: 'obj_int32 *'}, |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
786 lines: [ |
59 | 787 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
788 'intret->num = O_WRONLY;', |
59 | 789 'return &(intret->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
790 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
791 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
792 os.addMessage('O_RDWR', { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
793 vars: {intret: 'obj_int32 *'}, |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
794 lines: [ |
59 | 795 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
796 'intret->num = O_RDWR;', |
59 | 797 'return &(intret->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
798 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
799 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
800 os.addMessage('O_CREAT', { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
801 vars: {intret: 'obj_int32 *'}, |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
802 lines: [ |
59 | 803 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
804 'intret->num = O_CREAT;', |
59 | 805 'return &(intret->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
806 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
807 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
808 os.addMessage('O_APPEND', { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
809 vars: {intret: 'obj_int32 *'}, |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
810 lines: [ |
59 | 811 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
812 'intret->num = O_APPEND;', |
59 | 813 'return &(intret->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
814 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
815 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
816 os.addMessage('O_TRUNC', { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
817 vars: {intret: 'obj_int32 *'}, |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
818 lines: [ |
59 | 819 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
820 'intret->num = O_TRUNC;', |
59 | 821 'return &(intret->header);' |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
822 ] |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
823 }); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
824 toplevel.names['os'] = os; |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
825 } |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
826 |
66
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
61
diff
changeset
|
827 modulefile.prototype.toC = function(){ |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
828 return this.ast.toCModuleInstance(); |
66
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
61
diff
changeset
|
829 }; |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
61
diff
changeset
|
830 |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
831 function processUsedToplevel(toplevel) |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
832 { |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
833 var alwaysused = ['true', 'false']; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
834 var ret = ''; |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
835 var modulenum = 0; |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
836 var visited = {}; |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
837 for (var i in alwaysused) { |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
838 toplevel.used[alwaysused[i]] = true; |
66
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
61
diff
changeset
|
839 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
840 var newused = Object.keys(toplevel.used); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
841 var allused = newused; |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
842 while (newused.length) { |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
843 for (var i in newused) { |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
844 debugprint('//---module', newused[i], '--- populate symbols'); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
845 forwarddec += 'object * ' + toplevel.moduleVar(newused[i]) + ';\n'; |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
846 toplevel.names[newused[i]].populateSymbols(toplevel); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
847 visited[newused[i]] = true; |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
848 } |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
849 newused = []; |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
850 for (var symbol in toplevel.used) { |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
851 if (!(symbol in visited)) { |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
852 debugprint('//found new usage of module', symbol); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
853 newused.push(symbol); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
854 allused.push(symbol); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
855 } |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
856 } |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
857 } |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
858 |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
859 for (var i = allused.length-1; i >= 0; i--) { |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
860 var symbol = allused[i]; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
861 debugprint('//---module', symbol, '(' + i +')--- compile'); |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
862 ret += '\t' + toplevel.moduleVar(symbol) + ' = ' + toplevel.names[symbol].toC() + ';\n'; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
863 } |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
864 return ret; |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
865 } |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
866 |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
867 function makeCProg(obj) |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
868 { |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
869 forwarddec = toplevelcode = ''; |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
870 var builtins = builtinTypes(); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
871 for (var i in builtins) { |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
872 forwarddec += builtins[i].toEarlyCDef(); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
873 toplevelcode += builtins[i].toCDef(); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
874 } |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
875 addBuiltinModules(toplevel); |
54 | 876 debugprint('//------POPULATING SYMBOLS-----'); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
877 obj.populateSymbols(toplevel); |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
878 var moduleinit = processUsedToplevel(toplevel); |
54 | 879 debugprint('//------COMPILING AST-----'); |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
880 var rest = 'object * mainModule() {\n' + moduleinit + '\tmain_module = ' + obj.toCModuleInstance() + ';\n\treturn main_module;\n}\n'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
881 return '#include "runtime/proghead.inc"\n' + |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
882 '#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' + |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
883 forwarddec + toplevelcode + rest + '#include "runtime/progfoot.inc"\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
884 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
885 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
886 object.prototype.toCModule = function() { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
887 return makeCProg(this); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
888 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
889 |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
890 object.prototype.toCModuleInstance = function() { |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
891 return this.toC(); |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
892 } |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
893 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
894 lambda.prototype.toC = function() { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
895 var args = this.args ? this.args.slice(0, this.args.length) : []; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
896 var exprs = this.expressions; |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
897 debugprint('//', this.name); |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
898 if (Object.keys(this.symbols.closedover).length) { |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
899 this.symbols.envtype = this.name + '_env'; |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
900 forwarddec += 'typedef struct ' + this.symbols.envtype + ' ' + this.symbols.envtype + ';\n' |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
901 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
902 if (this.selftype) { |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
903 this.symbols.defineVar('self', this.selftype); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
904 if (args[0] && args[0].cleanName() == 'self') { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
905 args.splice(0, 1); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
906 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
907 var offset = 1; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
908 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
909 var offset = 0; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
910 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
911 for (var i = 0; i < args.length; ++i) { |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
912 var argname = args[i].toC(); |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
913 |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
914 args[i] = (argname.indexOf('->') < 0 ? '\tobject * ' : '\t') + argname + ' = va_arg(args, object *);\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
915 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
916 var compiled = [] |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
917 for (var i in exprs) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
918 var js = exprs[i].toC(); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
919 if (js) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
920 compiled.push(indent(js)); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
921 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
922 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
923 exprs = compiled; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
924 if (exprs.length) { |
72
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
925 exprs[exprs.length-1] = 'return (object *)(' + exprs[exprs.length-1] + ');'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
926 } |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
927 |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
928 if (Object.keys(this.symbols.closedover).length) { |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
929 forwarddec += 'struct ' + this.name + '_env {\n'; |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
930 if (this.symbols.needsParentEnv) { |
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
931 forwarddec += '\tstruct ' + this.symbols.parentEnvType() + ' * parent;\n'; |
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
932 } |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
933 for (var varname in this.symbols.closedover) { |
54 | 934 if (varname == 'self' && this.selftype) { |
935 forwarddec += '\tstruct ' + this.selftype + ' * self;\n'; | |
936 } else { | |
937 forwarddec += '\tobject * ' + escapeCName(varname) + ';\n'; | |
938 } | |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
939 } |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
940 forwarddec += '};\n' |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
941 |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
942 var myenvinit = '\t' + this.name + '_env * myenv = GC_MALLOC(sizeof(' + this.name + '_env));\n'; |
58
7b454d100dc8
Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents:
57
diff
changeset
|
943 if (this.symbols.needsParentEnv) { |
7b454d100dc8
Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents:
57
diff
changeset
|
944 myenvinit += '\tmyenv->parent = env;\n'; |
7b454d100dc8
Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents:
57
diff
changeset
|
945 } |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
946 this.symbols.envtype = this.name + '_env'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
947 } else { |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
948 var myenvinit = ''; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
949 } |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
950 forwarddec += 'object *' + this.name + ' (' + this.symbols.parentEnvType() + ' * env, uint32_t num_args, ...);\n'; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
951 |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
952 toplevelcode += 'object * ' + this.name + ' ( ' + this.symbols.parentEnvType() + ' * env, uint32_t num_args, ...) {\n\tva_list args;\n' + myenvinit + '\tva_start(args, num_args);\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
953 if (this.selftype) { |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
954 var selfvar = (new symbol('self', this.symbols)).toC(); |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
955 if (selfvar == 'self') { |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
956 toplevelcode += '\t' + this.selftype + ' * self = va_arg(args, ' + this.selftype + ' *);\n'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
957 } else { |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
958 toplevelcode += '\t' + selfvar + ' = va_arg(args, ' + this.selftype + ' *);\n'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
959 } |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
960 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
961 } |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
962 toplevelcode += args.join('') + '\tva_end(args);\n' + exprs.join(';\n\t') + '\n}\n'; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
963 |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
964 if (this.selftype) { |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
965 return this.name; |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
966 } else { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
967 if (this.symbols.parentEnvType() != 'void') { |
55
93ddb4ad6fcb
Fix some nested closure bugs
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
968 if (this.symbols.parent.passthruenv) { |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
969 var envvar = 'env'; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
970 } else { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
971 var envvar = 'myenv'; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
972 } |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
973 debugprint('//' + this.name, 'has envvar:', envvar, 'num vars closed over:', Object.keys(this.symbols.closedover).length); |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
974 return 'make_lambda(' + envvar + ', (closure_func)' + this.name + ')'; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
975 } else { |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
976 toplevelcode += 'lambda ' + this.name + '_obj = {{&lambda_meta, NULL}, NULL, lambda_' + mynum + '};\n'; |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
977 return '((object *)&' + this.name + '_obj)'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
978 } |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
979 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
980 }; |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
981 lambda.prototype.toCModuleInstance = function() { |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
982 this.toC(); |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
983 return this.name + '(NULL, 0)'; |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
984 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
985 lambda.prototype.toCObject = function(typename) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
986 this.selftype = typename; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
987 return this.toC(); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
988 }; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
989 lambda.prototype.toCModule = function() { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
990 return makeCProg(this); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
991 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
992 lambda.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
993 var lines = []; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
994 for (var i in this.args) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
995 var name = this.args[i].name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
996 if (name[0] == ':') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
997 name = name.substr(1); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
998 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
999 if(name != 'self') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1000 lines.push(name + ' = va_arg(args, ' + vars[name] + ');'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1001 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1002 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1003 for (var i in this.expressions) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1004 var exprlines = this.expressions[i].toCLines(vars, needsreturn && i == this.expressions.length - 1); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1005 for (var j in exprlines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1006 lines.push('\t' + exprlines[j]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1007 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1008 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1009 return lines; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1010 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1011 lambda.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1012 if (this.expressions.length != 1) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1013 throw new Error('lambda in expression context must have a single statement in llMessage block'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1014 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1015 return this.expressions[0].toCLLExpr(vars); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1016 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1017 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1018 assignment.prototype.toC = function() { |
54 | 1019 debugprint('//assignment', this.symbol.name); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1020 var existing = this.symbols.find(this.symbol.name); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1021 var prefix = ''; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1022 var val = this.expression.toC(); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1023 if (val === null) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1024 return null; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1025 } |
38 | 1026 if (existing.type == 'local' && !existing.isdeclared) { |
1027 prefix = 'object *'; | |
1028 this.symbols.declareVar(this.symbol.name); | |
54 | 1029 debugprint('//declared var', this.symbol.name); |
38 | 1030 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1031 return prefix + this.symbol.toC() + ' = ' + val; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1032 }; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1033 assignment.prototype.toCObject = function(cobj) { |
54 | 1034 debugprint('//message definition', this.symbol.name); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1035 if (this.expression.toCObject) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1036 var val = this.expression.toCObject(cobj.name); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1037 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1038 var val = this.expression.toC(); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1039 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1040 if (val === null) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1041 return; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1042 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1043 if (this.expression instanceof lambda) { |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1044 var params = ['((object *)self)']; |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1045 var paramget = ''; |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
1046 var messagevars = {}; |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1047 for (var i in this.expression.args) { |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1048 var escaped = escapeCName(this.expression.args[i].cleanName()); |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1049 if (escaped != 'self') { |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
1050 messagevars[escaped] = 'object *'; |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1051 params.push(escaped); |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1052 paramget += escaped + ' = va_arg(args, object *); '; |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1053 } |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1054 } |
122
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
1055 cobj.addMessage(getOpMethodName(this.symbol.name), { |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
1056 vars: messagevars, |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
1057 lines: [paramget + 'return ' + val + '(' + (cobj.hasenv ? 'self->env' : 'NULL') + ', ' + params.length + (params.length ? ', ' : '') + params.join(', ') + ');'] |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
1058 }); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1059 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1060 cobj.addProperty(this.symbol.name, val); |
59 | 1061 if (this.expression instanceof object && this.expression.symbols.needsparent) { |
72
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
1062 cobj.addInit('self->' + escapeCName(this.symbol.name) + '->parent = (object *)self;'); |
59 | 1063 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1064 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1065 }; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1066 assignment.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1067 return [(needsreturn ? 'return ' : '') + this.symbol.toCLLExpr(vars) + ' = ' + this.expression.toCLLExpr(vars) + ';'] |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1068 }; |