Skip to content

Commit 09a740e

Browse files
authoredJun 16, 2021
Make SICP compact and resolve conflict (#1011)
* make sicp compact * Fix sicp-prepare.ts * fix format
1 parent f85225d commit 09a740e

File tree

6 files changed

+32
-47
lines changed

6 files changed

+32
-47
lines changed
 

‎scripts/build_sicp_package.sh

+5-29
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,23 @@ write() {
3232
echo "\"use strict\";" >> $SICPJSPATH
3333
echo "Object.defineProperty(exports, \"__esModule\", { value: true });" >> $SICPJSPATH
3434
echo "const createContext_1 = require(\"./createContext\");" >> $SICPJSPATH
35-
echo "createContext_1.default(4);" >> $SICPJSPATH
36-
echo "const dict = createContext_1.dict;" >> $SICPJSPATH
35+
echo "const dict = createContext_1.default(4).nativeStorage.globals.variables;" >> $SICPJSPATH
3736

3837
cat sicp_publish/prelude.txt >> $SICPJSPATH
3938

40-
# Write Prelude
41-
COUNT=0
4239

40+
#Write prelude names
4341
while read -r CURRENT_LINE
4442
do
45-
IFS=' ' read -r -a array <<< "$CURRENT_LINE"
46-
47-
if [ "${array[0]}" == "function" ]
48-
then
49-
if [ $COUNT -le 0 ]
50-
then
51-
IFS='(' read -r -a name <<< "${array[1]}"
52-
echo "global.${name[0]} = ${name[0]};" >> $SICPJSPATH
53-
fi
54-
fi
55-
56-
if [ ${#array[@]} != 0 ]
57-
then
58-
if [ "${array[${#array[@]} - 1]}" == "{" ]
59-
then
60-
((COUNT++))
61-
fi
62-
fi
63-
64-
if [ "${array[0]}" == "}" ]
65-
then
66-
((COUNT--))
67-
fi
68-
done < "sicp_publish/prelude.txt"
43+
echo "global.$CURRENT_LINE = $CURRENT_LINE;" >> $SICPJSPATH
44+
done < "sicp_publish/prelude_names.txt"
6945

7046
#Write Functions
7147
while read -r CURRENT_LINE
7248
do
7349
if [ "$CURRENT_LINE" != "undefined" -a "$CURRENT_LINE" != "NaN" -a "$CURRENT_LINE" != "Infinity" ]
7450
then
75-
echo "global.$CURRENT_LINE = dict[\"$CURRENT_LINE\"];" >> $SICPJSPATH
51+
echo "global.$CURRENT_LINE = dict.get(\"$CURRENT_LINE\").getValue();" >> $SICPJSPATH
7652
fi
7753
done < "sicp_publish/names.txt"
7854
}

‎sicp_publish/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
names.txt
22
prelude.txt
3+
prelude_names.txt
34
package-lock.json

‎sicp_publish/package.json

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sicp",
3-
"version": "1.0.19",
3+
"version": "1.0.21",
44
"license": "Apache-2.0",
55
"description": "module to run programs of SICP JS in node.js",
66
"keywords": [
@@ -26,10 +26,5 @@
2626
"types": "dist/sicp",
2727
"files": [
2828
"dist"
29-
],
30-
"author": {
31-
"name": "Source Academy",
32-
"url": "https://github.com/source-academy/"
33-
},
34-
"license": "Apache-2.0"
29+
]
3530
}

‎src/createContext.ts

-4
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,7 @@ export const ensureGlobalEnvironmentExist = (context: Context) => {
162162
}
163163
}
164164

165-
export const dict = {}
166-
167165
export const defineSymbol = (context: Context, name: string, value: Value) => {
168-
dict[name] = value
169-
170166
const globalEnvironment = context.runtime.environments[0]
171167
Object.defineProperty(globalEnvironment.head, name, {
172168
value,

‎src/sicp-prepare.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import { dict, default as createContext } from './createContext'
1+
import { default as createContext } from './createContext'
22
import * as fs from 'fs'
3+
import { Program, FunctionDeclaration } from 'estree'
4+
import { parse } from 'acorn'
35

46
const context = createContext(4)
57

6-
const a = Object.getOwnPropertyNames(dict)
8+
// Generate names.txt
9+
const a = context.nativeStorage.builtins.keys()
710

811
const names_file = fs.createWriteStream('sicp_publish/names.txt')
912

1013
names_file.on('error', function (e: Error) {
1114
console.log(e)
1215
})
1316

14-
a.forEach(function (v) {
15-
names_file.write(v + '\n')
16-
})
17+
for (const name of a) {
18+
names_file.write(name + '\n')
19+
}
1720

1821
names_file.end()
1922

23+
// Generate prelude.txt
2024
const prelude_file = fs.createWriteStream('sicp_publish/prelude.txt')
2125

2226
prelude_file.on('error', function (e: Error) {
@@ -26,3 +30,18 @@ prelude_file.on('error', function (e: Error) {
2630
prelude_file.write(context.prelude)
2731

2832
prelude_file.end()
33+
34+
// Generate prelude_names.txt
35+
const b = parse(context.prelude || '', { ecmaVersion: 2020 }) as unknown as Program
36+
37+
const prelude_names = fs.createWriteStream('sicp_publish/prelude_names.txt')
38+
39+
prelude_names.on('error', function (e: Error) {
40+
console.log(e)
41+
})
42+
43+
b.body
44+
.map(node => (node as FunctionDeclaration).id?.name)
45+
.map(name => prelude_names.write(name + '\n'))
46+
47+
prelude_names.end()

‎src/stdlib/README.md

-2
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.