Mercurial > repos > tabletprog
comparison cbackend.js @ 170:18598163e3ef
Add linked list implementation and cons operator
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 13 Aug 2013 21:58:03 -0700 |
parents | d8f92ebf1ff6 |
children | 869399ff7faa |
comparison
equal
deleted
inserted
replaced
153:075b1e71feff | 170:18598163e3ef |
---|---|
12 return methodIds[methodName]; | 12 return methodIds[methodName]; |
13 } | 13 } |
14 | 14 |
15 function getOpMethodName(opname) | 15 function getOpMethodName(opname) |
16 { | 16 { |
17 var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_', '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_', '.': 'CAT_', '&&':'if', '||':'ifnot'}; | 17 var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_', |
18 '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_', | |
19 '.': 'CAT_', '&&':'if', '||':'ifnot', '|': 'CONS_'}; | |
18 if (opname in optoMeth) { | 20 if (opname in optoMeth) { |
19 return optoMeth[opname]; | 21 return optoMeth[opname]; |
20 } else { | 22 } else { |
21 return opname; | 23 return opname; |
22 } | 24 } |
23 } | 25 } |
24 | 26 |
25 op.prototype.toC = function(isReceiver) { | 27 op.prototype.toC = function(isReceiver) { |
26 var method = getOpMethodName(this.op); | 28 var method = getOpMethodName(this.op); |
27 return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.left.toC() + ', ' + this.right.toC() + ')\n'; | 29 if (this.op == '|') { |
30 return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.right.toC() + ', ' + this.left.toC() + ')\n'; | |
31 } else { | |
32 return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.left.toC() + ', ' + this.right.toC() + ')\n'; | |
33 } | |
28 }; | 34 }; |
29 op.prototype.toCLLExpr = function(vars) { | 35 op.prototype.toCLLExpr = function(vars) { |
30 var opmap = {'=': '==', 'xor': '^'}; | 36 var opmap = {'=': '==', 'xor': '^'}; |
31 return this.left.toCLLExpr(vars) + (this.op in opmap ? opmap[this.op] : this.op) + this.right.toCLLExpr(vars); | 37 return this.left.toCLLExpr(vars) + (this.op in opmap ? opmap[this.op] : this.op) + this.right.toCLLExpr(vars); |
32 }; | 38 }; |
179 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) +';' ]; | 185 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) +';' ]; |
180 }; | 186 }; |
181 | 187 |
182 listlit.prototype.toC = function() { | 188 listlit.prototype.toC = function() { |
183 var ret = 'make_list(' + this.val.length; | 189 var ret = 'make_list(' + this.val.length; |
184 for (var i = 0; i < this.val.length; i++) { | 190 for (var i = this.val.length-1; i >= 0; i--) { |
185 ret += ', ' + this.val[i].toC(); | 191 ret += ', ' + this.val[i].toC(); |
186 } | 192 } |
187 return ret + ')'; | 193 return ret + ')'; |
188 } | 194 } |
189 | 195 |
843 return this.ast.toCModuleInstance(); | 849 return this.ast.toCModuleInstance(); |
844 }; | 850 }; |
845 | 851 |
846 function processUsedToplevel(toplevel) | 852 function processUsedToplevel(toplevel) |
847 { | 853 { |
848 var alwaysused = ['true', 'false']; | 854 var alwaysused = ['true', 'false', 'list']; |
849 var ret = ''; | 855 var ret = ''; |
850 var modulenum = 0; | 856 var modulenum = 0; |
851 var visited = {}; | 857 var visited = {}; |
852 for (var i in alwaysused) { | 858 for (var i in alwaysused) { |
853 toplevel.used[alwaysused[i]] = true; | 859 toplevel.used[alwaysused[i]] = true; |
893 var moduleinit = processUsedToplevel(toplevel); | 899 var moduleinit = processUsedToplevel(toplevel); |
894 debugprint('//------COMPILING AST-----'); | 900 debugprint('//------COMPILING AST-----'); |
895 var rest = 'object * mainModule() {\n' + moduleinit + '\tmain_module = ' + obj.toCModuleInstance() + ';\n\treturn main_module;\n}\n'; | 901 var rest = 'object * mainModule() {\n' + moduleinit + '\tmain_module = ' + obj.toCModuleInstance() + ';\n\treturn main_module;\n}\n'; |
896 return '#include "runtime/proghead.inc"\n' + | 902 return '#include "runtime/proghead.inc"\n' + |
897 '#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' + | 903 '#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' + |
904 '#define METHOD_ID_EMPTY ' + getMethodId('empty') + '\n' + | |
905 '#define METHOD_ID_CONS ' + getMethodId(getOpMethodName('|')) + '\n' + | |
898 forwarddec + toplevelcode + rest + '#include "runtime/progfoot.inc"\n'; | 906 forwarddec + toplevelcode + rest + '#include "runtime/progfoot.inc"\n'; |
899 } | 907 } |
900 | 908 |
901 object.prototype.toCModule = function() { | 909 object.prototype.toCModule = function() { |
902 return makeCProg(this); | 910 return makeCProg(this); |