rust.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from sccd.test.static.syntax import *
  2. from sccd.statechart.codegen.rust import *
  3. from sccd.util.indenting_writer import *
  4. import os
  5. import sccd.statechart.codegen
  6. rustlib = os.path.dirname(sccd.statechart.codegen.__file__) + "/common.rs"
  7. # class TestRustGenerator(StatechartRustGenerator):
  8. # def visit_TestVariant(self, variant):
  9. def compile_test(variants: List[TestVariant], w: IndentingWriter):
  10. # Note: The reason for these is that we cannot convert the casing of our state's names:
  11. # SCCD allows any combination of upper and lower case symbols, and
  12. # converting to, say, camelcase, as Rust likes it for type names,
  13. # could cause naming collisions.
  14. # Rust may output a ton of warnings for this. We disable these types of warnings,
  15. # so that other, more interesting types of warnings don't go unnoticed.
  16. w.writeln("#![allow(non_camel_case_types)]")
  17. w.writeln("#![allow(non_snake_case)]")
  18. w.writeln("#![allow(unused_labels)]")
  19. w.writeln("#![allow(unused_variables)]")
  20. w.writeln("#![allow(dead_code)]")
  21. w.writeln("#![allow(unused_parens)]")
  22. w.writeln("#![allow(unused_macros)]")
  23. w.writeln("#![allow(non_upper_case_globals)]")
  24. w.writeln("#![allow(unused_mut)]")
  25. w.writeln("#![allow(unused_imports)]")
  26. with open(rustlib, 'r') as file:
  27. data = file.read()
  28. w.writeln(data)
  29. if len(variants) > 0:
  30. cd = variants[0].cd
  31. gen = StatechartRustGenerator(w, cd.globals)
  32. cd.get_default_class().accept(gen)
  33. # compile_statechart(cd.get_default_class(), cd.globals, w)
  34. w.writeln("fn main() {")
  35. w.indent()
  36. if DEBUG:
  37. w.writeln("debug_print_sizes();")
  38. for n, v in enumerate(variants):
  39. w.writeln("// Test variant %d" % n)
  40. w.writeln("let mut raised = Vec::<OutEvent>::new();")
  41. w.writeln("let mut output = |out: OutEvent| {")
  42. if DEBUG:
  43. w.writeln(" eprintln!(\"^{}:{}\", out.port, out.event);")
  44. w.writeln(" raised.push(out);")
  45. w.writeln("};")
  46. w.writeln("let mut controller = Controller::<InEvent>::new();")
  47. w.writeln("let mut sc: Statechart = Default::default();")
  48. w.writeln("sc.init(&mut controller, &mut output);")
  49. for i in v.input:
  50. if len(i.events) > 1:
  51. raise UnsupportedFeature("Multiple simultaneous input events not supported")
  52. elif len(i.events) == 0:
  53. raise UnsupportedFeature("Test declares empty bag of input events")
  54. w.writeln("controller.set_timeout(%d, InEvent::%s);" % (i.timestamp.opt, ident_event_type(i.events[0].name)))
  55. w.writeln("controller.run_until(&mut sc, Until::Eternity, &mut output);")
  56. w.writeln("assert_eq!(raised, [%s]);" % ", ".join('OutEvent{port:"%s", event:"%s"}' % (e.port, e.name) for o in v.output for e in o))
  57. w.writeln("eprintln!(\"Test variant %d passed\");" % n)
  58. w.dedent()
  59. w.writeln("}")