-
Notifications
You must be signed in to change notification settings - Fork 1
/
link-modules.ts
40 lines (37 loc) · 1.06 KB
/
link-modules.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import {Section} from './parse/module'
import {ModuleIndices} from './compile/context'
import {compileModule} from './compile/module'
export interface NamedModules {
[name: string]: Section[]
}
interface CompiledModuleString {
assembly: string
header: string
}
interface CompiledModules {
[name: string]: CompiledModuleString
}
interface Stringable {
readonly str: string
}
const makeString = (elements: Stringable[]): string =>
elements.map(({str}) => str + '\n').join('')
export function linkModules(modules: NamedModules): CompiledModules {
const moduleIndices: ModuleIndices = {}
let moduleIndex = 0
for (const moduleName in modules) moduleIndices[moduleName] = moduleIndex
const compiledModules: CompiledModules = {}
for (const moduleName in modules) {
const {instructions, declarations} = compileModule({
module: modules[moduleName],
index: moduleIndices[moduleName],
moduleIndices,
moduleName: moduleName
})
compiledModules[moduleName] = {
assembly: makeString(instructions),
header: makeString(declarations)
}
}
return compiledModules
}