Skip to content

Commit 01f7c81

Browse files
arsalan0cmartin-henzanubh-v
authored
REPL: add variant option (#549)
* REPL: add variant option * . * Fix bug: yield new array (#40) * fix bug: yield new array for array expression * Add user friendly REPL message (#43) * add user friendly REPL message * Add randomized amb operator (#15) Adds the ambR operator which functions similarly to amb but makes choices randomly instead of sequentially. Also adds landing page and library documentation for the operator, and adds this operator into the specs. * Add while loops (#39) This PR adds support for while loops, including break, continue and return statements in the loop body. It also tweaks the scheduler so that stackoverflow errors are captured and displayed to the user. Co-authored-by: anubh-v <[email protected]> * Clear the stack trace of any error created in REPL (#46) * Add for loops (#41) This PR adds support for for loops, including break, continue and return statements in the loop body. Co-authored-by: Anubhav <[email protected]> * Add documentation for loops (#44) Co-authored-by: anubh-v <[email protected]> * Improve comments in non-det interpreter (#47) * Remove unhelpful comment * Fix spelling in comment * allow only supported chapter and variant combinations * format Co-authored-by: martin-henz <[email protected]> Co-authored-by: anubh-v <[email protected]> Co-authored-by: Anubhav <[email protected]>
1 parent a538cd6 commit 01f7c81

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as es from 'estree'
2+
import { SourceLanguage } from './types'
23

34
export const CUT = 'cut' // cut operator for Source 4.3
45
export const TRY_AGAIN = 'try again' // command for Source 4.3
@@ -19,3 +20,16 @@ export const JSSLANG_PROPERTIES = {
1920
maxExecTime: 1000,
2021
factorToIncreaseBy: 10
2122
}
23+
24+
export const sourceLanguages: SourceLanguage[] = [
25+
{ chapter: 1, variant: 'default' },
26+
{ chapter: 1, variant: 'wasm' },
27+
{ chapter: 1, variant: 'lazy' },
28+
{ chapter: 2, variant: 'default' },
29+
{ chapter: 2, variant: 'lazy' },
30+
{ chapter: 3, variant: 'default' },
31+
{ chapter: 3, variant: 'concurrent' },
32+
{ chapter: 3, variant: 'non-det' },
33+
{ chapter: 4, variant: 'default' },
34+
{ chapter: 4, variant: 'gpu' }
35+
]

src/repl/repl.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { inspect } from 'util'
44
import { createContext, IOptions, parseError, runInContext } from '../index'
55
import { Variant, ExecutionMethod } from '../types'
66
import Closure from '../interpreter/closure'
7+
import { sourceLanguages } from '../constants'
78

89
function startRepl(
910
chapter = 1,
@@ -14,15 +15,15 @@ function startRepl(
1415
prelude = ''
1516
) {
1617
// use defaults for everything
17-
const context = createContext(chapter, undefined, undefined, undefined)
18+
const context = createContext(chapter, variant, undefined, undefined)
1819
const options: Partial<IOptions> = {
1920
scheduler: 'preemptive',
2021
executionMethod,
2122
variant,
2223
useSubst
2324
}
2425
runInContext(prelude, context, options).then(preludeResult => {
25-
if (preludeResult.status === 'finished') {
26+
if (preludeResult.status === 'finished' || preludeResult.status === 'suspended-non-det') {
2627
console.dir(preludeResult.value, { depth: null })
2728
if (!useRepl) {
2829
return
@@ -32,7 +33,7 @@ function startRepl(
3233
{
3334
eval: (cmd, unusedContext, unusedFilename, callback) => {
3435
runInContext(cmd, context, options).then(obj => {
35-
if (obj.status === 'finished') {
36+
if (obj.status === 'finished' || obj.status === 'suspended-non-det') {
3637
callback(null, obj.value)
3738
} else {
3839
callback(new Error(parseError(context.errors)), undefined)
@@ -57,23 +58,46 @@ function startRepl(
5758
})
5859
}
5960

61+
/**
62+
* Returns true iff the given chapter and variant combination is supported.
63+
*/
64+
function validChapterVariant(chapter: any, variant: any) {
65+
for (const lang of sourceLanguages) {
66+
if (lang.chapter === chapter && lang.variant === variant) return true
67+
}
68+
69+
return false
70+
}
71+
6072
function main() {
6173
const opt = require('node-getopt')
6274
.create([
6375
['c', 'chapter=CHAPTER', 'set the Source chapter number (i.e., 1-4)', '1'],
76+
[
77+
'v',
78+
'variant=VARIANT',
79+
'set the Source variant (i.e., default, lazy, non-det, concurrent, wasm, gpu)',
80+
'default'
81+
],
6482
['s', 'use-subst', 'use substitution'],
6583
['h', 'help', 'display this help'],
6684
['i', 'interpreter', 'use the interpreter for execution'],
67-
['l', 'lazy', 'use lazy evaluation'],
6885
['e', 'eval', "don't show REPL, only display output of evaluation"]
6986
])
7087
.bindHelp()
7188
.setHelp('Usage: js-slang [PROGRAM_STRING] [OPTION]\n\n[[OPTIONS]]')
7289
.parseSystem()
7390

74-
const executionMethod = opt.options.interpreter === true ? 'interpreter' : 'native'
75-
const variant = opt.options.lazy === true ? 'lazy' : 'default'
91+
const variant = opt.options.variant
7692
const chapter = parseInt(opt.options.chapter, 10)
93+
const areValidChapterVariant: boolean = validChapterVariant(chapter, variant)
94+
if (!areValidChapterVariant) {
95+
throw new Error(
96+
'The chapter and variant combination provided is unsupported. Use the -h option to view valid chapters and variants.'
97+
)
98+
}
99+
100+
const executionMethod = opt.options.interpreter === true ? 'interpreter' : 'native'
77101
const useSubst = opt.options.s
78102
const useRepl = !opt.options.e
79103
const prelude = opt.argv[0] ?? ''

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export interface Comment {
5959
export type ExecutionMethod = 'native' | 'interpreter' | 'auto'
6060
export type Variant = 'wasm' | 'lazy' | 'non-det' | 'concurrent' | 'gpu' | 'default' // this might replace EvaluationMethod
6161

62+
export interface SourceLanguage {
63+
chapter: number
64+
variant: Variant
65+
}
66+
6267
export interface Context<T = any> {
6368
/** The source version used */
6469
chapter: number

0 commit comments

Comments
 (0)