fez-lisp 1.0.7 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "fez-lisp",
3
- "description": "Immutable Lisp interpreted & compiled to JavaScript",
3
+ "description": "Lisp interpreted & compiled to JavaScript",
4
4
  "author": "AT290690",
5
- "version": "1.0.7",
5
+ "version": "1.0.9",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/compiler.js CHANGED
@@ -17,12 +17,20 @@ while(typeof result==='function')result=result()
17
17
  return result
18
18
  }`,
19
19
  isNumber: `numberPredicate=(number)=>typeof number==='number'`,
20
- isString: `stringPredicate=(string)=>typeof number==='string'`,
20
+ isString: `stringPredicate=(string)=>typeof string==='string'`,
21
21
  isLambda: `lambdaPredicate=(lambda)=>typeof lambda==='function'`,
22
22
  isArray: `arrayPredicate=(array)=>Array.isArray(array)`,
23
23
  isAtom: `atomPredicate=(value)=>typeof value==='number'||typeof value==='string'`,
24
24
  error: `_error=(error)=>{
25
25
  throw new Error(error)
26
+ }`,
27
+ set: `set=(array,index,value)=>{
28
+ array=[...array]
29
+ if (index < 0) {
30
+ const target = array.length + index
31
+ while (array.length !== target) array.pop()
32
+ } else array[index] = value;
33
+ return array
26
34
  }`,
27
35
  setArray: `setEffect=(array,index,value)=>{
28
36
  if (index < 0) {
@@ -46,9 +54,9 @@ return result
46
54
  case '${KEYWORDS.ANONYMOUS_FUNCTION}':
47
55
  return ()=>value
48
56
  case '${KEYWORDS.CHAR_CODE_TYPE}':
49
- return String.fromCharCode(value)
50
- case '${KEYWORDS.CHAR_TYPE}':
51
57
  return value.charCodeAt(0)
58
+ case '${KEYWORDS.CHAR_TYPE}':
59
+ return String.fromCharCode(value)
52
60
  default:
53
61
  return 0
54
62
  }
@@ -234,6 +242,9 @@ const compile = (tree, Variables) => {
234
242
  : `(${parseArgs(Arguments, Variables, token)});`
235
243
  : `(0);`
236
244
  case KEYWORDS.ADDITION:
245
+ return Arguments.length
246
+ ? `(${parseArgs(Arguments, Variables, token)});`
247
+ : `(0);`
237
248
  case KEYWORDS.BITWISE_AND:
238
249
  case KEYWORDS.BITWISE_OR:
239
250
  case KEYWORDS.BITWISE_XOR:
@@ -309,6 +320,8 @@ const compile = (tree, Variables) => {
309
320
  }
310
321
  case KEYWORDS.SERIALISE:
311
322
  return `_serialise(${compile(Arguments[0], Variables)});`
323
+ case KEYWORDS.SET_IMMUTABLE_ARRAY:
324
+ return `set(${parseArgs(Arguments, Variables)});`
312
325
  case KEYWORDS.SET_ARRAY:
313
326
  return `setEffect(${parseArgs(Arguments, Variables)});`
314
327
  case KEYWORDS.NOT_COMPILED_BLOCK:
package/src/enums.js CHANGED
@@ -76,4 +76,5 @@ export const KEYWORDS = {
76
76
  SERIALISE: 'serialise',
77
77
 
78
78
  SET_ARRAY: 'set!',
79
+ SET_IMMUTABLE_ARRAY: 'set',
79
80
  }
@@ -9,18 +9,18 @@ export const evaluate = (exp, env) => {
9
9
  case WORD: {
10
10
  const word = env[first[VALUE]]
11
11
  if (word == undefined)
12
- throw new ReferenceError(`Undefined variable ${first[VALUE]}.`)
12
+ throw new ReferenceError(`Undefined variable ${first[VALUE]}`)
13
13
  return word
14
14
  }
15
15
  case APPLY:
16
16
  const apply = env[first[VALUE]]
17
17
  if (apply == undefined)
18
18
  throw new ReferenceError(
19
- `Undefined (${KEYWORDS.ANONYMOUS_FUNCTION}) ${first[VALUE]}.`
19
+ `Undefined (${KEYWORDS.ANONYMOUS_FUNCTION}) ${first[VALUE]}`
20
20
  )
21
21
  if (typeof apply !== 'function')
22
22
  throw new TypeError(
23
- `${first[VALUE]} is not a (${KEYWORDS.ANONYMOUS_FUNCTION}).`
23
+ `${first[VALUE]} is not a (${KEYWORDS.ANONYMOUS_FUNCTION})`
24
24
  )
25
25
  return apply(rest, env)
26
26
  case ATOM:
package/src/parser.js CHANGED
@@ -46,7 +46,8 @@ export const parse = (source) => {
46
46
  export const stringify = (ast) => {
47
47
  if (ast == undefined) return '()'
48
48
  else if (typeof ast === 'object')
49
- if (Array.isArray(ast)) return `(array ${ast.map(stringify).join(' ')})`
49
+ if (Array.isArray(ast))
50
+ return ast.length ? `(array ${ast.map(stringify).join(' ')})` : '()'
50
51
  else
51
52
  return `(array ${Object.entries(ast)
52
53
  .map(([key, value]) => `("${key}" ${stringify(value)})`)