Skip to content

Commit

Permalink
Merge pull request #1182 from colinbellino/jai-bindgen-comments
Browse files Browse the repository at this point in the history
Include comments in Jai bindgen
  • Loading branch information
floooh authored Jan 12, 2025
2 parents 9d8850d + 578b37a commit ffa3bb6
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion bindgen/gen_jai.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#
# Generate Jai bindings.
#-------------------------------------------------------------------------------
import textwrap
import gen_ir
import gen_util as util
import os, shutil, sys
Expand Down Expand Up @@ -145,6 +146,16 @@ def l(s):
global out_lines
out_lines += s + '\n'

def c(s, indent=""):
if not s:
return
if '\n' in s:
l(f'{indent}/*')
l(textwrap.indent(textwrap.dedent(s), prefix=f" {indent}"))
l(f'{indent}*/')
else:
l(f'{indent}// {s.strip()}')

def check_override(name, default=None):
if name in overrides:
return overrides[name]
Expand Down Expand Up @@ -395,12 +406,15 @@ def gen_c_imports(inp, c_prefix, prefix):
args = funcdecl_args_c(decl, prefix)
res_type = funcdecl_result_c(decl, prefix)
res_str = '-> void' if res_type == '' else f'-> {res_type}'
if decl.get('comment'):
c(decl['comment'])
l(f"{decl['name']} :: ({args}) {res_str} #foreign {clib_import};")
l('')

def gen_consts(decl, prefix):
for item in decl['items']:
item_name = check_override(item['name'])
c(item.get('comment'))
l(f"{as_snake_case(item_name, prefix)} :: {item['value']};")
l('')

Expand All @@ -421,6 +435,8 @@ def gen_struct(decl, prefix):

def gen_enum(decl, prefix):
enum_name = check_override(decl['name'])
if decl.get('comment'):
c(decl['comment'])
l(f'{as_struct_or_enum_type(enum_name, prefix)} :: enum u32 {{')
for item in decl['items']:
item_name = as_enum_item_name(check_override(item['name']))
Expand Down Expand Up @@ -449,6 +465,9 @@ def gen_helpers(inp):
def gen_module(inp, c_prefix, dep_prefixes):
pre_parse(inp)
l('// machine generated, do not edit')
if inp.get('comment'):
l('')
c(inp['comment'])
gen_imports(dep_prefixes)
gen_helpers(inp)
prefix = inp['prefix']
Expand Down Expand Up @@ -495,7 +514,7 @@ def gen(c_header_path, c_prefix, dep_c_prefixes):
shutil.copyfile(c_header_path, f'{c_root}/{os.path.basename(c_header_path)}')
csource_path = get_csource_path(c_prefix)
module_name = module_names[c_prefix]
ir = gen_ir.gen(c_header_path, csource_path, module_name, c_prefix, dep_c_prefixes)
ir = gen_ir.gen(c_header_path, csource_path, module_name, c_prefix, dep_c_prefixes, with_comments=True)
gen_module(ir, c_prefix, dep_c_prefixes)
with open(f"{module_root}/{ir['module']}/module.jai", 'w', newline='\n') as f_outp:
f_outp.write(out_lines)

0 comments on commit ffa3bb6

Please sign in to comment.