8000 check types of expressions passed to log / assert · repos-javascript-compilers/JSX@39cd05e · GitHub
[go: up one dir, main page]

Skip to content

Commit 39cd05e

Browse files
committed
check types of expressions passed to log / assert
1 parent 4e595a8 commit 39cd05e

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

lib/parser.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,10 @@ var Parser = exports.Parser = Class.extend({
441441
return this._throwStatement();
442442
else if (this._expectKeywordOpt("try") != null)
443443
return this._tryStatement();
444-
else if (this._expectKeywordOpt("assert") != null)
445-
return this._assertStatement();
446-
else if (this._expectKeywordOpt("log") != null)
447-
return this._logStatement();
444+
else if ((token = this._expectKeywordOpt("assert")) != null)
445+
return this._assertStatement(token);
446+
else if ((token = this._expectKeywordOpt("log")) != null)
447+
return this._logStatement(token);
448448
// labelled or expression statement
449449
var identifier = this._expectIdentifierOpt();
450450
if (identifier != null) {
@@ -723,23 +723,23 @@ var Parser = exports.Parser = Class.extend({
723723
return true;
724724
},
725725

726-
_assertStatement: function () {
726+
_assertStatement: function (token) {
727727
var expr = this._expr();
728728
if (expr == null)
729729
return false;
730730
if (this._expectKeyword(";") == null)
731731
return false;
732-
this._statements.push(new AssertStatement(expr));
732+
this._statements.push(new AssertStatement(token, expr));
733733
},
734734

735-
_logStatement: function () {
735+
_logStatement: function (token) {
736736
var expr = this._commaSeparatedExprs(false);
737737
if (expr == null) {
738738
return false;
739739
}
740740
if (this._expectKeyword(";") == null)
741741
return null;
742-
this._statements.push(new LogStatement(expr));
742+
this._statements.push(new LogStatement(token, expr));
743743
return true;
744744
},
745745

lib/statement.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Class = require("./Class");
2-
eval(Class.$import("./expression.js"));
3-
eval(Class.$import("./util.js"));
2+
eval(Class.$import("./expression"));
3+
eval(Class.$import("./type"));
4+
eval(Class.$import("./util"));
45

56
"use strict";
67

@@ -491,47 +492,75 @@ var TryStatement = exports.TryStatement = Statement.extend({
491492

492493
var InformationStatement = exports.InformationStatement = Statement.extend({
493494

494-
initialize: function (exprs) {
495+
initialize: function (token, exprs) {
496+
this._token = token;
495497
this._exprs = exprs;
496498
},
497499

500+
getToken: function () {
501+
return this._token;
502+
},
503+
498504
getExprs: function () {
499505
return this._exprs;
500506
},
501507

502-
analyze: function (context) {
508+
_analyzeExprs: function (context) {
503509
for (var i = 0; i < this._exprs.length; ++i)
504-
this._exprs[i].analyze(context);
510+
if (! this._exprs[i].analyze(context))
511+
return false;
512+
return true;
505513
}
506514

507515
});
508516

509517
var AssertStatement = exports.AssertStatement = InformationStatement.extend({
510518

511-
initialize: function (exprs) {
512-
InformationStatement.prototype.initialize.call(this, exprs);
519+
initialize: function (token, exprs) {
520+
InformationStatement.prototype.initialize.call(this, token, exprs);
513521
},
514522

515523
serialize: function () {
516524
return [
517525
"AssertStatement",
518526
Util.serializeArray(this._exprs)
519527
];
528+
},
529+
530+
analyze: function (context) {
531+
if (! this._analyzeExprs(context))
532+
return;
533+
var exprType = this._exprs[this._exprs.length - 1].getType();
534+
if (exprType.equals(Type.voidType))
535+
context.errors.push(new CompileError(this._token, "cannot assert type void"));
536+
else if (exprType.equals(Type.nullType))
537+
context.errors.push(new CompileError(this._token, "assertion never succeeds"));
520538
}
521539

522540
});
523541

524542
var LogStatement = exports.LogStatement = InformationStatement.extend({
525543

526-
initialize: function (exprs) {
527-
InformationStatement.prototype.initialize.call(this, exprs);
544+
initialize: function (token, exprs) {
545+
InformationStatement.prototype.initialize.call(this, token, exprs);
528546
},
529547

530548
serialize: function () {
531549
return [
532550
"LogStatement",
533551
Util.serializeArray(this._exprs)
534552
];
553+
},
554+
555+
analyze: function (context) {
556+
if (! this._analyzeExprs(context))
557+
return;
558+
for (var i = 0; i < this._exprs.length; ++i) {
559+
if (this._exprs[i].getType().equals(Type.voidType)) {
560+
context.errors.push(new CompileError(this._token, "cannot log a void expression"));
561+
break;
562+
}
563+
}
535564
}
536565

537566
});

0 commit comments

Comments
 (0)
0