From 03fa84f6375235e7bede9ec69eaeb24abffbb792 Mon Sep 17 00:00:00 2001 From: Larry Xue Date: Sun, 23 Aug 2020 17:45:45 -0400 Subject: [PATCH] misc --- c2logic/compiler.py | 23 ++++++++++++----------- examples/control_flow.c | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/c2logic/compiler.py b/c2logic/compiler.py index 4cf5954..42b127b 100644 --- a/c2logic/compiler.py +++ b/c2logic/compiler.py @@ -25,14 +25,14 @@ class Compiler(c_ast.NodeVisitor): """ def __init__(self, opt_level=0): self.opt_level = opt_level - self.functions = [] + self.functions: dict = None self.curr_function: Function = None self.loop_start: int = None self.loop_end_jumps: list = None #self.cond_jump_offset: int = None def compile(self, filename: str): - self.functions = [] + self.functions = {} self.curr_function = None self.loop_start = None self.cond_jump_offset = None @@ -41,10 +41,10 @@ class Compiler(c_ast.NodeVisitor): #TODO actually handle functions properly out = [] offset = 0 - for function in self.functions: + for function in self.functions.values(): function.start = offset offset += len(function.instructions) - for function in self.functions: + for function in self.functions.values(): instructions = function.instructions out2 = [] for instruction in instructions: @@ -112,15 +112,16 @@ class Compiler(c_ast.NodeVisitor): #visitors def visit_FuncDef(self, node): # function definitions + func_name = node.decl.name func_decl = node.decl.type args = [arg_decl.name for arg_decl in func_decl.args.params] - self.curr_function = Function(node.decl.name, args, [], None) + self.curr_function = Function(func_name, args, [], None) self.visit(node.body) #in case for loop is the last thing in a function to ensure the jump target is valid if self.loop_start is not None: self.push(Noop()) - self.functions.append(self.curr_function) + self.functions[func_name] = self.curr_function def visit_Decl(self, node): if isinstance(node.type, TypeDecl): # variable declaration @@ -135,9 +136,8 @@ class Compiler(c_ast.NodeVisitor): #TODO structs raise NotImplementedError(node) elif isinstance(node.type, Enum): - if node.type.name != "RadarTarget": - #TODO enums - raise NotImplementedError(node) + #TODO enums + raise NotImplementedError(node) else: raise NotImplementedError(node) @@ -322,9 +322,10 @@ def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument("file") - parser.add_argument("-O", "--optimization-level", type=int, default=1) + parser.add_argument("-O", "--optimization-level", type=int, choices=range(3), default=1) + parser.add_argument("-o", "--output", type=argparse.FileType('w'), default="-") args = parser.parse_args() - print(Compiler(args.optimization_level).compile(args.file)) + print(Compiler(args.optimization_level).compile(args.file), file=args.output) if __name__ == "__main__": main() \ No newline at end of file diff --git a/examples/control_flow.c b/examples/control_flow.c index 3c9396f..3c90892 100644 --- a/examples/control_flow.c +++ b/examples/control_flow.c @@ -1,20 +1,28 @@ #include "../include/mindustry.h" +/*expected output: +5678 +0246 +0 is not 3 +*/ extern struct MindustryObject message1; void main(void) { - int i = 0; + int i; for (i = 5; i < 9; i++) { printd(i); } + i = 0; + print("\n"); do { if (i % 2 == 1) { + i++; continue; } printd(i); - ++i; - if (i == 8) { + if (i == 6) { break; } - } while (!(i >= 10)); + ++i; + } while (i < 10); print("\n"); i = 0; printd(i); @@ -25,5 +33,4 @@ void main(void) { } print("\n"); printflush(message1); - asm("noop"); } \ No newline at end of file