Skip to content

Commit 35466ea

Browse files
remo5000ning-y
authored andcommitted
Store external symbols (#21)
* Rename importExternals -> importExternalSymbols * Rename externals -> externalSymbols * Store externalSymbols in Context * Add externalSymbols to Context typedef
1 parent 5d457d5 commit 35466ea

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/createContext.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const createEmptyRuntime = () => ({
1818
nodes: []
1919
})
2020

21-
export const createEmptyContext = <T>(chapter: number, externalContext?: T): Context<T> => ({
21+
export const createEmptyContext = <T>(chapter: number, externalSymbols: string[], externalContext?: T): Context<T> => ({
2222
chapter,
23+
externalSymbols,
2324
errors: [],
2425
externalContext,
2526
cfg: createEmptyCFG(),
@@ -47,10 +48,10 @@ const defineSymbol = (context: Context, name: string, value: Value) => {
4748
globalFrame.environment[name] = value
4849
}
4950

50-
export const importExternals = (context: Context, externals: string[]) => {
51+
export const importExternalSymbols = (context: Context, externalSymbols: string[]) => {
5152
ensureGlobalEnvironmentExist(context)
5253

53-
externals.forEach(symbol => {
54+
externalSymbols.forEach(symbol => {
5455
defineSymbol(context, symbol, GLOBAL[symbol])
5556
})
5657
}
@@ -154,12 +155,12 @@ const defaultBuiltIns: CustomBuiltIns = {
154155
throw new Error('List visualizer is not enabled')}
155156
}
156157

157-
const createContext = <T>(chapter = 1, externals: string[] = [], externalContext?: T,
158+
const createContext = <T>(chapter = 1, externalSymbols: string[] = [], externalContext?: T,
158159
externalBuiltIns: CustomBuiltIns = defaultBuiltIns) => {
159-
const context = createEmptyContext(chapter, externalContext)
160+
const context = createEmptyContext(chapter, externalSymbols, externalContext)
160161

161162
importBuiltins(context, externalBuiltIns)
162-
importExternals(context, externals)
163+
importExternalSymbols(context, externalSymbols)
163164

164165
return context
165166
}

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ export interface Context<T = any> {
107107
/** The source version used */
108108
chapter: number
109109

110+
/** The external symbols that exist in the Context. */
111+
externalSymbols: string[]
112+
110113
/** All the errors gathered */
111114
errors: SourceError[]
112115

0 commit comments

Comments
 (0)