diff --git a/lib/validate.js b/lib/validate.js index dc95adf..137bfeb 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -248,6 +248,18 @@ Vp.returnType = function returnType(stmt) { return ty.Double; }); + when({ + type: 'UnaryExpression', + operator: '-', + argument: { + type: 'Literal', + value: match.number, + raw: hasDot + } + }, function() { + return ty.Double; + }); + when({ type: 'Literal', value: match.all(match.number, @@ -256,6 +268,19 @@ Vp.returnType = function returnType(stmt) { }, function() { return ty.Signed; }); + + when({ + type: 'UnaryExpression', + operator: '-', + argument: { + type: 'Literal', + value: match.all(match.number, + match.range(0, 0x80000001)), + raw: dotless + } + }, function() { + return ty.Signed; + }); }); }; @@ -471,12 +496,36 @@ Vp.local = function local(x, rhs) { return ty.Double; }, this); + when({ + type: 'UnaryExpression', + operator: '-', + argument: { + type: 'Literal', + value: match.number, + raw: hasDot + } + }, function() { + return ty.Double; + }); + when({ type: 'Literal', value: match.all(match.integer, match.range(-0x80000000, 0x100000000)) }, function(vars) { return ty.Int; }, this); + + when({ + type: 'UnaryExpression', + operator: '-', + argument: { + type: 'Literal', + value: match.all(match.number, match.range(0, 0x80000001)), + raw: dotless + } + }, function() { + return ty.Int; + }); }); }; @@ -894,6 +943,19 @@ Vp.expression = function expression(e) { return this.expression(last); }, this); + when({ + type: 'UnaryExpression', + operator: '-', + argument: { + type: 'Literal', + value: match.all(match.number, + match.range(0, 0x80000001)), + raw: dotless + } + }, function(vars) { + return ty.Signed; + }, this); + when({ type: 'UnaryExpression', operator: '~', diff --git a/test/index.js b/test/index.js index a787115..ba70407 100644 --- a/test/index.js +++ b/test/index.js @@ -354,3 +354,31 @@ exports.testForWithoutUpdate = asmAssert.one( } }, { pass: true }); + +exports.testNegativeIntReturn = asmAssert.one( + "negative integer literal as return value", + function f() { + return -42; + }, + { pass: true }); + +exports.testNegativeDoubleReturn = asmAssert.one( + "negative double literal as return value", + function f() { + return -42.1; + }, + { pass: true }); + +exports.testNegativeIntVar = asmAssert.one( + "negative integer literal as variable declaration", + function f() { + var i = -42; + }, + { pass: true }); + +exports.testNegativeDoubleVar = asmAssert.one( + "negative double literal as variable declaration", + function f() { + var i = -42.1; + }, + { pass: true });