Nested `package` in macros doesn't expand.

86 views
Skip to first unread message

Ramil Farkhshatov

unread,
Jun 23, 2015, 5:13:20 AM6/23/15
to qil...@googlegroups.com
Hello.

I encountered some nested `package` forms being left after
macro-expansion which is causing host language error:

\\ The code itself doesn't mean anything: it was made up to demonstrate
\\ the issue
(5-) (defmacro x.global
[x.global Var] -> (let Get-name Var
Set-name (concat Var ->)
[package null []
[define Get-name -> [value Var]]
[define Set-name
(protect X) -> [set Var (protect X)]]]))
x.global

(6-) (defmacro x.globals
[x.globals | Vars] -> [package null []
| (map (/. X [x.global X]) Vars)])

x.globals

(7-) (x.global some-global)
some-global
some-global->

(8-) (x.globals g1 g2 g3)
The function COMMON-LISP-USER::package is undefined.

Bruno Deferrari

unread,
Jul 19, 2015, 4:29:21 PM7/19/15
to qil...@googlegroups.com
Were you able to solve this?

I played a bit with the code in reader.shen and came up with this change. It works for your specific use-case, I may be missing other cases.

Changes marked in green:

(define fold-right
  F [] Acc -> Acc
  F [X | Rest] Acc -> (F X (fold-right F Rest Acc)))


(define package-macro
  [$ S] Stream -> (append (explode S) Stream)
  [package null _ | Code] Stream ->
    (fold-right (function package-macro) Code Stream) \* CHANGED HERE *\

  [package PackageName Exceptions | Code] Stream
  -> (let ListofExceptions (eval-without-macros Exceptions)
          External (record-exceptions ListofExceptions PackageName)
          PackageNameDot (intern (cn (str PackageName) "."))
          ExpPackageName (explode PackageName)
          Packaged (packageh PackageNameDot ListofExceptions Code ExpPackageName)
          Internal (record-internal PackageName (internal-symbols ExpPackageName Packaged))
       (append Packaged Stream))
  X Stream -> [X | Stream])




--
You received this message because you are subscribed to the Google Groups "Shen" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qilang+un...@googlegroups.com.
To post to this group, send an email to qil...@googlegroups.com.
Visit this group at http://groups.google.com/group/qilang.
For more options, visit https://groups.google.com/d/optout.



--
BD

Ramil Farkhshatov

unread,
Jul 19, 2015, 4:43:54 PM7/19/15
to uti...@gmail.com, qil...@googlegroups.com
Bruno Deferrari <uti...@gmail.com> wrote:

> Were you able to solve this?

I've solved that before posting bugreport. But I was hoping for a fix in
Shen core.

(define unwrap-package-null
[] Acc -> (reverse Acc)
[[package null _ | Code] | Xs] Acc -> (let Acc (append (reverse Code) Acc)
(unwrap-package-null Xs Acc))
[X | Xs] Acc -> (unwrap-package-null Xs [X | Acc]))

(defmacro x.package-null
[package Pkg Syms | X] -> [package Pkg Syms
| (unwrap-package-null X [])])


> I played a bit with the code in reader.shen and came up with this change.
> It works for your specific use-case, I may be missing other cases.
>
> Changes marked in green:

Sorry, I don't see colors (I'm using text-based MUA).

Bruno Deferrari

unread,
Jul 19, 2015, 4:48:33 PM7/19/15
to Ramil Farkhshatov, qil...@googlegroups.com
On Sun, Jul 19, 2015 at 5:43 PM, Ramil Farkhshatov <ra...@gmx.co.uk> wrote:
Bruno Deferrari <uti...@gmail.com> wrote:

> Were you able to solve this?

I've solved that before posting bugreport. But I was hoping for a fix in
Shen core.

    (define unwrap-package-null
      [] Acc -> (reverse Acc)
      [[package null _ | Code] | Xs] Acc -> (let Acc (append (reverse Code) Acc)
                                              (unwrap-package-null Xs Acc))
      [X | Xs] Acc -> (unwrap-package-null Xs [X | Acc]))

    (defmacro x.package-null
      [package Pkg Syms | X] -> [package Pkg Syms
                                 | (unwrap-package-null X [])])


> I played a bit with the code in reader.shen and came up with this change.
> It works for your specific use-case, I may be missing other cases.
>
> Changes marked in green:

Sorry, I don't see colors (I'm using text-based MUA).


Oops, sorry.

The new code is the fold-right function, and the code that handles the `[package null _ | Code] Stream` match clause.
In the new version it takes care of handling the nesting by using fold-right, the original version only handles the first level.

It was changed from:

  [package null _ | Code] Stream -> (append Code Stream)

to:



--
BD
Reply all
Reply to author
Forward
0 new messages