Skip to content

Commit 30543cb

Browse files
authored
Allow source file to be passed to repl (#396)
1 parent c756088 commit 30543cb

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ To try out *Source* in a REPL, run
3030
3131
$ node dist/repl/repl.js [chapter] # default: 1
3232
33+
If you wish, you can pass in a file path instead, to evaluate some *Source* before initialising the REPL
34+
It will be run in *Source* chapter 4.
35+
36+
.. code-block::
37+
38+
$ node dist/repl/repl.js [path/to/file]
39+
3340
or alternatively, install js-slang and run
3441

3542
.. code-block::

src/repl/repl.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
1+
import fs = require('fs')
12
import repl = require('repl') // 'repl' here refers to the module named 'repl' in index.d.ts
2-
import { createContext, parseError, runInContext } from '../index'
3+
import { createContext, IOptions, parseError, runInContext } from '../index'
34

4-
function startRepl(chapter = 1, useSubst: boolean) {
5+
function startRepl(chapter = 1, useSubst: boolean, prelude = '') {
56
// use defaults for everything
67
const context = createContext(chapter)
7-
repl.start(
8-
// the object being passed as argument fits the interface ReplOptions in the repl module.
9-
{
10-
eval: (cmd, unusedContext, unusedFilename, callback) => {
11-
runInContext(cmd, context, { scheduler: 'preemptive', useSubst }).then(obj => {
12-
if (obj.status === 'finished') {
13-
callback(null, obj.value)
14-
} else {
15-
callback(new Error(parseError(context.errors)), undefined)
8+
const options: Partial<IOptions> = { scheduler: 'preemptive', useSubst }
9+
runInContext(prelude, context, options).then(preludeResult => {
10+
if (preludeResult.status === 'finished') {
11+
console.log(preludeResult.value)
12+
repl.start(
13+
// the object being passed as argument fits the interface ReplOptions in the repl module.
14+
{
15+
eval: (cmd, unusedContext, unusedFilename, callback) => {
16+
runInContext(cmd, context, options).then(obj => {
17+
if (obj.status === 'finished') {
18+
callback(null, obj.value)
19+
} else {
20+
callback(new Error(parseError(context.errors)), undefined)
21+
}
22+
})
1623
}
17-
})
18-
}
24+
}
25+
)
26+
} else {
27+
throw new Error(parseError(context.errors))
1928
}
20-
)
29+
})
2130
}
2231

2332
function main() {
24-
const chapter = process.argv.length > 2 ? parseInt(process.argv[2], 10) : 1
25-
const useSubst = process.argv.length > 3 ? process.argv[3] === 'subst' : false
26-
startRepl(chapter, useSubst)
33+
const firstArg = process.argv[2]
34+
if (process.argv.length === 3 && String(Number(firstArg)) !== firstArg.trim()) {
35+
fs.readFile(firstArg, 'utf8', (err, data) => {
36+
if (err) {
37+
throw err
38+
}
39+
startRepl(4, false, data)
40+
})
41+
} else {
42+
const chapter = process.argv.length > 2 ? parseInt(firstArg, 10) : 1
43+
const useSubst = process.argv.length > 3 ? process.argv[3] === 'subst' : false
44+
startRepl(chapter, useSubst)
45+
}
2746
}
2847

2948
main()

0 commit comments

Comments
 (0)