-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.ts
More file actions
32 lines (28 loc) · 788 Bytes
/
Copy pathstring.ts
File metadata and controls
32 lines (28 loc) · 788 Bytes
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
import type { TreeWalk } from './env'
import { Fn } from './fn'
import { typeOf, type SExpr, type Var } from './types'
export function isString(x: Var): x is string {
return typeOf(x) === 'str'
}
export function stringConcat(a: Var, b: Var) {
if (!isString(a) || !isString(b)) {
throw Error('calling `string-concat`: expecting `string`')
}
return a + b
}
export function init(
env: TreeWalk.Environment,
nativeFn: (body: (...params: Var[]) => Var) => Fn,
parse: (input: string) => SExpr[],
evaluate: (expr: SExpr, env: TreeWalk.Environment) => Var,
) {
env.define('string?', nativeFn(isString))
env.define('string-concat', nativeFn(stringConcat))
evaluate(
parse(String.raw`
(impl Add str
(def add string-concat))
`)[0],
env,
)
}