1 Commits

Author SHA1 Message Date
Gašper Dobrovoljc
de2522c28c Added __LINE__ symbol 2025-06-03 18:07:27 +02:00
4 changed files with 22 additions and 40 deletions

View File

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

View File

@@ -148,7 +148,11 @@ public record Token(Report.Location location, Symbol symbol, String lexeme) impl
/**
* Simbol {@code )}.
*/
RPAREN;
RPAREN,
/**
* Simbol {@code __LINE__}.
*/
LINE
}
@Override

View File

@@ -227,10 +227,13 @@ public class CodeGen {
@Override
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<>();
code.addAll(binExpr.fstExpr.accept(this, frame));
code.addAll(binExpr.sndExpr.accept(this, frame));
PDM.OPER.Oper oper = switch (binExpr.oper) {
case OR -> PDM.OPER.Oper.OR;
case AND -> PDM.OPER.Oper.AND;
@@ -247,35 +250,8 @@ public class CodeGen {
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.add(new PDM.OPER(oper, loc));
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);
return code;
}

View File

@@ -319,6 +319,7 @@ public class LexAn implements AutoCloseable {
case "let" -> Token.Symbol.LET;
case "in" -> Token.Symbol.IN;
case "end" -> Token.Symbol.END;
case "__LINE__" -> Token.Symbol.LINE;
default -> null;
};
}
@@ -451,6 +452,15 @@ public class LexAn implements AutoCloseable {
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(
new Report.Location(startLocation, endLocation),
symbol,