def __init__( self, compiler=None, parser=None, functions=None, stdin=None, stdout=None, stderr=None, handle_error=None, ): if compiler is None: compiler = Compiler() if parser is None: parser = Parser(preprocessor=Preprocessor()) if functions is None: functions = {} if handle_error is None: handle_error = self._show_traceback self._handle_error = handle_error self.compiler = compiler self.parser = parser self.functions = functions # NOTE: This uses streamio, which by its own admission "isn't # ready for general usage" self.stdin = stdin if stdin is not None else _open(fd=0) self.stdout = stdout if stdout is not None else _open(fd=1) self.stderr = stderr if stderr is not None else _open(fd=2)
def test_run_source_files(self): self.assertEqual( cli.parse_args(["-I", "a/include", "-I", "b/include", "file.c"]), cli.CommandLine( action=cli.run_source, source_files=["file.c"], cycy=CyCy(parser=IncrementalParser(parser=Parser( preprocessor=preprocessor.with_directories( ["a/include", "b/include"], ), ), ), ), ), )
def test_run_source_string(self): self.assertEqual( cli.parse_args(["-I", "a/include", "-c", "int main (void) {}"]), cli.CommandLine( action=cli.run_source, source_string="int main (void) {}", cycy=CyCy(parser=IncrementalParser(parser=Parser( preprocessor=preprocessor.with_directories( ["a/include"], ), ), ), ), ), )
def parse_args(args): source_string = "" source_files = [] include_paths = [] arguments = iter(args) for argument in arguments: if argument in ("-h", "--help"): return CommandLine(action=print_help) if argument in ("-version", "--version"): return CommandLine(action=print_version) if argument in ("-I", "--include"): try: include_paths.append(next(arguments)) except StopIteration: return CommandLine( action=print_help, failure="-I expects an argument", ) elif argument == "-c": source = [] # this is the simplest valid RPython currently for argument in arguments: source.append(argument) source_string = " ".join(source) if not source_string: return CommandLine( action=print_help, failure="-c expects an argument", ) elif not argument.startswith("-"): source_files.append(argument) else: return CommandLine( action=print_help, failure="Unknown argument %s" % (argument, ), ) cycy = CyCy(parser=IncrementalParser(parser=Parser( preprocessor=preprocessor.with_directories(directories=include_paths, ), ), )) if source_files or source_string: return CommandLine( action=run_source, cycy=cycy, source_files=source_files, source_string=source_string, ) else: return CommandLine(action=run_repl, cycy=cycy)
class TestCompilerIntegration(TestCase): def setUp(self): self.compiler = compiler.Compiler() self.parser = Parser() def assertCompiles(self, source, to=None): """ The given source code is successfully compiled, optionally to the provided expected output. """ ast = self.parser.parse(source="int main(void) { " + source + "}") self.compiler.compile(ast) main = self.compiler.constants[self.compiler.functions["main"]] expected = dedent(cleaned(to).strip("\n")).strip("\n") self.assertEqual(main.bytecode.dump(pretty=False), expected) def test_basic_neq(self): self.assertCompiles( "2 != 3;", """ 0 LOAD_CONST 1 2 LOAD_CONST 2 4 BINARY_NEQ """) def test_char_array(self): self.assertCompiles( "const char* foo = \"foo\";", """ 0 LOAD_CONST 1 2 STORE_VARIABLE 0 """) def test_array_dereference(self): self.assertCompiles( "const char* foo = \"foo\"; foo[3];", """ 0 LOAD_CONST 1 2 STORE_VARIABLE 0 4 LOAD_CONST 2 6 LOAD_VARIABLE 0 8 DEREFERENCE """)
def setUp(self): self.preprocessor = Preprocessor(includers=[FakeIncluder()]) self.parser = Parser(preprocessor=self.preprocessor) self.cycy = CyCy(parser=self.parser)
def setUp(self): self.compiler = compiler.Compiler() self.parser = Parser()
def setUp(self): self.parse = Parser().parse