Macros in 'when' clause don't capture variables

Đang mở
#253 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
lua
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu từ đường dẫn mở rộng macro cho pattern matching của switch/when và so sánh với ví dụ direct array-pattern. Tái hiện trường hợp $binop được đưa ra và theo dõi xem các identifier có được bind sau khi mở rộng hay không; được xem là hoàn tất khi các pattern dựa trên macro capture a, b và c giống như pattern trực tiếp, hoặc hành vi này được ghi lại là có chủ đích.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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?

Ngôn ngữ chính
Lua
Star
620
Fork
45
Merge trung bình
9 giờ 5 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của IppClub/YueScript

Tất cả issue của IppClub/YueScript

Issue tương tự

Thêm issue về Lua

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.