Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
391b8d5
REPL: add variant option
arsalan0c Apr 16, 2020
9dfb1e7
Merge branch 'master' into repl_variant
arsalan0c Apr 16, 2020
637bd1f
.
arsalan0c Apr 16, 2020
44f30af
Merge branch 'master' into repl_variant
martin-henz Apr 20, 2020
876cf2f
Fix bug: yield new array (#40)
arsalan0c Apr 28, 2020
75c0fdd
Add user friendly REPL message (#43)
arsalan0c Apr 28, 2020
72836aa
Add randomized amb operator (#15)
arsalan0c Apr 29, 2020
f78fd56
Merge branch 'master' of https://github.com/source-academy/js-slang
arsalan0c Apr 30, 2020
c56a4e8
Merge branch 'master' into repl_variant
arsalan0c Apr 30, 2020
b9f8a78
Add while loops (#39)
arsalan0c Apr 30, 2020
29586b9
Merge branch 'master' into master
anubh-v Apr 30, 2020
da5901e
Clear the stack trace of any error created in REPL (#46)
anubh-v May 1, 2020
4c851c7
Add for loops (#41)
arsalan0c May 1, 2020
287adff
Add documentation for loops (#44)
arsalan0c May 1, 2020
4e64333
Improve comments in non-det interpreter (#47)
anubh-v May 1, 2020
f1f1995
Merge branch 'master' into master
anubh-v May 2, 2020
a5a0c02
Merge branch 'master' into master
martin-henz May 2, 2020
e9910fc
Merge branch 'master' of https://github.com/source-academy/js-slang
arsalan0c May 2, 2020
e58ed02
Merge branch 'master' into repl_variant
arsalan0c May 2, 2020
076d8ba
allow only supported chapter and variant combinations
arsalan0c May 2, 2020
ac9962e
Merge branch 'repl_variant' of https://github.com/arsalanc-v2/js-slan…
arsalan0c May 2, 2020
63a4ced
format
arsalan0c May 2, 2020
6e5bf70
Merge branch 'master' into repl_variant
martin-henz May 4, 2020
17812b7
Merge branch 'master' into repl_variant
martin-henz May 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as es from 'estree'
import { SourceLanguage } from './types'

export const CUT = 'cut' // cut operator for Source 4.3
export const TRY_AGAIN = 'try again' // command for Source 4.3
Expand All @@ -19,3 +20,16 @@ export const JSSLANG_PROPERTIES = {
maxExecTime: 1000,
factorToIncreaseBy: 10
}

export const sourceLanguages: SourceLanguage[] = [
{ chapter: 1, variant: 'default' },
{ chapter: 1, variant: 'wasm' },
{ chapter: 1, variant: 'lazy' },
{ chapter: 2, variant: 'default' },
{ chapter: 2, variant: 'lazy' },
{ chapter: 3, variant: 'default' },
{ chapter: 3, variant: 'concurrent' },
{ chapter: 3, variant: 'non-det' },
{ chapter: 4, variant: 'default' },
{ chapter: 4, variant: 'gpu' }
]
36 changes: 30 additions & 6 deletions src/repl/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { inspect } from 'util'
import { createContext, IOptions, parseError, runInContext } from '../index'
import { Variant, ExecutionMethod } from '../types'
import Closure from '../interpreter/closure'
import { sourceLanguages } from '../constants'

function startRepl(
chapter = 1,
Expand All @@ -14,15 +15,15 @@ function startRepl(
prelude = ''
) {
// use defaults for everything
const context = createContext(chapter, undefined, undefined, undefined)
const context = createContext(chapter, variant, undefined, undefined)
const options: Partial<IOptions> = {
scheduler: 'preemptive',
executionMethod,
variant,
useSubst
}
runInContext(prelude, context, options).then(preludeResult => {
if (preludeResult.status === 'finished') {
if (preludeResult.status === 'finished' || preludeResult.status === 'suspended-non-det') {
console.dir(preludeResult.value, { depth: null })
if (!useRepl) {
return
Expand All @@ -32,7 +33,7 @@ function startRepl(
{
eval: (cmd, unusedContext, unusedFilename, callback) => {
runInContext(cmd, context, options).then(obj => {
if (obj.status === 'finished') {
if (obj.status === 'finished' || obj.status === 'suspended-non-det') {
callback(null, obj.value)
} else {
callback(new Error(parseError(context.errors)), undefined)
Expand All @@ -57,23 +58,46 @@ function startRepl(
})
}

/**
* Returns true iff the given chapter and variant combination is supported.
*/
function validChapterVariant(chapter: any, variant: any) {
for (const lang of sourceLanguages) {
if (lang.chapter === chapter && lang.variant === variant) return true
}

return false
}

function main() {
const opt = require('node-getopt')
.create([
['c', 'chapter=CHAPTER', 'set the Source chapter number (i.e., 1-4)', '1'],
[
'v',
'variant=VARIANT',
'set the Source variant (i.e., default, lazy, non-det, concurrent, wasm, gpu)',
'default'
],
['s', 'use-subst', 'use substitution'],
['h', 'help', 'display this help'],
['i', 'interpreter', 'use the interpreter for execution'],
['l', 'lazy', 'use lazy evaluation'],
['e', 'eval', "don't show REPL, only display output of evaluation"]
])
.bindHelp()
.setHelp('Usage: js-slang [PROGRAM_STRING] [OPTION]\n\n[[OPTIONS]]')
.parseSystem()

const executionMethod = opt.options.interpreter === true ? 'interpreter' : 'native'
const variant = opt.options.lazy === true ? 'lazy' : 'default'
const variant = opt.options.variant
const chapter = parseInt(opt.options.chapter, 10)
const areValidChapterVariant: boolean = validChapterVariant(chapter, variant)
if (!areValidChapterVariant) {
throw new Error(
'The chapter and variant combination provided is unsupported. Use the -h option to view valid chapters and variants.'
)
}

const executionMethod = opt.options.interpreter === true ? 'interpreter' : 'native'
const useSubst = opt.options.s
const useRepl = !opt.options.e
const prelude = opt.argv[0] ?? ''
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export interface Comment {
export type ExecutionMethod = 'native' | 'interpreter' | 'auto'
export type Variant = 'wasm' | 'lazy' | 'non-det' | 'concurrent' | 'gpu' | 'default' // this might replace EvaluationMethod

export interface SourceLanguage {
chapter: number
variant: Variant
}

export interface Context<T = any> {
/** The source version used */
chapter: number
Expand Down