Compare commits
1 Commits
until
...
2025-predr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a7ac2a6f8 |
26
grammar.txt
26
grammar.txt
@@ -69,4 +69,28 @@ restinits -> COMMA initializer restinits | .
|
|||||||
initializer -> INTCONST intconstmult | CHARCONST | STRINGCONST .
|
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,10 +1,13 @@
|
|||||||
var i = 0
|
fun putstr(n)
|
||||||
|
fun putint(b)
|
||||||
|
|
||||||
|
fun a() = putstr("a called\n\00"), 40
|
||||||
|
fun b() = putstr("b called\n\00"), 7
|
||||||
|
|
||||||
fun main() =
|
fun main() =
|
||||||
until i >= 10 do
|
putint(10 % 7),
|
||||||
putint(i),
|
putstr("\n\00"),
|
||||||
i = i + 1
|
putint(30 % 25),
|
||||||
end,
|
putstr("\n\00"),
|
||||||
0
|
putint(a() % b()),
|
||||||
|
putstr("\n\00")
|
||||||
fun putint(n)
|
|
||||||
@@ -57,10 +57,6 @@ 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}.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -231,26 +231,60 @@ public class CodeGen {
|
|||||||
|
|
||||||
List<PDM.CodeInstr> code = new ArrayList<>();
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
code.addAll(binExpr.fstExpr.accept(this, frame));
|
if (binExpr.oper == AST.BinExpr.Oper.MOD) {
|
||||||
code.addAll(binExpr.sndExpr.accept(this, frame));
|
// a - a / b * b
|
||||||
|
|
||||||
PDM.OPER.Oper oper = switch (binExpr.oper) {
|
String tmpLabel = "$tmp@" + loc.location().begLine() + ":" + loc.location().begColumn();
|
||||||
case OR -> PDM.OPER.Oper.OR;
|
|
||||||
case AND -> PDM.OPER.Oper.AND;
|
|
||||||
case EQU -> PDM.OPER.Oper.EQU;
|
|
||||||
case NEQ -> PDM.OPER.Oper.NEQ;
|
|
||||||
case GTH -> PDM.OPER.Oper.GTH;
|
|
||||||
case LTH -> PDM.OPER.Oper.LTH;
|
|
||||||
case GEQ -> PDM.OPER.Oper.GEQ;
|
|
||||||
case LEQ -> PDM.OPER.Oper.LEQ;
|
|
||||||
case ADD -> PDM.OPER.Oper.ADD;
|
|
||||||
case SUB -> PDM.OPER.Oper.SUB;
|
|
||||||
case MUL -> PDM.OPER.Oper.MUL;
|
|
||||||
case DIV -> PDM.OPER.Oper.DIV;
|
|
||||||
case MOD -> PDM.OPER.Oper.MOD;
|
|
||||||
};
|
|
||||||
|
|
||||||
code.add(new PDM.OPER(oper, loc));
|
code.addAll(binExpr.fstExpr.accept(this, frame));
|
||||||
|
// dup a
|
||||||
|
code.add(new PDM.REGN(PDM.REGN.Reg.SP, loc));
|
||||||
|
code.add(new PDM.LOAD(loc));
|
||||||
|
|
||||||
|
code.addAll(binExpr.sndExpr.accept(this, frame));
|
||||||
|
// dup b
|
||||||
|
code.add(new PDM.REGN(PDM.REGN.Reg.SP, loc));
|
||||||
|
code.add(new PDM.LOAD(loc));
|
||||||
|
// save b
|
||||||
|
code.add(new PDM.NAME(tmpLabel, loc));
|
||||||
|
code.add(new PDM.SAVE(loc));
|
||||||
|
|
||||||
|
code.add(new PDM.OPER(PDM.OPER.Oper.DIV, loc));
|
||||||
|
|
||||||
|
// load second b
|
||||||
|
code.add(new PDM.NAME(tmpLabel, loc));
|
||||||
|
code.add(new PDM.LOAD(loc));
|
||||||
|
|
||||||
|
code.add(new PDM.OPER(PDM.OPER.Oper.MUL, loc));
|
||||||
|
|
||||||
|
code.add(new PDM.OPER(PDM.OPER.Oper.SUB, loc));
|
||||||
|
|
||||||
|
List<PDM.DataInstr> data = new ArrayList<>();
|
||||||
|
data.add(new PDM.LABEL(tmpLabel, loc));
|
||||||
|
data.add(new PDM.DATA(0, loc));
|
||||||
|
attrAST.attrData.put(binExpr, data);
|
||||||
|
} else {
|
||||||
|
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;
|
||||||
|
case EQU -> PDM.OPER.Oper.EQU;
|
||||||
|
case NEQ -> PDM.OPER.Oper.NEQ;
|
||||||
|
case GTH -> PDM.OPER.Oper.GTH;
|
||||||
|
case LTH -> PDM.OPER.Oper.LTH;
|
||||||
|
case GEQ -> PDM.OPER.Oper.GEQ;
|
||||||
|
case LEQ -> PDM.OPER.Oper.LEQ;
|
||||||
|
case ADD -> PDM.OPER.Oper.ADD;
|
||||||
|
case SUB -> PDM.OPER.Oper.SUB;
|
||||||
|
case MUL -> PDM.OPER.Oper.MUL;
|
||||||
|
case DIV -> PDM.OPER.Oper.DIV;
|
||||||
|
case MOD -> PDM.OPER.Oper.MOD;
|
||||||
|
};
|
||||||
|
|
||||||
|
code.add(new PDM.OPER(oper, loc));
|
||||||
|
}
|
||||||
|
|
||||||
attrAST.attrCode.put(binExpr, code);
|
attrAST.attrCode.put(binExpr, code);
|
||||||
return code;
|
return code;
|
||||||
@@ -683,6 +717,12 @@ public class CodeGen {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object visit(AST.BinExpr binExpr, Object arg) {
|
||||||
|
List<PDM.DataInstr> data = attrAST.attrData.get(binExpr);
|
||||||
|
if (data != null) dataSegment.addAll(data);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -315,7 +315,6 @@ 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;
|
||||||
|
|||||||
@@ -227,21 +227,6 @@ 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);
|
||||||
|
|||||||
Reference in New Issue
Block a user