Skip to content

Commit

Permalink
Fix CLI bugs and casting arith op as type wider than int
Browse files Browse the repository at this point in the history
  • Loading branch information
syg committed May 24, 2012
1 parent bd58c56 commit 59d27b7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bin/ljc
Expand Up @@ -2,6 +2,6 @@

var path = require('path');
var fs = require('fs');
var dir = path.join(path.dirname(fs.realpathSync(__filename)), '..');
var dir = path.join(path.dirname(fs.realpathSync(__filename)), '../src');

require(path.join(dir, 'ljc')).cli();
34 changes: 28 additions & 6 deletions src/compiler.js
Expand Up @@ -96,6 +96,7 @@

var e = new Error(message);
e.node = logger.context[logger.context.length - 1];
e.logged = true;
throw e;
}
}
Expand Down Expand Up @@ -875,10 +876,16 @@
return this;
};

CastExpression.prototype.transformNode = function (o) {
CastExpression.prototype.transform = function (o) {
if (this.as && !(this.ty = this.as.reflect(o))) {
return this.argument;
return this.argument.transform(o);
}

o = extend(o);
o.wantsToBe = this.ty;
this.argument = this.argument.transform(o);

return this;
};

Literal.prototype.transformNode = function (o) {
Expand Down Expand Up @@ -973,10 +980,17 @@
ty = i32ty;
} else if (BINOP_BITWISE.indexOf(op) >= 0) {
ty = i32ty;
} else if (BINOP_ARITHMETIC.indexOf(op) >= 0 && (lty && lty.numeric) && (rty && rty.numeric)) {
// Arithmetic on ints now begets ints. Force a CastExpression here so we
// convert it during lowering without warnings.
if (lty.integral && rty.integral) {
} else if (BINOP_ARITHMETIC.indexOf(op) >= 0 &&
(lty instanceof PrimitiveType && lty.numeric) &&
(rty instanceof PrimitiveType && rty.numeric)) {
// Arithmetic on ints now begets ints, unless it wants to be a wider
// primitive from an outside cast.
var wantsToBe;
if (lty.integral && rty.integral &&
!(((wantsToBe = o.wantsToBe) instanceof PrimitiveType) &&
wantsToBe.size > i32ty.size)) {
// Force a CastExpression here so we convert it during lowering
// without warnings.
return cast(this, i32ty, true);
}
ty = f64ty;
Expand Down Expand Up @@ -1179,6 +1193,14 @@
quote(tystr(rty, 0)) + " without cast", true);
}

if (!this.numeric) {
return expr;
}

if (!this.integral) {
return new CallExpression(new Identifier("Number"), [expr], expr.loc);
}

var conversion;
var lwidth = this.size << 3;
var rwidth = rty ? rty.size << 3 : 8;
Expand Down
2 changes: 2 additions & 0 deletions src/esprima.js
Expand Up @@ -190,6 +190,8 @@
uint: true,
void: true,
num: true,
float: true,
double: true,
dyn: true
};

Expand Down
22 changes: 15 additions & 7 deletions src/ljc.js
Expand Up @@ -131,7 +131,11 @@
}

var filename = files[0];
var basename = filename.substr(0, filename.lastIndexOf('.')) || filename;
var path = filename.split("/");
var basename = path.pop();
var dir = path.join("/");
basename = basename.substr(0, basename.lastIndexOf(".")) || basename;

var source = snarf(filename);
var code = compile(basename, filename, source, options);

Expand All @@ -141,11 +145,12 @@
// SpiderMonkey has no way to write to a file, but if we're on node we can
// emit .js.
if (mode === NODE_JS) {
var outname = dir + "/" + basename;
if (options["emit-ast"]) {
require('fs').writeFileSync(basename + ".json", JSON.stringify(code, null, 2));
require('fs').writeFileSync(outname + ".json", JSON.stringify(code, null, 2));
} else {
// Escodegen doesn't emit a final newline for some reason, so add one.
require('fs').writeFileSync(basename + ".js", code + "\n");
require('fs').writeFileSync(outname + ".js", code + "\n");
}
} else {
print(code);
Expand All @@ -163,8 +168,9 @@
}

var logger = new util.Logger("ljc", logName, source, options);
var code;

try {
var code;
var node = esprima.parse(source, { loc: true });
if (options["only-parse"]) {
code = node;
Expand All @@ -176,25 +182,27 @@
code = escodegen.generate(node, { base: "", indent: " " });
}
}
return code;
} catch (e) {
if (e.lineNumber) {
// Esprima error, make a loc out of it.
var lc = { line: e.lineNumber, column: e.column };
logger.error(e.message, { start: lc, end: lc });
logger.flush();
quit();
}

if (e.logged) {
// Compiler error thta has already been logged, so just flush and
// quit.
logger.flush();
quit();
}

throw e;
} finally {
logger.flush();
}

logger.flush();
return code;
}

exports.cli = cli;
Expand Down

0 comments on commit 59d27b7

Please sign in to comment.