Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions generator/sbpg/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@
import os
import pprint
import sbpg.specs.yaml2 as yaml
import sbpg.targets.c as c
import sbpg.targets.test_c as test_c
import sbpg.targets.java as java
import sbpg.targets.latex as tex
import sbpg.targets.haskell as hs
import sbpg.targets.protobuf as pb
import sbpg.targets.python as py
import sbpg.targets.pythonNG as pyNG
import sbpg.targets.javascript as js
import sbpg.targets.rust as rs

def get_args():
parser = argparse.ArgumentParser(description='Swift Navigation SBP generator.')
Expand Down Expand Up @@ -89,15 +79,15 @@ def main():
args = get_args().parse_args()
verbose = args.verbose
assert args.pythonNG or args.python or args.javascript or args.c or args.test_c or args.haskell or args.latex or args.protobuf or args.java or args.rust, \
"Please specify a target language."
"Please specify a target language."
input_file = os.path.abspath(args.input_file[0])
assert len(args.input_file) == 1
assert os.path.exists(input_file), \
"Invalid input file: %s. Exiting!" % input_file
"Invalid input file: %s. Exiting!" % input_file
output_dir = os.path.abspath(args.output_dir[0])
assert len(args.output_dir) == 1, "Only 1 output directory at a time."
assert os.path.exists(output_dir), \
"Invalid output directory: %s. Exiting!" % output_dir
"Invalid output directory: %s. Exiting!" % output_dir
# Ingest, parse, and validate.
test_mode = args.test_c

Expand All @@ -114,6 +104,7 @@ def main():
pprint.pprint(list(file_index.keys()))
print("Writing to %s" % output_dir)
if args.latex:
import sbpg.targets.latex as tex
parsed = [yaml.parse_spec(spec) for spec in file_index.values()]
tex.render_source(output_dir, parsed, args.release[0])
else:
Expand All @@ -129,22 +120,31 @@ def main():
if not parsed.render_source:
continue
if args.python:
import sbpg.targets.python as py
py.render_source(output_dir, parsed)
if args.pythonNG:
import sbpg.targets.pythonNG as pyNG
pyNG.render_source(output_dir, parsed)
elif args.javascript:
import sbpg.targets.javascript as js
js.render_source(output_dir, parsed)
elif args.c:
import sbpg.targets.c as c
c.render_source(output_dir, parsed)
elif args.test_c:
import sbpg.targets.test_c as test_c
test_c.render_source(output_dir, parsed)
elif args.haskell:
import sbpg.targets.haskell as hs
hs.render_source(output_dir, parsed)
elif args.java:
import sbpg.targets.java as java
java.render_source(output_dir, parsed)
elif args.rust:
import sbpg.targets.rust as rs
rs.render_source(output_dir, parsed)
elif args.protobuf:
import sbpg.targets.protobuf as pb
pb.render_source(output_dir, parsed)
if args.c:
c.render_version(output_dir, args.release[0])
Expand All @@ -167,5 +167,6 @@ def main():
except KeyboardInterrupt:
pass


if __name__ == '__main__':
main()
7 changes: 7 additions & 0 deletions generator/sbpg/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ def static(self):
def __repr__(self):
return fmt_repr(self)

def __lt__(self, o):
# Default Python 2 behavior according to:
# https://yz.mit.edu/wp/default-behavior-of-pythons-cmp/
if self.__class__.__name__ == o.__class__.__name__:
return id(self) < id(o)
return self.__class__.__name__ < o.__class__.__name__

class Struct(Definition):
def __init__(self, defn):
self.__dict__.update(defn.__dict__)
Expand Down
8 changes: 4 additions & 4 deletions generator/sbpg/targets/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def commentify(value):
'string': 'String'}

def type_map(field):
if TYPE_MAP.has_key(field.type_id):
if field.type_id in TYPE_MAP:
return TYPE_MAP[field.type_id]
elif field.type_id == 'array':
t = field.options['fill'].value
Expand All @@ -69,7 +69,7 @@ def parse_type(field):
Function to pull a type from the binary payload.
"""
if field.type_id == 'string':
if field.options.has_key('size'):
if 'size' in field.options:
return "::parser::read_string_limit(_buf, %s)" % field.options['size'].value
else:
return "::parser::read_string(_buf)"
Expand All @@ -84,12 +84,12 @@ def parse_type(field):
# Call function to build array
t = field.options['fill'].value
if t in TYPE_MAP.keys():
if field.options.has_key('size'):
if 'size' in field.options:
return '::parser::read_%s_array_limit(_buf, %d)' % (t, field.options['size'].value)
else:
return '::parser::read_%s_array(_buf)' % t
else:
if field.options.has_key('size'):
if 'size' in field.options:
return '%s::parse_array_limit(_buf, %d)' % (t, field.options['size'].value)
else:
return '%s::parse_array(_buf)' % t
Expand Down
2 changes: 2 additions & 0 deletions rust/sbp/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
comment_width = 95
wrap_comments = true