improve explain bits to be usable

This commit is contained in:
gnembon committed 2025-02-26 18:41:22 +01:00
1 parent 7087fa8510
commit 1928b4f866
4 files changed
+44 -12

No files matched your search

@@ -154,7 +154,7 @@ public class CarpetExpression
} }
} }
public List<Token> explain(ScriptHost host, BlockPos pos) public List<Token> explain(ScriptHost host, @Nullable String method, @Nullable String style, BlockPos pos)
{ {
CarpetScriptServer scriptServer = (CarpetScriptServer) host.scriptServer(); CarpetScriptServer scriptServer = (CarpetScriptServer) host.scriptServer();
try try
@@ -174,7 +174,7 @@ public class CarpetExpression
Value playerValue = new EntityValue(e).bindTo("p"); Value playerValue = new EntityValue(e).bindTo("p");
context.with("p", (cc, tt) -> playerValue); context.with("p", (cc, tt) -> playerValue);
} }
return scriptServer.events.handleEvents.getWhileDisabled(() -> this.expr.explain(context)); return scriptServer.events.handleEvents.getWhileDisabled(() -> this.expr.explain(context, method, style));
} }
catch (ExpressionException e) catch (ExpressionException e)
{ {
+11 -2
View File
@@ -1535,22 +1535,31 @@ public class Expression
} }
} }
public List<Token> explain(Context context) public List<Token> explain(Context context, @Nullable String method, @Nullable String style)
{ {
//todo convert to explain //todo convert to explain
Tokenizer tokenizer = new Tokenizer(context, this, expression, true, true); Tokenizer tokenizer = new Tokenizer(context, this, expression, true, true);
// stripping lousy but acceptable semicolons // stripping lousy but acceptable semicolons
List<Token> input = tokenizer.parseTokens(); List<Token> input = tokenizer.parseTokens();
if (style.equalsIgnoreCase("raw")) {
return input;
}
List<Token> cleanedTokens = Tokenizer.postProcess(input); List<Token> cleanedTokens = Tokenizer.postProcess(input);
if (style.equalsIgnoreCase("clean")) {
return cleanedTokens;
}
List<Token> rpn = shuntingYard(context, cleanedTokens); List<Token> rpn = shuntingYard(context, cleanedTokens);
validate(context, rpn); validate(context, rpn);
ExpressionNode root = RPNToParseTree(rpn, context); ExpressionNode root = RPNToParseTree(rpn, context);
List<Token> parsedTokens = root.tokensRecursive(this); List<Token> parsedTokens = root.tokensRecursive(this);
if (style.equalsIgnoreCase("canonical")) {
return parsedTokens;
}
Context optimizeOnlyContext = new Context.ContextForErrorReporting(context); Context optimizeOnlyContext = new Context.ContextForErrorReporting(context);
// pure functional // pure functional
optimizeTree(root, optimizeOnlyContext, null, true); optimizeTree(root, optimizeOnlyContext, null, style.equalsIgnoreCase("functional"));
List<Token> compileTimeOptimized = root.tokensRecursive(this); List<Token> compileTimeOptimized = root.tokensRecursive(this);
return compileTimeOptimized; return compileTimeOptimized;
+20 -8
View File
@@ -364,15 +364,27 @@ public class ScriptCommand
boolean success = ss(cc).uninstallApp(cc.getSource(), StringArgumentType.getString(cc, "app")); boolean success = ss(cc).uninstallApp(cc.getSource(), StringArgumentType.getString(cc, "app"));
return success ? 1 : 0; return success ? 1 : 0;
})); }));
LiteralArgumentBuilder<CommandSourceStack> x = literal("explain").
requires(Vanilla::ServerPlayer_canScriptACE). LiteralArgumentBuilder<CommandSourceStack> x = literal("explain").requires(Vanilla::ServerPlayer_canScriptACE).
then(argument("style", StringArgumentType.word()).suggests((cc, bb) -> suggest(List.of("raw", "clean", "functional", "canonical", "optimized"), bb)).
executes((cc) -> explain( executes((cc) -> explain(
cc, cc,
null)). null, null, StringArgumentType.getString(cc, "style"))).
then(argument("expr", StringArgumentType.greedyString()).suggests(ScriptCommand::suggestCode). then(literal("run").then(argument("expr", StringArgumentType.greedyString()).suggests(ScriptCommand::suggestCode).
executes((cc) -> explain( executes((cc) -> explain(
cc, cc,
StringArgumentType.getString(cc, "expr")))); StringArgumentType.getString(cc, "expr"), null, StringArgumentType.getString(cc, "style"))))).
then(literal("invoke").
then(argument("call", StringArgumentType.word()).suggests((cc, bb) -> suggest(suggestFunctionCalls(cc), bb)).
executes((cc) -> explain(
cc,
null,
StringArgumentType.getString(cc, "call"),
StringArgumentType.getString(cc, "style")
))
)
)
);
dispatcher.register(literal("script"). dispatcher.register(literal("script").
requires(Vanilla::ServerPlayer_canScriptGeneral). requires(Vanilla::ServerPlayer_canScriptGeneral).
@@ -551,13 +563,13 @@ public class ScriptCommand
}); });
} }
private static int explain(CommandContext<CommandSourceStack> context, @Nullable String expr) throws CommandSyntaxException private static int explain(CommandContext<CommandSourceStack> context, @Nullable String expr, @Nullable String method, String style) throws CommandSyntaxException
{ {
CommandSourceStack source = context.getSource(); CommandSourceStack source = context.getSource();
CarpetScriptHost host = getHost(context); CarpetScriptHost host = getHost(context);
return handleCall(source, host, () -> { return handleCall(source, host, () -> {
CarpetExpression ex = new CarpetExpression(host.main, expr == null ? ( host.main == null ? "" : host.main.code() ) : expr, source, new BlockPos(0, 0, 0)); CarpetExpression ex = new CarpetExpression(host.main, expr == null ? ( host.main == null ? "" : host.main.code() ) : expr, source, BlockPos.ZERO);
List<Token> results = ex.explain(host, BlockPos.containing(source.getPosition())); List<Token> results = ex.explain(host, method, style, BlockPos.containing(source.getPosition()));
prettyPrintTokens(source, results); prettyPrintTokens(source, results);
return NumericValue.of(results.size()); return NumericValue.of(results.size());
}); });
@@ -255,6 +255,7 @@ public class Tokenizer
} }
pos++; pos++;
linepos++; linepos++;
token.disguiseAs("'"+token.surface+"'", null);
} }
else if (Character.isLetter(ch) || "_".indexOf(ch) >= 0) else if (Character.isLetter(ch) || "_".indexOf(ch) >= 0)
@@ -298,6 +299,7 @@ public class Tokenizer
else if (ch == ',') else if (ch == ',')
{ {
token.type = Token.TokenType.COMMA; token.type = Token.TokenType.COMMA;
token.disguiseAs(", ", null);
} }
else else
{ {
@@ -379,12 +381,21 @@ public class Tokenizer
|| (previousToken.type == Token.TokenType.MARKER && (previousToken.surface.equals("{") || previousToken.surface.equals("["))) || (previousToken.type == Token.TokenType.MARKER && (previousToken.surface.equals("{") || previousToken.surface.equals("[")))
) )
{ {
token.disguiseAs(token.surface, null);
token.surface += "u"; token.surface += "u";
token.type = Token.TokenType.UNARY_OPERATOR; token.type = Token.TokenType.UNARY_OPERATOR;
} }
else else
{ {
token.type = Token.TokenType.OPERATOR; token.type = Token.TokenType.OPERATOR;
if (token.surface.equals(";"))
{
token.disguiseAs(token.surface + " ", null);
}
else
{
token.disguiseAs(" " + token.surface + " ", null);
}
} }
} }
if (expression != null && context != null && previousToken != null && if (expression != null && context != null && previousToken != null &&