Skip to content

Commit b1ef2be

Browse files
committed
Merge remote-tracking branch 'upstream/master' into ding/mark-region-in-source-2
2 parents a305914 + b1a0541 commit b1ef2be

16 files changed

+309
-1965
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
node_modules
22

33
*~
4+
5+
**/output
6+

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ All programs in this repository are runnable in the [Source Academy playground](
99
The evaluators in this section all follow the general style of SICP JS Chapter 4.
1010

1111
- [`src/evaluators/source-0.js`](https://github.com/source-academy/source-programs/blob/master/src/evaluators/source-0.js): evaluator for Source §0 (calculator language)
12-
- [`src/evaluators/source-0pp.js`](https://github.com/source-academy/source-programs/blob/master/src/evaluators/source-0pp.js): evaluator for Source §0++ (calculator language plus conditionals, blocks and sequences)
13-
- [`src/evaluators/source-4-1.js`](https://github.com/source-academy/source-programs/blob/master/src/evaluators/source-4-1.js): evaluator for Source §1, described in SICP JS 4.1
12+
- [`src/evaluators/source-0-pp.js`](https://github.com/source-academy/source-programs/blob/master/src/evaluators/source-0-pp.js): evaluator for Source §0++ (calculator language plus conditionals, blocks and sequences)
13+
- [`src/evaluators/source-2.js`](https://github.com/source-academy/source-programs/blob/master/src/evaluators/source-2.js): evaluator for Source §2, described in SICP JS 4.1
14+
- [`src/evaluators/source-2-lazy.js`](https://github.com/source-academy/source-programs/blob/master/src/evaluators/source-2-lazy.js): lazy evaluator for Source §2, described in SICP JS 4.2
15+
- [`src/evaluators/source-2-non-det.js`](https://github.com/source-academy/source-programs/blob/master/src/evaluators/source-2-non-det.js): evaluator for Source §2 with non-determinism, described in SICP JS 4.3
1416
- [`src/evaluators/typed-source.js`](https://github.com/source-academy/source-programs/blob/master/src/evaluators/typed-source.js): evaluator for Typed Source (typed version of a Source §1 sublanguage)
15-
- [`src/evaluators/source-4-3.js`](https://github.com/source-academy/source-programs/blob/master/src/evaluators/source-4-3.js): meta-circular evaluator for Source §4.3 (non-deterministic programming)
1617

1718
## Steppers
1819

@@ -68,6 +69,8 @@ The virtual machines in this section are SECD-style and follow a description in
6869

6970
# Testing
7071

72+
[requires bash (any version) and awk (BSD awk 20070501); does not work with gawk]
73+
7174
For testing your Source programs, you need `node` and `yarn`.
7275

7376
Write your test cases in a folder `__tests__` in each `src` subfolder. The name of the file specifies the targeted Source of your test case. For example, if `src/steppers/source-0.js` is the Source, a test case might be `src/steppers/__tests__/source-0.test1.js`.

scripts/test.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#! /usr/bin/env bash
22

3-
JS_SLANG="node node_modules/js-slang/dist/repl/repl.js"
3+
JS_SLANG="node --stack-size=2000 node_modules/js-slang/dist/repl/repl.js"
4+
5+
# must use BSD awk
6+
AWK="awk"
47

58
SOURCEFILES=src/*/*.js
69
SOURCE_TEST="src/test/framework/main.js"
@@ -53,13 +56,13 @@ test_source_framework() {
5356
do
5457
passed=$(($passed+1))
5558
echo "${green}PASS $2 $test_name"
56-
done < <(echo ${RESULTS} | grep -o '\w* PASSED' | awk -F 'PASSED' '{ print $1 }')
59+
done < <(echo ${RESULTS} | grep -o '\w* PASSED' | $AWK -F 'PASSED' '{ print $1 }')
5760

5861
# retrieve names and error messages for tests that failed
5962
while read test_info
6063
do
6164
failed=$(($failed+1))
62-
echo $test_info | awk -F 'FAILED:' '{ print $1 ":" $2 }' | awk -F '"' '{ print $1 $2 }' |
65+
echo $test_info | $AWK -F 'FAILED:' '{ print $1 ":" $2 }' | $AWK -F '"' '{ print $1 $2 }' |
6366
while read test_name test_error
6467
do echo "${red}FAIL $2 $test_name $test_error";
6568
done
@@ -78,13 +81,13 @@ main() {
7881
do
7982
if [ -f "$i" ]; then
8083
# check if first line of test file contains 'chapter=' and retrieve its value. Set to the default chapter if it does not
81-
chapter=$(awk -F 'chapter=' 'FNR==1{ if ($0~"chapter=") { print $2 } else { print '$DEFAULT_CHAPTER' } }' $i | awk -F ' ' '{ print $1 }')
84+
chapter=$($AWK -F 'chapter=' 'FNR==1{ if ($0~"chapter=") { print $2 } else { print '$DEFAULT_CHAPTER' } }' $i | $AWK -F ' ' '{ print $1 }')
8285

8386
# check if first line of test file contains 'variant=' and retrieve its value. Set to the default variant if it does not
84-
variant=$(awk -F 'variant=' 'FNR==1{ if ($0~"variant=") { print $2 } else { print '$DEFAULT_VARIANT' } }' $i | awk -F ' ' '{ print $1 }')
87+
variant=$($AWK -F 'variant=' 'FNR==1{ if ($0~"variant=") { print $2 } else { print '$DEFAULT_VARIANT' } }' $i | $AWK -F ' ' '{ print $1 }')
8588

8689
# check if first line of test file contains 'framework'
87-
use_source_test=$(awk 'FNR==1{ if ($0~"framework") print "yes" }' $i)
90+
use_source_test=$($AWK 'FNR==1{ if ($0~"framework") print "yes" }' $i)
8891
if [[ $use_source_test == "yes" ]]
8992
then chapter=4 ; test_source_framework $s $i $chapter $variant
9093
else test_source $s $i $chapter $variant
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parse_and_evaluate("function f(a,b) { return a === 1 ? a : b; } f(1, head(null));");
2+
// 1
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
parse_and_eval("function at(xs, n) {\
2+
return n === 0\
3+
? head(xs)\
4+
: at(tail(xs), n-1);\
5+
}\
6+
function intsfrom(n) {\
7+
return pair(n, intsfrom(n+1));\
8+
}\
9+
function zipWith(f, xs, ys) {\
10+
return xs === null\
11+
? null\
12+
: ys === null\
13+
? null\
14+
: pair(f(head(xs), head(ys)), zipWith(f, tail(xs), tail(ys)));\
15+
}\
16+
const facs = pair(1, zipWith((x, y) => x * y, intsfrom(1), facs));\
17+
function fac(n) {\
18+
return at(facs, n);\
19+
}\
20+
fac(6);");
21+
// 720
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const list_func = "function list (x,y) {\
2+
return pair (x,pair (y,null));}";
3+
const map_func = "function map (f,xs) {\
4+
return xs === null ? xs\
5+
: pair (f(head(xs)), map (f,tail (xs)));\
6+
}";
7+
const test2 = map_func + "const a = pair(2,a); const b = map (x => x * x, a);head(tail(b));";
8+
parse_and_eval(test2);
9+
// 4
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parse_and_run('function a(){const b = 2; return b;} a();');
2+
// 2
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parse_and_run('function factorial(n) {\n' +
2+
' return n === 1\n' +
3+
' ? 1\n' +
4+
' : factorial(n - 1) * n;\n' +
5+
'}' +
6+
'factorial(4);');
7+
// 24
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parse_and_run('function x() {\n' +
2+
' let a = 1;\n' +
3+
' return a;\n' +
4+
' a = 2;\n' +
5+
'}\n' +
6+
'x();');
7+
// 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parse_and_run("function foo() {\
2+
return 5;\
3+
}\
4+
foo();");
5+
// 5

0 commit comments

Comments
 (0)