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 a script that emits a JSON representation of wasm-delegations-fields.def #6469

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

Conversation

kripken
Copy link
Member

@kripken kripken commented Apr 3, 2024

This is simple to do after #6465 which converted the .def file into a very
parse-able format.

The emitted JSON file contains things like

  {
    id: 'Loop',
    fields: [
      {
        form: 'CHILD',
        name: 'body',
      },
      {
        form: 'SCOPE_NAME_DEF',
        name: 'name',
      },
    ]
  },

Full output: https://gist.github.com/kripken/2e7f681ff7878a6e06bb721327328623

May fix #6460 if this looks like the right style of output. Happy to make changes.

@phated
Copy link
Contributor

phated commented Apr 4, 2024

Thanks @kripken! I want to loop in @peblair since he was working to automate the Grain bindings and this might be helpful to him.

@tlively
Copy link
Member

tlively commented Apr 5, 2024

Instead of a python script, what if we had a small C++ executable that could emit the JSON? That way we wouldn't be in the business of having to parse the .def file ourselves.

@kripken
Copy link
Member Author

kripken commented Apr 5, 2024

@tlively what do you see as the benefit of not parsing the .def file ourselves? I think the code here shows that doing so is trivial. It's less work than writing C++ that does the same, I think 😄

@tlively
Copy link
Member

tlively commented Apr 5, 2024

For your consideration:

void emitHeader() { std::cout << "[\n"; }

void emitClassStart(std::string name) {
  std::cout << "  {\n";
  std::cout << "    id: '" << name << "',\n";
  std::cout << "    fields: [\n";
}

void emitField(std::string form, std::string name) {
  std::cout << "      {\n";
  std::cout << "        form: '" << form << "',\n";
  std::cout << "        name: '" << name << "',\n";
  std::cout << "      },\n";
}

void emitClassEnd() {
  std::cout << "    ]\n";
  std::cout << "  },\n";
}

void emitFooter() { std::cout << "]\n"; }

void emitJSON() {
#define DELEGATE_FIELD_MAIN_START emitHeader();

#define DELEGATE_FIELD_CASE_START(id) emitClassStart(#id);

#define DELEGATE_FIELD_CHILD(id, field) emitField("CHILD", #field);
#define DELEGATE_FIELD_CHILD_VECTOR(id, field)                                 \
  emitField("CHILD_VECTOR", #field);
#define DELEGATE_FIELD_INT(id, field) emitField("INT", #field);
#define DELEGATE_FIELD_INT_ARRAY(id, field) emitField("INT_ARRAY", #field);
#define DELEGATE_FIELD_INT_VECTOR(id, field) emitField("INT_VECTOR", #field);
#define DELEGATE_FIELD_LITERAL(id, field) emitField("LITERAL", #field);
#define DELEGATE_FIELD_NAME(id, field) emitField("NAME", #field);
#define DELEGATE_FIELD_NAME_VECTOR(id, field) emitField("NAME_VECTOR", #field);
#define DELEGATE_FIELD_SCOPE_NAME_DEF(id, field)                               \
  emitField("SCOPE_NAME_DEF", #field);
#define DELEGATE_FIELD_SCOPE_NAME_USE(id, field)                               \
  emitField("SCOPE_NAME_USE", #field);
#define DELEGATE_FIELD_SCOPE_NAME_USE_VECTOR(id, field)                        \
  emitField("SCOPE_NAME_USE_VECTOR", #field);
#define DELEGATE_FIELD_TYPE(id, field) emitField("TYPE", #field);
#define DELEGATE_FIELD_TYPE_VECTOR(id, field) emitField("TYPE_VECTOR", #field);
#define DELEGATE_FIELD_HEAPTYPE(id, field) emitField("HEAPTYPE", #field);
#define DELEGATE_FIELD_ADDRESS(id, field) emitField("ADDRESS", #field);

#define DELEGATE_FIELD_CASE_END(name) emitClassEnd();

#define DELEGATE_FIELD_MAIN_END emitFooter();

#include "wasm-delegations-fields.def"
}

@kripken
Copy link
Member Author

kripken commented Apr 5, 2024

Thanks!

That looks like 53 lines. The Python here is only 49 lines after removing the large comment at the top of the file that explains the JSON format, or only 38 if you remove all comments to match your C++. Also, the C++ would need to define a new executable to be run, or at least a new pass or something like that, which would also add to the size.

So the Python seems smaller and simpler to me - am I looking at this wrong somehow?

@tlively
Copy link
Member

tlively commented Apr 5, 2024

The python may be shorter, but the cognitive overhead of all the string munging makes it much more complex than the C++ IMO. If it weren't for the comment at the top of the python script, it would be very difficult to figure out what it would emit just by reading the code. In contrast, it's immediately clear what the C++ code will emit.

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.

Output structured data format based on wasm-delegations.def
3 participants