2 require 'sexp/simple_processor'
7 def initialize(header_path=".")
9 @funcs = Cuby::HeaderParser.find_in_directory(@headers).values.flatten
17 maps = Hash.new { |h,k| h[k] = {}}
19 m = /(.*?)_(.*)/.match(func.name)
22 map = m[1].downcase.to_sym
24 arg = func.arguments.first
25 if arg and arg[:type] == "STATE"
26 map_to = "#{func.name}(state, %s)"
31 maps[map][name.to_sym] = map_to
39 %w!_int j k m!.each do |var|
40 c.declare_var "int", var
43 %w!self _lit t1 t2 t3!.each do |var|
44 c.declare_var "OBJECT", var
47 c.declare_var "rstate", "state"
50 c.declare_method = :declare
53 add_builtin_methods(c)
55 c.declare_function 'int', 'sizeof', ['void']
57 c.add_method(:fail) do
61 c.on_return = proc { |val|
62 "stack_push(#{val}); break"
65 c.true_value = "Qtrue"
66 c.false_value = "Qfalse"
70 def declare_function(cuby, func)
71 args = func.arguments.map { |a| a[:type] }
72 cuby.declare_function func.return_type, func.name, args
76 :fixnum? => "FIXNUM_P(%s)",
85 :set_value => ["char", proc { |s, v|
89 :[] => ["char", proc { |s,a|
95 :as_char => ["char", proc { |s| "'#{s[1..-2]}'"}]
98 Operators = [:+, :==, :-, :*, :<, :>, :-@, :%, :/]
100 def add_header_maps(cuby)
101 @funcs.each do |func|
102 declare_function(cuby, func)
105 @maps.each do |name, map|
106 cuby.add_map name, map
109 cuby.add_map :object, ObjectMap
110 cuby.add_map :numeric, NumericMap
111 cuby.add_map :charstar, CharStarMap
112 cuby.add_map :string, StringMap
113 cuby.add_type_map "OBJECT", :object
114 cuby.add_type_map "int", :numeric
115 cuby.add_type_map "char*", :charstar
117 Operators.each do |op|
122 def add_builtin_methods(c)
126 def fixup_body(args, c_code)
127 preamble = "self = stack_pop();\n"
129 preamble << "#{a} = stack_pop();\n"
132 return preamble + c_code
135 class Processor < SimpleSexpProcessor
136 def initialize(cont, output)
138 self.require_expected = false
140 self.auto_shift_type = true
166 an = body[1].delete_at(1)
172 @output[name] = @cont.fixup_body(args, c.code)
178 syd = SydneyParser.load_file io
181 pro = Processor.new(self, @output)
186 cp = CubyPrimitives.new("shotgun/lib")
187 cp.parse_file ARGV.shift
188 cp.output.each do |meth, code|