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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews