Compare commits
1 Commits
2025-predr
...
add-right-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8251a3608c |
29
grammar.txt
29
grammar.txt
@@ -39,8 +39,9 @@ restcmp -> EQU addexpr
|
||||
| .
|
||||
|
||||
addexpr -> multexpr restadd .
|
||||
restadd -> ADD multexpr restadd
|
||||
| SUB multexpr restadd | .
|
||||
restadd -> ADD addexpr
|
||||
| SUB addexpr
|
||||
| .
|
||||
|
||||
multexpr -> prefixexpr restmult .
|
||||
restmult -> MUL prefixexpr restmult
|
||||
@@ -70,27 +71,3 @@ initializer -> INTCONST intconstmult | CHARCONST | STRINGCONST .
|
||||
intconstmult -> MUL const | .
|
||||
|
||||
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,13 +1,3 @@
|
||||
fun putstr(n)
|
||||
fun putint(b)
|
||||
|
||||
fun a() = putstr("a called\n\00"), 40
|
||||
fun b() = putstr("b called\n\00"), 7
|
||||
|
||||
fun main() =
|
||||
putint(10 % 7),
|
||||
putstr("\n\00"),
|
||||
putint(30 % 25),
|
||||
putstr("\n\00"),
|
||||
putint(a() % b()),
|
||||
putstr("\n\00")
|
||||
3 - 2 - 1
|
||||
@@ -231,60 +231,26 @@ public class CodeGen {
|
||||
|
||||
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||
|
||||
if (binExpr.oper == AST.BinExpr.Oper.MOD) {
|
||||
// a - a / b * b
|
||||
code.addAll(binExpr.fstExpr.accept(this, frame));
|
||||
code.addAll(binExpr.sndExpr.accept(this, frame));
|
||||
|
||||
String tmpLabel = "$tmp@" + loc.location().begLine() + ":" + loc.location().begColumn();
|
||||
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.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));
|
||||
}
|
||||
code.add(new PDM.OPER(oper, loc));
|
||||
|
||||
attrAST.attrCode.put(binExpr, code);
|
||||
return code;
|
||||
@@ -717,12 +683,6 @@ public class CodeGen {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -540,20 +540,20 @@ public class SynAn implements AutoCloseable {
|
||||
Token token = lexAn.peekToken();
|
||||
switch (token.symbol()) {
|
||||
case ADD: {
|
||||
// restadd -> ADD multexpr restadd
|
||||
// restadd -> ADD addexpr
|
||||
check(Token.Symbol.ADD);
|
||||
AST.Expr right = parseMultiplicationExpression();
|
||||
AST.Expr right = parseAdditionExpression();
|
||||
AST.Expr binExpr = new AST.BinExpr(AST.BinExpr.Oper.ADD, left, right);
|
||||
attrLoc.put(binExpr, new Report.Location(attrLoc.get(left), attrLoc.get(right)));
|
||||
return parseRestAdditionExpressions(binExpr);
|
||||
return binExpr;
|
||||
}
|
||||
case SUB: {
|
||||
// restadd -> SUB multexpr restadd
|
||||
// restadd -> ADD addexpr
|
||||
check(Token.Symbol.SUB);
|
||||
AST.Expr right = parseMultiplicationExpression();
|
||||
AST.Expr right = parseAdditionExpression();
|
||||
AST.Expr binExpr = new AST.BinExpr(AST.BinExpr.Oper.SUB, left, right);
|
||||
attrLoc.put(binExpr, new Report.Location(attrLoc.get(left), attrLoc.get(right)));
|
||||
return parseRestAdditionExpressions(binExpr);
|
||||
return binExpr;
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user