Adding primitives in metacircular evaluator

503 Views Asked by At

I am working on the metacircular evaluator, and I'm trying to add primitive procedures. I am almost done, except I'm not sure how to add the error. Here is what I have so far:

(define primitive-procedures
  (list (list 'car car)
        (list 'cdr cdr)
        (list 'cons cons)
        (list 'null? null?)
        (list '+ +)
        (list '* *)
        (list '- -)
        (list '/ /)
        (list '< <)
        (list '<= <=)
        (list '= =)
        (list '>= >=)
        (list '> >)))

This works so far. I tried adding (list '(error) (error "Metacircular Interpreter Aborted")) for error, but it's obviously not working... How do I go about that?

Thanks!

3

There are 3 best solutions below

0
On BEST ANSWER

It's the same as with the other primitives, you just have to add it like this:

(list 'error error)
0
On

There is no difference than with the other primitives.

 (define primitive-procedures
  (list (list 'car car)
        ...
        (list '> >)
        (list 'error error)))

As with the all the others the arity is checked in the underlying implementation. That means that you need to supply an argument eg. (error "something bad happened") will work from the interpreter. From the try to use (error) I'm guessing you are expecting to use it without an argument you need to supply a procedure that takes no arguments. Here is how I would have done that:

 (define (error-primitive)
   (error "Metacircular Interpreter Aborted"))

 (define primitive-procedures
  (list (list 'car car)
        ...
        (list '> >)
        (list 'error error-primitive)))

Now when you call (error) it will call the lambda and it will call (error "Metacircular Interpreter Aborted"). You can also just put a lambda in the primitive-procedures definition, but if you are doing a more data driver version of the interpreter later giving it a name will help with that since at this moment it is treated the same as >.

0
On

When you advance something more, you will learn how to catch errors in the target language, without using the error from the source language.

This can be done using concepts like monads, current-continuation, continuation passing style, control with shift/reset, etc.