|
| 1 | +import { AISCRIPT_VERSION, Parser, Interpreter, utils, errors, type Ast } from 'aiscript1_1'; |
| 2 | +import { Runner } from '../runner'; |
| 3 | + |
| 4 | +export default class extends Runner { |
| 5 | + version = AISCRIPT_VERSION; |
| 6 | + |
| 7 | + parse(code: string) { |
| 8 | + const ast = Parser.parse(code); |
| 9 | + const metadata = Interpreter.collectMetadata(ast); |
| 10 | + return [ast, metadata] as const; |
| 11 | + } |
| 12 | + |
| 13 | + private interpreter = new Interpreter({}, { |
| 14 | + out: (value) => { |
| 15 | + this.print( |
| 16 | + value.type === 'num' ? value.value.toString() |
| 17 | + : value.type === 'str' ? `"${value.value}"` |
| 18 | + : JSON.stringify(utils.valToJs(value), null, 2) ?? '', |
| 19 | + ); |
| 20 | + }, |
| 21 | + log: (type, params) => { |
| 22 | + if (type === 'end' && params.val != null && 'type' in params.val) { |
| 23 | + this.print(utils.valToString(params.val, true)); |
| 24 | + } |
| 25 | + }, |
| 26 | + }); |
| 27 | + async exec(node: unknown): Promise<void> { |
| 28 | + await this.interpreter.exec(node as Ast.Node[]); |
| 29 | + } |
| 30 | + isAiScriptError(error: unknown): error is errors.AiScriptError { |
| 31 | + return error instanceof errors.AiScriptError; |
| 32 | + } |
| 33 | + getErrorName(error: errors.AiScriptError): string | undefined { |
| 34 | + if (error instanceof errors.AiScriptSyntaxError) { |
| 35 | + return 'SyntaxError'; |
| 36 | + } |
| 37 | + if (error instanceof errors.AiScriptTypeError) { |
| 38 | + return 'TypeError'; |
| 39 | + } |
| 40 | + if (error instanceof errors.AiScriptRuntimeError) { |
| 41 | + return 'RuntimeError'; |
| 42 | + } |
| 43 | + if (error instanceof errors.AiScriptIndexOutOfRangeError) { |
| 44 | + return 'IndexOutOfRangeError'; |
| 45 | + } |
| 46 | + if (error instanceof errors.AiScriptUserError) { |
| 47 | + return 'UserError'; |
| 48 | + } |
| 49 | + return 'AiScriptError'; |
| 50 | + } |
| 51 | + dispose() { |
| 52 | + this.interpreter.abort(); |
| 53 | + } |
| 54 | +} |
0 commit comments