This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
155 lines (118 loc) · 4.08 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { builtinsEval, Func, Evaluator, NULL, printError, Enviroment, objectStringify, Options, Lexer, Parser, Program, LangObject } from './core'
export * from './core'
type Stdio = (...x: Array<any>) => any
interface SesanOption extends Options {
enviroment?: Enviroment
root?: string
filename?: string
}
interface StdioOptions {
stdin: Stdio
stdout: Stdio
stderr: Stdio
}
const stdin: Stdio = (...x) => NULL
const stdout: Stdio = (...x) => (process ? process.stdout.write(x.join(' ')) : console.log(x.join(' ')))
const stderr: Stdio = (...x) => (process ? process.stderr.write(`${x.join(' ')}\n`) : console.error(x.join(' ')))
const defaultFilename: string = '<Sesan>'
export default class Sesan {
public option: SesanOption
public builtins: Map<string, Func> = new Map()
public stdio: StdioOptions = { stdin, stdout, stderr }
constructor(public x: string, option?: SesanOption) {
this.option = { ...option }
}
public tokenizer(): Lexer {
return new Lexer(
this.x,
{
...this.option,
stderr: this.stdio.stderr,
},
this.option.filename ?? defaultFilename
)
}
public parser(): Parser {
return new Parser(this.tokenizer(), this.option)
}
public parseProgram(): Program {
const program = this.parser().parseProgram()
program.errors.forEach((error) =>
printError(error, this.option.filename ?? defaultFilename, this.stdio.stderr, {
...this.option,
})
)
return program
}
public includeStdlib(env: Enviroment) {
if (this.option.useStdLibAutomatically)
new Evaluator(
new Parser(
new Lexer(
`import('@std/lib');`,
{
...this.option,
stderr: this.stdio.stderr,
},
this.option.filename ?? defaultFilename
),
this.option
).parseProgram(),
env,
{
...this.option,
stdio: this.stdio,
filename: this.option.filename ?? defaultFilename,
root: this.option.root ?? './',
}
).eval()
}
public evaluate(program: Program, env: Enviroment): LangObject {
const result = new Evaluator(program, env, {
...this.option,
stdio: this.stdio,
filename: this.option.filename ?? defaultFilename,
root: this.option.root ?? './',
}).eval()
return result
}
public eval(): string {
const env = this.option.enviroment ?? new Enviroment()
this.includeStdlib(env)
return objectStringify(this.evaluate(this.parseProgram(), env))
}
public evalProgram(program: Program): string {
const env = this.option.enviroment ?? new Enviroment()
this.includeStdlib(env)
return objectStringify(this.evaluate(program, env))
}
public setBuiltin(name: string, func: Func): Sesan {
this.builtins.set(name, func)
return this
}
public setBuiltins(builtins: Map<string, Func>): Sesan {
builtins.forEach((func, name) => this.setBuiltin(name, func))
return this
}
public applyBuiltins(): Sesan {
this.builtins.forEach((func, name) => builtinsEval.set(name, func))
return this
}
public setStdin(func: Stdio): Sesan {
this.stdio = { ...this.stdio, stdin: func }
return this
}
public setStdout(func: Stdio): Sesan {
this.stdio = { ...this.stdio, stdout: func }
return this
}
public setStderr(func: Stdio): Sesan {
this.stdio = { ...this.stdio, stderr: func }
return this
}
public setFileName(filename: string): Sesan {
this.option.filename = filename
return this
}
}
export { SesanOption, Stdio, StdioOptions, stdin, stdout, stderr }