-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
41 lines (35 loc) · 992 Bytes
/
index.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
41
import { scanTokens } from './src/scanner'
import { parse } from './src/parser'
import { resolve } from './src/resolver'
import { emit } from './src/backend'
import { ReportError } from './src/util'
export interface CompileResult {
// Generated WAT code for the given program.
// If compilation fails, this is null.
program: string | null
// List of errors reported during compilation.
errors: string[]
}
export function compile(source: string): CompileResult {
const result: CompileResult = {
program: null,
errors: []
}
const reportError: ReportError = (line, msg) => {
result.errors.push(`${line}: ${msg}`)
}
const tokens = scanTokens(source, reportError)
if (result.errors.length > 0) {
return result
}
const context = parse(tokens, reportError)
if (result.errors.length > 0) {
return result
}
resolve(context, reportError)
if (result.errors.length > 0) {
return result
}
result.program = emit(context)
return result
}