Browse Source

misc

rlbr-dev
Larry Xue 5 years ago
parent
commit
03fa84f637
  1. 23
      c2logic/compiler.py
  2. 17
      examples/control_flow.c

23
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()

17
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");
}
Loading…
Cancel
Save