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

Add foam.DEF_MACRO and foam.APPLY_MACRO #1869

Open
wants to merge 2 commits into
base: development
Choose a base branch
from

Conversation

KernelDeimos
Copy link
Collaborator

Meta-programming is already possible in FOAM by calling methods like foam.CLASS dynamically. This PR introduces explicit meta-programming support so that this behavior can be better controlled by FOAM.

The following example defines a macro that creates two models for each value it's applied to:

foam.DEF_MACRO({
  package: 'com.example',
  name: 'ExampleMacro',
  
  code: function (name) {
    foam.CLASS({
      package: 'com.example',
      name: 'Greedy' + name
    });
    foam.CLASS({
      package: 'com.example',
      name: 'Lazy' + name
    });
  }
});

We can then imply models GreedyCat, LazyCat, GreedyDog and LazyDog as follows:

foam.APPLY_MACRO('com.example.ExampleMacro', ['Cat', 'Dog']);

In order to justify adding explicit support, the need for meta-programming is a prerequisite justification. Sometimes it makes sense to generate individual models from tabular data in a similar fashion that FOAM can already generate models from XSD files. I came across a good example while modelling WebAssembly functions. Each instruction has different properties and creating a general model to handle all cases would add complexity and prevent adding refinements to individual instructions.

With foam.APPLY_MACRO I was able to represent the differences between instructions concisely in a few lines of code:

foam.APPLY_MACRO("wasm.ins.InstructionMacro", [
    [0x25,     'table.get',  'idx:u32'],
    [0x26,     'table.set',  'idx:u32'],
    [0xFC, 12, 'table.init', 'elemIdx:u32', 'tableidx:u32'],
    [0xFC, 13, 'elem.drop',  'idx'],
    [0xFC, 14, 'table.copy', 'a:u32', 'b:u32'],
    [0xFC, 15, 'table.grow', 'idx:u32'],
    [0xFC, 16, 'table.size', 'idx:u32'],
    [0xFC, 17, 'table.fill', 'idx:u32'],
]);

Here are some advantages of explicit support as opposed to the meta-programming that is already possible:

  • Explicit support makes it easy to spot. When you see _MACRO, you know meta-programming is happening.
  • Generated models can be handled differently by code generators if needed.
  • Helpful axioms like documentation can be added to the macro itself

@kgrgreer
Copy link
Owner

Explicit support makes it easy to spot. When you see _MACRO, you know meta-programming is happening.
It's FOAM, meta-programming is always happening.
I'm not convinced that this is worth adding.

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.

2 participants