Skip to content

Commit

Permalink
layerdStatesをLayeredStatesに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
takejohn committed Jan 15, 2025
1 parent ded74a7 commit 42285eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion etc/aiscript.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ type Return = NodeBase & {

// @public (undocumented)
export class Scope {
constructor(layerdStates?: Scope['layerdStates'], parent?: Scope, name?: Scope['name'], nsName?: string);
constructor(layeredStates?: Scope['layeredStates'], parent?: Scope, name?: Scope['name'], nsName?: string);
add(name: string, variable: Variable): void;
assign(name: string, val: Value): void;
// (undocumented)
Expand Down
32 changes: 16 additions & 16 deletions src/interpreter/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import type { Value } from './value.js';
import type { Variable } from './variable.js';
import type { LogObject } from './index.js';

export type LayerdStates = [Map<string, Variable>, ...Map<string, Variable>[]]
export type LayeredStates = [Map<string, Variable>, ...Map<string, Variable>[]]

export class Scope {
private parent?: Scope;
private layerdStates: LayerdStates;
private layeredStates: LayeredStates;
public name: string;
public opts: {
log?(type: string, params: LogObject): void;
onUpdated?(name: string, value: Value): void;
} = {};
public nsName?: string;

constructor(layerdStates: Scope['layerdStates'] = [new Map()], parent?: Scope, name?: Scope['name'], nsName?: string) {
this.layerdStates = layerdStates;
constructor(layeredStates: Scope['layeredStates'] = [new Map()], parent?: Scope, name?: Scope['name'], nsName?: string) {
this.layeredStates = layeredStates;
this.parent = parent;
this.name = name || (layerdStates.length === 1 ? '<root>' : '<anonymous>');
this.name = name || (layeredStates.length === 1 ? '<root>' : '<anonymous>');
this.nsName = nsName;
}

Expand All @@ -43,13 +43,13 @@ export class Scope {

@autobind
public createChildScope(states: Map<string, Variable> = new Map(), name?: Scope['name']): Scope {
const layer: LayerdStates = [states, ...this.layerdStates];
const layer: LayeredStates = [states, ...this.layeredStates];
return new Scope(layer, this, name);
}

@autobind
public createChildNamespaceScope(nsName: string, states: Map<string, Variable> = new Map(), name?: Scope['name']): Scope {
const layer: LayerdStates = [states, ...this.layerdStates];
const layer: LayeredStates = [states, ...this.layeredStates];
return new Scope(layer, this, name, nsName);
}

Expand All @@ -59,7 +59,7 @@ export class Scope {
*/
@autobind
public get(name: string): Value {
for (const layer of this.layerdStates) {
for (const layer of this.layeredStates) {
const value = layer.get(name);
if (value) {
const state = value.value;
Expand All @@ -70,7 +70,7 @@ export class Scope {

throw new AiScriptRuntimeError(
`No such variable '${name}' in scope '${this.name}'`,
{ scope: this.layerdStates });
{ scope: this.layeredStates });
}

/**
Expand All @@ -88,7 +88,7 @@ export class Scope {
*/
@autobind
public exists(name: string): boolean {
for (const layer of this.layerdStates) {
for (const layer of this.layeredStates) {
if (layer.has(name)) {
this.log('exists', { var: name });
return true;
Expand All @@ -104,7 +104,7 @@ export class Scope {
*/
@autobind
public getAll(): Map<string, Variable> {
const vars = this.layerdStates.reduce((arr, layer) => {
const vars = this.layeredStates.reduce((arr, layer) => {
return [...arr, ...layer];
}, [] satisfies [string, Variable][]);
return new Map(vars);
Expand All @@ -118,11 +118,11 @@ export class Scope {
@autobind
public add(name: string, variable: Variable): void {
this.log('add', { var: name, val: variable });
const states = this.layerdStates[0];
const states = this.layeredStates[0];
if (states.has(name)) {
throw new AiScriptRuntimeError(
`Variable '${name}' already exists in scope '${this.name}'`,
{ scope: this.layerdStates });
{ scope: this.layeredStates });
}
states.set(name, variable);
if (this.parent == null) this.onUpdated(name, variable.value);
Expand All @@ -137,7 +137,7 @@ export class Scope {
@autobind
public assign(name: string, val: Value): void {
let i = 1;
for (const layer of this.layerdStates) {
for (const layer of this.layeredStates) {
const variable = layer.get(name);
if (variable != null) {
if (!variable.isMutable) {
Expand All @@ -147,14 +147,14 @@ export class Scope {
variable.value = val;

this.log('assign', { var: name, val: val });
if (i === this.layerdStates.length) this.onUpdated(name, val);
if (i === this.layeredStates.length) this.onUpdated(name, val);
return;
}
i++;
}

throw new AiScriptRuntimeError(
`No such variable '${name}' in scope '${this.name}'`,
{ scope: this.layerdStates });
{ scope: this.layeredStates });
}
}

0 comments on commit 42285eb

Please sign in to comment.