mlogjs 0.5.2 → 0.6.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 (79) hide show
  1. package/README.md +2 -2
  2. package/dist/{index.cjs → Compiler-7GMG4MDr.js} +1746 -452
  3. package/dist/Compiler.d.ts +8 -12
  4. package/dist/CompilerError.d.ts +13 -3
  5. package/dist/Scope.d.ts +8 -8
  6. package/dist/bin/files.d.ts +18 -0
  7. package/dist/bin.mjs +170 -0
  8. package/dist/handlers/Import.d.ts +5 -0
  9. package/dist/handlers/Literal.d.ts +2 -2
  10. package/dist/handlers/Typescript.d.ts +2 -2
  11. package/dist/handlers/index.d.ts +1 -0
  12. package/dist/index.mjs +3 -4267
  13. package/dist/instructions/AddressResolver.d.ts +4 -4
  14. package/dist/instructions/ControlInstructions.d.ts +3 -3
  15. package/dist/instructions/InstructionBase.d.ts +1 -0
  16. package/dist/instructions/JumpInstruction.d.ts +5 -5
  17. package/dist/instructions/index.d.ts +0 -1
  18. package/dist/macros/DynamicArray.d.ts +4 -4
  19. package/dist/macros/Function.d.ts +2 -2
  20. package/dist/macros/GetBuildings.d.ts +4 -0
  21. package/dist/macros/GetColor.d.ts +4 -0
  22. package/dist/macros/Marker.d.ts +4 -0
  23. package/dist/macros/Namespace.d.ts +16 -0
  24. package/dist/macros/Queries.d.ts +10 -0
  25. package/dist/macros/StringView.d.ts +4 -0
  26. package/dist/macros/commands/Effect.d.ts +4 -0
  27. package/dist/macros/commands/Format.d.ts +4 -0
  28. package/dist/macros/commands/LocalePrint.d.ts +4 -0
  29. package/dist/macros/commands/PlayMusic.d.ts +4 -0
  30. package/dist/macros/commands/PlaySound.d.ts +4 -0
  31. package/dist/macros/commands/PrintChar.d.ts +4 -0
  32. package/dist/macros/commands/Query.d.ts +4 -0
  33. package/dist/macros/commands/SetProp.d.ts +4 -0
  34. package/dist/macros/commands/SpawnBullet.d.ts +4 -0
  35. package/dist/macros/commands/Sync.d.ts +6 -0
  36. package/dist/macros/commands/UnpackColor.d.ts +4 -0
  37. package/dist/macros/commands/WeatherSense.d.ts +4 -0
  38. package/dist/macros/commands/WeatherSet.d.ts +4 -0
  39. package/dist/macros/commands/index.d.ts +13 -0
  40. package/dist/macros/index.d.ts +7 -1
  41. package/dist/macros/util.d.ts +5 -2
  42. package/dist/modules.d.ts +8 -0
  43. package/dist/operators.d.ts +1 -1
  44. package/dist/types.d.ts +84 -59
  45. package/dist/utils/constants.d.ts +9 -4
  46. package/dist/utils/general.d.ts +8 -9
  47. package/dist/utils/instruction.d.ts +5 -1
  48. package/dist/utils/value.d.ts +14 -5
  49. package/dist/values/AssignmentValue.d.ts +1 -1
  50. package/dist/values/BaseValue.d.ts +4 -2
  51. package/dist/values/DestructuringValue.d.ts +5 -7
  52. package/dist/values/FunctionValue.d.ts +8 -6
  53. package/dist/values/JumpOutValue.d.ts +4 -4
  54. package/dist/values/LazyValue.d.ts +3 -3
  55. package/dist/values/LiteralValue.d.ts +2 -3
  56. package/dist/values/ObjectValue.d.ts +6 -11
  57. package/dist/values/StoreValue.d.ts +10 -5
  58. package/dist/values/VoidValue.d.ts +2 -1
  59. package/dist/values/index.d.ts +0 -1
  60. package/lib/commands.d.ts +776 -730
  61. package/lib/eslib/lib.es2015.collection.d.ts +50 -39
  62. package/lib/eslib/lib.es2015.core.d.ts +59 -0
  63. package/lib/eslib/lib.es2015.d.ts +0 -3
  64. package/lib/eslib/lib.es2015.generator.d.ts +54 -53
  65. package/lib/eslib/lib.es2015.iterable.d.ts +9 -6
  66. package/lib/eslib/lib.es5.d.ts +53 -6
  67. package/lib/globals.d.ts +309 -18
  68. package/lib/helpers.d.ts +86 -52
  69. package/lib/kinds.d.ts +42 -7
  70. package/lib/macros.d.ts +53 -18
  71. package/lib/traits.d.ts +308 -146
  72. package/lib/util.d.ts +144 -48
  73. package/lib/world.d.ts +1197 -0
  74. package/package.json +27 -35
  75. package/dist/bin.cjs +0 -111
  76. package/dist/initScope.d.ts +0 -5
  77. package/dist/instructions/ZeroSpaceInstruction.d.ts +0 -4
  78. package/dist/values/SenseableValue.d.ts +0 -24
  79. /package/dist/{bin.d.ts → bin/index.d.ts} +0 -0
