From ccd92f9b09dfbf1f16f50787ef2510e038dccf06 Mon Sep 17 00:00:00 2001 From: student Date: Mon, 5 Mar 2018 16:48:49 -0500 Subject: Update --- .../java/bjc/pratt/examples/lang/ast/LangAST.java | 27 +++++++++++++++++++++- .../bjc/pratt/examples/lang/ast/LiteralAST.java | 13 +++++++++++ .../examples/lang/evaluator/LangEvaluator.java | 13 +++++++++++ .../pratt/examples/lang/evaluator/LangResult.java | 6 +++++ .../bjc/pratt/commands/MetaInitialCommand.java | 14 +++++++++++ .../bjc/pratt/commands/MetaNonInitialCommand.java | 13 ++++++++--- 6 files changed, 82 insertions(+), 4 deletions(-) (limited to 'JPratt/src') 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 4fa84b8..3ee1887 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 @@ -5,13 +5,33 @@ import bjc.pratt.examples.lang.evaluator.LangResult; import bjc.pratt.tokens.Token; import bjc.utils.data.TopDownTransformResult; +/** + * Represents the AST for the language. + * + * @author student + * + */ public interface LangAST { + /** + * Evaluate the AST. + * + * @return The evaluated AST + */ LangResult toResult(); + /** + * Create an AST from a token. + * + * @param token + * The token to create the AST from + * @return The new AST + * @throws EvaluatorException + * If something goes wrong. + */ static LangAST fromToken(Token token) throws EvaluatorException { String key = token.getKey(); - switch(key) { + switch (key) { case "(literal)": return LiteralAST.fromToken(token); default: @@ -21,6 +41,11 @@ public interface LangAST { } } + /** + * Get the way an AST node should be evaluated. + * + * @return The way to evaluate the AST node. + */ default TopDownTransformResult getEvaluationStrategy() { return TopDownTransformResult.PUSHDOWN; } 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 d9a7ad2..85289d9 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 @@ -3,7 +3,20 @@ package bjc.pratt.examples.lang.ast; import bjc.pratt.tokens.Token; import bjc.utils.data.TopDownTransformResult; +/** + * AST node for a literal. + * + * @author student + * + */ public interface LiteralAST extends LangAST { + /** + * Create a new literal AST + * + * @param tok + * The token to build from. + * @return The AST for the token. + */ static LiteralAST fromToken(Token tok) { return null; } diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/evaluator/LangEvaluator.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/evaluator/LangEvaluator.java index 7ce1939..3ebab16 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/evaluator/LangEvaluator.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/evaluator/LangEvaluator.java @@ -3,7 +3,20 @@ package bjc.pratt.examples.lang.evaluator; import bjc.pratt.examples.lang.ast.LangAST; import bjc.utils.data.ITree; +/** + * Evaluate an AST. + * + * @author student + * + */ public class LangEvaluator { + /** + * Evaluate an AST + * + * @param ast + * The AST to evaluate + * @return The result of evaluation. + */ public LangResult evaluate(ITree ast) { ITree evaluatedTree = ast.topDownTransform(LangAST::getEvaluationStrategy, this::evaluateNode); diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/evaluator/LangResult.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/evaluator/LangResult.java index 06b3471..35c5bb3 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/lang/evaluator/LangResult.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/evaluator/LangResult.java @@ -1,5 +1,11 @@ package bjc.pratt.examples.lang.evaluator; +/** + * The result of evaluating an AST. + * + * @author student + * + */ public class LangResult { } \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java index 9f14f36..d209177 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/MetaInitialCommand.java @@ -6,8 +6,22 @@ import bjc.pratt.ParserContext; * A 'meta-command' that yields the actual command to use. * * @author bjculkin + * + * @param + * The key type of the context. + * @param + * The value type of the context. + * @param + * The storage type of the context. * */ public interface MetaInitialCommand { + /** + * Get the command to use. + * + * @param ctx + * The current parser context. + * @return The command to use. + */ InitialCommand getCommand(ParserContext ctx); } diff --git a/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java index 9d09eef..956dcec 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/MetaNonInitialCommand.java @@ -8,14 +8,21 @@ import bjc.pratt.ParserContext; * @author bjculkin * * @param - * The token key type. + * The token key type. * * @param - * The token value type. + * The token value type. * * @param - * The parser state type. + * The parser state type. */ public interface MetaNonInitialCommand { + /** + * Get the command to use. + * + * @param ctx + * The context to use. + * @return The command to use. + */ NonInitialCommand getCommand(ParserContext ctx); } -- cgit v1.2.3