Skip to content

Commit

Permalink
Interpreterのコンストラクタ引数を以前同様に戻す (aiscript-dev#345)
Browse files Browse the repository at this point in the history
* Interpreterのコンストラクタ引数の型を戻す

* fix by lint

* api extractor
  • Loading branch information
FineArchs authored Sep 12, 2023
1 parent 5fe3935 commit dac5aec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
5 changes: 3 additions & 2 deletions etc/aiscript.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,7 @@ type InfixOperator = '||' | '&&' | '==' | '!=' | '<=' | '>=' | '<' | '>' | '+' |

// @public (undocumented)
export class Interpreter {
// Warning: (ae-forgotten-export) The symbol "Variable" needs to be exported by the entry point index.d.ts
constructor(vars: Record<string, Variable>, opts?: {
constructor(consts: Record<string, Value>, opts?: {
in?(q: string): Promise<string>;
out?(value: Value): void;
log?(type: string, params: Record<string, any>): void;
Expand Down Expand Up @@ -816,6 +815,8 @@ export class Scope {
constructor(layerdStates?: Scope['layerdStates'], parent?: Scope, name?: Scope['name']);
add(name: string, variable: Variable): void;
assign(name: string, val: Value): void;
// Warning: (ae-forgotten-export) The symbol "Variable" needs to be exported by the entry point index.d.ts
//
// (undocumented)
createChildScope(states?: Map<string, Variable>, name?: Scope['name']): Scope;
exists(name: string): boolean;
Expand Down
24 changes: 11 additions & 13 deletions src/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { std } from './lib/std.js';
import { assertNumber, assertString, assertFunction, assertBoolean, assertObject, assertArray, eq, isObject, isArray, expectAny, reprValue } from './util.js';
import { NULL, RETURN, unWrapRet, FN_NATIVE, BOOL, NUM, STR, ARR, OBJ, FN, BREAK, CONTINUE } from './value.js';
import { getPrimProp } from './primitive-props.js';
import { Variable } from './variable.js';
import type { Value, VFn } from './value.js';
import type * as Ast from '../node.js';
import { Variable } from './variable.js';

const IRQ_RATE = 300;
const IRQ_AT = IRQ_RATE - 1;
Expand All @@ -21,9 +21,10 @@ export class Interpreter {
private stop = false;
public scope: Scope;
private abortHandlers: (() => void)[] = [];
private vars: Record<string, Variable> = {};

constructor(
private vars: Record<string, Variable>,
consts: Record<string, Value>,
private opts: {
in?(q: string): Promise<string>;
out?(value: Value): void;
Expand All @@ -32,27 +33,24 @@ export class Interpreter {
} = {},
) {
const io = {
print: Variable.const(FN_NATIVE(([v]) => {
print: FN_NATIVE(([v]) => {
expectAny(v);
if (this.opts.out) this.opts.out(v);
})),
readline: Variable.const(FN_NATIVE(async args => {
}),
readline: FN_NATIVE(async args => {
const q = args[0];
assertString(q);
if (this.opts.in == null) return NULL;
const a = await this.opts.in!(q.value);
return STR(a);
})),
}),
};

this.vars = {
...vars,
...Object.fromEntries(
Object.entries(std)
.map(([k, v]) => [k, Variable.const(v)]),
),
this.vars = Object.fromEntries(Object.entries({
...consts,
...std,
...io,
};
}).map(([k, v]) => [k, Variable.const(v)]));

this.scope = new Scope([new Map(Object.entries(this.vars))]);
this.scope.opts.log = (type, params): void => {
Expand Down

0 comments on commit dac5aec

Please sign in to comment.