Skip to content

Conversation

@kddnewton
Copy link
Member

@kddnewton kddnewton commented Apr 12, 2022

For posterity - most of this was generated through a script and then checked. The gist of the script is below:

#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "syntax_tree"

program = SyntaxTree.parse(SyntaxTree.read(File.expand_path("../lib/syntax_tree/node.rb", __dir__)))
program => {
  statements: {
    body: [
      *,
      SyntaxTree::ModuleDeclaration[bodystmt: {
        statements: {
          body: [
            *,
            SyntaxTree::ClassDeclaration[
              constant: { constant: { value: ^(ARGV.first) } },
              bodystmt: { statements: { body: } }
            ],
            *
          ]
        }
      }]
    ]
  }
}

body => [*, SyntaxTree::Def[name: { value: "accept" }, bodystmt: { statements: { body: [SyntaxTree::Call[message: { value: visit_method_name }]] } }], *]
body => [*, SyntaxTree::Def[name: { value: "to_json"}, bodystmt: { statements: { body: [SyntaxTree::Call[receiver: SyntaxTree::HashLiteral => hash_literal]] } }] => method, *]

output = StringIO.new
output.puts("def #{visit_method_name}(node) {")

hash_literal.assocs.each do |assoc|
  output.print("#{assoc.key.value} ")

  case assoc.value
  in SyntaxTree::SymbolLiteral
    output.print(":#{assoc.value.value.value}")
  in SyntaxTree::VCall[value: { value: "location" }]
    output.print("visit_location(node.location)")
  in SyntaxTree::VCall[value: { value: "comments" }]
    output.print("visit_all(node.comments)")
  in SyntaxTree::VCall[value: { value: }]
    output.print("visit(node.#{value})")
  end

  output.puts(",")
end

output.puts("} end")
puts SyntaxTree.format(output.string)

@kddnewton kddnewton merged commit 5411343 into main Apr 12, 2022
@kddnewton kddnewton deleted the json branch April 12, 2022 01:27
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