Skip to content

@txus /dsl_to_ast_generator.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Draft implementation of the idea of generating DSLs as ASTs. Credits to Evan Light
require 'pp'
# Draft implementation of the idea of generating DSLs as ASTs exposed by
# Evan Light in this presentation:
#
# http://evan.tiggerpalace.com/files/yourdsl.pdf
class DSL
attr_reader :global
def initialize(&block)
@global = Scope.new(&block)
end
class Scope
attr_reader :body
def initialize(&block)
@body = []
instance_eval(&block)
end
def method_missing(m, *args, &block)
result = [m.to_sym, args]
result.push Scope.new(&block) if block
@body << result
end
end
end
dsl = DSL.new do
hey :ho, :lets, :go do
hooray 2, 3, 4
configure :nothing do
a 3
b 84
end
end
end
pp dsl
# #<DSL:0x0000010082e0c8
# @global=
# #<DSL::Scope:0x0000010082e078
# @body=
# [[:hey,
# [:ho, :lets, :go],
# #<DSL::Scope:0x0000010082df38
# @body=
# [[:hooray, [2, 3, 4]],
# [:configure,
# [:nothing],
# #<DSL::Scope:0x0000010082dd30 @body=[[:a, [3]], [:b, [84]]]>]]>]]>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.