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

View File

@ -67,7 +67,7 @@ public final class Value {
return ((Number) value).longValue() != 0;
if (value instanceof Boolean)
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);
}
/**