Skip to content

Commit

Permalink
Replace Error with Assertion
Browse files Browse the repository at this point in the history
Assertion violation errors currently report the location of the falsy
expression, but we'd prefer to print the actual expression (#16)
  • Loading branch information
Botffy committed Oct 18, 2019
1 parent 645310d commit 0e53d2d
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 29 deletions.
4 changes: 1 addition & 3 deletions examples/raise_error.plang → examples/assertions.plang
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ program hibás_bemenet
ki: "Írj be egy természetes számot!", SV
be: x

ha x < 0 akkor
hiba: "nem természetes szám"
ha_vége
szerintem x > 0
program_vége
2 changes: 1 addition & 1 deletion src/main/java/ppke/itk/xplang/ast/ASTVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface ASTVisitor {
void visit(Loop loop);
void visit(Input input);
void visit(Output output);
void visit(ErrorRaising errorRaising);
void visit(Assertion assertion);
void visit(FunctionCall functionCall);
void visit(ConditionalConnective conditionalConnective);
void visit(VarRef varRef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import ppke.itk.xplang.common.Location;

public class ErrorRaising extends Statement {
public ErrorRaising(Location location, RValue errorMessage) {
public class Assertion extends Statement {
public Assertion(Location location, RValue condition) {
super(location);
this.children.add(0, errorMessage);
this.children.add(0, condition);
}

public RValue getErrorMessage() {
public RValue getCondition() {
return (RValue) this.children.get(0);
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/ppke/itk/xplang/interpreter/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ public void visit(Output output) throws InterpreterError {
}

@Override
public void visit(ErrorRaising errorRaising) throws InterpreterError {
errorRaising.getErrorMessage().accept(this);
StringValue message = valueStack.pop(StringValue.class);
throw new InterpreterError(ErrorCode.ASSERTION_FAILURE, message.getValue());
public void visit(Assertion assertion) throws InterpreterError {
assertion.getCondition().accept(this);
BooleanValue evaulated = valueStack.pop(BooleanValue.class);
if (evaulated.equals(BooleanValue.FALSE)) {
throw new InterpreterError(ErrorCode.ASSERTION_FAILURE, assertion.getCondition().location());
}
}

@Override public void visit(FunctionCall call) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package ppke.itk.xplang.lang;

import ppke.itk.xplang.ast.RValue;
import ppke.itk.xplang.ast.ErrorRaising;
import ppke.itk.xplang.ast.Assertion;
import ppke.itk.xplang.common.Location;
import ppke.itk.xplang.parser.*;
import ppke.itk.xplang.type.Archetype;

class ErrorStatementParser {
static ErrorRaising parse(Parser parser) throws ParseError {
Location startLoc = parser.accept(parser.symbol(PlangSymbol.ERROR)).location();
parser.accept(parser.symbol(PlangSymbol.COLON));
class AssertStatementParser {
static Assertion parse(Parser parser) throws ParseError {
Location startLoc = parser.accept(parser.symbol(PlangSymbol.ASSERT)).location();

Expression messageExpression = parser.parseExpression();
Location endLoc = messageExpression.getLocation();
Expression assertion = parser.parseExpression();
Location endLoc = assertion.getLocation();
RValue message = TypeChecker.in(parser.context())
.checking(messageExpression)
.expecting(Archetype.STRING_TYPE)
.checking(assertion)
.expecting(Archetype.BOOLEAN_TYPE)
.withCustomErrorMessage(
node -> new ParseError(node.location(), ErrorCode.TYPE_MISMATCH_ERROR_MESSAGE, node.getType())
node -> new ParseError(node.location(), ErrorCode.TYPE_MISMATCH_ASSERTION, node.getType())
)
.build()
.resolve();

Location location = Location.between(startLoc, endLoc);

return new ErrorRaising(location, message);
return new Assertion(location, message);
}
}
2 changes: 1 addition & 1 deletion src/main/java/ppke/itk/xplang/lang/PlangGrammar.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void setup(Context ctx) {
makeSymbol(PlangSymbol.LOOP).register(ctx);
makeSymbol(PlangSymbol.WHILE).register(ctx);
makeSymbol(PlangSymbol.END_LOOP).register(ctx);
makeSymbol(PlangSymbol.ERROR).register(ctx);
makeSymbol(PlangSymbol.ASSERT).register(ctx);
makeSymbol(PlangSymbol.ASSIGNMENT).register(ctx);
makeSymbol(PlangSymbol.IN).register(ctx);
makeSymbol(PlangSymbol.OUT).register(ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ppke/itk/xplang/lang/PlangSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum PlangSymbol {
LOOP,
WHILE,
END_LOOP,
ERROR,
ASSERT,
ASSIGNMENT,
IN,
OUT,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ppke/itk/xplang/lang/StatementParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class StatementParser {
statementParsers.put(PlangSymbol.OUT, OutputStatementParser::parse);
statementParsers.put(PlangSymbol.OPEN, OpenStatementParser::parse);
statementParsers.put(PlangSymbol.CLOSE, CloseStatementParser::parse);
statementParsers.put(PlangSymbol.ERROR, ErrorStatementParser::parse);
statementParsers.put(PlangSymbol.ASSERT, AssertStatementParser::parse);
}

private StatementParser() { /* empty private ctor */ }
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ppke/itk/xplang/parser/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum ErrorCode {
TYPE_MISMATCH_ARRAY_INDEX,
TYPE_MISMATCH_NOT_SLICABLE,
TYPE_MISMATCH_SLICE_INDEX,
TYPE_MISMATCH_ERROR_MESSAGE,
TYPE_MISMATCH_ASSERTION,
TYPE_MISMATCH_NOT_READABLE,

FUNCTION_AMBIGUOUS,
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/language/plang/lexical.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ symbol.pattern.ENDIF=ha_v[ée]ge
symbol.pattern.LOOP=ciklus
symbol.pattern.WHILE=am[íi]g
symbol.pattern.END_LOOP=ciklus_v[ée]ge
symbol.pattern.ERROR=hiba
symbol.pattern.ASSERT=szerintem
symbol.pattern.ASSIGNMENT=:[^\\S\\r\\n]*=
symbol.pattern.IN=BE
symbol.pattern.OUT=KI
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/compiler_messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TYPE_MISMATCH_NOT_ADDRESSABLE=Arrays and strings can only be indexed with intege
TYPE_MISMATCH_ARRAY_INDEX=The [] operator is only defined for strings and array types (found %s)
TYPE_MISMATCH_NOT_SLICABLE=Arrays and strings can only be indexed with integers (found %s)
TYPE_MISMATCH_SLICE_INDEX=The [:] operator is only defined for strings and array tpyes (found %s)
TYPE_MISMATCH_ERROR_MESSAGE=The error argument must be a string (found %s)
TYPE_MISMATCH_ASSERTION=The assertion must be a boolean expression (found %s)
TYPE_MISMATCH_NOT_READABLE=Cannot read data from variables of type %s
FUNCTION_AMBIGUOUS=Function %1$s is ambiguous in context %3$s. Possible interpretations are: %2$s
NO_VIABLE_FUNCTION=No function '%1$s' matches context %3$s. Candidates were: %2$s
Expand Down

0 comments on commit 0e53d2d

Please sign in to comment.