Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Draft implementation of the idea of generating DSLs as ASTs. Credits to Evan Light

View dsl_to_ast_generator.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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.