Skip to content

Commit

Permalink
Started to rewrite compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbebenita committed May 5, 2012
1 parent 5efc388 commit da6e8da
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 14 deletions.
8 changes: 4 additions & 4 deletions docs/css/docs.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -402,7 +402,7 @@ div.code pre:first-child {
}

.jcResult {
font-size: 10px;
font-size: 11px;
width: 600px;
}

Expand Down
61 changes: 60 additions & 1 deletion src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
7 changes: 0 additions & 7 deletions src/jc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -92,6 +86,5 @@ for (var i = 0; i < 1000; i++) {
}
print("Malloc: " + mTotal + ", Free: " + fTotal);
print("Done in " + (new Date() - start) + " checksum: " + sum);
*/
5 changes: 3 additions & 2 deletions src/jcc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("util.js");
load("esprima.js");
load("escodegen.js");
load("compiler.js");

var options = new OptionSet("option(s)");

Expand Down Expand Up @@ -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: " "}));



26 changes: 26 additions & 0 deletions src/tests/simple.jc
Original file line number Diff line number Diff line change
@@ -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.