redscript-mc 1.1.0 → 1.2.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/CHANGELOG.md +54 -0
- package/dist/__tests__/cli.test.js +138 -0
- package/dist/__tests__/codegen.test.js +25 -0
- package/dist/__tests__/e2e.test.js +190 -12
- package/dist/__tests__/lexer.test.js +12 -2
- package/dist/__tests__/lowering.test.js +164 -9
- package/dist/__tests__/mc-integration.test.js +145 -51
- package/dist/__tests__/optimizer-advanced.test.js +3 -3
- package/dist/__tests__/parser.test.js +80 -0
- package/dist/__tests__/runtime.test.js +8 -8
- package/dist/__tests__/typechecker.test.js +158 -0
- package/dist/ast/types.d.ts +20 -1
- package/dist/codegen/mcfunction/index.js +30 -1
- package/dist/codegen/structure/index.js +25 -0
- package/dist/compile.d.ts +10 -0
- package/dist/compile.js +36 -5
- package/dist/events/types.d.ts +35 -0
- package/dist/events/types.js +59 -0
- package/dist/index.js +3 -2
- package/dist/ir/types.d.ts +4 -0
- package/dist/lexer/index.d.ts +1 -1
- package/dist/lexer/index.js +2 -0
- package/dist/lowering/index.d.ts +32 -1
- package/dist/lowering/index.js +439 -15
- package/dist/parser/index.d.ts +2 -0
- package/dist/parser/index.js +79 -10
- package/dist/typechecker/index.d.ts +17 -0
- package/dist/typechecker/index.js +343 -17
- package/docs/ENTITY_TYPE_SYSTEM.md +242 -0
- package/editors/vscode/CHANGELOG.md +9 -0
- package/editors/vscode/out/extension.js +1144 -72
- package/editors/vscode/package-lock.json +2 -2
- package/editors/vscode/package.json +1 -1
- package/package.json +1 -1
- package/src/__tests__/cli.test.ts +166 -0
- package/src/__tests__/codegen.test.ts +27 -0
- package/src/__tests__/e2e.test.ts +201 -12
- package/src/__tests__/fixtures/event-test.mcrs +13 -0
- package/src/__tests__/fixtures/impl-test.mcrs +46 -0
- package/src/__tests__/fixtures/interval-test.mcrs +11 -0
- package/src/__tests__/fixtures/is-check-test.mcrs +20 -0
- package/src/__tests__/fixtures/timeout-test.mcrs +7 -0
- package/src/__tests__/lexer.test.ts +14 -2
- package/src/__tests__/lowering.test.ts +178 -9
- package/src/__tests__/mc-integration.test.ts +166 -51
- package/src/__tests__/optimizer-advanced.test.ts +3 -3
- package/src/__tests__/parser.test.ts +91 -5
- package/src/__tests__/runtime.test.ts +8 -8
- package/src/__tests__/typechecker.test.ts +171 -0
- package/src/ast/types.ts +25 -1
- package/src/codegen/mcfunction/index.ts +31 -1
- package/src/codegen/structure/index.ts +27 -0
- package/src/compile.ts +54 -6
- package/src/events/types.ts +69 -0
- package/src/index.ts +4 -3
- package/src/ir/types.ts +4 -0
- package/src/lexer/index.ts +3 -1
- package/src/lowering/index.ts +528 -16
- package/src/parser/index.ts +90 -12
- package/src/stdlib/README.md +34 -4
- package/src/stdlib/tags.mcrs +951 -0
- package/src/stdlib/timer.mcrs +54 -33
- package/src/typechecker/index.ts +404 -18
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { TypeNode } from '../ast/types'
|
|
2
|
+
|
|
3
|
+
export const EVENT_TYPES = {
|
|
4
|
+
PlayerDeath: {
|
|
5
|
+
tag: 'rs.just_died',
|
|
6
|
+
params: ['player: Player'],
|
|
7
|
+
detection: 'scoreboard',
|
|
8
|
+
},
|
|
9
|
+
PlayerJoin: {
|
|
10
|
+
tag: 'rs.just_joined',
|
|
11
|
+
params: ['player: Player'],
|
|
12
|
+
detection: 'tag',
|
|
13
|
+
},
|
|
14
|
+
BlockBreak: {
|
|
15
|
+
tag: 'rs.just_broke_block',
|
|
16
|
+
params: ['player: Player', 'block: string'],
|
|
17
|
+
detection: 'advancement',
|
|
18
|
+
},
|
|
19
|
+
EntityKill: {
|
|
20
|
+
tag: 'rs.just_killed',
|
|
21
|
+
params: ['player: Player'],
|
|
22
|
+
detection: 'scoreboard',
|
|
23
|
+
},
|
|
24
|
+
ItemUse: {
|
|
25
|
+
tag: 'rs.just_used_item',
|
|
26
|
+
params: ['player: Player'],
|
|
27
|
+
detection: 'scoreboard',
|
|
28
|
+
},
|
|
29
|
+
} as const
|
|
30
|
+
|
|
31
|
+
export type EventTypeName = keyof typeof EVENT_TYPES
|
|
32
|
+
|
|
33
|
+
export interface EventParamSpec {
|
|
34
|
+
name: string
|
|
35
|
+
type: TypeNode
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isEventTypeName(value: string): value is EventTypeName {
|
|
39
|
+
return value in EVENT_TYPES
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function getEventParamSpecs(eventType: EventTypeName): EventParamSpec[] {
|
|
43
|
+
return EVENT_TYPES[eventType].params.map(parseEventParam)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function parseEventParam(spec: string): EventParamSpec {
|
|
47
|
+
const match = spec.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*:\s*([A-Za-z_][A-Za-z0-9_]*)$/)
|
|
48
|
+
if (!match) {
|
|
49
|
+
throw new Error(`Invalid event parameter spec: ${spec}`)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const [, name, typeName] = match
|
|
53
|
+
return {
|
|
54
|
+
name,
|
|
55
|
+
type: toTypeNode(typeName),
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function toTypeNode(typeName: string): TypeNode {
|
|
60
|
+
if (typeName === 'Player') {
|
|
61
|
+
return { kind: 'entity', entityType: 'Player' }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (typeName === 'string' || typeName === 'int' || typeName === 'bool' || typeName === 'float' || typeName === 'void' || typeName === 'BlockPos' || typeName === 'byte' || typeName === 'short' || typeName === 'long' || typeName === 'double') {
|
|
65
|
+
return { kind: 'named', name: typeName }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { kind: 'struct', name: typeName }
|
|
69
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
generateDatapackWithStats,
|
|
20
20
|
DatapackFile,
|
|
21
21
|
} from './codegen/mcfunction'
|
|
22
|
-
import { preprocessSource } from './compile'
|
|
22
|
+
import { preprocessSource, preprocessSourceWithMetadata } from './compile'
|
|
23
23
|
import type { IRModule } from './ir/types'
|
|
24
24
|
import type { Program } from './ast/types'
|
|
25
25
|
import type { DiagnosticError } from './diagnostics'
|
|
@@ -54,7 +54,8 @@ export function compile(source: string, options: CompileOptions = {}): CompileRe
|
|
|
54
54
|
const shouldOptimize = options.optimize ?? true
|
|
55
55
|
const shouldTypeCheck = options.typeCheck ?? true
|
|
56
56
|
const filePath = options.filePath
|
|
57
|
-
const
|
|
57
|
+
const preprocessed = preprocessSourceWithMetadata(source, { filePath })
|
|
58
|
+
const preprocessedSource = preprocessed.source
|
|
58
59
|
|
|
59
60
|
// Lexing
|
|
60
61
|
const tokens = new Lexer(preprocessedSource, filePath).tokenize()
|
|
@@ -70,7 +71,7 @@ export function compile(source: string, options: CompileOptions = {}): CompileRe
|
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
// Lowering to IR
|
|
73
|
-
const lowering = new Lowering(namespace)
|
|
74
|
+
const lowering = new Lowering(namespace, preprocessed.ranges)
|
|
74
75
|
const ir = lowering.lower(ast)
|
|
75
76
|
|
|
76
77
|
let optimizedIR: IRModule = ir
|
package/src/ir/types.ts
CHANGED
|
@@ -107,6 +107,10 @@ export interface IRFunction {
|
|
|
107
107
|
kind: 'advancement' | 'craft' | 'death' | 'login' | 'join_team'
|
|
108
108
|
value?: string
|
|
109
109
|
}
|
|
110
|
+
eventHandler?: {
|
|
111
|
+
eventType: string
|
|
112
|
+
tag: string
|
|
113
|
+
}
|
|
110
114
|
}
|
|
111
115
|
|
|
112
116
|
// ---------------------------------------------------------------------------
|
package/src/lexer/index.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { DiagnosticError } from '../diagnostics'
|
|
|
15
15
|
export type TokenKind =
|
|
16
16
|
// Keywords
|
|
17
17
|
| 'fn' | 'let' | 'const' | 'if' | 'else' | 'while' | 'for' | 'foreach' | 'match'
|
|
18
|
-
| 'return' | 'as' | 'at' | 'in' | 'struct' | 'enum' | 'trigger' | 'namespace'
|
|
18
|
+
| 'return' | 'as' | 'at' | 'in' | 'is' | 'struct' | 'impl' | 'enum' | 'trigger' | 'namespace'
|
|
19
19
|
| 'execute' | 'run' | 'unless'
|
|
20
20
|
// Types
|
|
21
21
|
| 'int' | 'bool' | 'float' | 'string' | 'void'
|
|
@@ -75,7 +75,9 @@ const KEYWORDS: Record<string, TokenKind> = {
|
|
|
75
75
|
as: 'as',
|
|
76
76
|
at: 'at',
|
|
77
77
|
in: 'in',
|
|
78
|
+
is: 'is',
|
|
78
79
|
struct: 'struct',
|
|
80
|
+
impl: 'impl',
|
|
79
81
|
enum: 'enum',
|
|
80
82
|
trigger: 'trigger',
|
|
81
83
|
namespace: 'namespace',
|