Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"Hello again, Peabody here..." -- Mister Peabody


devel / comp.lang.lisp / Re: name? - global vars via symbol macros

SubjectAuthor
* name? - global vars via symbol macrosMadhu
`* Re: name? - global vars via symbol macrosMadhu
 `- Re: name? - global vars via symbol macrosZyni Moë

1
name? - global vars via symbol macros

<m3fsrhydic.fsf@leonis4.robolove.meer.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16715&group=comp.lang.lisp#16715

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: enometh@meer.net (Madhu)
Newsgroups: comp.lang.lisp
Subject: name? - global vars via symbol macros
Date: Sat, 27 Nov 2021 18:14:11 +0530
Organization: Motzarella
Lines: 36
Message-ID: <m3fsrhydic.fsf@leonis4.robolove.meer.net>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="108096645e0d434f0c53bbbd7eb22e10";
logging-data="17690"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/f93Vm0Z9rU2guLm12fq9/Ev6umGPxdoQ="
Cancel-Lock: sha1:AR+JmTfQ8/HpItO2AZZrDWRe+DU=
sha1:kqwXnKcd7NYKRwMbSLluZfEWsLg=
 by: Madhu - Sat, 27 Nov 2021 12:44 UTC

For experimental programming I find it easiest to manipulate state in
global variables and refer to them with a $ prefix - as $line.
Typically this state the state would be put into a struct "context"
object to avoid the global environment. Here is a defmacro helper to do
just that

(defun make-def-forms (name vars)
"Return forms that: define a struct named NAME with slots VARS. Define
a global variable of the form *NAME* and initialize it with the
default constructor for the structure. Define symbol macros of the
form $VAR to access each slot VARS in the global variable."
(destructuring-bind (var-name constructor-name acc-names)
(read-from-string
(format nil "(*~(~A~)* make-~(~:*~A~) (~{$~(~A~)~^ ~}))"
name vars))
`(progn
(defstruct ,name ,@vars)
(defvar ,var-name (,constructor-name))
,@(loop for acc-name in acc-names for var in vars
collect `(define-symbol-macro ,acc-name
(slot-value ,var-name ',var))))))

e.g.
(eval (make-def-forms 'ctx '(buffer index)))
(setq $buffer (make-array 10) $index 0)
(setf (elt $buffer (setf $index 1)) 10)
(elt $buffer $index)
(macroexpand-1 '$index)

The question is there a better name than make-def-forms for this
particular "general" functionality, and the associated macro?

also I prefer vars to to be passed in from a list object, and a defmacro
does not help there (unless it is in a global variable).

Re: name? - global vars via symbol macros

<m3fsrdm2vj.fsf@leonis4.robolove.meer.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16731&group=comp.lang.lisp#16731

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: enometh@meer.net (Madhu)
Newsgroups: comp.lang.lisp
Subject: Re: name? - global vars via symbol macros
Date: Wed, 01 Dec 2021 08:46:00 +0530
Organization: Motzarella
Lines: 52
Message-ID: <m3fsrdm2vj.fsf@leonis4.robolove.meer.net>
References: <m3fsrhydic.fsf@leonis4.robolove.meer.net>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b558823d79e10b219d6893ca2c4fa933";
logging-data="31834"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19QK/afiHTJRy+YdQmw2bBCcb0NjKeK5C4="
Cancel-Lock: sha1:PbA8hRRG3mG3g2GakuG8MAYK+o0=
sha1:OaTg3k8nwDmup0dxdLur13miVJY=
 by: Madhu - Wed, 1 Dec 2021 03:16 UTC

* Madhu <m3fsrhydic.fsf@leonis4.robolove.meer.net> :
Wrote on Sat, 27 Nov 2021 18:14:11 +0530:

> For experimental programming I find it easiest to manipulate state in
> global variables and refer to them with a $ prefix - as $line.
> Typically this state the state would be put into a struct "context"
> object to avoid the global environment. Here is a defmacro helper to do
> just that
>
> (defun make-def-forms (name vars)
> "Return forms that: define a struct named NAME with slots VARS. Define
> a global variable of the form *NAME* and initialize it with the
> default constructor for the structure. Define symbol macros of the
> form $VAR to access each slot VARS in the global variable."
> (destructuring-bind (var-name constructor-name acc-names)
> (read-from-string
> (format nil "(*~(~A~)* make-~(~:*~A~) (~{$~(~A~)~^ ~}))"
> name vars))
> `(progn
> (defstruct ,name ,@vars)
> (defvar ,var-name (,constructor-name))
> ,@(loop for acc-name in acc-names for var in vars
> collect `(define-symbol-macro ,acc-name
> (slot-value ,var-name ',var))))))
>
>
>
> e.g.
> (eval (make-def-forms 'ctx '(buffer index)))
> (setq $buffer (make-array 10) $index 0)
> (setf (elt $buffer (setf $index 1)) 10)
> (elt $buffer $index)
> (macroexpand-1 '$index)
>
> The question is there a better name than make-def-forms for this
> particular "general" functionality, and the associated macro?
>
> also I prefer vars to to be passed in from a list object, and a defmacro
> does not help there (unless it is in a global variable).

No takers? :( I'm going with

(defmacro define-contexted-global-lexicals (context-name &rest vars)
(make-def-forms context-name vars))

(define-contexted-global-lexicals buffer-context buffer buffer-len)

which defines $buffer and $buffer-len

not that i'm happy with it

Re: name? - global vars via symbol macros

<so7jmp$h1d$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16732&group=comp.lang.lisp#16732

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: no_email@invalid.invalid (Zyni Moë)
Newsgroups: comp.lang.lisp
Subject: Re: name? - global vars via symbol macros
Date: Wed, 1 Dec 2021 10:44:41 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <so7jmp$h1d$1@dont-email.me>
References: <m3fsrhydic.fsf@leonis4.robolove.meer.net>
<m3fsrdm2vj.fsf@leonis4.robolove.meer.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 1 Dec 2021 10:44:41 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="0dde145bb7550eb052bb778037e62abd";
logging-data="17453"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lyVuTwzh9PqJiJOD3RX+LbcbPDSVeu4w="
User-Agent: NewsTap/5.5 (iPad)
Cancel-Lock: sha1:1j5i0CEW9UFVt5KovNmRl8CdnGE=
sha1:myHWcDNcGwnVoz8FGcLmx64NWDw=
 by: Zyni Moë - Wed, 1 Dec 2021 10:44 UTC

Madhu <enometh@meer.net> wrote:
> * Madhu <m3fsrhydic.fsf@leonis4.robolove.meer.net> :

>
>> For experimental programming I find it easiest to manipulate state in
>> global variables and refer to them with a $ prefix - as $line.
>>

Tim Bradshaw's global lexicals hack
(https://tfeb.github.io/tfeb-lisp-toys/#global-lexical-variables-glex) now
has reader support: you can make it be so that #$x refers to a global
lexical x:

(setf #$x 3)
means that #$x will be 3, and if you then do (defglex x), x will be 3 too,
but x is not bound in the symbol-value sense.

Suspect this is not what you are after, but perhaps worth mentioning.

Disclaimer: reader support only exists because I asked him to add it, I
only asked him to add it because of your post (hence $ is the char). So it
is nicely circular any way.

--
the small snake


devel / comp.lang.lisp / Re: name? - global vars via symbol macros

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor