validate more statements

This commit is contained in:
Dave Holoway
2020-06-29 19:35:19 +01:00
parent 2f5ed65461
commit 1b2cd957ec
5 changed files with 43 additions and 10 deletions

View File

@@ -1,12 +1,12 @@
/** /**
* @typedef {import('./Statement').Statement} Statement * @typedef {import('./Statement').Statement} Statement
* @typedef {import('../body-types').Local} Local * @typedef {import('../body-types').Local} Local
* @typedef {import('../body-types').ResolvedIdent} ResolvedIdent
* @typedef {import('../body-types').ValidateInfo} ValidateInfo * @typedef {import('../body-types').ValidateInfo} ValidateInfo
* @typedef {import('../tokenizer').Token} Token * @typedef {import('../tokenizer').Token} Token
*/ */
const { KeywordStatement } = require("./KeywordStatement"); const { KeywordStatement } = require("./KeywordStatement");
const { checkNonVarDeclStatement } = require('../statement-validater'); const { checkNonVarDeclStatement } = require('../statement-validater');
const { Local, ResolvedIdent } = require('../body-types');
class ForStatement extends KeywordStatement { class ForStatement extends KeywordStatement {
/** @type {ResolvedIdent[] | Local[]} */ /** @type {ResolvedIdent[] | Local[]} */
@@ -24,7 +24,26 @@ class ForStatement extends KeywordStatement {
* @param {ValidateInfo} vi * @param {ValidateInfo} vi
*/ */
validate(vi) { validate(vi) {
if (this.init) {
this.init.forEach(x => {
if (x instanceof ResolvedIdent) {
x.resolveExpression(vi);
} else if (x instanceof Local) {
if (x.init) {
x.init.resolveExpression(vi);
}
}
})
}
if (this.test) {
this.test.resolveExpression(vi);
}
if (this.update) {
this.update.forEach(e => e.resolveExpression(vi));
}
if (this.iterable) {
this.iterable.resolveExpression(vi);
}
if (this.statement) { if (this.statement) {
vi.statementStack.unshift('for'); vi.statementStack.unshift('for');
checkNonVarDeclStatement(this.statement, vi); checkNonVarDeclStatement(this.statement, vi);

View File

@@ -19,9 +19,10 @@ class IfStatement extends KeywordStatement {
* @param {ValidateInfo} vi * @param {ValidateInfo} vi
*/ */
validate(vi) { validate(vi) {
const value = this.test.resolveExpression(vi); if (this.test) {
checkBooleanBranchCondition(value, () => this.test.tokens, vi.problems); const value = this.test.resolveExpression(vi);
checkBooleanBranchCondition(value, () => this.test.tokens, vi.problems);
}
if (this.statement) { if (this.statement) {
vi.statementStack.unshift('if'); vi.statementStack.unshift('if');
checkNonVarDeclStatement(this.statement, vi); checkNonVarDeclStatement(this.statement, vi);
@@ -29,7 +30,7 @@ class IfStatement extends KeywordStatement {
} }
if (this.elseStatement) { if (this.elseStatement) {
vi.statementStack.unshift('else'); vi.statementStack.unshift('else');
checkNonVarDeclStatement(this.statement, vi); checkNonVarDeclStatement(this.elseStatement, vi);
vi.statementStack.shift(); vi.statementStack.shift();
} }
} }

View File

@@ -59,7 +59,8 @@ class SwitchStatement extends KeywordStatement {
} else { } else {
vi.problems.push(ParseProblem.Error(c.tokens, `Expression expected`)); vi.problems.push(ParseProblem.Error(c.tokens, `Expression expected`));
} }
}) });
caseblock.statements.forEach(statement => statement.validate(vi));
}) })
vi.statementStack.shift(); vi.statementStack.shift();

View File

@@ -5,6 +5,7 @@
*/ */
const { KeywordStatement } = require("./KeywordStatement"); const { KeywordStatement } = require("./KeywordStatement");
const { ResolvedIdent } = require('../body-types'); const { ResolvedIdent } = require('../body-types');
const { Block } = require('./Block');
class TryStatement extends KeywordStatement { class TryStatement extends KeywordStatement {
/** @type {(ResolvedIdent|Local[])[]} */ /** @type {(ResolvedIdent|Local[])[]} */
@@ -28,6 +29,16 @@ class TryStatement extends KeywordStatement {
this.block.validate(vi); this.block.validate(vi);
vi.statementStack.shift(); vi.statementStack.shift();
} }
this.catches.forEach(c => {
if (c instanceof Block) {
// finally
c.validate(vi);
} else if (c.block) {
// catch block
c.block.validate(vi);
}
})
} }
} }

View File

@@ -16,9 +16,10 @@ class WhileStatement extends KeywordStatement {
* @param {ValidateInfo} vi * @param {ValidateInfo} vi
*/ */
validate(vi) { validate(vi) {
const value = this.test.resolveExpression(vi); if (this.test) {
checkBooleanBranchCondition(value, () => this.test.tokens, vi.problems); const value = this.test.resolveExpression(vi);
checkBooleanBranchCondition(value, () => this.test.tokens, vi.problems);
}
if (this.statement) { if (this.statement) {
vi.statementStack.unshift('while'); vi.statementStack.unshift('while');
this.statement.validate(vi); this.statement.validate(vi);