redscript-mc 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/bug_report.md +40 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +31 -0
- package/.github/ISSUE_TEMPLATE/wrong_output.md +33 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +34 -0
- package/.github/workflows/ci.yml +29 -0
- package/.github/workflows/publish-extension.yml +35 -0
- package/LICENSE +21 -0
- package/README.md +261 -0
- package/README.zh.md +261 -0
- package/dist/__tests__/cli.test.d.ts +1 -0
- package/dist/__tests__/cli.test.js +140 -0
- package/dist/__tests__/codegen.test.d.ts +1 -0
- package/dist/__tests__/codegen.test.js +121 -0
- package/dist/__tests__/diagnostics.test.d.ts +4 -0
- package/dist/__tests__/diagnostics.test.js +149 -0
- package/dist/__tests__/e2e.test.d.ts +6 -0
- package/dist/__tests__/e2e.test.js +1528 -0
- package/dist/__tests__/lexer.test.d.ts +1 -0
- package/dist/__tests__/lexer.test.js +316 -0
- package/dist/__tests__/lowering.test.d.ts +1 -0
- package/dist/__tests__/lowering.test.js +819 -0
- package/dist/__tests__/mc-integration.test.d.ts +12 -0
- package/dist/__tests__/mc-integration.test.js +395 -0
- package/dist/__tests__/mc-syntax.test.d.ts +1 -0
- package/dist/__tests__/mc-syntax.test.js +112 -0
- package/dist/__tests__/nbt.test.d.ts +1 -0
- package/dist/__tests__/nbt.test.js +82 -0
- package/dist/__tests__/optimizer-advanced.test.d.ts +1 -0
- package/dist/__tests__/optimizer-advanced.test.js +124 -0
- package/dist/__tests__/optimizer.test.d.ts +1 -0
- package/dist/__tests__/optimizer.test.js +118 -0
- package/dist/__tests__/parser.test.d.ts +1 -0
- package/dist/__tests__/parser.test.js +717 -0
- package/dist/__tests__/repl.test.d.ts +1 -0
- package/dist/__tests__/repl.test.js +27 -0
- package/dist/__tests__/runtime.test.d.ts +1 -0
- package/dist/__tests__/runtime.test.js +276 -0
- package/dist/__tests__/structure-optimizer.test.d.ts +1 -0
- package/dist/__tests__/structure-optimizer.test.js +33 -0
- package/dist/__tests__/typechecker.test.d.ts +1 -0
- package/dist/__tests__/typechecker.test.js +364 -0
- package/dist/ast/types.d.ts +357 -0
- package/dist/ast/types.js +9 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +407 -0
- package/dist/codegen/cmdblock/index.d.ts +26 -0
- package/dist/codegen/cmdblock/index.js +45 -0
- package/dist/codegen/mcfunction/index.d.ts +34 -0
- package/dist/codegen/mcfunction/index.js +413 -0
- package/dist/codegen/structure/index.d.ts +18 -0
- package/dist/codegen/structure/index.js +249 -0
- package/dist/compile.d.ts +30 -0
- package/dist/compile.js +152 -0
- package/dist/data/arena/function/__load.mcfunction +6 -0
- package/dist/data/arena/function/__tick.mcfunction +2 -0
- package/dist/data/arena/function/announce_leaders/else_1.mcfunction +3 -0
- package/dist/data/arena/function/announce_leaders/foreach_0/merge_2.mcfunction +1 -0
- package/dist/data/arena/function/announce_leaders/foreach_0/then_0.mcfunction +3 -0
- package/dist/data/arena/function/announce_leaders/foreach_0.mcfunction +7 -0
- package/dist/data/arena/function/announce_leaders/foreach_1/merge_2.mcfunction +1 -0
- package/dist/data/arena/function/announce_leaders/foreach_1/then_0.mcfunction +4 -0
- package/dist/data/arena/function/announce_leaders/foreach_1.mcfunction +6 -0
- package/dist/data/arena/function/announce_leaders/merge_2.mcfunction +1 -0
- package/dist/data/arena/function/announce_leaders/then_0.mcfunction +4 -0
- package/dist/data/arena/function/announce_leaders.mcfunction +6 -0
- package/dist/data/arena/function/arena_tick/merge_2.mcfunction +1 -0
- package/dist/data/arena/function/arena_tick/then_0.mcfunction +4 -0
- package/dist/data/arena/function/arena_tick.mcfunction +11 -0
- package/dist/data/counter/function/__load.mcfunction +5 -0
- package/dist/data/counter/function/__tick.mcfunction +2 -0
- package/dist/data/counter/function/counter_tick/merge_2.mcfunction +1 -0
- package/dist/data/counter/function/counter_tick/then_0.mcfunction +3 -0
- package/dist/data/counter/function/counter_tick.mcfunction +11 -0
- package/dist/data/minecraft/tags/function/load.json +5 -0
- package/dist/data/minecraft/tags/function/tick.json +5 -0
- package/dist/data/quiz/function/__load.mcfunction +16 -0
- package/dist/data/quiz/function/__tick.mcfunction +6 -0
- package/dist/data/quiz/function/__trigger_quiz_a_dispatch.mcfunction +4 -0
- package/dist/data/quiz/function/__trigger_quiz_b_dispatch.mcfunction +4 -0
- package/dist/data/quiz/function/__trigger_quiz_c_dispatch.mcfunction +4 -0
- package/dist/data/quiz/function/__trigger_quiz_start_dispatch.mcfunction +4 -0
- package/dist/data/quiz/function/answer_a.mcfunction +4 -0
- package/dist/data/quiz/function/answer_b.mcfunction +4 -0
- package/dist/data/quiz/function/answer_c.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question/else_1.mcfunction +5 -0
- package/dist/data/quiz/function/ask_question/else_4.mcfunction +5 -0
- package/dist/data/quiz/function/ask_question/else_7.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question/merge_2.mcfunction +1 -0
- package/dist/data/quiz/function/ask_question/merge_5.mcfunction +2 -0
- package/dist/data/quiz/function/ask_question/merge_8.mcfunction +2 -0
- package/dist/data/quiz/function/ask_question/then_0.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question/then_3.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question/then_6.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question.mcfunction +7 -0
- package/dist/data/quiz/function/finish_quiz.mcfunction +6 -0
- package/dist/data/quiz/function/handle_answer/else_1.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/else_10.mcfunction +3 -0
- package/dist/data/quiz/function/handle_answer/else_16.mcfunction +3 -0
- package/dist/data/quiz/function/handle_answer/else_4.mcfunction +3 -0
- package/dist/data/quiz/function/handle_answer/else_7.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/merge_11.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/merge_14.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/merge_17.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/merge_2.mcfunction +8 -0
- package/dist/data/quiz/function/handle_answer/merge_5.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/merge_8.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/then_0.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/then_12.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/then_15.mcfunction +6 -0
- package/dist/data/quiz/function/handle_answer/then_3.mcfunction +6 -0
- package/dist/data/quiz/function/handle_answer/then_6.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/then_9.mcfunction +6 -0
- package/dist/data/quiz/function/handle_answer.mcfunction +11 -0
- package/dist/data/quiz/function/start_quiz.mcfunction +5 -0
- package/dist/data/shop/function/__load.mcfunction +7 -0
- package/dist/data/shop/function/__tick.mcfunction +3 -0
- package/dist/data/shop/function/__trigger_shop_buy_dispatch.mcfunction +4 -0
- package/dist/data/shop/function/complete_purchase/else_1.mcfunction +5 -0
- package/dist/data/shop/function/complete_purchase/else_4.mcfunction +5 -0
- package/dist/data/shop/function/complete_purchase/else_7.mcfunction +3 -0
- package/dist/data/shop/function/complete_purchase/merge_2.mcfunction +2 -0
- package/dist/data/shop/function/complete_purchase/merge_5.mcfunction +2 -0
- package/dist/data/shop/function/complete_purchase/merge_8.mcfunction +2 -0
- package/dist/data/shop/function/complete_purchase/then_0.mcfunction +4 -0
- package/dist/data/shop/function/complete_purchase/then_3.mcfunction +4 -0
- package/dist/data/shop/function/complete_purchase/then_6.mcfunction +4 -0
- package/dist/data/shop/function/complete_purchase.mcfunction +7 -0
- package/dist/data/shop/function/handle_shop_trigger.mcfunction +3 -0
- package/dist/data/turret/function/__load.mcfunction +5 -0
- package/dist/data/turret/function/__tick.mcfunction +4 -0
- package/dist/data/turret/function/__trigger_deploy_turret_dispatch.mcfunction +4 -0
- package/dist/data/turret/function/deploy_turret.mcfunction +8 -0
- package/dist/data/turret/function/turret_tick/at_1.mcfunction +2 -0
- package/dist/data/turret/function/turret_tick/foreach_0.mcfunction +2 -0
- package/dist/data/turret/function/turret_tick/foreach_2.mcfunction +2 -0
- package/dist/data/turret/function/turret_tick/tick_body.mcfunction +3 -0
- package/dist/data/turret/function/turret_tick/tick_skip.mcfunction +1 -0
- package/dist/data/turret/function/turret_tick.mcfunction +5 -0
- package/dist/diagnostics/index.d.ts +44 -0
- package/dist/diagnostics/index.js +140 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +126 -0
- package/dist/ir/builder.d.ts +32 -0
- package/dist/ir/builder.js +99 -0
- package/dist/ir/types.d.ts +117 -0
- package/dist/ir/types.js +15 -0
- package/dist/lexer/index.d.ts +36 -0
- package/dist/lexer/index.js +458 -0
- package/dist/lowering/index.d.ts +106 -0
- package/dist/lowering/index.js +2041 -0
- package/dist/mc-test/client.d.ts +128 -0
- package/dist/mc-test/client.js +174 -0
- package/dist/mc-test/runner.d.ts +28 -0
- package/dist/mc-test/runner.js +150 -0
- package/dist/mc-test/setup.d.ts +11 -0
- package/dist/mc-test/setup.js +98 -0
- package/dist/mc-validator/index.d.ts +17 -0
- package/dist/mc-validator/index.js +322 -0
- package/dist/nbt/index.d.ts +86 -0
- package/dist/nbt/index.js +250 -0
- package/dist/optimizer/commands.d.ts +36 -0
- package/dist/optimizer/commands.js +349 -0
- package/dist/optimizer/passes.d.ts +34 -0
- package/dist/optimizer/passes.js +227 -0
- package/dist/optimizer/structure.d.ts +8 -0
- package/dist/optimizer/structure.js +344 -0
- package/dist/pack.mcmeta +6 -0
- package/dist/parser/index.d.ts +76 -0
- package/dist/parser/index.js +1193 -0
- package/dist/repl.d.ts +16 -0
- package/dist/repl.js +165 -0
- package/dist/runtime/index.d.ts +101 -0
- package/dist/runtime/index.js +1288 -0
- package/dist/typechecker/index.d.ts +42 -0
- package/dist/typechecker/index.js +629 -0
- package/docs/COMPILATION_STATS.md +142 -0
- package/docs/IMPLEMENTATION_GUIDE.md +512 -0
- package/docs/LANGUAGE_REFERENCE.md +415 -0
- package/docs/MC_MAPPING.md +280 -0
- package/docs/STRUCTURE_TARGET.md +80 -0
- package/docs/mc-reference/commands.md +259 -0
- package/editors/vscode/.vscodeignore +10 -0
- package/editors/vscode/LICENSE +21 -0
- package/editors/vscode/README.md +78 -0
- package/editors/vscode/build.mjs +28 -0
- package/editors/vscode/icon.png +0 -0
- package/editors/vscode/mcfunction-language-configuration.json +28 -0
- package/editors/vscode/out/extension.js +7236 -0
- package/editors/vscode/package-lock.json +566 -0
- package/editors/vscode/package.json +137 -0
- package/editors/vscode/redscript-language-configuration.json +28 -0
- package/editors/vscode/snippets/redscript.json +114 -0
- package/editors/vscode/src/codeactions.ts +89 -0
- package/editors/vscode/src/completion.ts +130 -0
- package/editors/vscode/src/extension.ts +239 -0
- package/editors/vscode/src/hover.ts +1120 -0
- package/editors/vscode/src/symbols.ts +207 -0
- package/editors/vscode/syntaxes/mcfunction.tmLanguage.json +740 -0
- package/editors/vscode/syntaxes/redscript.tmLanguage.json +357 -0
- package/editors/vscode/tsconfig.json +13 -0
- package/jest.config.js +5 -0
- package/package.json +38 -0
- package/src/__tests__/cli.test.ts +130 -0
- package/src/__tests__/codegen.test.ts +128 -0
- package/src/__tests__/diagnostics.test.ts +195 -0
- package/src/__tests__/e2e.test.ts +1721 -0
- package/src/__tests__/fixtures/mc-commands-1.21.4.json +18734 -0
- package/src/__tests__/formatter.test.ts +46 -0
- package/src/__tests__/lexer.test.ts +356 -0
- package/src/__tests__/lowering.test.ts +962 -0
- package/src/__tests__/mc-integration.test.ts +409 -0
- package/src/__tests__/mc-syntax.test.ts +96 -0
- package/src/__tests__/nbt.test.ts +58 -0
- package/src/__tests__/optimizer-advanced.test.ts +144 -0
- package/src/__tests__/optimizer.test.ts +129 -0
- package/src/__tests__/parser.test.ts +800 -0
- package/src/__tests__/repl.test.ts +33 -0
- package/src/__tests__/runtime.test.ts +289 -0
- package/src/__tests__/structure-optimizer.test.ts +38 -0
- package/src/__tests__/typechecker.test.ts +395 -0
- package/src/ast/types.ts +248 -0
- package/src/cli.ts +445 -0
- package/src/codegen/cmdblock/index.ts +63 -0
- package/src/codegen/mcfunction/index.ts +471 -0
- package/src/codegen/structure/index.ts +305 -0
- package/src/compile.ts +188 -0
- package/src/diagnostics/index.ts +186 -0
- package/src/examples/README.md +77 -0
- package/src/examples/SHOWCASE_GAME.md +43 -0
- package/src/examples/arena.rs +44 -0
- package/src/examples/counter.rs +12 -0
- package/src/examples/pvp_arena.rs +131 -0
- package/src/examples/quiz.rs +90 -0
- package/src/examples/rpg.rs +13 -0
- package/src/examples/shop.rs +30 -0
- package/src/examples/showcase_game.rs +552 -0
- package/src/examples/stdlib_demo.rs +181 -0
- package/src/examples/turret.rs +27 -0
- package/src/examples/world_manager.rs +23 -0
- package/src/formatter/index.ts +22 -0
- package/src/index.ts +161 -0
- package/src/ir/builder.ts +114 -0
- package/src/ir/types.ts +119 -0
- package/src/lexer/index.ts +555 -0
- package/src/lowering/index.ts +2406 -0
- package/src/mc-test/client.ts +259 -0
- package/src/mc-test/runner.ts +140 -0
- package/src/mc-test/setup.ts +70 -0
- package/src/mc-validator/index.ts +367 -0
- package/src/nbt/index.ts +321 -0
- package/src/optimizer/commands.ts +416 -0
- package/src/optimizer/passes.ts +233 -0
- package/src/optimizer/structure.ts +441 -0
- package/src/parser/index.ts +1437 -0
- package/src/repl.ts +165 -0
- package/src/runtime/index.ts +1403 -0
- package/src/stdlib/README.md +156 -0
- package/src/stdlib/combat.rs +20 -0
- package/src/stdlib/cooldown.rs +45 -0
- package/src/stdlib/math.rs +49 -0
- package/src/stdlib/mobs.rs +99 -0
- package/src/stdlib/player.rs +29 -0
- package/src/stdlib/strings.rs +7 -0
- package/src/stdlib/timer.rs +51 -0
- package/src/templates/README.md +126 -0
- package/src/templates/combat.rs +96 -0
- package/src/templates/economy.rs +40 -0
- package/src/templates/mini-game-framework.rs +117 -0
- package/src/templates/quest.rs +78 -0
- package/src/test_programs/zombie_game.rs +25 -0
- package/src/typechecker/index.ts +737 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const lexer_1 = require("../lexer");
|
|
4
|
+
const parser_1 = require("../parser");
|
|
5
|
+
function parse(source, namespace = 'test') {
|
|
6
|
+
const tokens = new lexer_1.Lexer(source).tokenize();
|
|
7
|
+
return new parser_1.Parser(tokens).parse(namespace);
|
|
8
|
+
}
|
|
9
|
+
function parseExpr(source) {
|
|
10
|
+
const program = parse(`fn _test() { ${source}; }`);
|
|
11
|
+
const stmt = program.declarations[0].body[0];
|
|
12
|
+
if (stmt.kind !== 'expr')
|
|
13
|
+
throw new Error('Expected expr stmt');
|
|
14
|
+
return stmt.expr;
|
|
15
|
+
}
|
|
16
|
+
function parseStmt(source) {
|
|
17
|
+
const program = parse(`fn _test() { ${source} }`);
|
|
18
|
+
return program.declarations[0].body[0];
|
|
19
|
+
}
|
|
20
|
+
describe('Parser', () => {
|
|
21
|
+
describe('program structure', () => {
|
|
22
|
+
it('parses empty program', () => {
|
|
23
|
+
const program = parse('');
|
|
24
|
+
expect(program.namespace).toBe('test');
|
|
25
|
+
expect(program.declarations).toEqual([]);
|
|
26
|
+
expect(program.enums).toEqual([]);
|
|
27
|
+
expect(program.consts).toEqual([]);
|
|
28
|
+
});
|
|
29
|
+
it('parses namespace declaration', () => {
|
|
30
|
+
const program = parse('namespace mypack;', 'default');
|
|
31
|
+
expect(program.namespace).toBe('mypack');
|
|
32
|
+
});
|
|
33
|
+
it('parses function declaration', () => {
|
|
34
|
+
const program = parse('fn foo() {}');
|
|
35
|
+
expect(program.declarations).toHaveLength(1);
|
|
36
|
+
expect(program.declarations[0].name).toBe('foo');
|
|
37
|
+
});
|
|
38
|
+
it('parses top-level const declarations', () => {
|
|
39
|
+
const program = parse('const MAX_HP: int = 100\nconst NAME: string = "Arena"');
|
|
40
|
+
expect(program.consts).toEqual([
|
|
41
|
+
{ name: 'MAX_HP', type: { kind: 'named', name: 'int' }, value: { kind: 'int_lit', value: 100 } },
|
|
42
|
+
{ name: 'NAME', type: { kind: 'named', name: 'string' }, value: { kind: 'str_lit', value: 'Arena' } },
|
|
43
|
+
]);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
describe('function declarations', () => {
|
|
47
|
+
it('parses function with no params', () => {
|
|
48
|
+
const program = parse('fn hello() {}');
|
|
49
|
+
const fn = program.declarations[0];
|
|
50
|
+
expect(fn.name).toBe('hello');
|
|
51
|
+
expect(fn.params).toEqual([]);
|
|
52
|
+
expect(fn.returnType).toEqual({ kind: 'named', name: 'void' });
|
|
53
|
+
});
|
|
54
|
+
it('parses function with params', () => {
|
|
55
|
+
const program = parse('fn add(a: int, b: int) -> int { return a + b; }');
|
|
56
|
+
const fn = program.declarations[0];
|
|
57
|
+
expect(fn.name).toBe('add');
|
|
58
|
+
expect(fn.params).toEqual([
|
|
59
|
+
{ name: 'a', type: { kind: 'named', name: 'int' }, default: undefined },
|
|
60
|
+
{ name: 'b', type: { kind: 'named', name: 'int' }, default: undefined },
|
|
61
|
+
]);
|
|
62
|
+
expect(fn.returnType).toEqual({ kind: 'named', name: 'int' });
|
|
63
|
+
});
|
|
64
|
+
it('parses function params with defaults', () => {
|
|
65
|
+
const program = parse('fn greet(name: string, formal: bool = false) {}');
|
|
66
|
+
expect(program.declarations[0].params).toEqual([
|
|
67
|
+
{ name: 'name', type: { kind: 'named', name: 'string' }, default: undefined },
|
|
68
|
+
{ name: 'formal', type: { kind: 'named', name: 'bool' }, default: { kind: 'bool_lit', value: false } },
|
|
69
|
+
]);
|
|
70
|
+
});
|
|
71
|
+
it('parses function with decorators', () => {
|
|
72
|
+
const program = parse('@tick\nfn game_loop() {}');
|
|
73
|
+
const fn = program.declarations[0];
|
|
74
|
+
expect(fn.decorators).toEqual([{ name: 'tick' }]);
|
|
75
|
+
});
|
|
76
|
+
it('parses decorator with args', () => {
|
|
77
|
+
const program = parse('@tick(rate=20)\nfn slow_loop() {}');
|
|
78
|
+
const fn = program.declarations[0];
|
|
79
|
+
expect(fn.decorators).toEqual([{ name: 'tick', args: { rate: 20 } }]);
|
|
80
|
+
});
|
|
81
|
+
it('parses multiple decorators', () => {
|
|
82
|
+
const program = parse('@tick\n@on_trigger\nfn both() {}');
|
|
83
|
+
const fn = program.declarations[0];
|
|
84
|
+
expect(fn.decorators).toHaveLength(2);
|
|
85
|
+
});
|
|
86
|
+
it('parses advancement and death decorators', () => {
|
|
87
|
+
const program = parse('@on_advancement("story/mine_diamond")\n@on_death\nfn handler() {}');
|
|
88
|
+
expect(program.declarations[0].decorators).toEqual([
|
|
89
|
+
{ name: 'on_advancement', args: { advancement: 'story/mine_diamond' } },
|
|
90
|
+
{ name: 'on_death' },
|
|
91
|
+
]);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
describe('types', () => {
|
|
95
|
+
it('parses primitive types', () => {
|
|
96
|
+
const program = parse('fn f(a: int, b: bool, c: float, d: string) {}');
|
|
97
|
+
const params = program.declarations[0].params;
|
|
98
|
+
expect(params.map(p => p.type)).toEqual([
|
|
99
|
+
{ kind: 'named', name: 'int' },
|
|
100
|
+
{ kind: 'named', name: 'bool' },
|
|
101
|
+
{ kind: 'named', name: 'float' },
|
|
102
|
+
{ kind: 'named', name: 'string' },
|
|
103
|
+
]);
|
|
104
|
+
});
|
|
105
|
+
it('parses array types', () => {
|
|
106
|
+
const program = parse('fn f(a: int[]) {}');
|
|
107
|
+
const param = program.declarations[0].params[0];
|
|
108
|
+
expect(param.type).toEqual({ kind: 'array', elem: { kind: 'named', name: 'int' } });
|
|
109
|
+
});
|
|
110
|
+
it('parses BlockPos types', () => {
|
|
111
|
+
const program = parse('fn f(pos: BlockPos) {}');
|
|
112
|
+
const param = program.declarations[0].params[0];
|
|
113
|
+
expect(param.type).toEqual({ kind: 'named', name: 'BlockPos' });
|
|
114
|
+
});
|
|
115
|
+
it('parses function types', () => {
|
|
116
|
+
const program = parse('fn apply(val: int, cb: (int) -> int) -> int { return cb(val); }');
|
|
117
|
+
expect(program.declarations[0].params[1].type).toEqual({
|
|
118
|
+
kind: 'function_type',
|
|
119
|
+
params: [{ kind: 'named', name: 'int' }],
|
|
120
|
+
return: { kind: 'named', name: 'int' },
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
it('parses enum declarations', () => {
|
|
124
|
+
const program = parse('enum Direction { North, South = 3, East, West }');
|
|
125
|
+
expect(program.enums).toEqual([
|
|
126
|
+
{
|
|
127
|
+
name: 'Direction',
|
|
128
|
+
variants: [
|
|
129
|
+
{ name: 'North', value: 0 },
|
|
130
|
+
{ name: 'South', value: 3 },
|
|
131
|
+
{ name: 'East', value: 4 },
|
|
132
|
+
{ name: 'West', value: 5 },
|
|
133
|
+
],
|
|
134
|
+
},
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
describe('statements', () => {
|
|
139
|
+
it('parses let statement', () => {
|
|
140
|
+
const stmt = parseStmt('let x: int = 5;');
|
|
141
|
+
expect(stmt).toEqual({
|
|
142
|
+
kind: 'let',
|
|
143
|
+
name: 'x',
|
|
144
|
+
type: { kind: 'named', name: 'int' },
|
|
145
|
+
init: { kind: 'int_lit', value: 5 },
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
it('parses let without type annotation', () => {
|
|
149
|
+
const stmt = parseStmt('let x = 5;');
|
|
150
|
+
expect(stmt.kind).toBe('let');
|
|
151
|
+
expect(stmt.type).toBeUndefined();
|
|
152
|
+
});
|
|
153
|
+
it('parses return statement', () => {
|
|
154
|
+
const stmt = parseStmt('return 42;');
|
|
155
|
+
expect(stmt).toEqual({
|
|
156
|
+
kind: 'return',
|
|
157
|
+
value: { kind: 'int_lit', value: 42 },
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
it('parses empty return', () => {
|
|
161
|
+
const stmt = parseStmt('return;');
|
|
162
|
+
expect(stmt).toEqual({ kind: 'return', value: undefined });
|
|
163
|
+
});
|
|
164
|
+
it('parses if statement', () => {
|
|
165
|
+
const stmt = parseStmt('if (x > 0) { y = 1; }');
|
|
166
|
+
expect(stmt.kind).toBe('if');
|
|
167
|
+
expect(stmt.cond.kind).toBe('binary');
|
|
168
|
+
expect(stmt.then).toHaveLength(1);
|
|
169
|
+
expect(stmt.else_).toBeUndefined();
|
|
170
|
+
});
|
|
171
|
+
it('parses if-else statement', () => {
|
|
172
|
+
const stmt = parseStmt('if (x > 0) { y = 1; } else { y = 2; }');
|
|
173
|
+
expect(stmt.kind).toBe('if');
|
|
174
|
+
expect(stmt.else_).toHaveLength(1);
|
|
175
|
+
});
|
|
176
|
+
it('parses while statement', () => {
|
|
177
|
+
const stmt = parseStmt('while (i > 0) { i = i - 1; }');
|
|
178
|
+
expect(stmt.kind).toBe('while');
|
|
179
|
+
expect(stmt.cond.kind).toBe('binary');
|
|
180
|
+
expect(stmt.body).toHaveLength(1);
|
|
181
|
+
});
|
|
182
|
+
it('parses for statement', () => {
|
|
183
|
+
const stmt = parseStmt('for (let i: int = 0; i < 10; i = i + 1) { say("loop"); }');
|
|
184
|
+
expect(stmt.kind).toBe('for');
|
|
185
|
+
expect(stmt.init.kind).toBe('let');
|
|
186
|
+
expect(stmt.init.name).toBe('i');
|
|
187
|
+
expect(stmt.cond.kind).toBe('binary');
|
|
188
|
+
expect(stmt.cond.op).toBe('<');
|
|
189
|
+
expect(stmt.step.kind).toBe('assign');
|
|
190
|
+
expect(stmt.body).toHaveLength(1);
|
|
191
|
+
});
|
|
192
|
+
it('parses for statement without init', () => {
|
|
193
|
+
const stmt = parseStmt('for (; i < 10; i = i + 1) { say("loop"); }');
|
|
194
|
+
expect(stmt.kind).toBe('for');
|
|
195
|
+
expect(stmt.init).toBeUndefined();
|
|
196
|
+
expect(stmt.cond.kind).toBe('binary');
|
|
197
|
+
});
|
|
198
|
+
it('parses foreach statement', () => {
|
|
199
|
+
const stmt = parseStmt('foreach (z in @e[type=zombie]) { kill(z); }');
|
|
200
|
+
expect(stmt.kind).toBe('foreach');
|
|
201
|
+
expect(stmt.binding).toBe('z');
|
|
202
|
+
expect(stmt.iterable.kind).toBe('selector');
|
|
203
|
+
expect(stmt.iterable.sel.kind).toBe('@e');
|
|
204
|
+
});
|
|
205
|
+
it('parses match statement', () => {
|
|
206
|
+
const stmt = parseStmt('match (choice) { 1 => { say("one"); } 2 => { say("two"); } _ => { say("other"); } }');
|
|
207
|
+
expect(stmt.kind).toBe('match');
|
|
208
|
+
expect(stmt.expr).toEqual({ kind: 'ident', name: 'choice' });
|
|
209
|
+
expect(stmt.arms).toEqual([
|
|
210
|
+
{ pattern: { kind: 'int_lit', value: 1 }, body: [{ kind: 'expr', expr: { kind: 'call', fn: 'say', args: [{ kind: 'str_lit', value: 'one' }] } }] },
|
|
211
|
+
{ pattern: { kind: 'int_lit', value: 2 }, body: [{ kind: 'expr', expr: { kind: 'call', fn: 'say', args: [{ kind: 'str_lit', value: 'two' }] } }] },
|
|
212
|
+
{ pattern: null, body: [{ kind: 'expr', expr: { kind: 'call', fn: 'say', args: [{ kind: 'str_lit', value: 'other' }] } }] },
|
|
213
|
+
]);
|
|
214
|
+
});
|
|
215
|
+
it('parses as block', () => {
|
|
216
|
+
const stmt = parseStmt('as @a { say("hello"); }');
|
|
217
|
+
expect(stmt.kind).toBe('as_block');
|
|
218
|
+
expect(stmt.selector.kind).toBe('@a');
|
|
219
|
+
});
|
|
220
|
+
it('parses at block', () => {
|
|
221
|
+
const stmt = parseStmt('at @s { summon("zombie"); }');
|
|
222
|
+
expect(stmt.kind).toBe('at_block');
|
|
223
|
+
expect(stmt.selector.kind).toBe('@s');
|
|
224
|
+
});
|
|
225
|
+
it('parses as at combined', () => {
|
|
226
|
+
const stmt = parseStmt('as @a at @s { particle("flame"); }');
|
|
227
|
+
expect(stmt.kind).toBe('as_at');
|
|
228
|
+
expect(stmt.as_sel.kind).toBe('@a');
|
|
229
|
+
expect(stmt.at_sel.kind).toBe('@s');
|
|
230
|
+
});
|
|
231
|
+
it('parses raw command', () => {
|
|
232
|
+
const stmt = parseStmt('raw("say hello");');
|
|
233
|
+
expect(stmt).toEqual({ kind: 'raw', cmd: 'say hello' });
|
|
234
|
+
});
|
|
235
|
+
it('parses execute as run block', () => {
|
|
236
|
+
const stmt = parseStmt('execute as @a run { say("hello"); }');
|
|
237
|
+
expect(stmt.kind).toBe('execute');
|
|
238
|
+
expect(stmt.subcommands).toHaveLength(1);
|
|
239
|
+
expect(stmt.subcommands[0]).toEqual({ kind: 'as', selector: { kind: '@a' } });
|
|
240
|
+
expect(stmt.body).toHaveLength(1);
|
|
241
|
+
});
|
|
242
|
+
it('parses execute as at run block', () => {
|
|
243
|
+
const stmt = parseStmt('execute as @a at @s run { particle("flame"); }');
|
|
244
|
+
expect(stmt.kind).toBe('execute');
|
|
245
|
+
expect(stmt.subcommands).toHaveLength(2);
|
|
246
|
+
expect(stmt.subcommands[0]).toEqual({ kind: 'as', selector: { kind: '@a' } });
|
|
247
|
+
expect(stmt.subcommands[1]).toEqual({ kind: 'at', selector: { kind: '@s' } });
|
|
248
|
+
});
|
|
249
|
+
it('parses execute with if entity condition', () => {
|
|
250
|
+
const stmt = parseStmt('execute as @a if entity @s[tag=admin] run { give(@s, "diamond", 1); }');
|
|
251
|
+
expect(stmt.kind).toBe('execute');
|
|
252
|
+
expect(stmt.subcommands).toHaveLength(2);
|
|
253
|
+
expect(stmt.subcommands[1].kind).toBe('if_entity');
|
|
254
|
+
expect(stmt.subcommands[1].selector.filters.tag).toEqual(['admin']);
|
|
255
|
+
});
|
|
256
|
+
it('parses execute with unless entity condition', () => {
|
|
257
|
+
const stmt = parseStmt('execute as @a unless entity @s[tag=dead] run { effect(@s, "regeneration", 5); }');
|
|
258
|
+
expect(stmt.kind).toBe('execute');
|
|
259
|
+
expect(stmt.subcommands).toHaveLength(2);
|
|
260
|
+
expect(stmt.subcommands[1].kind).toBe('unless_entity');
|
|
261
|
+
});
|
|
262
|
+
it('parses execute with in dimension', () => {
|
|
263
|
+
const stmt = parseStmt('execute in the_nether run { say("in nether"); }');
|
|
264
|
+
expect(stmt.kind).toBe('execute');
|
|
265
|
+
expect(stmt.subcommands).toHaveLength(1);
|
|
266
|
+
expect(stmt.subcommands[0]).toEqual({ kind: 'in', dimension: 'the_nether' });
|
|
267
|
+
});
|
|
268
|
+
it('parses complex execute chain', () => {
|
|
269
|
+
const stmt = parseStmt('execute as @a at @s if entity @s[tag=vip] in overworld run { particle("heart"); }');
|
|
270
|
+
expect(stmt.kind).toBe('execute');
|
|
271
|
+
expect(stmt.subcommands).toHaveLength(4);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
describe('lambda expressions', () => {
|
|
275
|
+
it('parses expression-body lambdas', () => {
|
|
276
|
+
const stmt = parseStmt('let double = (x: int) => x * 2;');
|
|
277
|
+
expect(stmt.kind).toBe('let');
|
|
278
|
+
expect(stmt.init).toEqual({
|
|
279
|
+
kind: 'lambda',
|
|
280
|
+
params: [{ name: 'x', type: { kind: 'named', name: 'int' } }],
|
|
281
|
+
returnType: undefined,
|
|
282
|
+
body: {
|
|
283
|
+
kind: 'binary',
|
|
284
|
+
op: '*',
|
|
285
|
+
left: { kind: 'ident', name: 'x' },
|
|
286
|
+
right: { kind: 'int_lit', value: 2 },
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
it('parses block-body lambdas', () => {
|
|
291
|
+
const stmt = parseStmt('let process: (int) -> int = (x: int) => { let doubled: int = x * 2; return doubled + 1; };');
|
|
292
|
+
expect(stmt.kind).toBe('let');
|
|
293
|
+
expect(stmt.init.kind).toBe('lambda');
|
|
294
|
+
expect(Array.isArray(stmt.init.body)).toBe(true);
|
|
295
|
+
});
|
|
296
|
+
it('parses single-parameter lambdas without parens', () => {
|
|
297
|
+
const stmt = parseStmt('let double: (int) -> int = x => x * 2;');
|
|
298
|
+
expect(stmt.kind).toBe('let');
|
|
299
|
+
expect(stmt.init).toEqual({
|
|
300
|
+
kind: 'lambda',
|
|
301
|
+
params: [{ name: 'x' }],
|
|
302
|
+
returnType: undefined,
|
|
303
|
+
body: {
|
|
304
|
+
kind: 'binary',
|
|
305
|
+
op: '*',
|
|
306
|
+
left: { kind: 'ident', name: 'x' },
|
|
307
|
+
right: { kind: 'int_lit', value: 2 },
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
it('parses immediately-invoked lambdas', () => {
|
|
312
|
+
const expr = parseExpr('((x: int) => x * 2)(5)');
|
|
313
|
+
expect(expr).toEqual({
|
|
314
|
+
kind: 'invoke',
|
|
315
|
+
callee: {
|
|
316
|
+
kind: 'lambda',
|
|
317
|
+
params: [{ name: 'x', type: { kind: 'named', name: 'int' } }],
|
|
318
|
+
returnType: undefined,
|
|
319
|
+
body: {
|
|
320
|
+
kind: 'binary',
|
|
321
|
+
op: '*',
|
|
322
|
+
left: { kind: 'ident', name: 'x' },
|
|
323
|
+
right: { kind: 'int_lit', value: 2 },
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
args: [{ kind: 'int_lit', value: 5 }],
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
describe('expressions', () => {
|
|
331
|
+
describe('literals', () => {
|
|
332
|
+
it('parses integer literal', () => {
|
|
333
|
+
const expr = parseExpr('42');
|
|
334
|
+
expect(expr).toEqual({ kind: 'int_lit', value: 42 });
|
|
335
|
+
});
|
|
336
|
+
it('parses float literal', () => {
|
|
337
|
+
const expr = parseExpr('3.14');
|
|
338
|
+
expect(expr).toEqual({ kind: 'float_lit', value: 3.14 });
|
|
339
|
+
});
|
|
340
|
+
it('parses string literal', () => {
|
|
341
|
+
const expr = parseExpr('"hello"');
|
|
342
|
+
expect(expr).toEqual({ kind: 'str_lit', value: 'hello' });
|
|
343
|
+
});
|
|
344
|
+
it('parses interpolated string literal', () => {
|
|
345
|
+
const expr = parseExpr('"Hello ${name}, score is ${score + 1}"');
|
|
346
|
+
expect(expr).toEqual({
|
|
347
|
+
kind: 'str_interp',
|
|
348
|
+
parts: [
|
|
349
|
+
'Hello ',
|
|
350
|
+
{ kind: 'ident', name: 'name' },
|
|
351
|
+
', score is ',
|
|
352
|
+
{
|
|
353
|
+
kind: 'binary',
|
|
354
|
+
op: '+',
|
|
355
|
+
left: { kind: 'ident', name: 'score' },
|
|
356
|
+
right: { kind: 'int_lit', value: 1 },
|
|
357
|
+
},
|
|
358
|
+
],
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
it('parses boolean literals', () => {
|
|
362
|
+
expect(parseExpr('true')).toEqual({ kind: 'bool_lit', value: true });
|
|
363
|
+
expect(parseExpr('false')).toEqual({ kind: 'bool_lit', value: false });
|
|
364
|
+
});
|
|
365
|
+
it('parses range literals', () => {
|
|
366
|
+
expect(parseExpr('..5')).toEqual({ kind: 'range_lit', range: { max: 5 } });
|
|
367
|
+
expect(parseExpr('1..')).toEqual({ kind: 'range_lit', range: { min: 1 } });
|
|
368
|
+
expect(parseExpr('1..10')).toEqual({ kind: 'range_lit', range: { min: 1, max: 10 } });
|
|
369
|
+
});
|
|
370
|
+
it('parses absolute block positions', () => {
|
|
371
|
+
expect(parseExpr('(0, 64, 0)')).toEqual({
|
|
372
|
+
kind: 'blockpos',
|
|
373
|
+
x: { kind: 'absolute', value: 0 },
|
|
374
|
+
y: { kind: 'absolute', value: 64 },
|
|
375
|
+
z: { kind: 'absolute', value: 0 },
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
it('parses relative block positions', () => {
|
|
379
|
+
expect(parseExpr('(~1, ~0, ~-1)')).toEqual({
|
|
380
|
+
kind: 'blockpos',
|
|
381
|
+
x: { kind: 'relative', offset: 1 },
|
|
382
|
+
y: { kind: 'relative', offset: 0 },
|
|
383
|
+
z: { kind: 'relative', offset: -1 },
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
it('parses local block positions', () => {
|
|
387
|
+
expect(parseExpr('(^0, ^1, ^0)')).toEqual({
|
|
388
|
+
kind: 'blockpos',
|
|
389
|
+
x: { kind: 'local', offset: 0 },
|
|
390
|
+
y: { kind: 'local', offset: 1 },
|
|
391
|
+
z: { kind: 'local', offset: 0 },
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
it('parses mixed block positions', () => {
|
|
395
|
+
expect(parseExpr('(~0, 64, ~0)')).toEqual({
|
|
396
|
+
kind: 'blockpos',
|
|
397
|
+
x: { kind: 'relative', offset: 0 },
|
|
398
|
+
y: { kind: 'absolute', value: 64 },
|
|
399
|
+
z: { kind: 'relative', offset: 0 },
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
describe('identifiers and calls', () => {
|
|
404
|
+
it('parses identifier', () => {
|
|
405
|
+
const expr = parseExpr('foo');
|
|
406
|
+
expect(expr).toEqual({ kind: 'ident', name: 'foo' });
|
|
407
|
+
});
|
|
408
|
+
it('parses function call', () => {
|
|
409
|
+
const expr = parseExpr('foo(1, 2)');
|
|
410
|
+
expect(expr).toEqual({
|
|
411
|
+
kind: 'call',
|
|
412
|
+
fn: 'foo',
|
|
413
|
+
args: [
|
|
414
|
+
{ kind: 'int_lit', value: 1 },
|
|
415
|
+
{ kind: 'int_lit', value: 2 },
|
|
416
|
+
],
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
it('parses no-arg call', () => {
|
|
420
|
+
const expr = parseExpr('foo()');
|
|
421
|
+
expect(expr).toEqual({ kind: 'call', fn: 'foo', args: [] });
|
|
422
|
+
});
|
|
423
|
+
it('parses enum variant member access', () => {
|
|
424
|
+
const expr = parseExpr('Direction.North');
|
|
425
|
+
expect(expr).toEqual({
|
|
426
|
+
kind: 'member',
|
|
427
|
+
obj: { kind: 'ident', name: 'Direction' },
|
|
428
|
+
field: 'North',
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
describe('binary operators', () => {
|
|
433
|
+
it('parses arithmetic', () => {
|
|
434
|
+
const expr = parseExpr('1 + 2');
|
|
435
|
+
expect(expr).toEqual({
|
|
436
|
+
kind: 'binary',
|
|
437
|
+
op: '+',
|
|
438
|
+
left: { kind: 'int_lit', value: 1 },
|
|
439
|
+
right: { kind: 'int_lit', value: 2 },
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
it('respects precedence (mul before add)', () => {
|
|
443
|
+
const expr = parseExpr('1 + 2 * 3');
|
|
444
|
+
expect(expr.kind).toBe('binary');
|
|
445
|
+
expect(expr.op).toBe('+');
|
|
446
|
+
expect(expr.right.op).toBe('*');
|
|
447
|
+
});
|
|
448
|
+
it('respects precedence (compare before logical)', () => {
|
|
449
|
+
const expr = parseExpr('a < b && c > d');
|
|
450
|
+
expect(expr.kind).toBe('binary');
|
|
451
|
+
expect(expr.op).toBe('&&');
|
|
452
|
+
expect(expr.left.op).toBe('<');
|
|
453
|
+
expect(expr.right.op).toBe('>');
|
|
454
|
+
});
|
|
455
|
+
it('is left associative', () => {
|
|
456
|
+
const expr = parseExpr('1 - 2 - 3');
|
|
457
|
+
// Should be (1 - 2) - 3
|
|
458
|
+
expect(expr.kind).toBe('binary');
|
|
459
|
+
expect(expr.op).toBe('-');
|
|
460
|
+
expect(expr.left.kind).toBe('binary');
|
|
461
|
+
expect(expr.right.kind).toBe('int_lit');
|
|
462
|
+
});
|
|
463
|
+
});
|
|
464
|
+
describe('unary operators', () => {
|
|
465
|
+
it('parses negation', () => {
|
|
466
|
+
const expr = parseExpr('-5');
|
|
467
|
+
expect(expr).toEqual({
|
|
468
|
+
kind: 'unary',
|
|
469
|
+
op: '-',
|
|
470
|
+
operand: { kind: 'int_lit', value: 5 },
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
it('parses logical not', () => {
|
|
474
|
+
const expr = parseExpr('!flag');
|
|
475
|
+
expect(expr).toEqual({
|
|
476
|
+
kind: 'unary',
|
|
477
|
+
op: '!',
|
|
478
|
+
operand: { kind: 'ident', name: 'flag' },
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
describe('assignment', () => {
|
|
483
|
+
it('parses simple assignment', () => {
|
|
484
|
+
const expr = parseExpr('x = 5');
|
|
485
|
+
expect(expr).toEqual({
|
|
486
|
+
kind: 'assign',
|
|
487
|
+
target: 'x',
|
|
488
|
+
op: '=',
|
|
489
|
+
value: { kind: 'int_lit', value: 5 },
|
|
490
|
+
});
|
|
491
|
+
});
|
|
492
|
+
it('parses compound assignment', () => {
|
|
493
|
+
const expr = parseExpr('x += 1');
|
|
494
|
+
expect(expr).toEqual({
|
|
495
|
+
kind: 'assign',
|
|
496
|
+
target: 'x',
|
|
497
|
+
op: '+=',
|
|
498
|
+
value: { kind: 'int_lit', value: 1 },
|
|
499
|
+
});
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
describe('selectors', () => {
|
|
503
|
+
it('parses simple selector', () => {
|
|
504
|
+
const expr = parseExpr('@a');
|
|
505
|
+
expect(expr).toEqual({
|
|
506
|
+
kind: 'selector',
|
|
507
|
+
raw: '@a',
|
|
508
|
+
isSingle: false,
|
|
509
|
+
sel: { kind: '@a' },
|
|
510
|
+
});
|
|
511
|
+
});
|
|
512
|
+
it('marks single-entity selectors', () => {
|
|
513
|
+
expect(parseExpr('@p')).toEqual({
|
|
514
|
+
kind: 'selector',
|
|
515
|
+
raw: '@p',
|
|
516
|
+
isSingle: true,
|
|
517
|
+
sel: { kind: '@p' },
|
|
518
|
+
});
|
|
519
|
+
expect(parseExpr('@e[limit=1, tag=target]')).toEqual({
|
|
520
|
+
kind: 'selector',
|
|
521
|
+
raw: '@e[limit=1, tag=target]',
|
|
522
|
+
isSingle: true,
|
|
523
|
+
sel: {
|
|
524
|
+
kind: '@e',
|
|
525
|
+
filters: { limit: 1, tag: ['target'] },
|
|
526
|
+
},
|
|
527
|
+
});
|
|
528
|
+
});
|
|
529
|
+
it('parses selector with type filter', () => {
|
|
530
|
+
const expr = parseExpr('@e[type=zombie]');
|
|
531
|
+
expect(expr).toEqual({
|
|
532
|
+
kind: 'selector',
|
|
533
|
+
raw: '@e[type=zombie]',
|
|
534
|
+
isSingle: false,
|
|
535
|
+
sel: {
|
|
536
|
+
kind: '@e',
|
|
537
|
+
filters: { type: 'zombie' },
|
|
538
|
+
},
|
|
539
|
+
});
|
|
540
|
+
});
|
|
541
|
+
it('parses selector with distance filter', () => {
|
|
542
|
+
const expr = parseExpr('@e[distance=..5]');
|
|
543
|
+
expect(expr.sel.filters.distance).toEqual({ max: 5 });
|
|
544
|
+
});
|
|
545
|
+
it('parses selector with tag filter', () => {
|
|
546
|
+
const expr = parseExpr('@e[tag=boss, tag=!excluded]');
|
|
547
|
+
expect(expr.sel.filters.tag).toEqual(['boss']);
|
|
548
|
+
expect(expr.sel.filters.notTag).toEqual(['excluded']);
|
|
549
|
+
});
|
|
550
|
+
it('parses selector with limit and sort', () => {
|
|
551
|
+
const expr = parseExpr('@e[limit=1, sort=nearest]');
|
|
552
|
+
expect(expr.sel.filters.limit).toBe(1);
|
|
553
|
+
expect(expr.sel.filters.sort).toBe('nearest');
|
|
554
|
+
});
|
|
555
|
+
it('parses selector with scores', () => {
|
|
556
|
+
const expr = parseExpr('@a[scores={kills=1..}]');
|
|
557
|
+
expect(expr.sel.filters.scores).toEqual({
|
|
558
|
+
kills: { min: 1 },
|
|
559
|
+
});
|
|
560
|
+
});
|
|
561
|
+
});
|
|
562
|
+
describe('member access', () => {
|
|
563
|
+
it('parses member access', () => {
|
|
564
|
+
const expr = parseExpr('entity.health');
|
|
565
|
+
expect(expr).toEqual({
|
|
566
|
+
kind: 'member',
|
|
567
|
+
obj: { kind: 'ident', name: 'entity' },
|
|
568
|
+
field: 'health',
|
|
569
|
+
});
|
|
570
|
+
});
|
|
571
|
+
it('parses array len property', () => {
|
|
572
|
+
const expr = parseExpr('arr.len');
|
|
573
|
+
expect(expr).toEqual({
|
|
574
|
+
kind: 'member',
|
|
575
|
+
obj: { kind: 'ident', name: 'arr' },
|
|
576
|
+
field: 'len',
|
|
577
|
+
});
|
|
578
|
+
});
|
|
579
|
+
});
|
|
580
|
+
describe('arrays', () => {
|
|
581
|
+
it('parses array literal', () => {
|
|
582
|
+
expect(parseExpr('[1, 2, 3]')).toEqual({
|
|
583
|
+
kind: 'array_lit',
|
|
584
|
+
elements: [
|
|
585
|
+
{ kind: 'int_lit', value: 1 },
|
|
586
|
+
{ kind: 'int_lit', value: 2 },
|
|
587
|
+
{ kind: 'int_lit', value: 3 },
|
|
588
|
+
],
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
it('parses array index access', () => {
|
|
592
|
+
expect(parseExpr('arr[i]')).toEqual({
|
|
593
|
+
kind: 'index',
|
|
594
|
+
obj: { kind: 'ident', name: 'arr' },
|
|
595
|
+
index: { kind: 'ident', name: 'i' },
|
|
596
|
+
});
|
|
597
|
+
});
|
|
598
|
+
it('parses array push call', () => {
|
|
599
|
+
expect(parseExpr('arr.push(4)')).toEqual({
|
|
600
|
+
kind: 'call',
|
|
601
|
+
fn: '__array_push',
|
|
602
|
+
args: [
|
|
603
|
+
{ kind: 'ident', name: 'arr' },
|
|
604
|
+
{ kind: 'int_lit', value: 4 },
|
|
605
|
+
],
|
|
606
|
+
});
|
|
607
|
+
});
|
|
608
|
+
it('parses array pop call', () => {
|
|
609
|
+
expect(parseExpr('arr.pop()')).toEqual({
|
|
610
|
+
kind: 'call',
|
|
611
|
+
fn: '__array_pop',
|
|
612
|
+
args: [
|
|
613
|
+
{ kind: 'ident', name: 'arr' },
|
|
614
|
+
],
|
|
615
|
+
});
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
describe('grouping', () => {
|
|
619
|
+
it('parses parenthesized expression', () => {
|
|
620
|
+
const expr = parseExpr('(1 + 2) * 3');
|
|
621
|
+
expect(expr.kind).toBe('binary');
|
|
622
|
+
expect(expr.op).toBe('*');
|
|
623
|
+
expect(expr.left.kind).toBe('binary');
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
});
|
|
627
|
+
describe('complex programs', () => {
|
|
628
|
+
it('parses add function', () => {
|
|
629
|
+
const source = `
|
|
630
|
+
fn add(a: int, b: int) -> int {
|
|
631
|
+
return a + b;
|
|
632
|
+
}
|
|
633
|
+
`;
|
|
634
|
+
const program = parse(source);
|
|
635
|
+
expect(program.declarations).toHaveLength(1);
|
|
636
|
+
const fn = program.declarations[0];
|
|
637
|
+
expect(fn.name).toBe('add');
|
|
638
|
+
expect(fn.body).toHaveLength(1);
|
|
639
|
+
expect(fn.body[0].kind).toBe('return');
|
|
640
|
+
});
|
|
641
|
+
it('parses abs function with if/else', () => {
|
|
642
|
+
const source = `
|
|
643
|
+
fn abs(x: int) -> int {
|
|
644
|
+
if (x < 0) {
|
|
645
|
+
return -x;
|
|
646
|
+
} else {
|
|
647
|
+
return x;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
`;
|
|
651
|
+
const program = parse(source);
|
|
652
|
+
const fn = program.declarations[0];
|
|
653
|
+
expect(fn.body).toHaveLength(1);
|
|
654
|
+
const ifStmt = fn.body[0];
|
|
655
|
+
expect(ifStmt.kind).toBe('if');
|
|
656
|
+
});
|
|
657
|
+
it('parses tick function', () => {
|
|
658
|
+
const source = `
|
|
659
|
+
@tick(rate=20)
|
|
660
|
+
fn heartbeat() {
|
|
661
|
+
say("still alive");
|
|
662
|
+
}
|
|
663
|
+
`;
|
|
664
|
+
const program = parse(source);
|
|
665
|
+
const fn = program.declarations[0];
|
|
666
|
+
expect(fn.decorators).toEqual([{ name: 'tick', args: { rate: 20 } }]);
|
|
667
|
+
expect(fn.body).toHaveLength(1);
|
|
668
|
+
});
|
|
669
|
+
it('parses foreach with kill', () => {
|
|
670
|
+
const source = `
|
|
671
|
+
fn kill_zombies() {
|
|
672
|
+
foreach (z in @e[type=zombie, distance=..10]) {
|
|
673
|
+
kill(z);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
`;
|
|
677
|
+
const program = parse(source);
|
|
678
|
+
const fn = program.declarations[0];
|
|
679
|
+
const stmt = fn.body[0];
|
|
680
|
+
expect(stmt.kind).toBe('foreach');
|
|
681
|
+
expect(stmt.binding).toBe('z');
|
|
682
|
+
expect(stmt.iterable.sel.filters.type).toBe('zombie');
|
|
683
|
+
expect(stmt.iterable.sel.filters.distance).toEqual({ max: 10 });
|
|
684
|
+
});
|
|
685
|
+
it('parses foreach over array', () => {
|
|
686
|
+
const source = `
|
|
687
|
+
fn walk() {
|
|
688
|
+
let arr: int[] = [1, 2, 3];
|
|
689
|
+
foreach (x in arr) {
|
|
690
|
+
say("tick");
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
`;
|
|
694
|
+
const program = parse(source);
|
|
695
|
+
const stmt = program.declarations[0].body[1];
|
|
696
|
+
expect(stmt.kind).toBe('foreach');
|
|
697
|
+
expect(stmt.binding).toBe('x');
|
|
698
|
+
expect(stmt.iterable).toEqual({ kind: 'ident', name: 'arr' });
|
|
699
|
+
});
|
|
700
|
+
it('parses while loop', () => {
|
|
701
|
+
const source = `
|
|
702
|
+
fn count_down() {
|
|
703
|
+
let i: int = 10;
|
|
704
|
+
while (i > 0) {
|
|
705
|
+
i = i - 1;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
`;
|
|
709
|
+
const program = parse(source);
|
|
710
|
+
const fn = program.declarations[0];
|
|
711
|
+
expect(fn.body).toHaveLength(2);
|
|
712
|
+
expect(fn.body[0].kind).toBe('let');
|
|
713
|
+
expect(fn.body[1].kind).toBe('while');
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
});
|
|
717
|
+
//# sourceMappingURL=parser.test.js.map
|