summaryrefslogtreecommitdiffhomepage
path: root/generate.rb
blob: 9eda39342bf210c27a100600ecb8da26ad781af3 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'optparse'
require 'json'
require 'set'
require 'active_record'
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 <raylib.h>
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/numeric.h>
#include <mruby/string.h>
#include <stdlib.h>
}
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

def debug_mark_binding(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

# generates functions
glue.first.each do |func, params|
  # for now dont worry about params or returns
  rpart = func.rpartition(' ')
  func_datatype = rpart.first
  func_name = rpart.last

  if func_datatype == 'void' && params[0] == 'void'
    body = "#{func_name}();\nreturn mrb_nil_value();"
    defines += Template.function(func_name, body)
    init_body += Template.init_module_function('test', func_name.underscore, func_name, "MRB_ARGS_NONE()")

    bound[func] = params
    debug_mark_binding(func, params)
  elsif (standard_types.include? func_datatype) && (params[0] == 'void')
    if func_datatype == 'int'
      body = "return mrb_fixnum_value(#{func_name}());"
      defines += Template.function(func_name, body)
      init_body += Template.init_module_function('test', func_name.underscore, func_name, "MRB_ARGS_NONE()")

      bound[func] = params
      debug_mark_binding(func, params)
    elsif func_datatype == 'float' || func_datatype == 'double'
      body = "return mrb_float_value(mrb, #{func_name}());"
      defines += Template.function(func_name, body)
      init_body += Template.init_module_function('test', func_name.underscore, func_name, "MRB_ARGS_NONE()")

      bound[func] = params
      debug_mark_binding(func, params)
    elsif func_datatype == 'bool'
      body = "return mrb_bool_value(#{func_name}());"
      defines += Template.function(func_name, body)
      init_body += Template.init_module_function('test', func_name.underscore, func_name, "MRB_ARGS_NONE()")

      bound[func] = params
      debug_mark_binding(func, params)

    end
  end
end

init_body.prepend(Template.define_module('Test'))

result = %{
#{includes}
#{defines}
#{Template.base('test', init_body, nil)}
}

result += "//Bound Functions: #{$complete_phase1.length + $complete_phase2.length + $complete_phase3.length + $complete_phase4.length + $complete_phase5.length} / #{$phase1.length + $phase2.length + $phase3.length + $phase4.length + $phase5.length}\n//---\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