From 15a2b29e48f134bc93cfd0a3d8512001e9242f3d Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Mon, 3 Jun 2024 17:33:53 -0400 Subject: Rename package to new domain Rename the package to the new domain --- .../pratt/commands/impls/PreTernaryCommand.java | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PreTernaryCommand.java (limited to 'JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PreTernaryCommand.java') diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PreTernaryCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PreTernaryCommand.java new file mode 100644 index 0000000..cd01333 --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PreTernaryCommand.java @@ -0,0 +1,85 @@ +package com.ashardalon.pratt.commands.impls; + +import bjc.data.Tree; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.blocks.ParseBlock; +import com.ashardalon.pratt.commands.AbstractInitialCommand; +import com.ashardalon.pratt.commands.CommandResult; +import com.ashardalon.pratt.commands.CommandResult.Status; +import com.ashardalon.pratt.tokens.Token; + +import bjc.data.SimpleTree; +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 final Token trm; + + private final ParseBlock condBlock; + + private final ParseBlock opblock1; + private final 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(final ParseBlock cond, final ParseBlock op1, + final ParseBlock op2, final 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"); + + condBlock = cond; + opblock1 = op1; + opblock2 = op2; + + trm = term; + } + + @Override + protected CommandResult intNullDenotation(final Token operator, final ParserContext ctx) + throws ParserException { + final CommandResult condRes = condBlock.parse(ctx); + if (condRes.status != Status.SUCCESS) return condRes; + Tree> cond = condRes.success(); + + final CommandResult op1Res = opblock1.parse(ctx); + if (op1Res.status != Status.SUCCESS) return op1Res; + Tree> op1 = op1Res.success(); + + final CommandResult op2Res = opblock2.parse(ctx); + if (op2Res.status != Status.SUCCESS) return op2Res; + Tree> op2 = op2Res.success(); + return CommandResult.success(new SimpleTree<>(trm, cond, op1, op2)); + } +} \ No newline at end of file -- cgit v1.2.3