summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2022-03-12 04:48:00 -0500
committerrealtradam <[email protected]>2022-03-12 04:48:00 -0500
commit3acb6c636d1ffac07e1dedbbdc22cb88fb16fada (patch)
tree87415368e182e8bf6bfa9ce00272a0e95f092d18
parent9c0d9e5e086d8b593057b0eab5981968e10874bb (diff)
downloadFelBind-3acb6c636d1ffac07e1dedbbdc22cb88fb16fada.tar.gz
FelBind-3acb6c636d1ffac07e1dedbbdc22cb88fb16fada.zip
finished phase 4
-rw-r--r--Readme.mdown42
-rw-r--r--generate.rb54
2 files changed, 68 insertions, 28 deletions
diff --git a/Readme.mdown b/Readme.mdown
index 296955e..06d29bf 100644
--- a/Readme.mdown
+++ b/Readme.mdown
@@ -6,36 +6,40 @@
A binding assistant and generator for C/C++ to mruby(Under heavy WIP)
----
+ ---
### How I plan for it to work:
1. Run the scanner which will generate a glue.json file. This json file will contain all functions(and their params) as well as all structs(and their params)
-2. Create a configuration file where you can reference specific functions and how you want their bindings to be generated differently
+ 2. Create a configuration file where you can reference specific functions and how you want their bindings to be generated differently
- for example, under what module or class a function should belong
- if a certain param should use self instead of passing in something
- ignore some functions if you dont need them
- insert bindings you made yourself
-3. Run the generator with the configuration file - this generates the resulting binding code
+ 3. Run the generator with the configuration file - this generates the resulting binding code
#### Todo:
-- [X] parse C files for function and struct declarations
-- [X] plan for DSL for configuration file
-- [ ] create generator's default output
+ - [X] parse C files for function and struct declarations
+ - [X] plan for DSL for configuration file
+ - [ ] create generator's default output
- [X] phase 1 - bind returnless, paramless functions
- [X] phase 2 - bind standard type return functions(e.g string or int), but still paramless
- - bool
- - int
- - float
- - double
- - string
+ - bool
+ - int
+ - float
+ - double
+ - string
- [X] phase 3 - bind standard type return or params
- - [ ] phase 4 - bind struct construction(returning struct objects)
- - [X] returning structs
- - [X] accessors for values inside structs
- - [ ] bind struct initializer
- - [ ] phase 5 - bind struct params(unwrapping structs)
-- [ ] clean up code
-- [ ] have generator use config DSL file to customize bindings
-- [ ] do it all again for C++
+- [X] phase 4 - bind struct construction(returning struct objects)
+ - [X] returning structs
+ - [X] accessors for values inside structs
+ - [X] bind struct initializer
+ - [ ] phase 5 - bind remaining struct related things
+ - [ ] struct as params of functions
+ - [ ] fix structs inside of structs
+ - [ ] for initilization
+ - [ ] for accessors
+ - [ ] clean up code
+ - [ ] have generator use config DSL file to customize bindings
+ - [ ] do it all again for C++
diff --git a/generate.rb b/generate.rb
index 1502bfb..df84164 100644
--- a/generate.rb
+++ b/generate.rb
@@ -108,14 +108,13 @@ end
$all_params = []
$bound_params = []
-# generates structs
-# TODO
-# Auto generate struct accessors
-#
+# generates structs, accessors, and initializers
glue.last.each do |struct, params|
defines += Tplt.init_struct_wrapper(struct)
init_body += Tplt.init_class(struct, LibraryName.downcase)
+ init_vars = ''
+
params.each do |param|
$all_params.push param
rpart = param.rpartition(' ')
@@ -147,9 +146,50 @@ glue.last.each do |struct, params|
defines += Tplt.function("#{struct}_set_#{param_name}", body)
init_body += Tplt.init_function("#{struct.downcase}_class", "#{param_name}=", "#{struct}_set_#{param_name}", "MRB_ARGS_REQ(1)")
- # init var with correct type
end
+
+ # initializer
+ # init the struct(using mrb to allocate)
+ # get values
+ # assign values to struct
+ # wrap struct
+ # return self
+ body = ''
+ body += Tplt.get_module(LibraryName)
+ body += Tplt.get_class(struct, LibraryName.downcase)
+ body += "#{struct} *wrapped_value = (#{struct} *)mrb_malloc(mrb, sizeof(#{struct}));\n"
+ #body += "*wrapped_value = {0};\n" #{func_name}("
+
+ init_array_body = ''
+ unwrapped_kwargs = ''
+ params.each_with_index do |param, index|
+ temp = param
+ temp_rpart = temp.rpartition(' ')
+ #if temp_rpart.first == 'const char *'
+ # temp = 'char *' + temp_rpart.last
+ #end
+ #init_var_body += temp + ";\n"
+ init_array_body += "mrb_intern_lit(mrb, \"#{temp_rpart.last}\"),\n"
+ #unwrapped_kwargs += Tplt.unwrap_kwarg(index, "#{temp_rpart.last} = #{Tplt.to_c(temp_rpart.first, "kw_values[#{index}]")};", nil, "#{temp_rpart.last} Argument Missing")
+ if Tplt.non_struct_types.include? temp_rpart.first
+ unwrapped_kwargs += Tplt.unwrap_kwarg(index, "wrapped_value->#{temp_rpart.last} = #{Tplt.to_c(temp_rpart.first, "kw_values[#{index}]")};\n")
+ else
+ # this is for structs or "undetermined" types
+ # doesnt work yet
+ next
+ #unwrapped_kwargs += Tplt.unwrap_kwarg(index, "wrapped_value->#{temp_rpart.last} = (#{temp_rpart.first})kw_values[#{index}];\n")
+ end
+ end
+ body += Tplt.get_kwargs(params.length, '', init_array_body)
+ body += unwrapped_kwargs
+
+ body += "mrb_data_init(self, wrapped_value, &mrb_#{struct}_struct);\n"
+ body += 'return self;'
+ defines += Tplt.function("#{struct}_initialize", body)
+ init_body += Tplt.init_function("#{struct.downcase}_class", "initialize", "#{struct}_initialize", "MRB_ARGS_OPT(1)")
+
+
end
# generates functions
@@ -221,10 +261,6 @@ glue.first.each do |func, params|
else
body += Tplt.get_module(LibraryName)
body += Tplt.get_class(func_datatype, LibraryName.downcase)
- #body += "mrb_value *object_mrb;"
- #body += Tplt.wrap_struct('wrapped_value', '*object_mrb', "mrb_#{func_datatype}_struct" , func_datatype)
- #body += Tplt.return_format_struct(func)
- #body += Tplt.make_mrb_obj_from_struct("*object_mrb", func, 'wrapped_value')
body += "#{func_datatype} *wrapped_value = (#{func_datatype} *)mrb_malloc(mrb, sizeof(#{func_datatype}));\n"
body += "*wrapped_value = #{func_name}("
params.each do |param|