Permalink
Browse files

Started to rewrite compiler.

  • Loading branch information...
1 parent 5efc388 commit da6e8da661fb0efbcdc3915b72e6db9c887df503 @mbebenita mbebenita committed May 5, 2012
Showing with 93 additions and 14 deletions.
  1. +4 −4 docs/css/docs.css
  2. +60 −1 src/compiler.js
  3. +0 −7 src/jc.js
  4. +3 −2 src/jcc.js
  5. +26 −0 src/tests/simple.jc
View
@@ -62,10 +62,10 @@ table.definitions td {
*/
code, pre, tt, textarea {
font-family: Monaco, Consolas, "Lucida Console", monospace;
- font-size: 10px;
- font-weight: bold;
+ font-size: 12px;
+ // font-weight: bold;
line-height: 18px;
- color: #845;
+ color: #255;
white-space: pre-wrap;
word-wrap: break-word;
}
@@ -402,7 +402,7 @@ div.code pre:first-child {
}
.jcResult {
- font-size: 10px;
+ font-size: 11px;
width: 600px;
}
View
@@ -1291,7 +1291,7 @@ ForStatement.prototype = {
}
};
-function compile(source, generateExports) {
+function compile2(source, generateExports) {
var program = parser.parse(source);
// print (JSON.stringify(program, null, 2));
program.computeType(types);
@@ -1304,4 +1304,63 @@ function compile(source, generateExports) {
return str;
}
+function walk(node, match) {
+ print("Walk : " + node.type);
+ var fn = match[node.type];
+ if (fn) {
+ fn.call(node);
+ } else {
+ for (var key in node) {
+ var val = node[key];
+ if (val) {
+ if (val instanceof Object && "type" in val) {
+ walk(val, match);
+ } else if (val instanceof Array) {
+ for (var i = 0; i < val.length; i++) {
+ if (val[i] instanceof Object && "type" in val[i]) {
+ walk(val[i], match);
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+
+function compile(node) {
+ print (JSON.stringify(node, null, 2));
+
+ computeTypes(node);
+ print (JSON.stringify(node, null, 2));
+}
+
+function computeTypes(node) {
+ var scope = node.scope = new Scope(null, "Program", true);
+ var match = {
+ VariableDeclaration: function (scope) {
+ var type = getType(this.typeSpecifier);
+ walkList(this.declarations, {
+ VariableDeclarator: function () {
+ if (this.pointer) {
+ for (var i = 0; i < this.pointer.count; i++) {
+ type = new PointerType(type);
+ }
+ }
+ scope.addVariable(this.variable = new Variable(this.id.name, type));
+ }
+ });
+ }
+ };
+ function walk(node, scope) {
+ assert (node.type in match);
+ return match[node.type].call(node, scope);
+ }
+ function walkList(list, match) {
+ for (var i = 0; i < list.length; i++) {
+ match[list[i].type].call(list[i]);
+ }
+ }
+ walk(node, match);
+}
View
@@ -61,18 +61,12 @@ var o = fn();
var start = new Date();
-
-var malloc = extern.malloc;
-var free = extern.free;
-
function time (fn) {
var start = new Date();
fn();
return new Date() - start;
}
-
var mTotal = 0, fTotal = 0;
-
var sum = 0;
for (var i = 0; i < 1000; i++) {
var ptrs = new Uint32Array(10000);
@@ -92,6 +86,5 @@ for (var i = 0; i < 1000; i++) {
}
print("Malloc: " + mTotal + ", Free: " + fTotal);
-
print("Done in " + (new Date() - start) + " checksum: " + sum);
*/
View
@@ -1,6 +1,7 @@
load("util.js");
load("esprima.js");
load("escodegen.js");
+load("compiler.js");
var options = new OptionSet("option(s)");
@@ -32,9 +33,9 @@ var source = snarf(file);
// var node = esprima.parse(source);
var node = esprima.parse(source);
-print (JSON.stringify(node, null, 2));
+compile(node);
-print (escodegen.generate(node, {base: "", indent: " "}));
+// print (escodegen.generate(node, {base: "", indent: " "}));
View
@@ -1,5 +1,31 @@
+struct Header {
+
+}
+
+var uint nUnits = (nBytes + 2 - 1) / 3 + 1;
+
+/*
var int x;
+
var int *y = &x;
+
var int **z = &y;
*y = 1;
**z = ***(&z);
+
+for (var int x; true; x++) {
+
+}
+
+function foo (int *x, b) {
+
+}
+
+struct A {
+ var int a, *b;
+}
+
+function foo (int *x, b) {
+
+}
+*/

0 comments on commit da6e8da

Please sign in to comment.