diff 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
line wrap: on
line diff
--- a/cbackend.js	Fri Aug 09 21:01:11 2013 -0700
+++ b/cbackend.js	Tue Aug 13 21:58:03 2013 -0700
@@ -14,7 +14,9 @@
 
 function getOpMethodName(opname)
 {
-	var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_', '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_', '.': 'CAT_', '&&':'if', '||':'ifnot'};
+	var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_',
+	                '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_',
+					'.': 'CAT_', '&&':'if', '||':'ifnot', '|': 'CONS_'};
 	if (opname in optoMeth) {
 		return optoMeth[opname];
 	} else {
@@ -24,7 +26,11 @@
 
 op.prototype.toC = function(isReceiver) {
 	var method = getOpMethodName(this.op);
-	return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.left.toC() + ', ' + this.right.toC() + ')\n';
+	if (this.op == '|') {
+		return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.right.toC() + ', ' + this.left.toC() + ')\n';
+	} else {
+		return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.left.toC() + ', ' + this.right.toC() + ')\n';
+	}
 };
 op.prototype.toCLLExpr = function(vars) {
 	var opmap = {'=': '==', 'xor': '^'};
@@ -181,7 +187,7 @@
 
 listlit.prototype.toC = function() {
 	var ret = 'make_list(' + this.val.length;
-	for (var i = 0; i < this.val.length; i++) {
+	for (var i = this.val.length-1; i >= 0; i--) {
 		ret += ', ' + this.val[i].toC();
 	}
 	return ret + ')';
@@ -845,7 +851,7 @@
 
 function processUsedToplevel(toplevel)
 {
-	var alwaysused = ['true', 'false'];
+	var alwaysused = ['true', 'false', 'list'];
 	var ret = '';
 	var modulenum = 0;
 	var visited = {};
@@ -895,6 +901,8 @@
 	var rest = 'object * mainModule() {\n' + moduleinit + '\tmain_module = ' + obj.toCModuleInstance() + ';\n\treturn main_module;\n}\n';
 	return '#include "runtime/proghead.inc"\n' +
 		'#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' +
+		'#define METHOD_ID_EMPTY ' + getMethodId('empty') + '\n' +
+		'#define METHOD_ID_CONS ' + getMethodId(getOpMethodName('|')) + '\n' +
 		forwarddec + toplevelcode + rest + '#include "runtime/progfoot.inc"\n';
 }