From 4869146748ed51eb212935d2b971388fb9e73d37 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sun, 17 Jul 2022 22:40:11 -0400 Subject: Add support for panfix operators Panfix operators are inspired by Chris Pressey's Quythulg esolang --- .../bjc/pratt/commands/impls/InitialCommands.java | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'JPratt/src/main') diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java index 8a9108b..0cfec29 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java @@ -9,6 +9,7 @@ import java.util.function.UnaryOperator; import bjc.pratt.blocks.ParseBlock; import bjc.pratt.commands.InitialCommand; import bjc.pratt.tokens.Token; +import bjc.data.SimpleTree; import bjc.data.Tree; /** @@ -192,4 +193,33 @@ public class InitialCommands { public static InitialCommand denest(final InitialCommand comm) { return new DenestingCommand<>(comm); } + + /** + * Create a new 'panfix' command. + * + * Form is + * + * @param The key type for the tokens. + * @param The value type for the tokens. + * @param The context type for the tokens. + * + * @param precedence The precedence for this operator + * @param term The indicator for the operator + * @param marker The token to mark this tree + * + * @return A command that implements a panfix operator + */ + public static InitialCommand panfix(final int precedence, final K term, final Token marker) { + return (operator, ctx) -> { + Tree> leftSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false); + ctx.tokens.expect(term); + ctx.tokens.next(); + + Tree> rightSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false); + ctx.tokens.expect(term); + ctx.tokens.next(); + + return new SimpleTree<>(marker, leftSide, rightSide); + }; + } } \ No newline at end of file -- cgit v1.2.3