Compare commits
1 Commits
2025-predr
...
or-skip-ri
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73e1113764 |
24
grammar.txt
24
grammar.txt
@@ -70,27 +70,3 @@ initializer -> INTCONST intconstmult | CHARCONST | STRINGCONST .
|
|||||||
intconstmult -> MUL const | .
|
intconstmult -> MUL const | .
|
||||||
|
|
||||||
const -> INTCONST | CHARCONST | STRINGCONST .
|
const -> INTCONST | CHARCONST | STRINGCONST .
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------
|
|
||||||
|
|
||||||
//LEFT ASOC.
|
|
||||||
exp0 = exp0 op other | other
|
|
||||||
|
|
||||||
|
|
|
||||||
v
|
|
||||||
|
|
||||||
exp0 = other exp0'
|
|
||||||
exp0' = op other exp0' | .
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//RIGHT ASOC.
|
|
||||||
exp0 = other op exp0 | other
|
|
||||||
|
|
||||||
|
|
|
||||||
v
|
|
||||||
|
|
||||||
exp0 = other exp0'
|
|
||||||
exp0' = op exp0 | .
|
|
||||||
@@ -1,5 +1,12 @@
|
|||||||
// line1 --
|
fun putstr(s)
|
||||||
line2 -----x
|
|
||||||
line3
|
|
||||||
|
|
||||||
fun main() = 0
|
fun f1() =
|
||||||
|
putstr("funkcija 1\n\00"),
|
||||||
|
0
|
||||||
|
|
||||||
|
fun f2() =
|
||||||
|
putstr("funkcija 2\n\00"),
|
||||||
|
0
|
||||||
|
|
||||||
|
fun main() =
|
||||||
|
f1() || f2()
|
||||||
@@ -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,7 +247,34 @@ 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;
|
||||||
|
|||||||
@@ -247,13 +247,7 @@ public class LexAn implements AutoCloseable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder builder = new StringBuilder();
|
while (buffChar != '\n' && buffChar != -1) {
|
||||||
|
|
||||||
while (buffChar != -1) {
|
|
||||||
if (buffChar == '\n' && !builder.toString().endsWith("--")) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
builder.append((char) buffChar);
|
|
||||||
nextChar();
|
nextChar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user