1 Commits

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

View File

@@ -1,12 +1,10 @@
fun putstr(s) var i = 0
fun f1() =
putstr("funkcija 1\n\00"),
0
fun f2() =
putstr("funkcija 2\n\00"),
0
fun main() = fun main() =
f1() || f2() 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}.
*/ */

View File

@@ -227,10 +227,13 @@ public class CodeGen {
@Override @Override
public List<PDM.CodeInstr> visit(AST.BinExpr binExpr, Mem.Frame frame) { public List<PDM.CodeInstr> visit(AST.BinExpr binExpr, Mem.Frame frame) {
Report.Location loc = attrAST.attrLoc.get(binExpr).location(); Report.Locatable loc = attrAST.attrLoc.get(binExpr);
List<PDM.CodeInstr> code = new ArrayList<>(); List<PDM.CodeInstr> code = new ArrayList<>();
code.addAll(binExpr.fstExpr.accept(this, frame));
code.addAll(binExpr.sndExpr.accept(this, frame));
PDM.OPER.Oper oper = switch (binExpr.oper) { PDM.OPER.Oper oper = switch (binExpr.oper) {
case OR -> PDM.OPER.Oper.OR; case OR -> PDM.OPER.Oper.OR;
case AND -> PDM.OPER.Oper.AND; case AND -> PDM.OPER.Oper.AND;
@@ -247,34 +250,7 @@ public class CodeGen {
case MOD -> PDM.OPER.Oper.MOD; case MOD -> PDM.OPER.Oper.MOD;
}; };
if (oper == PDM.OPER.Oper.OR) {
String locStr = loc.begLine() + ":" + loc.begColumn();
String rightLabel = "$or:right" + locStr;
String skipLabel = "$or:skip@" + locStr;
String endLabel = "$or:end@" + locStr;
code.addAll(binExpr.fstExpr.accept(this, frame));
code.add(new PDM.NAME(skipLabel, loc));
code.add(new PDM.NAME(rightLabel, loc));
code.add(new PDM.CJMP(loc));
code.add(new PDM.LABEL(rightLabel, loc));
code.add(new PDM.PUSH(0, loc));
code.addAll(binExpr.sndExpr.accept(this, frame));
code.add(new PDM.OPER(oper, loc)); code.add(new PDM.OPER(oper, loc));
code.add(new PDM.NAME(endLabel, loc));
code.add(new PDM.UJMP(loc));
code.add(new PDM.LABEL(skipLabel, loc));
code.add(new PDM.PUSH(1, loc));
code.add(new PDM.LABEL(endLabel, loc));
} else {
code.addAll(binExpr.fstExpr.accept(this, frame));
code.addAll(binExpr.sndExpr.accept(this, frame));
code.add(new PDM.OPER(oper, loc));
}
attrAST.attrCode.put(binExpr, code); attrAST.attrCode.put(binExpr, code);
return code; return code;

View File

@@ -315,6 +315,7 @@ 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;

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);