Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env bash

JS_SLANG="node node_modules/js-slang/dist/repl/repl.js"
JS_SLANG="node --stack-size=2000 node_modules/js-slang/dist/repl/repl.js"

# must use BSD awk
AWK="awk"
Expand Down
2 changes: 2 additions & 0 deletions src/evaluators/__tests__/source-4-3.test1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parse_and_run('function a(){const b = 2; return b;} a();');
// 2
7 changes: 7 additions & 0 deletions src/evaluators/__tests__/source-4-3.test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parse_and_run('function factorial(n) {\n' +
' return n === 1\n' +
' ? 1\n' +
' : factorial(n - 1) * n;\n' +
'}' +
'factorial(4);');
// 24
7 changes: 7 additions & 0 deletions src/evaluators/__tests__/source-4-3.test3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parse_and_run('function x() {\n' +
' let a = 1;\n' +
' return a;\n' +
' a = 2;\n' +
'}\n' +
'x();');
// 1
5 changes: 5 additions & 0 deletions src/evaluators/__tests__/source-4-3.test4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parse_and_run("function foo() {\
return 5;\
}\
foo();");
// 5
31 changes: 31 additions & 0 deletions src/evaluators/__tests__/source-4-3.test5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const result = parse_and_run(" \
function multiple_dwelling() { \
const baker = amb(1, 2, 3, 4, 5); \
const cooper = amb(1, 2, 3, 4, 5); \
const fletcher = amb(1, 2, 3, 4, 5); \
const miller = amb(1, 2, 3, 4, 5); \
const smith = amb(1, 2, 3, 4, 5); \
require(distinct(list(baker, cooper, fletcher, miller, smith))); \
require(! (baker === 5)); \
require(! (cooper === 1)); \
require(! (fletcher === 5)); \
require(! (fletcher === 1)); \
require(! (miller > cooper)); \
require(! ((math_abs(smith - fletcher)) === 1)); \
require(! ((math_abs(fletcher - cooper)) === 1)); \
return list(list('baker', baker), \
list('cooper', cooper), \
list('fletcher', fletcher), \
list('miller', miller), \
list('smith', smith)); \
} \
multiple_dwelling(); \
");

function print_one_line_list(li) {
let result = "";
for_each((s) => { result = result + head(s) + stringify(head(tail(s))) + ","; }, li);
return result;
}
print_one_line_list(result);
// 'baker1,cooper4,fletcher2,miller3,smith5,'
58 changes: 30 additions & 28 deletions src/evaluators/source-4-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ function analyze_sequence(stmts) {
a(env,
(a_value, fail2) => {
if (is_return_value(a_value)) {
return succeed(return_value_content(a_value), fail2);
return succeed(a_value, fail2);
} else {
return b(env, succeed, fail2);
}
Expand All @@ -613,8 +613,7 @@ function analyze_sequence(stmts) {
function analyze_block(stmts) {
const body = block_body(stmts);
const locals = local_names(body);
const temp_values = map(x => no_value_yet,
locals);
const temp_values = map(x => no_value_yet, locals);
return (env, succeed, fail) => analyze(body)(extend_environment(locals, temp_values, env), succeed, fail);
}

Expand Down Expand Up @@ -732,7 +731,6 @@ function execute_application(fun, args, succeed, fail) {
function_environment(fun)),
(return_value, fail2) => {
if (is_return_value(return_value)) {

return succeed(return_value_content(return_value),fail2);
} else {
return succeed(undefined,fail2);
Expand Down Expand Up @@ -844,32 +842,36 @@ function parse_and_run(str) {
// 'factorial(4);');

// parse_and_run('function x() {\n' +
// ' const a = 1;\n' +
// ' let a = 1;\n' +
// ' return a;\n' +
// ' a = 2;\n' +
// '}\n' +
// 'x();');

parse_and_run(" \
function multiple_dwelling() { \
const baker = amb(1, 2, 3, 4, 5); \
const cooper = amb(1, 2, 3, 4, 5); \
const fletcher = amb(1, 2, 3, 4, 5); \
const miller = amb(1, 2, 3, 4, 5); \
const smith = amb(1, 2, 3, 4, 5); \
require(distinct(list(baker, cooper, fletcher, miller, smith))); \
require(! (baker === 5)); \
require(! (cooper === 1)); \
require(! (fletcher === 5)); \
require(! (fletcher === 1)); \
require(! (miller > cooper)); \
require(! ((math_abs(smith - fletcher)) === 1)); \
require(! ((math_abs(fletcher - cooper)) === 1)); \
return list(list('baker', baker), \
list('cooper', cooper), \
list('fletcher', fletcher), \
list('miller', miller), \
list('smith', smith)); \
} \
multiple_dwelling(); \
");
// parse_and_run("function foo() {\
// return 5;\
// }\
// foo();");
// parse_and_run(" \
// function multiple_dwelling() { \
// const baker = amb(1, 2, 3, 4, 5); \
// const cooper = amb(1, 2, 3, 4, 5); \
// const fletcher = amb(1, 2, 3, 4, 5); \
// const miller = amb(1, 2, 3, 4, 5); \
// const smith = amb(1, 2, 3, 4, 5); \
// require(distinct(list(baker, cooper, fletcher, miller, smith))); \
// require(! (baker === 5)); \
// require(! (cooper === 1)); \
// require(! (fletcher === 5)); \
// require(! (fletcher === 1)); \
// require(! (miller > cooper)); \
// require(! ((math_abs(smith - fletcher)) === 1)); \
// require(! ((math_abs(fletcher - cooper)) === 1)); \
// return list(list('baker', baker), \
// list('cooper', cooper), \
// list('fletcher', fletcher), \
// list('miller', miller), \
// list('smith', smith)); \
// } \
// multiple_dwelling(); \
// ");