Skip to content

달빛약속 / core/mod / Scope

Class: Scope ​

실행 컨텍스트(Execution Context)를 관리하는 클래스입니다. 변수와 함수의 저장, 검색, 수정을 담당하며, 스코프 체인을 통해 렉시컬 스코핑을 구현합니다.

Description ​

Scope 인스턴스는 계층적인 구조를 가집니다. 각 스코프는 부모 스코프(parent)를 가질 수 있으며, 변수나 함수를 찾을 때 현재 스코프에 없으면 부모 스코프로 거슬러 올라가며 재귀적으로 탐색합니다. 이는 달빛 약속 언어의 클로저와 변수 유효 범위를 결정하는 핵심적인 메커니즘입니다.

Constructors ​

new Scope() ​

new Scope(config): Scope

Parameters ​

config ​
codeFile ​

CodeFile

initialVariable ​

Record<string, ValueType>

parent ​

Scope

Returns ​

Scope

Defined in ​

core/executer/scope.ts:23

Properties ​

codeFile? ​

optional codeFile: CodeFile

Defined in ​

core/executer/scope.ts:20


parent ​

parent: Scope

Defined in ​

core/executer/scope.ts:19


variables ​

variables: Record<string, ValueType>

Defined in ​

core/executer/scope.ts:18

Methods ​

addFunctionObject() ​

addFunctionObject(functionObject): void

현재 스코프에 새로운 함수(약속)를 추가합니다.

Parameters ​

functionObject ​

RunnableObject

추가할 함수를 나타내는 RunnableObject입니다.

Returns ​

void

Defined in ​

core/executer/scope.ts:110


askSetVariable() ​

askSetVariable(name, value): boolean

상위 스코프로 거슬러 올라가며 변수가 존재하는지 확인하고, 존재하면 값을 설정합니다. setVariable 내부에서 호출되는 헬퍼 메서드입니다.

Parameters ​

name ​

string

설정할 변수의 이름입니다.

value ​

ValueType

변수에 할당할 값입니다.

Returns ​

boolean

변수를 성공적으로 설정했는지 여부를 반환합니다.

Defined in ​

core/executer/scope.ts:65


getFunctionObject() ​

getFunctionObject(name): RunnableObject

현재 스코프 또는 상위 스코프에서 함수(약속)를 찾아 반환합니다.

Parameters ​

name ​

string

찾을 함수의 이름입니다.

Returns ​

RunnableObject

함수를 나타내는 RunnableObject를 반환합니다.

Description ​

변수 검색과 마찬가지로, 스코프 체인을 따라 올라가며 재귀적으로 함수를 찾습니다.

Defined in ​

core/executer/scope.ts:132


getVariable() ​

getVariable(name): ValueType

현재 스코프 또는 상위 스코프에서 변수를 찾아 그 값을 반환합니다.

Parameters ​

name ​

string

찾을 변수의 이름입니다.

Returns ​

ValueType

변수의 값을 담은 ValueType 객체를 반환합니다.

Description ​

스코프 체인 탐색: 먼저 현재 스코프의 variables에서 변수를 찾습니다. 만약 없다면, parent 스코프의 getVariable을 재귀적으로 호출하여 스코프 체인을 따라 올라가며 변수를 찾습니다. 최상위 스코프에도 변수가 없으면 NotDefinedIdentifierError를 발생시킵니다.

Defined in ​

core/executer/scope.ts:87


setVariable() ​

setVariable(name, value): void

변수를 설정합니다. 이미 상위 스코프에 변수가 존재하면 그 변수의 값을 갱신하고, 그렇지 않으면 현재 스코프에 새로운 변수를 생성합니다.

Parameters ​

name ​

string

설정할 변수의 이름입니다.

value ​

ValueType

변수에 할당할 값입니다.

Returns ​

void

Defined in ​

core/executer/scope.ts:53