Macros in 'when' clause don't capture variables

Aperta
#253 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
lua
Ambito
compilers

Direzione di ricerca

Inizia dal percorso di macro-expansion per il pattern matching di switch/when e confrontalo con l’esempio diretto di array pattern. Riproduci il caso $binop mostrato e verifica se gli identificatori vengono associati dopo l’espansione; il lavoro è completato quando i pattern basati su macro catturano a, b e c come il pattern diretto, oppure quando il comportamento viene documentato come intenzionale.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I want to have something at least resembling algebraic data types in Yuescript - these are called enums in Rust, variants in OCaml, tagged unions in C. These are types that you can construct and they carry the underlying variant and some fields that the variant has. An example of this in Rust can be seen here: https://doc.rust-lang.org/rust-by-example/custom_types/enum.html

To that end I've been trying to use existing pattern matching functionality and macros to get this behavior. For the sake of example, let's say we want to encode variants of an enum as an array ['VariantName', ...fields], for example:

lang =
    BinOp: (lhs, op, rhs) => ['BinOp', lhs, op, rhs]
    Var: (name, expr) => ['Var', name, expr]
    Const: (val) => ['Const', val]

Then I'd like to do pattern matching on different variants, but have a bit nicer syntax:

macro binop = (x,y,z) -> {
    code: "['Binop', #{x}, #{y}, #{z}]"
    type: "yue"
}

switch e
    when $binop a,b,c
        print "foo"

But this compiles to:

if {
	'Binop',
	a,
	b,
	c
} == e then
	return print("foo")
end

But this isn't what I want, as it does not assign a, b, c variables for me to use inside the 'when' block. If I use the expected output of the macro directly:

switch e
    when ['Binop', a, b, c]
        print "bar"

I get what I expected - a, b, c are assigned values of e[2..4]:

local _type_0 = type(e)
local _tab_0 = "table" == _type_0 or "userdata" == _type_0
if _tab_0 then
	local a = e[2]
	local b = e[3]
	local c = e[4]
	if 'Binop' == e[1] and a ~= nil and b ~= nil and c ~= nil then
		return print("bar")
	end
end

This behavior is a bit unexpected, seems like macro evaluation happens at some later stage of compilation so instead of variable capture I get table comparison. Is this a bug or working as intended?

Lingua principale
Lua
Stelle
620
Fork
45
Merge medio
9h 5m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di IppClub/YueScript

Tutte le issue di IppClub/YueScript

Issue simili

Altre issue su Lua

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.