diff options
| author | student <student@69.161.224.76> | 2018-03-19 17:03:00 -0400 |
|---|---|---|
| committer | student <student@69.161.224.76> | 2018-03-19 17:03:00 -0400 |
| commit | 10575fbfd89720eb5152c672f69a62d5444393e3 (patch) | |
| tree | c0c245b5afd665943483c99bee55d83631a162c0 /JPratt/src | |
| parent | ccd92f9b09dfbf1f16f50787ef2510e038dccf06 (diff) | |
More work on ASTs
Diffstat (limited to 'JPratt/src')
9 files changed, 347 insertions, 35 deletions
diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/LeafConverter.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/LeafConverter.java new file mode 100644 index 0000000..94df050 --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/LeafConverter.java @@ -0,0 +1,14 @@ +package bjc.pratt.examples.lang; + +import java.util.function.Function; + +import bjc.pratt.examples.lang.ast.LangAST; +import bjc.pratt.examples.lang.ast.LiteralAST; +import bjc.pratt.tokens.Token; + +final class LeafConverter implements Function<Token<String, String>, LangAST> { + @Override + public LangAST apply(Token<String, String> leaf) { + return LiteralAST.fromToken(leaf.getValue()); + } +}
\ No newline at end of file diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/NodeCollapser.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/NodeCollapser.java new file mode 100644 index 0000000..61041e5 --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/NodeCollapser.java @@ -0,0 +1,15 @@ +package bjc.pratt.examples.lang; + +import java.util.function.BiFunction; + +import bjc.pratt.examples.lang.ast.LangAST; +import bjc.pratt.tokens.Token; +import bjc.utils.funcdata.IList; + +final class NodeCollapser implements BiFunction<Token<String, String>, IList<LangAST>, LangAST> { + @Override + public LangAST apply(Token<String, String> token, IList<LangAST> children) { + // TODO Auto-generated method stub + return null; + } +}
\ No newline at end of file diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java index cf5e521..f2fa778 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java @@ -36,6 +36,7 @@ import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.TransformIterator; import bjc.utils.funcdata.IList; +import bjc.utils.functypes.ID; import bjc.utils.parserutils.ParserException; import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter; import bjc.utils.parserutils.splitter.ExcludingTokenSplitter; @@ -53,7 +54,7 @@ public class PrattParserTest { * Main method. * * @param args - * Unused CLI arguments. + * Unused CLI arguments. */ public static void main(final String[] args) { /* @@ -124,9 +125,8 @@ public class PrattParserTest { System.out.print("Enter a command (blank line to exit): "); String ln = scn.nextLine(); - while(!ln.trim().equals("")) { - final Iterator<Token<String, String>> tokens = preprocessInput(ops, filtered, ln, reserved, - ctx); + while (!ln.trim().equals("")) { + final Iterator<Token<String, String>> tokens = preprocessInput(ops, filtered, ln, reserved, ctx); try { final StringTokenStream tokenStream = new StringTokenStream(tokens); @@ -136,20 +136,20 @@ public class PrattParserTest { */ tokenStream.next(); - final ITree<Token<String, String>> rawTree = parser.parseExpression(0, tokenStream, ctx, - true); + final ITree<Token<String, String>> rawTree = parser.parseExpression(0, tokenStream, ctx, true); - if(!tokenStream.headIs("(end)")) { + if (!tokenStream.headIs("(end)")) { System.out.println("\nMultiple expressions on line"); } System.out.printf("\nParsed expression:\n%s", rawTree); - final ITree<LangAST> tokenTree = rawTree.rebuildTree(LangAST::fromToken, - LangAST::fromToken); + final Object ast = rawTree.collapse(new LeafConverter(), new NodeCollapser(), ID.id()); - System.out.printf("\nAST-ized expression:\n%s", tokenTree); - } catch(ParserException pex) { + final ITree<LangAST> tokenTree = rawTree.rebuildTree(LangAST::fromToken, LangAST::fromToken); + + System.out.printf("\nAST-ized expression:\n%s\nNEW:\n%s", tokenTree, ast); + } catch (ParserException pex) { pex.printStackTrace(); } @@ -169,19 +169,20 @@ public class PrattParserTest { final List<String> splitTokens = new LinkedList<>(); - for(final String raw : rawTokens) { - if(raw.equals("")) continue; + for (final String raw : rawTokens) { + if (raw.equals("")) + continue; boolean doSplit = false; - for(final String op : ops) { - if(raw.contains(op)) { + for (final String op : ops) { + if (raw.contains(op)) { doSplit = true; break; } } - if(doSplit) { + if (doSplit) { IList<String> splitStrangs = split.split(raw); splitStrangs.removeMatching(""); @@ -244,8 +245,7 @@ public class PrattParserTest { /* * Inline conditional. */ - final NonInitialCommand<String, String, TestContext> ifElse = ternary(6, 0, "else", litToken("cond"), - false); + final NonInitialCommand<String, String, TestContext> ifElse = ternary(6, 0, "else", litToken("cond"), false); parser.addNonInitialCommand("if", ifElse); /* @@ -319,8 +319,7 @@ public class PrattParserTest { /* * Array indexing. */ - final NonInitialCommand<String, String, TestContext> arrayIdx = postCircumfix(60, 0, "]", - litToken("idx")); + final NonInitialCommand<String, String, TestContext> arrayIdx = postCircumfix(60, 0, "]", litToken("idx")); parser.addNonInitialCommand("[", arrayIdx); /* @@ -346,15 +345,15 @@ public class PrattParserTest { /* * Array literals. */ - final InitialCommand<String, String, TestContext> arrayLiteral = delimited(0, ",", "]", - litToken("array"), idfun, idfun, idfun, false); + final InitialCommand<String, String, TestContext> arrayLiteral = delimited(0, ",", "]", litToken("array"), + idfun, idfun, idfun, false); parser.addInitialCommand("[", arrayLiteral); /* * JSON literals. */ - final InitialCommand<String, String, TestContext> jsonLiteral = delimited(0, ",", "}", litToken("json"), - idfun, idfun, idfun, false); + final InitialCommand<String, String, TestContext> jsonLiteral = delimited(0, ",", "}", litToken("json"), idfun, + idfun, idfun, false); parser.addInitialCommand("{", jsonLiteral); /* diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/BooleanAST.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/BooleanAST.java new file mode 100644 index 0000000..e900a93 --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/BooleanAST.java @@ -0,0 +1,46 @@ +package bjc.pratt.examples.lang.ast; + +import bjc.pratt.examples.lang.evaluator.LangResult; + +public class BooleanAST extends LiteralAST { + public final boolean val; + + public BooleanAST(boolean vl) { + super(LiteralType.BOOLEAN); + + val = vl; + } + + @Override + public LangResult toResult() { + // TODO Auto-generated method stub + return null; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (val ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BooleanAST other = (BooleanAST) obj; + if (val != other.val) + return false; + return true; + } + + @Override + public String toString() { + return "BooleanAST [val=" + val + "]"; + } +} diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/IntegerAST.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/IntegerAST.java new file mode 100644 index 0000000..bd4b2ab --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/IntegerAST.java @@ -0,0 +1,60 @@ +package bjc.pratt.examples.lang.ast; + +import bjc.pratt.examples.lang.evaluator.LangResult; + +/** + * AST containing an integer. + * + * @author student + * + */ +public class IntegerAST extends LiteralAST { + /** + * Value of the integer. + */ + public final int val; + + /** + * Create a new integer AST. + * + * @param vl + * The value of the integer. + */ + public IntegerAST(int vl) { + super(LiteralType.INTEGER); + + val = vl; + } + + @Override + public LangResult toResult() { + return null; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + val; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + IntegerAST other = (IntegerAST) obj; + if (val != other.val) + return false; + return true; + } + + @Override + public String toString() { + return "IntegerAST [val=" + val + "]"; + } +} diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/LangAST.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/LangAST.java index 3ee1887..cffde91 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/LangAST.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/LangAST.java @@ -11,13 +11,23 @@ import bjc.utils.data.TopDownTransformResult; * @author student * */ -public interface LangAST { +public abstract class LangAST { + public static enum ASTType { + LITERAL, OPERATOR + } + + public final ASTType type; + + protected LangAST(ASTType typ) { + type = typ; + } + /** * Evaluate the AST. * * @return The evaluated AST */ - LangResult toResult(); + public abstract LangResult toResult(); /** * Create an AST from a token. @@ -28,16 +38,18 @@ public interface LangAST { * @throws EvaluatorException * If something goes wrong. */ - static LangAST fromToken(Token<String, String> token) throws EvaluatorException { + public static LangAST fromToken(Token<String, String> token) throws EvaluatorException { String key = token.getKey(); switch (key) { case "(literal)": - return LiteralAST.fromToken(token); + return LiteralAST.fromToken(token.getValue()); default: String msg = String.format("Unknown token type '%s'", key); - throw new EvaluatorException(msg); + // @TODO uncomment this later + //throw new EvaluatorException(msg); + return new StringAST("RAW: " + token.toString()); } } @@ -46,7 +58,34 @@ public interface LangAST { * * @return The way to evaluate the AST node. */ - default TopDownTransformResult getEvaluationStrategy() { + public TopDownTransformResult getEvaluationStrategy() { return TopDownTransformResult.PUSHDOWN; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((type == null) ? 0 : type.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + LangAST other = (LangAST) obj; + if (type != other.type) + return false; + return true; + } + + @Override + public String toString() { + return "LangAST [type=" + type + "]"; + } }
\ No newline at end of file diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/LiteralAST.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/LiteralAST.java index 85289d9..832a0e3 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/LiteralAST.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/LiteralAST.java @@ -9,20 +9,64 @@ import bjc.utils.data.TopDownTransformResult; * @author student * */ -public interface LiteralAST extends LangAST { +public abstract class LiteralAST extends LangAST { + public static enum LiteralType { + INTEGER, STRING, BOOLEAN + } + + public final LiteralType type; + + protected LiteralAST(LiteralType typ) { + super(ASTType.LITERAL); + + type = typ; + } /** * Create a new literal AST * * @param tok - * The token to build from. + * The token value to build from. * @return The AST for the token. */ - static LiteralAST fromToken(Token<String, String> tok) { - return null; + public static LiteralAST fromToken(String tok) { + if(tok.matches("[+-]?\\d+")) { + return new IntegerAST(Integer.parseInt(tok)); + } else if(tok.equalsIgnoreCase("true")) { + return new BooleanAST(true); + } else if(tok.equalsIgnoreCase("false")) { + return new BooleanAST(false); + } + + return new StringAST("RAW: " + tok); } @Override - default TopDownTransformResult getEvaluationStrategy() { + public TopDownTransformResult getEvaluationStrategy() { return TopDownTransformResult.TRANSFORM; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((type == null) ? 0 : type.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + LiteralAST other = (LiteralAST) obj; + if (type != other.type) + return false; + return true; + } + @Override + public String toString() { + return "LiteralAST [type=" + type + "]"; + } } diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/OperatorAST.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/OperatorAST.java new file mode 100644 index 0000000..036f8bd --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/OperatorAST.java @@ -0,0 +1,31 @@ +package bjc.pratt.examples.lang.ast; + +import bjc.pratt.examples.lang.evaluator.LangResult; + +public abstract class OperatorAST extends LangAST { + public static enum OperatorType { + + } + + public final OperatorType type; + + public OperatorAST(OperatorType typ) { + super(ASTType.OPERATOR); + + type = typ; + } + + public static OperatorAST fromToken(String tok) { + switch (tok) { + + } + + return null; + } + @Override + public LangResult toResult() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/StringAST.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/StringAST.java new file mode 100644 index 0000000..891c625 --- /dev/null +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/ast/StringAST.java @@ -0,0 +1,64 @@ +package bjc.pratt.examples.lang.ast; + +import bjc.pratt.examples.lang.evaluator.LangResult; + +/** + * Holds a string value. + * + * @author student + * + */ +public class StringAST extends LiteralAST { + /** + * The value of the string. + */ + public final String val; + + /** + * Create a new string AST. + * + * @param vl + * The string value. + */ + public StringAST(String vl) { + super(LiteralType.STRING); + + val = vl; + } + + @Override + public LangResult toResult() { + // @TODO Auto-generated method stub + return null; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((val == null) ? 0 : val.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + StringAST other = (StringAST) obj; + if (val == null) { + if (other.val != null) + return false; + } else if (!val.equals(other.val)) + return false; + return true; + } + + @Override + public String toString() { + return "StringAST [val=" + val + "]"; + } +} |
