Skip to content

Add lambdas, add chaining, fix docstrings#52

Open
THinnerichs wants to merge 2 commits intomasterfrom
feat/add_lambdas
Open

Add lambdas, add chaining, fix docstrings#52
THinnerichs wants to merge 2 commits intomasterfrom
feat/add_lambdas

Conversation

@THinnerichs
Copy link
Copy Markdown
Member

I got a bit carried away, so this includes way more things than I originallly anticipated:

  • Added lambda function support
g = @csgrammar begin
                Arr = _arg_1
                Arr = map(Func, Arr)

                Func = inc
                Func = x -> 2*x
                Func = x -> x + 1
                Func = x -> 2*Func(x) + 1
                Func = x -> Func(Func(x))
            end

            interpret_custom = HerbInterpret.make_interpreter(
                g;
                target_module=@__MODULE__,
            )

            input = Dict{Symbol,Any}(:_arg_1 => [1, 2, 3, 4])
            
            # map(x -> (x -> 2*x)(inc(x)), arr) = map(x -> 2*inc(x), arr)
            interpret_custom(@rulenode(2{7{4,3},1}), input) # yiels [4, 6, 8, 10]
  • added support for chaining of operators in stateful grammars:
module LocalStateDSL2
    using HerbCore
    using HerbGrammar
    using RuntimeGeneratedFunctions
    RuntimeGeneratedFunctions.init(LocalStateDSL2)
 
    struct St
        x::Int
    end

    Base.:(==)(a::St, b::St) = a.x == b.x

    inc(st::St) = St(st.x + 1)
    dec(st::St) = St(st.x - 1)
    iseven(st::St) = Base.iseven(st.x)

    g2 = @cfgrammar begin
        Start = Step
        Step  = IF(Cond, Step, Step)
        Step  = inc()
        Step  = dec()
        Step  = (Step; Step)
        Cond  = iseven()
    end
end

interp2 = HerbInterpret.make_stateful_interpreter(
                LocalStateDSL2.g2;
                target_module = LocalStateDSL2,
                cache_module  = @__MODULE__,
            )
            
prog_two_incs = @rulenode(5{3,3})
interp2(prog_two_incs, LocalStateDSL2.St(0)) # yields LocalStateDSL2.St(2)
  • added tests
  • updated docstrings for core functions

…s in stateful grammars; added tests; updated docstrings for core functions
@THinnerichs THinnerichs requested a review from ReubenJ March 26, 2026 16:15
@THinnerichs THinnerichs self-assigned this Mar 26, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 26, 2026

Codecov Report

❌ Patch coverage is 81.11111% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.21%. Comparing base (b5a6c5c) to head (2f64610).

Files with missing lines Patch % Lines
src/make_interpret.jl 81.11% 17 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #52      +/-   ##
==========================================
+ Coverage   69.11%   74.21%   +5.10%     
==========================================
  Files           3        3              
  Lines         204      256      +52     
==========================================
+ Hits          141      190      +49     
- Misses         63       66       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 26, 2026

Benchmark Results

master 2f64610... master / 2f64610...
interpret/Random Expressions 0.123 ± 0.0016 ms 0.123 ± 0.0016 ms 0.999 ± 0.019
time_to_load 0.0847 ± 0.0022 s 0.0863 ± 0.0015 s 0.981 ± 0.031

Benchmark Plots

A plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR.
Go to "Actions"->"Benchmark a pull request"->[the most recent run]->"Artifacts" (at the bottom).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant