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.
Files changed (63) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/dist/__tests__/cli.test.js +138 -0
  3. package/dist/__tests__/codegen.test.js +25 -0
  4. package/dist/__tests__/e2e.test.js +190 -12
  5. package/dist/__tests__/lexer.test.js +12 -2
  6. package/dist/__tests__/lowering.test.js +164 -9
  7. package/dist/__tests__/mc-integration.test.js +145 -51
  8. package/dist/__tests__/optimizer-advanced.test.js +3 -3
  9. package/dist/__tests__/parser.test.js +80 -0
  10. package/dist/__tests__/runtime.test.js +8 -8
  11. package/dist/__tests__/typechecker.test.js +158 -0
  12. package/dist/ast/types.d.ts +20 -1
  13. package/dist/codegen/mcfunction/index.js +30 -1
  14. package/dist/codegen/structure/index.js +25 -0
  15. package/dist/compile.d.ts +10 -0
  16. package/dist/compile.js +36 -5
  17. package/dist/events/types.d.ts +35 -0
  18. package/dist/events/types.js +59 -0
  19. package/dist/index.js +3 -2
  20. package/dist/ir/types.d.ts +4 -0
  21. package/dist/lexer/index.d.ts +1 -1
  22. package/dist/lexer/index.js +2 -0
  23. package/dist/lowering/index.d.ts +32 -1
  24. package/dist/lowering/index.js +439 -15
  25. package/dist/parser/index.d.ts +2 -0
  26. package/dist/parser/index.js +79 -10
  27. package/dist/typechecker/index.d.ts +17 -0
  28. package/dist/typechecker/index.js +343 -17
  29. package/docs/ENTITY_TYPE_SYSTEM.md +242 -0
  30. package/editors/vscode/CHANGELOG.md +9 -0
  31. package/editors/vscode/out/extension.js +1144 -72
  32. package/editors/vscode/package-lock.json +2 -2
  33. package/editors/vscode/package.json +1 -1
  34. package/package.json +1 -1
  35. package/src/__tests__/cli.test.ts +166 -0
  36. package/src/__tests__/codegen.test.ts +27 -0
  37. package/src/__tests__/e2e.test.ts +201 -12
  38. package/src/__tests__/fixtures/event-test.mcrs +13 -0
  39. package/src/__tests__/fixtures/impl-test.mcrs +46 -0
  40. package/src/__tests__/fixtures/interval-test.mcrs +11 -0
  41. package/src/__tests__/fixtures/is-check-test.mcrs +20 -0
  42. package/src/__tests__/fixtures/timeout-test.mcrs +7 -0
  43. package/src/__tests__/lexer.test.ts +14 -2
  44. package/src/__tests__/lowering.test.ts +178 -9
  45. package/src/__tests__/mc-integration.test.ts +166 -51
  46. package/src/__tests__/optimizer-advanced.test.ts +3 -3
  47. package/src/__tests__/parser.test.ts +91 -5
  48. package/src/__tests__/runtime.test.ts +8 -8
  49. package/src/__tests__/typechecker.test.ts +171 -0
  50. package/src/ast/types.ts +25 -1
  51. package/src/codegen/mcfunction/index.ts +31 -1
  52. package/src/codegen/structure/index.ts +27 -0
  53. package/src/compile.ts +54 -6
  54. package/src/events/types.ts +69 -0
  55. package/src/index.ts +4 -3
  56. package/src/ir/types.ts +4 -0
  57. package/src/lexer/index.ts +3 -1
  58. package/src/lowering/index.ts +528 -16
  59. package/src/parser/index.ts +90 -12
  60. package/src/stdlib/README.md +34 -4
  61. package/src/stdlib/tags.mcrs +951 -0
  62. package/src/stdlib/timer.mcrs +54 -33
  63. 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 preprocessedSource = preprocessSource(source, { filePath })
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
  // ---------------------------------------------------------------------------
@@ -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',