1 Commits

Author SHA1 Message Date
Gašper Dobrovoljc
73e1113764 Skip right expression evaluation in OR if left returns true 2025-06-03 18:26:19 +02:00
4 changed files with 40 additions and 22 deletions

View File

@@ -1,4 +1,12 @@
fun putint(n) fun putstr(s)
fun f1() =
putstr("funkcija 1\n\00"),
0
fun f2() =
putstr("funkcija 2\n\00"),
0
fun main() = fun main() =
putint(__LINE__) f1() || f2()

View File

@@ -148,11 +148,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

@@ -227,13 +227,10 @@ 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.Locatable loc = attrAST.attrLoc.get(binExpr); Report.Location loc = attrAST.attrLoc.get(binExpr).location();
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;
@@ -250,8 +247,35 @@ public class CodeGen {
case MOD -> PDM.OPER.Oper.MOD; case MOD -> PDM.OPER.Oper.MOD;
}; };
code.add(new PDM.OPER(oper, loc)); 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.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

@@ -319,7 +319,6 @@ public class LexAn implements AutoCloseable {
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 +451,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,