|
| 1 | +package toylangs.bolog; |
| 2 | + |
| 3 | +import org.antlr.v4.runtime.BailErrorStrategy; |
| 4 | +import org.antlr.v4.runtime.CharStreams; |
| 5 | +import org.antlr.v4.runtime.CommonTokenStream; |
| 6 | +import org.antlr.v4.runtime.tree.ParseTree; |
| 7 | +import toylangs.bolog.ast.BologNode; |
| 8 | +import utils.ExceptionErrorListener; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +import static toylangs.bolog.BologParser.*; |
| 17 | +import static toylangs.bolog.ast.BologNode.*; |
| 18 | + |
| 19 | +public class BologAst { |
| 20 | + |
| 21 | + |
| 22 | + public static Set<BologNode> makeBologAst(String sisend) { |
| 23 | + BologLexer lexer = new BologLexer(CharStreams.fromString(sisend)); |
| 24 | + lexer.removeErrorListeners(); |
| 25 | + lexer.addErrorListener(new ExceptionErrorListener()); |
| 26 | + |
| 27 | + BologParser parser = new BologParser(new CommonTokenStream(lexer)); |
| 28 | + parser.removeErrorListeners(); |
| 29 | + parser.setErrorHandler(new BailErrorStrategy()); |
| 30 | + |
| 31 | + ParseTree tree = parser.init(); |
| 32 | + //System.out.println(tree.toStringTree(parser)); |
| 33 | + return parseTreetoAst(tree); |
| 34 | + } |
| 35 | + |
| 36 | + // Implementeerida parsepuu -> AST teisendus! |
| 37 | + private static Set<BologNode> parseTreetoAst(ParseTree tree) { |
| 38 | + Set<BologNode> nodes = new HashSet<>(); |
| 39 | + new BologBaseVisitor<BologNode>() { |
| 40 | + |
| 41 | + @Override |
| 42 | + public BologNode visitProg(ProgContext ctx) { |
| 43 | + for (ExprContext exprContext : ctx.expr()) { |
| 44 | + nodes.add(visit(exprContext)); |
| 45 | + } |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public BologNode visitTvLit(TvLitContext ctx) { |
| 51 | + return tv(ctx.getText().equals("JAH")); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public BologNode visitVar(VarContext ctx) { |
| 56 | + return var(ctx.getText()); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public BologNode visitBinary(BinaryContext ctx) { |
| 61 | + BologNode left = visit(ctx.expr(0)); |
| 62 | + BologNode right = visit(ctx.expr(1)); |
| 63 | + return switch (ctx.op.getText()) { |
| 64 | + case "&&" -> and(left, right); |
| 65 | + case "||" -> or(left, right); |
| 66 | + case "<>" -> xor(left, right); |
| 67 | + default -> throw new IllegalArgumentException(); |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public BologNode visitImp(ImpContext ctx) { |
| 73 | + List<BologNode> assumptions = ctx.ass.stream().map(this::visit).toList(); |
| 74 | + return imp(visit(ctx.con), assumptions); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public BologNode visitUnary(UnaryContext ctx) { |
| 79 | + return not(visit(ctx.expr())); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public BologNode visitParen(ParenContext ctx) { |
| 84 | + return visit(ctx.expr()); |
| 85 | + } |
| 86 | + }.visit(tree); |
| 87 | + |
| 88 | + return nodes; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + static void main() throws IOException { |
| 93 | + String input = "X kui Y\nZ && Y"; |
| 94 | + Set<BologNode> bologNodes = makeBologAst(input); |
| 95 | + int idx = 1; |
| 96 | + for (BologNode bologNode : bologNodes) { |
| 97 | + bologNode.renderPngFile(Paths.get("graphs", "bolog" + idx++ + ".png")); |
| 98 | + System.out.println(bologNode); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +} |
0 commit comments