1 Commits

Author SHA1 Message Date
Gašper Dobrovoljc
018a80b0f7 Added until statement 2025-06-03 22:39:32 +02:00
4 changed files with 29 additions and 17 deletions

View File

@@ -1,4 +1,10 @@
fun putint(n) var i = 0
fun main() = fun main() =
putint(__LINE__) until i >= 10 do
putint(i),
i = i + 1
end,
0
fun putint(n)

View File

@@ -57,6 +57,10 @@ public record Token(Report.Location location, Symbol symbol, String lexeme) impl
* Kljucna beseda {@code while}. * Kljucna beseda {@code while}.
*/ */
WHILE, WHILE,
/**
* Kljucna beseda {@code until}.
*/
UNTIL,
/** /**
* Kljucna beseda {@code do}. * Kljucna beseda {@code do}.
*/ */
@@ -148,11 +152,7 @@ public record Token(Report.Location location, Symbol symbol, String lexeme) impl
/** /**
* Simbol {@code )}. * Simbol {@code )}.
*/ */
RPAREN, RPAREN;
/**
* Simbol {@code __LINE__}.
*/
LINE
} }
@Override @Override

View File

@@ -315,11 +315,11 @@ public class LexAn implements AutoCloseable {
case "then" -> Token.Symbol.THEN; case "then" -> Token.Symbol.THEN;
case "else" -> Token.Symbol.ELSE; case "else" -> Token.Symbol.ELSE;
case "while" -> Token.Symbol.WHILE; case "while" -> Token.Symbol.WHILE;
case "until" -> Token.Symbol.UNTIL;
case "do" -> Token.Symbol.DO; case "do" -> Token.Symbol.DO;
case "let" -> Token.Symbol.LET; case "let" -> Token.Symbol.LET;
case "in" -> Token.Symbol.IN; case "in" -> Token.Symbol.IN;
case "end" -> Token.Symbol.END; case "end" -> Token.Symbol.END;
case "__LINE__" -> Token.Symbol.LINE;
default -> null; default -> null;
}; };
} }
@@ -452,15 +452,6 @@ public class LexAn implements AutoCloseable {
symbol = Token.Symbol.IDENTIFIER; symbol = Token.Symbol.IDENTIFIER;
} }
if (symbol == Token.Symbol.LINE) {
buffToken = new Token(
new Report.Location(startLocation, endLocation),
Token.Symbol.INTCONST,
Integer.toString(startLocation.begLine())
);
return;
}
buffToken = new Token( buffToken = new Token(
new Report.Location(startLocation, endLocation), new Report.Location(startLocation, endLocation),
symbol, symbol,

View File

@@ -227,6 +227,21 @@ public class SynAn implements AutoCloseable {
return whileStmt; return whileStmt;
} }
case UNTIL: {
// statement -> until expression do statements end
Token untilT = check(Token.Symbol.UNTIL);
AST.Expr cond = parseExpression();
check(Token.Symbol.DO);
List<AST.Stmt> stmts = parseStatements();
Token end = check(Token.Symbol.END);
AST.Expr negCond = new AST.UnExpr(AST.UnExpr.Oper.NOT, cond);
AST.WhileStmt whileStmt = new AST.WhileStmt(negCond, stmts);
attrLoc.put(whileStmt, new Report.Location(untilT, end));
return whileStmt;
}
case LET: { case LET: {
// statement -> let definition reststmtdefs in statements end // statement -> let definition reststmtdefs in statements end
Token let = check(Token.Symbol.LET); Token let = check(Token.Symbol.LET);