guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: local eval


From: Damien Mattei
Subject: Re: local eval
Date: Thu, 27 Apr 2023 11:45:10 +0200

finally i arrive to this solution that works:

;; now quoting all the operators ... so we are no more annoyed with macro
'bad syntax' error and also this  keep the 'and and 'or short-circuited
functionalities.

(define-macro (quote-all . Largs)
  (if (null? Largs)
      `(quote ,Largs)
      `(cons (quote ,(car Largs)) (quote-all ,@(cdr Largs)))))

;; but we then have to eval-uate all the expression at end

(define-macro ($nfx$ . Largs)
  `(local-eval (infix-with-precedence2prefix (! (quote-all ,@Largs)))
(the-environment)))

and with the magic re-export in module:

;; (use-modules (Scheme+))



(define-module (Scheme+)

  #:use-module (growable-vector)
  #:use-module (ice-9 local-eval)
  #:use-module (srfi srfi-1) ;; any,every
  #:use-module (srfi srfi-69) ;; Basic hash tables
  #:use-module (srfi srfi-31) ;; rec
  #:export (infix-with-precedence2prefix ! quote-all overload
overload-procedure overload-operator overload-function $nfx$ def
$bracket-apply$ <- ← -> → <+ ⥆ +> ⥅ declare $ $>  condx <> ≠ ** <v v> ⇜ ⇝
repeat % << >> & |)
  #:re-export (local-eval the-environment)
  #:replace (do when unless))



(include-from-path "def.scm")
(include-from-path "array.scm")
(include-from-path "set-values-plus.scm")
(include-from-path "apply-square-brackets.scm")
(include-from-path "assignment.scm")
(include-from-path "declare.scm")
(include-from-path "condx.scm")
(include-from-path "block.scm")
(include-from-path "not-equal.scm")
(include-from-path "exponential.scm")
(include-from-path "while-do-when-unless.scm")
(include-from-path "repeat-until.scm")
(include-from-path "scheme-infix.scm")
(include-from-path "overload.scm")
(include-from-path "modulo.scm")
(include-from-path "bitwise.scm")


but define-macro is not so much portable

Damien




On Wed, Apr 26, 2023 at 9:27 PM Damien Mattei <damien.mattei@gmail.com>
wrote:

> hello Mikael,
> hello Robby,
>
> yes my infix evaluator _with_ precedence is more complex than evaluating a
> single var. What became your macro Mikael in case of an expression? , in my
> original code ,now, i'm passing all vars quoted, so i need to evaluate the
> whole expression, because evaluating special form would fail (even if i can
> remove special form) ok it all start with $nfx$ from SRFI 105 that i have
> to define for operator precedence so there is some procedures  call by the
> $nfx$ macro, i skip the details,it looks like that:
>
> (define-syntax $nfx$
>   (syntax-rules ()
>
>     ((_ sexpr ...) (let ((env (the-environment)))
>
>     (compute (infix-with-precedence2prefix (! (quote sexpr) ...)) env)))))
>
> basically infix-with-precedence2prefix and ! and other procedures not
> listed do the infix precedence conversion to prefix scheme and 'compute' do
> the evaluation
>
> (define (compute expr env)
>   (display "evaluate : expr =") (display expr) (newline)
>   ;;(eval expr2eval (interaction-environment)))
>   (local-eval expr2eval env))
>
> i just need the good macro that finally replace the last line:
> (local-eval expr2eval env)
>
> evaluate the expression expr2eval in the environment env, example of
> expr2eval:
> (<+ x (<- y (+ (* 3 5) 2)))   ( was in infix SRFI 105: {x <+ y <- 3 * 5 +
> 2}  and in srfi 105 this expression is converted by the reader in: ($nfx$ x
> <+ y <- 3 * 5 + 2)  )
>
> so $nfx$ is here a macro that quote all the arguments to pass it to the
> procedures and finally the result is evaluated. (there is other strategies
> and version in my code that do only partial 'quoting or no 'quoting at all
> but the more powerful (can deal special form and easily operator
> overload,will be to quote all)
>
> Damien
>
>
> On Wed, Apr 26, 2023 at 7:25 PM Mikael Djurfeldt <mikael@djurfeldt.com>
> wrote:
>
>> (However, I suspect that you didn't *really* mean (quote var) in your
>> original code, or? If you didn't, then of course my simpler syntax-rules
>> macro isn't what you want.)
>>
>> On Wed, Apr 26, 2023 at 7:20 PM Mikael Djurfeldt <mikael@djurfeldt.com>
>> wrote:
>>
>>> How about:
>>>
>>> (define-syntax eval-var
>>>   (syntax-rules ()
>>>     ((_ var) var)))
>>>
>>> ?
>>>
>>> Then you don't need to import (ice-9 local-eval) and re-export things
>>> from there.
>>>
>>> Just for elucidation:
>>>
>>> The reason why we chose the syntax-case macro system for Guile was that
>>> it *both* gives you hygiene *and* quite a lot of control. The reason why
>>> the original code didn't work (your first post in this thread) is that
>>> "(the-environment)" was expanded in the lexical context where it was
>>> written (the Scheme+ module),
>>>
>>> However, syntax-case gives you the ability to insert that identifier
>>> into the context of the expansion instead:
>>>
>>> (define-syntax eval-var
>>>   (lambda (x)
>>>     (syntax-case x ()
>>>       ((_ var)
>>>        (with-syntax ((the-environment (datum->syntax #'var
>>> 'the-environment)))
>>>          #'(let ((env (the-environment)))
>>>               (local-eval (quote var) env)))))))
>>>
>>> (It's the datum->syntax form which gives the lexical context of var to
>>> the-environment.)
>>>
>>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]