@@ -4,36 +4,32 @@ type THandlerMap = {
4
4
  [k in es.Node["type"]]?: THandler<IValue | null>;
5
5
  };
6
6
  export interface CompilerOptions {
7
- stackName?: string;
8
- /** Wether the compiler should preserve or compact variable and function names*/
7
+ /** Wether the compiler should preserve or compact variable and function names */
9
8
  compactNames?: boolean;
10
9
  sourcemap?: boolean;
11
10
  }
12
11
  export declare class Compiler {
13
12
  protected stackName?: string;
14
- protected usingStack: boolean;
15
13
  protected handlers: THandlerMap;
16
14
  readonly compactNames: boolean;
17
15
  readonly sourcemap: boolean;
18
- constructor({ stackName, compactNames, sourcemap, }?: CompilerOptions);
16
+ constructor({ compactNames, sourcemap, }?: CompilerOptions);
19
17
  compile(script: string): [string, null, es.SourceLocation[] | undefined] | [null, CompilerError];
20
18
  protected resolve(valueInst: TValueInstructions<IValue | null>): void;
21
19
  protected serialize(resLines: TValueInstructions<IValue | null>): string;
22
20
  protected parse(script: string): import("@babel/parser").ParseResult<es.File>;
23
21
  protected mapSources(valueInst: TValueInstructions<IValue | null>): es.SourceLocation[];
24
- handle(scope: IScope, node: es.Node, handler?: THandler<IValue | null> | undefined, out?: TEOutput): TValueInstructions<IValue | null>;
22
+ handle(scope: IScope, node: es.Node, handler?: THandler<IValue | null> | undefined, out?: TEOutput, arg?: unknown): TValueInstructions<IValue | null>;
25
23
  /** Handles the node and asserts that it resolves to a value. */
26
- handleValue(scope: IScope, node: es.Node, handler?: THandler<IValue | null> | undefined, out?: TEOutput): TValueInstructions;
27
- handleEval(scope: IScope, node: es.Node, out?: TEOutput): TValueInstructions;
24
+ handleValue(scope: IScope, node: es.Node, handler?: THandler<IValue | null> | undefined, out?: TEOutput, arg?: unknown): TValueInstructions;
25
+ handleEval(scope: IScope, node: es.Node, out?: TEOutput, arg?: unknown): TValueInstructions;
28
26
  /**
29
27
  * Handles many nodes in order.
30
28
  *
31
- * The usage of this method over a regular loop over an array of nodes
32
- * is only required if the code inside the loop generates
33
- * instructions that are not tracked by the compiler
34
- * handler methods ({@link handle}, {@link handleEval}
29
+ * The usage of this method over a regular loop over an array of nodes is only
30
+ * required if the code inside the loop generates instructions that are not
31
+ * tracked by the compiler handler methods ({@link handle}, {@link handleEval}
35
32
  * and {@link handleMany})
36
- *
37
33
  */
38
34
  handleMany<T extends es.Node>(scope: IScope, nodes: T[], handler?: (node: T) => TValueInstructions<IValue | null>): TValueInstructions<null>;
39
35
  }
@@ -16,17 +16,27 @@ type CompilerErrorSource = es.Node | es.SourceLocation | {
16
16
  line: number;
17
17
  column: number;
18
18
  };
