fez-lisp 1.1.23 → 1.1.24

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.23",
5
+ "version": "1.1.24",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/compiler.js CHANGED
@@ -223,14 +223,18 @@ const compile = (tree, Drill) => {
223
223
  const vars = InnerDrills.Variables.size
224
224
  ? `var ${[...InnerDrills.Variables].join(',')};`
225
225
  : ''
226
- return `((${parseArgs(
226
+ const args = parseArgs(
227
227
  functionArgs.map((node, index) =>
228
228
  node[VALUE] === PLACEHOLDER
229
229
  ? leaf(node[TYPE], `_${index}`)
230
230
  : leaf(node[TYPE], node[VALUE])
231
231
  ),
232
232
  InnerDrills
233
- )})=>{${vars}return ${evaluatedBody.toString().trimStart()}});`
233
+ )
234
+ // const $ = [${args}];
235
+ return `((${args})=>{${vars}return ${evaluatedBody
236
+ .toString()
237
+ .trimStart()}});`
234
238
  }
235
239
  case KEYWORDS.AND:
236
240
  return `((${parseArgs(Arguments, Drill, '&&')}) ? 1 : 0);`
@@ -317,9 +317,11 @@ const keywords = {
317
317
  } ${stringifyArgs(args)})`
318
318
  )
319
319
  const localEnv = Object.create(env)
320
+ // localEnv[KEYWORDS.BLOCK] = block[KEYWORDS.BLOCK]
320
321
  for (let i = 0; i < props.length; ++i) {
322
+ const value = evaluate(props[i], scope)
321
323
  Object.defineProperty(localEnv, params[i][VALUE], {
322
- value: evaluate(props[i], scope),
324
+ value,
323
325
  writable: true
324
326
  })
325
327
  }
@@ -902,7 +904,16 @@ keywords[KEYWORDS.DOC] = (args, env) => {
902
904
  .map((x) => `(${x.join(' ')})`)
903
905
  }
904
906
  }
907
+
905
908
  export const deSuggar = (ast) => {
909
+ if (ast.length === 0)
910
+ throw new SyntaxError(
911
+ `Top level ${KEYWORDS.NUMBER_TYPE} need to be wrapped in a (${KEYWORDS.IDENTITY})`
912
+ )
913
+ // for (const node of ast)
914
+ // if (node[0] && node[0][TYPE] === APPLY && node[0][VALUE] === KEYWORDS.BLOCK)
915
+ // throw new SyntaxError(`Top level (${KEYWORDS.BLOCK}) is not allowed`)
916
+ let prev = undefined
906
917
  const evaluate = (exp) => {
907
918
  const [first, ...rest] = isLeaf(exp) ? [exp] : exp
908
919
  if (first != undefined) {
@@ -914,6 +925,33 @@ export const deSuggar = (ast) => {
914
925
  case APPLY:
915
926
  {
916
927
  switch (first[VALUE]) {
928
+ case KEYWORDS.BLOCK:
929
+ {
930
+ if (
931
+ prev == undefined ||
932
+ (prev &&
933
+ prev[TYPE] === APPLY &&
934
+ prev[VALUE] !== KEYWORDS.ANONYMOUS_FUNCTION)
935
+ )
936
+ throw new SyntaxError(
937
+ `Can only use (${KEYWORDS.BLOCK}) as a body of a (${KEYWORDS.ANONYMOUS_FUNCTION})`
938
+ )
939
+ }
940
+ break
941
+ // case KEYWORDS.DEFINE_VARIABLE:
942
+ // {
943
+ // if (
944
+ // rest[1] &&
945
+ // rest[1][0] &&
946
+ // rest[1][0][TYPE] === APPLY &&
947
+ // rest[1][0][VALUE] === KEYWORDS.BLOCK
948
+ // ) {
949
+ // throw new SyntaxError(
950
+ // `Can't use (${KEYWORDS.BLOCK}) in (${KEYWORDS.DEFINE_VARIABLE})`
951
+ // )
952
+ // }
953
+ // }
954
+ break
917
955
  case KEYWORDS.PIPE:
918
956
  {
919
957
  if (rest.length < 1)
@@ -957,6 +995,7 @@ export const deSuggar = (ast) => {
957
995
  }
958
996
  break
959
997
  }
998
+ prev = first
960
999
  }
961
1000
  break
962
1001
  default:
package/src/keywords.js CHANGED
@@ -5,9 +5,9 @@ export const WORD = 1
5
5
  export const ATOM = 2
6
6
  export const PLACEHOLDER = '.'
7
7
  export const KEYWORDS = {
8
- RECURSION: 'rec',
9
8
  NUMBER_TYPE: 'number',
10
9
  ARRAY_TYPE: 'array',
10
+ IDENTITY: 'identity',
11
11
  ARRAY_LENGTH: 'length',
12
12
  IS_ATOM: 'atom?',
13
13
  ADDITION: '+',
package/src/utils.js CHANGED
@@ -233,10 +233,6 @@ export const fez = (source, options = {}) => {
233
233
  if (!options.mutation) code = removeMutation(code)
234
234
  if (!code.length && options.throw) throw new Error('Nothing to parse!')
235
235
  const parsed = deSuggar(LISP.parse(code))
236
- if (parsed.length === 0 && options.throw)
237
- throw new Error(
238
- 'Top level expressions need to be wrapped in a (do) block'
239
- )
240
236
  const ast = [...treeShake(parsed, std), ...parsed]
241
237
  // if (options.check) typeCheck(ast)
242
238
  if (options.compile) {