From c82452e59b1547392c3e89d08d9173cc6dc79e23 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 5 Apr 2017 15:35:13 -0400 Subject: Reorganize --- .../java/bjc/pratt/commands/PreTernaryCommand.java | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java (limited to 'JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java') diff --git a/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java b/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java new file mode 100644 index 0000000..efa7872 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java @@ -0,0 +1,75 @@ +package bjc.pratt.commands; + +import bjc.pratt.ParseBlock; +import bjc.pratt.ParserContext; +import bjc.pratt.Token; +import bjc.utils.data.ITree; +import bjc.utils.data.Tree; +import bjc.utils.parserutils.ParserException; + +/** + * A prefix ternary operator, like an if/then/else group. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class PreTernaryCommand extends AbstractInitialCommand { + private Token term; + + private ParseBlock condBlock; + + private ParseBlock opblock1; + private ParseBlock opblock2; + + /** + * Create a new ternary statement. + * + * @param cond + * The block for handling the condition. + * + * @param op1 + * The block for handling the first operator. + * + * @param op2 + * The block for handling the second operator. + * + * @param term + * The token to use as the node for the AST. + */ + public PreTernaryCommand(ParseBlock cond, ParseBlock op1, ParseBlock op2, + Token term) { + super(); + + if (cond == null) + throw new NullPointerException("Cond block must not be null"); + else if (op1 == null) + throw new NullPointerException("Op block #1 must not be null"); + else if (op2 == null) throw new NullPointerException("Op block #2 must not be null"); + + this.condBlock = cond; + this.opblock1 = op1; + this.opblock2 = op2; + + this.term = term; + } + + @Override + protected ITree> intNullDenotation(Token operator, ParserContext ctx) + throws ParserException { + ITree> cond = condBlock.parse(ctx); + + ITree> op1 = opblock1.parse(ctx); + + ITree> op2 = opblock2.parse(ctx); + + return new Tree<>(term, cond, op1, op2); + } +} \ No newline at end of file -- cgit v1.2.3