Compare commits
1 Commits
times-init
...
2025-predr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a7ac2a6f8 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,4 +3,3 @@ out/
|
|||||||
*.zip
|
*.zip
|
||||||
prg/**/*.ast
|
prg/**/*.ast
|
||||||
prg/**/*.out
|
prg/**/*.out
|
||||||
prg/**/*.pins25
|
|
||||||
24
grammar.txt
24
grammar.txt
@@ -70,3 +70,27 @@ 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 | .
|
||||||
13
prg/test.pins25
Normal file
13
prg/test.pins25
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
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")
|
||||||
@@ -69,10 +69,6 @@ public record Token(Report.Location location, Symbol symbol, String lexeme) impl
|
|||||||
* Kljucna beseda {@code in}.
|
* Kljucna beseda {@code in}.
|
||||||
*/
|
*/
|
||||||
IN,
|
IN,
|
||||||
/**
|
|
||||||
* Kljucna beseda {@code times}.
|
|
||||||
*/
|
|
||||||
TIMES,
|
|
||||||
/**
|
/**
|
||||||
* Kljucna beseda {@code end}.
|
* Kljucna beseda {@code end}.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -231,6 +231,39 @@ public class CodeGen {
|
|||||||
|
|
||||||
List<PDM.CodeInstr> code = new ArrayList<>();
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
|
if (binExpr.oper == AST.BinExpr.Oper.MOD) {
|
||||||
|
// a - a / b * b
|
||||||
|
|
||||||
|
String tmpLabel = "$tmp@" + loc.location().begLine() + ":" + loc.location().begColumn();
|
||||||
|
|
||||||
|
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.fstExpr.accept(this, frame));
|
||||||
code.addAll(binExpr.sndExpr.accept(this, frame));
|
code.addAll(binExpr.sndExpr.accept(this, frame));
|
||||||
|
|
||||||
@@ -251,6 +284,7 @@ public class CodeGen {
|
|||||||
};
|
};
|
||||||
|
|
||||||
code.add(new PDM.OPER(oper, loc));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 "times" -> Token.Symbol.TIMES;
|
|
||||||
default -> null;
|
default -> null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -360,9 +360,9 @@ public class SynAn implements AutoCloseable {
|
|||||||
|
|
||||||
private AST.AtomExpr parseIntegerConstantMultiplier() {
|
private AST.AtomExpr parseIntegerConstantMultiplier() {
|
||||||
Token token = lexAn.peekToken();
|
Token token = lexAn.peekToken();
|
||||||
if (token.symbol() == Token.Symbol.TIMES) {
|
if (token.symbol() == Token.Symbol.MUL) {
|
||||||
// intconstmult -> TIMES const
|
// intconstmult -> MUL const
|
||||||
check(Token.Symbol.TIMES);
|
check(Token.Symbol.MUL);
|
||||||
return parseConstant();
|
return parseConstant();
|
||||||
}
|
}
|
||||||
// intconstmult -> ε
|
// intconstmult -> ε
|
||||||
|
|||||||
Reference in New Issue
Block a user