-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31-repl.js
93 lines (83 loc) · 2.21 KB
/
31-repl.js
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
import { packages }
from "./01-lisp.js"
import { execute }
from "./02-keval.js"
import { readFromString }
from "./03-read.js"
import Context
from "./12-react-context.js"
import { Term }
from "./30-term.js"
import { Component, h }
from "./vendor/preact.js"
export class Repl extends Component {
constructor(props) {
super(props)
this.state = {
history: props.history,
input: "",
ctx: {
home: packages.user,
used: [packages.user, packages.lisp],
print: x => this.append({ output: x }),
}
}
}
append = x => setTimeout(() => {
console.log("print", x)
this.setState({
history: [...this.state.history, x]
})
})
setInput = e => this.setState({ input: e.target.value })
submit = e => {
e.preventDefault()
this.setState({ input: "" })
let term = readFromString(this.state.ctx, this.state.input)
this.append({ input: term })
console.log(this.state)
try {
let s = execute({
ctx: this.state.ctx,
term,
limit: 100,
})
if (s.plan === null) {
this.append({ result: s.value })
} else {
this.append({ timeout: true })
}
} catch (e) {
this.append({ error: e })
}
}
render(props, state) {
function History(x) {
if (x.output)
return h("lisp-output", {}, h(Term, { term: x.output, expanded: true }))
else if (x.input)
return h("lisp-input", {}, h(Term, { term: x.input, expanded: true }))
else if (x.result)
return h("lisp-result", {}, h(Term, { term: x.result, expanded: true }))
else if (x.error)
return h("lisp-error", {}, h(Term, { term: x.error.stack, expanded: true }))
else if (x.note)
return h("i", {}, x.note)
else
throw new Error(x)
}
return h(Context.Provider, { value: this.state.ctx }, [
h("lisp-repl", {}, [
h("lisp-history", {}, state.history.map(x => History(x))),
h("form", {
onsubmit: e => this.submit(e)
}, h("input", {
placeholder: "Lisp term...",
autofocus: true,
value: this.state.input,
onchange: e => this.setInput(e),
})),
])
])
}
}