19
+ export interface CompilerErrorLoc {
20
+ start: {
21
+ line: number;
22
+ column: number;
23
+ };
24
+ end: {
25
+ line: number;
26
+ column: number;
27
+ };
28
+ }
19
29
  export declare class CompilerError extends Error {
20
- protected _loc?: es.SourceLocation;
30
+ protected _loc?: CompilerErrorLoc;
21
31
  /**
22
32
  * The error that preceded this `CompilerError`.
23
33
  *
24
34
  * If it's not `undefined` it means that `this` is just wrapping the error to
25
35
  * append extra data like the node stack
26
- * */
36
+ */
27
37
  inner?: unknown;
28
38
  constructor(message: string, source?: CompilerErrorSource);
29
- get loc(): es.SourceLocation | undefined;
39
+ get loc(): CompilerErrorLoc | undefined;
30
40
  set loc(value: CompilerErrorSource | undefined);
31
41
  static from(error: unknown, source?: CompilerErrorSource): CompilerError;
32
42
  }
package/dist/Scope.d.ts CHANGED
@@ -1,31 +1,31 @@
1
- import { SenseableValue } from "./values";
1
+ import { StoreValue } from "./values";
2
2
  import { AddressResolver } from "./instructions";
3
3
  import { IFunctionValue, IInstruction, INamedValue, IScope, IValue } from "./types";
4
4
  export declare class Scope implements IScope {
5
+ data: Record<string, IValue | null>;
5
6
  parent: IScope | null;
6
- stacked: boolean;
7
7
  ntemp: number;
8
8
  name: string;
9
9
  inst: IInstruction[];
10
10
  operationCache: Record<string, IValue>;
11
11
  cacheDependencies: Record<string, string[]>;
12
- data: Record<string, IValue | null>;
13
12
  break: AddressResolver;
14
13
  continue: AddressResolver;
15
14
  function: IFunctionValue;
16
15
  label?: string;
17
16
  checkIndexes: boolean;
18
- constructor(values: Record<string, IValue | null>, parent?: IScope | null, stacked?: boolean, ntemp?: number, name?: string, inst?: IInstruction[], operationCache?: Record<string, IValue>, cacheDependencies?: Record<string, string[]>);
17
+ builtInModules: Record<string, IValue>;
18
+ constructor({ data, parent, ntemp, name, inst, operationCache, cacheDependencies, builtInModules, }?: Partial<Pick<IScope, "data" | "parent" | "ntemp" | "name" | "inst" | "operationCache" | "cacheDependencies" | "builtInModules">>);
19
19
  copy(): IScope;
20
20
  createScope(): IScope;
21
- createFunction(name: string, stacked?: boolean): IScope;
21
+ createFunction(name: string): IScope;
22
22
  has(identifier: string): boolean;
23
23
  get(identifier: string): INamedValue;
24
24
  set<T extends IValue>(name: string, value: T): T;
25
25
  hardSet<T extends IValue>(name: string, value: T): T;
26
- make(identifier: string, name: string): SenseableValue;
26
+ make(identifier: string, name: string): StoreValue;
27
27
  makeTempName(): string;
28
- addCachedOperation(op: string, result: IValue, left: IValue, right?: IValue | undefined): void;
29
- getCachedOperation(op: string, left: IValue, right?: IValue | undefined): IValue | undefined;
28
+ addCachedOperation(op: string, result: IValue, left: IValue, right?: IValue): void;
29
+ getCachedOperation(op: string, left: IValue, right?: IValue): IValue | undefined;
30
30
  clearDependentCache(value: IValue): void;
31
31
  }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Returns the contents of the tsconfig.json file that should be placed on the
