简体   繁体   English

在元循环求值器中添加基元

[英]Adding primitives in metacircular evaluator

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?我尝试添加(list '(error) (error "Metacircular Interpreter Aborted"))以获取错误,但它显然不起作用......我该怎么做?

Thanks!谢谢!

它与其他原语相同,您只需像这样添加它:

(list 'error error)

When you advance something more, you will learn how to catch errors in the target language, without using the error from the source language.当您进一步推进时,您将学习如何在不使用源语言的error情况下捕获目标语言中的error

This can be done using concepts like monads, current-continuation, continuation passing style, control with shift/reset, etc.这可以使用诸如 monads、当前延续、延续传递风格、带有移位/重置的控制等概念来完成。

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.与所有其他人一样,在底层实现中检查 arity。 That means that you need to supply an argument eg.这意味着您需要提供一个参数,例如。 (error "something bad happened") will work from the interpreter. (error "something bad happened")将从解释器中工作。 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.从尝试使用(error)我猜你希望在没有参数的情况下使用它,你需要提供一个不带参数的过程。 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") .现在,当您调用(error) ,它将调用 lambda 并调用(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 > .您也可以在primitive-procedures定义中放置一个 lambda,但是如果您正在执行解释器的更多数据驱动程序版本,则稍后为其命名将有助于解决此问题,因为此时它被视为与>相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM