Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion etc/aiscript.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ type Index = NodeBase & {

// @public (undocumented)
export class Interpreter {
constructor(consts: Record<string, Value>, opts?: {
constructor(globals: Record<string, Value>, opts?: {
in?(q: string): Promise<string>;
out?(value: Value): void;
err?(e: AiScriptError): void;
Expand Down Expand Up @@ -713,6 +713,9 @@ export class Scope {
// @public (undocumented)
type Statement = Definition | Return | Each | For | Loop | Break | Continue | Assign | AddAssign | SubAssign;

// @public (undocumented)
export const std: Record<string, Value>;

// @public (undocumented)
const STR: (str: VStr["value"]) => VStr;

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//export * from './interpreter/index';
//export * as utils from './interpreter/util';
//export * as values from './interpreter/value';
import { Interpreter } from './interpreter/index.js';
import { Interpreter, std } from './interpreter/index.js';
import { Scope } from './interpreter/scope.js';
import * as utils from './interpreter/util.js';
import * as values from './interpreter/value.js';
Expand All @@ -12,7 +12,7 @@ import * as errors from './error.js';
import * as Ast from './node.js';
import { AISCRIPT_VERSION } from './constants.js';
import type { ParserPlugin, PluginType } from './parser/index.js';
export { Interpreter };
export { Interpreter, std };
export { Scope };
export { utils };
export { values };
Expand Down
19 changes: 9 additions & 10 deletions src/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import * as Ast from '../node.js';
import { nodeToJs } from '../utils/node-to-js.js';
import { Scope } from './scope.js';
import { std } from './lib/std.js';
import { RETURN, unWrapRet, BREAK, CONTINUE, assertValue, isControl, type Control, unWrapLabeledBreak } from './control.js';
import { assertNumber, assertString, assertFunction, assertBoolean, assertObject, assertArray, eq, isObject, isArray, expectAny, reprValue, isFunction } from './util.js';
import { NULL, FN_NATIVE, BOOL, NUM, STR, ARR, OBJ, FN, ERROR } from './value.js';
Expand All @@ -17,6 +16,8 @@
import type { JsValue } from './util.js';
import type { Value, VFn, VUserFn } from './value.js';

export { std } from './lib/std.js';

export type LogObject = {
scope?: string;
var?: string;
Expand All @@ -36,12 +37,11 @@
private abortHandlers: (() => void)[] = [];
private pauseHandlers: (() => void)[] = [];
private unpauseHandlers: (() => void)[] = [];
private vars: Record<string, Variable> = {};
private irqRate: number;
private irqSleep: () => Promise<void>;

constructor(
consts: Record<string, Value>,
globals: Record<string, Value>,
private opts: {
in?(q: string): Promise<string>;
out?(value: Value): void;
Expand All @@ -67,13 +67,12 @@
}),
};

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 = new Scope([
new Map(
Object.entries({ ...globals, ...io })
.map(([k, v]) => [k, Variable.const(v)]),
),
]);
this.scope.opts.log = (type, params): void => {
switch (type) {
case 'add': this.log('var:add', params); break;
Expand Down Expand Up @@ -548,7 +547,7 @@

case 'loop': {
// eslint-disable-next-line no-constant-condition
while (true) {

Check warning on line 550 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy
const v = await this._run(node.statements, scope.createChildScope(), callStack);
if (v.type === 'break') {
if (v.label != null && v.label !== node.label) {
Expand Down Expand Up @@ -1081,7 +1080,7 @@

case 'loop': {
// eslint-disable-next-line no-constant-condition
while (true) {

Check warning on line 1083 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy
const v = this._runSync(node.statements, scope.createChildScope(), callStack);
if (v.type === 'break') {
if (v.label != null && v.label !== node.label) {
Expand Down Expand Up @@ -1631,7 +1630,7 @@
public pause(): void {
if (this.pausing) return;
let resolve: () => void;
const promise = new Promise<void>(r => { resolve = () => r(); });

Check warning on line 1633 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
this.pausing = { promise, resolve: resolve! };
for (const handler of this.pauseHandlers) {
handler();
Expand Down
3 changes: 2 additions & 1 deletion test/interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'assert';
import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest';
import { Parser, Interpreter, values, errors, utils, Ast } from '../src';
import { Parser, Interpreter, std, values, errors, utils, Ast } from '../src';
import { FALSE, NUM, OBJ, STR, TRUE, Value } from '../src/interpreter/value';

let { FN_NATIVE } = values;
Expand Down Expand Up @@ -261,6 +261,7 @@ describe('pause', () => {
let count = 0;

const interpreter = new Interpreter({
...std,
count: values.FN_NATIVE(() => { count++; }),
}, {});

Expand Down
6 changes: 3 additions & 3 deletions test/testutils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect as globalExpect } from 'vitest';
import { Parser, Interpreter } from '../src';
import { Parser, Interpreter, std } from '../src';
import { Value } from '../src/interpreter/value';

export async function exe(script: string): Promise<Value | undefined> {
const parser = new Parser();
let result = undefined;
const interpreter = new Interpreter({}, {
const interpreter = new Interpreter(std, {
out(value) {
if (!result) result = value;
else if (!Array.isArray(result)) result = [result, value];
Expand All @@ -23,7 +23,7 @@ export async function exe(script: string): Promise<Value | undefined> {

export function exeSync(script: string): Value | undefined {
const parser = new Parser();
const interpreter = new Interpreter({}, {
const interpreter = new Interpreter(std, {
out(value) {
},
log(type, {val}) {
Expand Down
Loading