CharlesIrvine

CharlesIrvine

I’m wondering if the following is possible:

I have an application where I would like to parse boolean expressions and produce corresponding anonymous functions. These expressions:

x > y
x == y
x == y + 3

would be converted to:

fn data -> data.x > data.y
fn data -> data.x == data.y
fn data -> data.x == data.y + 3

Possible?

Showing Posts 1 to 4

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @CharlesIrvine can you elaborate if you are being passed these expressions as strings from users or other untrusted sources at runtime, vs compile time expressions?

dimitarvp

dimitarvp

Ben beat me to the question so I’ll just add that if you simply want to convert an existing program and NOT do it at runtime while accepting user input then using a tool like GitHub - ast-grep/ast-grep: ⚡A CLI tool for code structural search, lint and rewriting. Written in Rust · GitHub that parses code properly and can replace it should be more than enough.

CharlesIrvine

CharlesIrvine OP

@benwilson512 @dimitarvp I should have provided more context. Sorry.

I currently have these functions and macro:

def x_less_than_y(data) do
    data.x < data.y
  end

  def x_greater_or_equal_y(data) do
    data.x >= data.y
  end

defprocess "two case process" do
    case_task("yes or no", [
      case_i &ME.x_less_than_y/1 do
        user_task("1", groups: "admin")
        user_task("2", groups: "admin")
      end,
      case_i &ME.x_greater_or_equal_y/1 do
        user_task("3", groups: "admin")
        user_task("4", groups: "admin")
      end
    ])
  end

From this file.

I would be really useful if the users of my BPM library could write this instead:

defprocess "two case process" do
    case_task("yes or no", [
      case_i x < y do
        user_task("1", groups: "admin")
        user_task("2", groups: "admin")
      end,
      case_i x >= y do
        user_task("3", groups: "admin")
        user_task("4", groups: "admin")
      end
    ])
  end

This is my first foray into writing Elixir macros. I thought there might be some macro tricks that could be used for this.

al2o3cr

al2o3cr

There are useful shorthands like quote, but ultimately macros are transformations from AST to AST. For your example:

# INPUT
iex(1)> quote do: x > y 
{
  :>,
  [context: Elixir, import: Kernel],
  [
    {:x, [if_undefined: :apply], Elixir},
    {:y, [if_undefined: :apply], Elixir}
  ]
}
# OUTPUT
iex(2)> quote do: data.x > data.y
{
  :>,
  [context: Elixir, import: Kernel],
  [
    {
      {
        :.,
        [],
        [
          {:data, [if_undefined: :apply], Elixir},
          :x
        ]
      },
      [no_parens: true],
      []
    },
    {
      {
       :.,
       [],
       [
         {:data, [if_undefined: :apply], Elixir},
         :y
       ]
     },
     [no_parens: true],
    []
   }
 ]
}

The underlying pattern looks like:

{:some_var, _, _} -> {{:., [], {:data, [], Elixir}, :some_var}

You’d apply a transformation like this using a function like Macro.prewalk or your own recursive traversal code.

Once you’ve got the data-fied expression, you’d wrap it in the AST corresponding to fn data -> ... end. You can find that shape using quote:

iex(4)> quote do: fn data -> :ok end
{:fn, [], [{:->, [], [[{:data, [if_undefined: :apply], Elixir}], :ok]}]}
— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews