Added line numbers to HGS runtime exceptions

This commit is contained in:
hneemann 2018-03-22 23:15:22 +01:00
parent 2c10b211e1
commit efa68ed9a8
2 changed files with 18 additions and 14 deletions

View File

@ -142,11 +142,11 @@ public class Parser {
switch (refToken) { switch (refToken) {
case COLON: case COLON:
expect(EQUAL); expect(EQUAL);
final Expression initVal = linoE(parseExpression()); final Expression initVal = parseExpression();
if (isRealStatement) expect(SEMICOLON); if (isRealStatement) expect(SEMICOLON);
return lino(c -> ref.declareVar(c, initVal.value(c))); return lino(c -> ref.declareVar(c, initVal.value(c)));
case EQUAL: case EQUAL:
final Expression val = linoE(parseExpression()); final Expression val = parseExpression();
if (isRealStatement) expect(SEMICOLON); if (isRealStatement) expect(SEMICOLON);
return lino(c -> { return lino(c -> {
final Object value = val.value(c); final Object value = val.value(c);
@ -171,32 +171,32 @@ public class Parser {
final String str = tok.readText(); final String str = tok.readText();
return c -> c.print(str); return c -> c.print(str);
case EQUAL: case EQUAL:
final Expression exp = linoE(parseExpression()); final Expression exp = parseExpression();
if (tok.peek() != CODEEND) expect(SEMICOLON); if (tok.peek() != CODEEND) expect(SEMICOLON);
return lino(c -> c.print(exp.value(c).toString())); return lino(c -> c.print(exp.value(c).toString()));
case IF: case IF:
expect(OPEN); expect(OPEN);
final Expression ifCond = linoE(parseExpression()); final Expression ifCond = toBool(parseExpression());
expect(CLOSE); expect(CLOSE);
final Statement ifStatement = parseStatement(); final Statement ifStatement = parseStatement();
if (nextIs(ELSE)) { if (nextIs(ELSE)) {
final Statement elseStatement = parseStatement(); final Statement elseStatement = parseStatement();
return c -> { return c -> {
if (Value.toBool(ifCond.value(c))) if ((boolean) ifCond.value(c))
ifStatement.execute(c); ifStatement.execute(c);
else else
elseStatement.execute(c); elseStatement.execute(c);
}; };
} else } else
return c -> { return c -> {
if (Value.toBool(ifCond.value(c))) if ((boolean) ifCond.value(c))
ifStatement.execute(c); ifStatement.execute(c);
}; };
case FOR: case FOR:
expect(OPEN); expect(OPEN);
Statement init = parseStatement(false); // parse like an expression Statement init = parseStatement(false); // parse like an expression
expect(SEMICOLON); expect(SEMICOLON);
final Expression forCond = linoE(parseExpression()); final Expression forCond = toBool(parseExpression());
expect(SEMICOLON); expect(SEMICOLON);
Statement inc = parseStatement(false); // parse like an expression Statement inc = parseStatement(false); // parse like an expression
expect(CLOSE); expect(CLOSE);
@ -204,30 +204,30 @@ public class Parser {
return c -> { return c -> {
Context iC = new Context(c, false); Context iC = new Context(c, false);
init.execute(iC); init.execute(iC);
while (Value.toBool(forCond.value(iC))) { while ((boolean) forCond.value(iC)) {
inner.execute(iC); inner.execute(iC);
inc.execute(iC); inc.execute(iC);
} }
}; };
case WHILE: case WHILE:
expect(OPEN); expect(OPEN);
final Expression whileCond = linoE(parseExpression()); final Expression whileCond = toBool(parseExpression());
expect(CLOSE); expect(CLOSE);
inner = parseStatement(); inner = parseStatement();
return c -> { return c -> {
Context iC = new Context(c, false); Context iC = new Context(c, false);
while (Value.toBool(whileCond.value(iC))) inner.execute(iC); while ((boolean) whileCond.value(iC)) inner.execute(iC);
}; };
case REPEAT: case REPEAT:
final Statement repeatInner = parseStatement(); final Statement repeatInner = parseStatement();
expect(UNTIL); expect(UNTIL);
final Expression repeatCond = linoE(parseExpression()); final Expression repeatCond = toBool(parseExpression());
if (isRealStatement) expect(SEMICOLON); if (isRealStatement) expect(SEMICOLON);
return c -> { return c -> {
Context iC = new Context(c, false); Context iC = new Context(c, false);
do { do {
repeatInner.execute(iC); repeatInner.execute(iC);
} while (!Value.toBool(repeatCond.value(iC))); } while (!(boolean) repeatCond.value(iC));
}; };
case OPENBRACE: case OPENBRACE:
Statements s = new Statements(); Statements s = new Statements();
@ -235,7 +235,7 @@ public class Parser {
s.add(parseStatement()); s.add(parseStatement());
return s.optimize(); return s.optimize();
case RETURN: case RETURN:
Expression retExp = linoE(parseExpression()); Expression retExp = parseExpression();
expect(SEMICOLON); expect(SEMICOLON);
return lino(c -> FirstClassFunctionCall.returnFromFunc(retExp.value(c))); return lino(c -> FirstClassFunctionCall.returnFromFunc(retExp.value(c)));
case FUNC: case FUNC:
@ -248,6 +248,10 @@ public class Parser {
} }
} }
private Expression toBool(Expression expression) {
return linoE(c -> Value.toBool(expression.value(c)));
}
private ArrayList<Expression> parseArgList() throws IOException, ParserException { private ArrayList<Expression> parseArgList() throws IOException, ParserException {
ArrayList<Expression> args = new ArrayList<>(); ArrayList<Expression> args = new ArrayList<>();
if (!nextIs(CLOSE)) { if (!nextIs(CLOSE)) {

View File

@ -67,7 +67,7 @@ public final class Value {
return ((Number) value).longValue() != 0; return ((Number) value).longValue() != 0;
if (value instanceof Boolean) if (value instanceof Boolean)
return ((Boolean) value); return ((Boolean) value);
throw new HGSEvalException("must be an integer or a bool, is: " + value.getClass().getSimpleName()); throw new HGSEvalException("Must be an integer or a bool, is: " + value.getClass().getSimpleName() + "=" + value);
} }
/** /**