3
+ * project's root directory
4
+ */
5
+ export declare function getRootTSConfig(): string;
6
+ /**
7
+ * Returns the contents of the tsconfig.json file that should be placed inside
8
+ * the .mlogjs folder
9
+ */
10
+ export declare function getInnerTSConfig(): string;
11
+ /**
12
+ * Returns the path to the lib folder of the compiler.
13
+ *
14
+ * To achieve this, this function assumes that the output bin.cjs file is one
15
+ * folder bellow the package folder. If this condition is not met, then the
16
+ * function will not return the correct path to the lib folder.
17
+ */
18
+ export declare function getLibFolderPath(): string;
package/dist/bin.mjs ADDED
@@ -0,0 +1,170 @@
1
+ #!/usr/bin/env node
2
+ import { existsSync, watchFile, rmSync, cpSync, writeFileSync, readFileSync } from 'node:fs';
3
+ import { hideBin } from 'yargs/helpers';
4
+ import { highlight } from 'cli-highlight';
5
+ import yargs from 'yargs';
6
+ import chalk from 'chalk';
7
+ import { join, resolve, parse } from 'node:path';
8
+ import { C as Compiler } from './Compiler-7GMG4MDr.js';
9
+ import '@babel/parser';
10
+ import '@babel/types';
11
+
12
+ // The files generated by the setup command follow the following structure:
13
+ // ./mlogjs
14
+ // ├── lib/
15
+ // └── tsconfig.json
16
+ //
17
+ // The lib folder contains the type definitions for the compiler's environment.
18
+ // The tsconfig allows us to have a base configuration that can be updated
19
+ // without modifying the user's tsconfig.json.
20
+ /**
21
+ * Returns the contents of the tsconfig.json file that should be placed on the
22
+ * project's root directory
23
+ */
24
+ function getRootTSConfig() {
25
+ const config = {
26
+ extends: "./.mlogjs/tsconfig.json",
27
+ include: ["./.mlogjs/lib", "*.js", "*.ts", "src"],
28
+ exclude: ["node_modules"],
29
+ };
30
+ return JSON.stringify(config, null, "\t");
31
+ }
32
+ /**
33
+ * Returns the contents of the tsconfig.json file that should be placed inside
34
+ * the .mlogjs folder
35
+ */
36
+ function getInnerTSConfig() {
37
+ const config = {
38
+ compilerOptions: {
39
+ allowJs: true,
40
+ checkJs: true,
41
+ moduleDetection: "force",
42
+ noEmit: true,
43
+ noLib: true,
44
+ target: "ESNext",
45
+ },
46
+ };
47
+ return JSON.stringify(config, null, "\t");
48
+ }
49
+ /**
50
+ * Returns the path to the lib folder of the compiler.
51
+ *
52
+ * To achieve this, this function assumes that the output bin.cjs file is one
53
+ * folder bellow the package folder. If this condition is not met, then the
54
+ * function will not return the correct path to the lib folder.
55
+ */
56
+ function getLibFolderPath() {
57
+ const currentPath = import.meta.dirname.replace(/\\/g, "/");
58
+ const parent = currentPath.split("/").slice(0, -1).join("/");
59
+ return join(parent, "lib");
60
+ }
61
+
62
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
63
+ yargs(hideBin(process.argv))
64
+ .command("setup [dir]", "Copies the compiler type definitions in the current folder", yargs => {
65
+ return yargs
66
+ .positional("dir", {
67
+ default: ".",
68
+ type: "string",
69
+ describe: "The directory where the type definitions folder will be copied",
70
+ })
71
+ .option("tsconfig", {
72
+ type: "boolean",
73
+ default: false,
74
+ describe: "Wether a tsconfig.json file should be created in the current directory",
75
+ });
76
+ }, argv => {
77
+ const dir = resolve(process.cwd(), argv.dir);
78
+ console.log(process.cwd());
79
+ console.log(argv.dir);
80
+ setup(dir, argv.tsconfig);
81
+ })
82
+ .command("$0 [path] [out]", "compiles your Javascript file to MLog", yargs => {
83
+ return yargs
84
+ .positional("path", {
85
+ describe: "path of the file to compile",
86
+ type: "string",
87
+ })
88
+ .positional("out", {
89
+ describe: "path of the output file",
90
+ type: "string",
91
+ })
92
+ .option("compact-names", {
93
+ type: "boolean",
94
+ default: false,
95
+ describe: "Wether the compiler should preserve or compact variable and function names",
96
+ })
97
+ .option("watch", {
98
+ type: "boolean",
99
+ default: false,
100
+ describe: "Wether the compiler should watch for file changes",
101
+ });
102
+ }, argv => {
103
+ const path = argv.path;
104
+ if (!path)
105
+ return console.log("missing required path argument");
106
+ if (!existsSync(path))
107
+ return console.log(`file at ${path} does not exist`);
108
+ const out = argv.out ?? defaultOutPath(path);
109
+ if (path == out)
110
+ return console.log("The out path cannot be the same as the input path");
111
+ const options = {
112
+ compactNames: argv["compact-names"],
113
+ };
114
+ compile(path, out, options);
115
+ if (!argv.watch)
116
+ return;
117
+ // using interval of 600 milliseconds
118
+ // because 1 second might feel too slow to the user
119
+ watchFile(path, { interval: 600 }, () => compile(path, out, options));
120
+ })
121
+ .help()
122
+ .scriptName("mlogjs")
123
+ .demandCommand()
124
+ .parse();
125
+ function setup(dir, tsconfig) {
126
+ const dotDir = join(dir, ".mlogjs");
127
+ if (existsSync(dotDir)) {
128
+ rmSync(dotDir, { recursive: true });
129
+ }
130
+ const libPath = getLibFolderPath();
131
+ cpSync(libPath, join(dotDir, "lib"), {
132
+ recursive: true,
133
+ });
134
+ const innerTSConfig = getInnerTSConfig();
135
+ writeFileSync(join(dotDir, "tsconfig.json"), innerTSConfig);
136
+ if (!tsconfig)
137
+ return;
138
+ const rootTSConfig = getRootTSConfig();
139
+ writeFileSync(join(dir, "tsconfig.json"), rootTSConfig);
140
+ }
141
+ function compile(path, outPath, options) {
142
+ const resolvedPath = resolve(process.cwd(), path);
143
+ const code = readFileSync(path, "utf8");
144
+ const compiler = new Compiler(options);
145
+ const [result, error] = compiler.compile(code);
146
+ if (!error) {
147
+ writeFileSync(outPath, result);
148
+ console.log(`Success: Compiled ${path}. Your compiled code is at ${outPath}.`);
149
+ return;
150
+ }
151
+ if (!error.loc) {
152
+ console.log(error.inner ?? error);
153
+ return;
154
+ }
155
+ const { start, end } = error.loc;
156
+ const lines = code.split("\n");
157
+ console.log(chalk.cyanBright([resolvedPath, start.line, start.column].join(":")));
158
+ for (let i = Math.max(start.line - 3, 0); i < Math.min(end.line + 2, lines.length); i++) {
159
+ const n = i + 1;
160
+ if (n !== start.line)
161
+ continue;
162
+ const head = chalk.gray(`${n} | `.padStart(6, " "));
163
+ console.log(head + highlight(lines[i], { language: "ts" }));
164
+ console.log(chalk.red(" ".repeat(6 + start.column) + "^ " + error.message));
165
+ }
166
+ }
167
+ function defaultOutPath(path) {
168
+ const parsed = parse(path);
169
+ return join(parsed.dir, `${parsed.name}.mlog`);
170
+ }
@@ -0,0 +1,5 @@
1
+ import { THandler } from "../types";
2
+ export declare const ImportDeclaration: THandler<null>;
3
+ export declare const ImportDefaultSpecifier: THandler<null>;
4
+ export declare const ImportNamespaceSpecifier: THandler<null>;
5
+ export declare const ImportSpecifier: THandler<null>;
@@ -1,5 +1,5 @@
1
1
  import { THandler } from "../types";
2
- export declare const NumericLiteral: THandler<import("../types").IValue>;
3
- export declare const StringLiteral: THandler<import("../types").IValue>;
2
+ export declare const NumericLiteral: THandler;
3
+ export declare const StringLiteral: THandler;
4
4
  export declare const NullLiteral: THandler;
5
5
  export declare const BooleanLiteral: THandler;
@@ -1,6 +1,6 @@
1
1
  import { IValue, THandler } from "../types";
2
- export declare const TSAsExpression: THandler<IValue>;
3
- export declare const TSTypeAssertion: THandler<IValue>;
2
+ export declare const TSAsExpression: THandler;
3
+ export declare const TSTypeAssertion: THandler;
4
4
  export declare const TSInterfaceDeclaration: THandler<null>;
5
5
  export declare const TSTypeAliasDeclaration: THandler<null>;
6
6
  export declare const TSEnumDeclaration: THandler<null>;
@@ -15,3 +15,4 @@ export * from "./TemplateExpression";
15
15
  export * from "./BlockStatement";
16
16
  export * from "./File";
17
17
  export * from "./Typescript";
18
+ export * from "./Import";