From 29067fc6eeb8c753dccba98c113bdb7bbeffb3e8 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Mon, 5 Jun 2017 22:29:55 +0100 Subject: [PATCH 1/2] Accept negative literals as return values The parser handles negative literals as a unary minus expression rather than a literal, and so we need to match UnaryExpression as an extra case. --- lib/validate.js | 38 ++++++++++++++++++++++++++++++++++++++ test/index.js | 14 ++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/validate.js b/lib/validate.js index dc95adf..e72c0c1 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; + }); }); }; @@ -894,6 +919,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..3767250 100644 --- a/test/index.js +++ b/test/index.js @@ -354,3 +354,17 @@ 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 }); From 62a2f97692ee7dbfd7ba18335ea01f5b148cc34f Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Tue, 6 Jun 2017 01:29:02 +0100 Subject: [PATCH 2/2] Allow negative literals in variable declarations --- lib/validate.js | 24 ++++++++++++++++++++++++ test/index.js | 14 ++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/validate.js b/lib/validate.js index e72c0c1..137bfeb 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -496,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; + }); }); }; diff --git a/test/index.js b/test/index.js index 3767250..ba70407 100644 --- a/test/index.js +++ b/test/index.js @@ -368,3 +368,17 @@ exports.testNegativeDoubleReturn = asmAssert.one( 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 });