Skip to content

Commit

Permalink
refactor: Primitive Propertiesの取得処理をprimitive-props.tsに移動 (#322)
Browse files Browse the repository at this point in the history
* improve primitive-props.ts's independency

* fix by lint

* undo removal of conditional
  • Loading branch information
FineArchs authored Sep 5, 2023
1 parent bc94751 commit 63eb8fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
25 changes: 4 additions & 21 deletions src/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { autobind } from '../utils/mini-autobind.js';
import { IndexOutOfRangeError, RuntimeError } from '../error.js';
import { Scope } from './scope.js';
import { std } from './lib/std.js';
import { assertNumber, assertString, assertFunction, assertBoolean, assertObject, assertArray, eq, isObject, isArray, isString, expectAny, isNumber, reprValue } from './util.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 { PRIMITIVE_PROPS } from './primitive-props.js';
import { getPrimProp } from './primitive-props.js';
import type { Value, VFn } from './value.js';
import type * as Ast from '../node.js';

Expand Down Expand Up @@ -167,6 +167,7 @@ export class Interpreter {
registerAbortHandler: this.registerAbortHandler,
unregisterAbortHandler: this.unregisterAbortHandler,
});
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
return result ?? NULL;
} else {
const _args = new Map() as Map<string, Value>;
Expand Down Expand Up @@ -367,26 +368,8 @@ export class Interpreter {
} else {
return NULL;
}
} else if (isNumber(target)) {
if (Object.hasOwn(PRIMITIVE_PROPS.num, node.name)) {
return PRIMITIVE_PROPS.num[node.name as keyof typeof PRIMITIVE_PROPS['num']](target);
} else {
throw new RuntimeError(`No such prop (${node.name}) in ${target.type}.`);
}
} else if (isString(target)) {
if (Object.hasOwn(PRIMITIVE_PROPS.str, node.name)) {
return PRIMITIVE_PROPS.str[node.name as keyof typeof PRIMITIVE_PROPS['str']](target);
} else {
throw new RuntimeError(`No such prop (${node.name}) in ${target.type}.`);
}
} else if (isArray(target)) {
if (Object.hasOwn(PRIMITIVE_PROPS.arr, node.name)) {
return PRIMITIVE_PROPS.arr[node.name as keyof typeof PRIMITIVE_PROPS['arr']](target);
} else {
throw new RuntimeError(`No such prop (${node.name}) in ${target.type}.`);
}
} else {
throw new RuntimeError(`Cannot read prop (${node.name}) of ${target.type}.`);
return getPrimProp(target, node.name);
}
}

Expand Down
20 changes: 19 additions & 1 deletion src/interpreter/primitive-props.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { substring, length, indexOf, toArray } from 'stringz';
import { RuntimeError } from '../error.js';
import { assertArray, assertBoolean, assertFunction, assertNumber, assertString, expectAny } from './util.js';
import { ARR, FALSE, FN_NATIVE, NULL, NUM, STR, TRUE } from './value.js';
import type { Value, VArr, VFn, VNum, VStr } from './value.js';

export const PRIMITIVE_PROPS = {
type VWithPP = VNum|VStr|VArr;

const PRIMITIVE_PROPS: {
[key in VWithPP['type']]: { [key: string]: (target: Value) => Value }
} = {
num: {
to_str: (target: VNum): VFn => FN_NATIVE(async (_, _opts) => {
return STR(target.value.toString());
Expand Down Expand Up @@ -217,3 +222,16 @@ export const PRIMITIVE_PROPS = {
}),
},
} as const;

export function getPrimProp(target: Value, name: string): Value {
if (Object.hasOwn(PRIMITIVE_PROPS, target.type)) {
const props = PRIMITIVE_PROPS[target.type as VWithPP['type']];
if (Object.hasOwn(props, name)) {
return props[name]!(target);
} else {
throw new RuntimeError(`No such prop (${name}) in ${target.type}.`);
}
} else {
throw new RuntimeError(`Cannot read prop of ${target.type}. (reading ${name})`);
}
}

0 comments on commit 63eb8fd

Please sign in to comment.