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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews