Permalink
Browse files

Fix CLI bugs and casting arith op as type wider than int

  • Loading branch information...
1 parent bd58c56 commit 59d27b7f6a101bcae9c364225f5defd34e617c87 @syg syg committed May 24, 2012
Showing with 46 additions and 14 deletions.
  1. +1 −1 bin/ljc
  2. +28 −6 src/compiler.js
  3. +2 −0 src/esprima.js
  4. +15 −7 src/ljc.js
View
@@ -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();
View
@@ -96,6 +96,7 @@
var e = new Error(message);
e.node = logger.context[logger.context.length - 1];
+ e.logged = true;
throw e;
}
}
@@ -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) {
@@ -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;
@@ -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;
View
@@ -190,6 +190,8 @@
uint: true,
void: true,
num: true,
+ float: true,
+ double: true,
dyn: true
};
View
@@ -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);
@@ -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);
@@ -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;
@@ -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;

0 comments on commit 59d27b7

Please sign in to comment.