fez-lisp 1.1.11 → 1.1.12

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "fez-lisp",
3
3
  "description": "Lisp interpreted & compiled to JavaScript",
4
4
  "author": "AT290690",
5
- "version": "1.1.11",
5
+ "version": "1.1.12",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/compiler.js CHANGED
@@ -66,7 +66,6 @@ const lispToJavaScriptVariableName = (name) =>
66
66
  )
67
67
  )
68
68
  const Helpers = {
69
- __string: `__string=(...args)=>{const str=args.flat();str.isString=true;return str}`,
70
69
  __add: `__add=(...numbers)=>{return numbers.reduce((a,b)=>a+b,0)}`,
71
70
  __sub: `__sub=(...numbers)=>{return numbers.reduce((a,b)=>a-b,0)}`,
72
71
  __mult: `__mult=(...numbers)=>{return numbers.reduce((a,b)=>a*b,1)}`,
@@ -95,6 +94,7 @@ const Helpers = {
95
94
  return args.at(-1) ? 1 : 0
96
95
  }`,
97
96
  logEffect: `logEffect=(msg)=>{console.log(msg);return msg}`,
97
+ logStringEffect: `logStringEffect=(msg)=>{console.log(msg.map(x=>String.fromCharCode(x)).join(''));return msg}`,
98
98
  clearEffect: `clearEffect=()=>{console.clear();return 0}`,
99
99
  array_cons: `array_cons=(A, ...B)=> B.reduce((a, b) => a.concat(b), A)`,
100
100
  car: 'car=(arr)=>arr.at(0)',
@@ -105,7 +105,6 @@ const Helpers = {
105
105
  numberPredicate: `numberPredicate=(number)=>+(typeof number==='number')`,
106
106
  lambdaPredicate: `lambdaPredicate=(lambda)=>+(typeof lambda==='function')`,
107
107
  arrayPredicate: `arrayPredicate=(array)=>+Array.isArray(array)`,
108
- error: `error=(error)=>{throw new Error(error)}`,
109
108
  array_setEffect: `array_setEffect=(array,index,value)=>{if(index<0){const target=array.length+index;while(array.length!==target)array.pop()}else array[index] = value;return array}`
110
109
  }
111
110
  const semiColumnEdgeCases = new Set([
@@ -197,11 +197,6 @@ const keywords = {
197
197
  if (evaluate(args[i], env)) return evaluate(args[i + 1], env)
198
198
  return 0
199
199
  },
200
- [KEYWORDS.STRING_TYPE]: (args, env) => {
201
- const str = args.flatMap((x) => evaluate(x, env))
202
- str.isString = true
203
- return str
204
- },
205
200
  [KEYWORDS.ARRAY_TYPE]: (args, env) => {
206
201
  if (!args.length) return []
207
202
  const isCapacity =
@@ -906,15 +901,26 @@ const keywords = {
906
901
  return array
907
902
  },
908
903
  [KEYWORDS.LOG]: (args, env) => {
909
- if (!args.length)
904
+ if (args.length !== 1)
910
905
  throw new RangeError(
911
- `Invalid number of arguments to (${KEYWORDS.LOG}) (>= 1 required) (${
906
+ `Invalid number of arguments to (${KEYWORDS.LOG}) (= 1 required) (${
912
907
  KEYWORDS.LOG
913
908
  } ${stringifyArgs(args)})`
914
909
  )
915
- const expressions = args.map((x) => evaluate(x, env))
916
- console.log(...expressions)
917
- return expressions.at(-1)
910
+ const expression = evaluate(args[0], env)
911
+ console.log(expression)
912
+ return expression
913
+ },
914
+ [KEYWORDS.LOG_STRING]: (args, env) => {
915
+ if (args.length !== 1)
916
+ throw new RangeError(
917
+ `Invalid number of arguments to (${
918
+ KEYWORDS.LOG_STRING
919
+ }) (= 1 required) (${KEYWORDS.LOG_STRING} ${stringifyArgs(args)})`
920
+ )
921
+ const expression = evaluate(args[0], env)
922
+ console.log(expression.map((x) => String.fromCharCode(x)).join(''))
923
+ return expression
918
924
  },
919
925
  [KEYWORDS.CLEAR_CONSOLE]: (args) => {
920
926
  if (args.length)
package/src/keywords.js CHANGED
@@ -6,7 +6,6 @@ export const ATOM = 2
6
6
  export const PLACEHOLDER = '.'
7
7
  export const KEYWORDS = {
8
8
  NUMBER_TYPE: 'number',
9
- STRING_TYPE: 'string',
10
9
  ARRAY_TYPE: 'array',
11
10
  ARRAY_LENGTH: 'length',
12
11
  IS_ARRAY: 'array?',
@@ -50,6 +49,7 @@ export const KEYWORDS = {
50
49
  TEST_CASE: 'case',
51
50
  TEST_BED: 'assert',
52
51
  LOG: 'log!',
52
+ LOG_STRING: 'log-string!',
53
53
  CLEAR_CONSOLE: 'clear!',
54
54
  SET_ARRAY: 'array:set!',
55
55
  DOC: 'fez-manual'
package/src/utils.js CHANGED
@@ -12,7 +12,7 @@ export const replaceStrings = (source) => {
12
12
  for (const q of quotes)
13
13
  source = source.replaceAll(
14
14
  q,
15
- `(string ${[...q]
15
+ `(array ${[...q]
16
16
  .slice(1, -1)
17
17
  .map((x) => x.charCodeAt(0))
18
18
  .join(' ')})`
@@ -134,7 +134,7 @@ export const handleUnbalancedQuotes = (source) => {
134
134
  }
135
135
  export const removeMutation = (source) => source.replace(new RegExp(/!/g), 'ǃ')
136
136
  export const treeShake = (ast, libs) => {
137
- const deps = libs.reduce((a, x) => a.add(x.at(1)[VALUE]), new Set())
137
+ const deps = libs.reduce((a, x) => a.set(x.at(1)[VALUE], x), new Map())
138
138
  const visited = new Set()
139
139
  const dfs = (tree) => {
140
140
  if (!isLeaf(tree)) tree.forEach(dfs)
@@ -145,13 +145,14 @@ export const treeShake = (ast, libs) => {
145
145
  ) {
146
146
  visited.add(tree[VALUE])
147
147
  // Recursively explore the dependencies of the current node
148
- const dependency = libs.find((x) => x.at(1)[VALUE] === tree[VALUE])
148
+ const dependency = deps.get(tree[VALUE])
149
149
  if (dependency) dfs(dependency.at(-1))
150
150
  }
151
151
  }
152
152
  dfs(ast)
153
153
  // Filter out libraries that are not in the visited set
154
- return libs.filter((x) => visited.has(x.at(1)[VALUE]))
154
+ // return libs.filter((x) => visited.has(x.at(1)[VALUE]))
155
+ return [...visited].reverse().map((x) => deps.get(x))
155
156
  }
156
157
  export const dfs = (tree, callback) => {
157
158
  if (!isLeaf(tree)) for (const leaf of tree) dfs(leaf)