require 'optparse' require 'json' require 'set' require 'active_support/core_ext/string' require_relative './templates.rb' options = {} OptionParser.new do |parser| parser.banner = "Usage: example.rb [options]" parser.on("-gGLUE", "--glue=GLUE", "Path to file(defaults to ./glue.rb)") do |glue| options[:glue] = glue end parser.on('-cCONFIG', '--config=CONFIG', 'Path to config file') do |config| options[:config] = config end end.parse! options[:glue] ||= './glue.json' glue = JSON.parse(File.read(options[:glue])) bound = {} phase1 = {} phase2 = {} phase3 = {} phase4 = {} phase5 = {} complete_phase1 = {} complete_phase2 = {} complete_phase3 = {} complete_phase4 = {} complete_phase5 = {} result = "" includes = %{ #include #include #include #include #include #include #include } defines = "" init_body = "" standard_types = ['bool', 'int', 'float', 'double', 'float', 'const char *', 'unsigned int', 'void'] # for displaying statistics glue.first.each do |func, params| if (func.rpartition(' ').first == 'void') && (params[0] == 'void') phase1[func] = params elsif (standard_types.include? func.rpartition(' ').first) && (params[0] == 'void') phase2[func] = params else no_struct_param = true params.each do |param| if !(standard_types.include? param.rpartition(' ').first) no_struct_param = false break end end if no_struct_param if standard_types.include? func.rpartition(' ').first phase3[func] = params else phase4[func] = params end else phase5[func] = params end end end # generates functions glue.first.each do |func, params| # for now dont worry about params or returns if func.rpartition(' ').first != 'void' || params[0] != 'void' next else bound[func] = params if phase1.include? func complete_phase1[func] = params elsif phase2.include? func complete_phase2[func] = params elsif phase3.include? func complete_phase3[func] = params elsif phase4.include? func complete_phase4[func] = params elsif phase5.include? func complete_phase5[func] = params end end body = "#{func.split(' ').last}();\nreturn mrb_nil_value();" defines += Template.function(func.split(' ').last, body) init_body += Template.init_module_function('test', func.split(' ').last.underscore, func.split(' ').last, "MRB_ARGS_NONE()") end init_body.prepend(Template.define_module('Test')) result = %{ #{includes} #{defines} #{Template.base('test', init_body, nil)} } result += "//Bound Functions: #{bound.length} / #{phase1.length + phase2.length + phase3.length + phase4.length + phase5.length}\n" result += "//Phase 1 Functions: #{complete_phase1.length} / #{phase1.length}\n" result += "//Phase 2 Functions: #{complete_phase2.length} / #{phase2.length}\n" result += "//Phase 3 Functions: #{complete_phase3.length} / #{phase3.length}\n" result += "//Phase 4 Functions: #{complete_phase4.length} / #{phase4.length}\n" result += "//Phase 5 Functions: #{complete_phase5.length} / #{phase5.length}\n" puts result