Skip to content Skip to sidebar Skip to footer

Lisp Macros Quotation Implementation In Javascript

I have basic scheme like lisp in JavaScript and have problem with backquote and quote macros, they evaluate symbols if they are at first element of the array like > `(foo 10) g

Solution 1:

With help from @WillNess in comment I was able to resolve the problem, I need to add special case into the parser.

if (first instanceofSymbol) {
    value = env.get(first);
    if (value instanceofMacro) {
      value = value.invoke(rest, env);
      if (value instanceofConstant) {
        return value.value;
      }
      return evaluate(value, env);
    }
  ...

and quasiquote Macro wrap the output with Constant. which is just:

functionConstant(value) {
   this.value = value;
}

Solution 2:

The intent of the quasiquote is to return code that can be evaluated to return a new form. For example:

`(ab ,@list (,c ,d))

... might expand as:

(append (list 'a 'b) list (list (list c d)))

... so that, during evaluation, it produces the expected list. You could find some ways to avoid allocating lists that are actually constants, like '(a b) instead of (list 'a 'b), but this implies the value built from quasi-quotation cannot be guaranteed to be always modifiable.

However, you seem to be expanding macros at runtime, evaluating when necessary its nested forms, and so in your case, the returned list will be different each time. In that case, I believe the approach taken with Constant should work.

Post a Comment for "Lisp Macros Quotation Implementation In Javascript"