Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@matchast does not produce informative error message #162

Open
jariji opened this issue Apr 6, 2023 · 2 comments
Open

@matchast does not produce informative error message #162

jariji opened this issue Apr 6, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@jariji
Copy link
Contributor

jariji commented Apr 6, 2023

The @matchast docs say there is no need to quote patterns so I tried that but I got an error. Using @match and quoting works.

using MLStyle
using MLStyle.Modules.AST

@active BinaryFloat(x) begin
    sign(x), significand(x), exponent(x)
end

@match 3.4 begin
    BinaryFloat(s,m,e) => (;s,m,e)
end

struct Floating end

@active BinaryFloatF(x) begin
    sign(x), significand(x)-1, exponent(x)
end

function MLStyle.pattern_uncall(::Type{Floating}, to_pattern, _, _, arguments)    
    @match only(arguments) begin
        :($s*(1+$f)*2^$e) => to_pattern(:(BinaryFloatF($s,$f,$e)))
        :($s*$m*2^$e) => to_pattern(:(BinaryFloat($s,$m,$e)))        
    end
end

@match 3.4 begin
    Floating(s*m*2^e) => (;s,m,e)
end

@match 3.4 begin
    Floating(s*(1+f)*2^e) => (;s,f,e)
end

function MLStyle.pattern_uncall(::Type{Floating}, to_pattern, _, _, arguments)    
    @matchast only(arguments) begin
        $s*(1+$f)*2^$e => to_pattern(:(BinaryFloatF($s,$f,$e)))
        $s*$m*2^$e => to_pattern(:(BinaryFloat($s,$m,$e)))        
    end
end
# SyntaxError("Malformed ast template, the second arg should be a block with a series of pairs(`a => b`),

MLStyle v0.4.17
Julia 1.9.0-beta3

@thautwarm
Copy link
Owner

Thanks for your informative issue.

You might change @matchast only(arguments) begin to @matchast only(arguments) quote.

function MLStyle.pattern_uncall(::Type{Floating}, to_pattern, _, _, arguments)    
    @matchast only(arguments) quote
        $s*(1+$f)*2^$e => to_pattern(:(BinaryFloatF($s,$f,$e)))
        $s*$m*2^$e => to_pattern(:(BinaryFloat($s,$m,$e)))        
    end
end

The error message should be improved and I'm to figure out why we decided to use quote instead of begin here.

@thautwarm thautwarm added the enhancement New feature or request label Apr 7, 2023
@jariji
Copy link
Contributor Author

jariji commented Apr 7, 2023

I see, it is documented that way, I just didn't notice. Thanks.

@thautwarm thautwarm changed the title @matchast fails to compile @matchast does not produce informative error message Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants