Browse Source
Remove hardcoded instructions and start on means to read builtin.c for instructions
dynamic
Remove hardcoded instructions and start on means to read builtin.c for instructions
dynamic
4 changed files with 105 additions and 103 deletions
-
54c2logic/instruction_parser.py
-
105c2logic/instructions.py
-
2include/builtins.c
-
47include/builtins_new.h
@ -0,0 +1,54 @@ |
|||||
|
from pycparser import c_parser, c_ast |
||||
|
|
||||
|
def extract_asm(body: c_ast.Compound): |
||||
|
for item in body.block_items: |
||||
|
if isinstance(item, c_ast.FuncCall): |
||||
|
if item.name.name == 'asm': |
||||
|
for expr in item.args.exprs: |
||||
|
if isinstance(expr, c_ast.Constant) and expr.type == 'string': |
||||
|
return expr.value |
||||
|
|
||||
|
def get_decl_info(decl: c_ast.Decl): |
||||
|
decl_info = decl.type |
||||
|
maybe_string = False |
||||
|
if isinstance(decl_info, c_ast.PtrDecl): |
||||
|
maybe_string = True |
||||
|
decl_info = decl_info.type |
||||
|
name = decl_info.declname |
||||
|
datatype = decl_info.type |
||||
|
if isinstance(datatype, c_ast.Struct): |
||||
|
return name, datatype.name |
||||
|
elif maybe_string: |
||||
|
if datatype.names[0] == 'char': |
||||
|
return name, 'string' |
||||
|
else: |
||||
|
return name, datatype.names[0] |
||||
|
|
||||
|
class InstructionReader(c_ast.NodeVisitor): |
||||
|
def __init__(self): |
||||
|
self.funcs = dict() |
||||
|
super().__init__() |
||||
|
|
||||
|
def visit_FuncDef(self, node): |
||||
|
func_name = node.decl.name |
||||
|
record = self.funcs.setdefault(func_name, dict()) |
||||
|
record['asm'] = extract_asm(node.body)[1:-1] |
||||
|
self.parse_func_decl(node.decl.type) |
||||
|
|
||||
|
def parse_func_decl(self, node: c_ast.FuncDecl): |
||||
|
return_info = node.type |
||||
|
func_name = return_info.declname |
||||
|
record = self.funcs.setdefault(func_name, dict()) |
||||
|
try: |
||||
|
record['args'] = tuple(get_decl_info(decl) for decl in node.args.params) |
||||
|
|
||||
|
except AttributeError: |
||||
|
record['args'] = tuple() |
||||
|
|
||||
|
if __name__ == "__main__": |
||||
|
parser = c_parser.CParser() |
||||
|
with open('/home/raphael/cloned-repos/c2logic/include/builtins.c') as c_file: |
||||
|
text = c_file.read() |
||||
|
my_ast = parser.parse(text) |
||||
|
reader = InstructionReader() |
||||
|
reader.visit(my_ast) |
||||
@ -0,0 +1,47 @@ |
|||||
|
struct MindustryObject {}; |
||||
|
// builtin instructions |
||||
|
void print(char* s); |
||||
|
void printd(double s); |
||||
|
void printflush(struct MindustryObject message); |
||||
|
struct MindustryObject radar(struct MindustryObject obj, char* target1, char* target2, |
||||
|
char* target3, char* sort, double index); |
||||
|
double sensor(struct MindustryObject obj, char* prop); |
||||
|
void enable(struct MindustryObject obj, double enabled); |
||||
|
void configure(struct MindustryObject obj, char* configure); |
||||
|
void setcolor(struct MindustryObject obj, double r, double g, double b); |
||||
|
void shoot(struct MindustryObject obj, double x, double y, double shoot); |
||||
|
void shootp(struct MindustryObject obj, double unit, double shoot); |
||||
|
struct MindustryObject get_link(double index); |
||||
|
double read(struct MindustryObject cell, double index); |
||||
|
void write(double val, struct MindustryObject cell, double index); |
||||
|
void drawclear(double r, double g, double b); |
||||
|
void drawcolor(double r, double g, double b, double a); |
||||
|
void drawstroke(double width); |
||||
|
void drawline(double x1, double y1, double x2, double y2); |
||||
|
void drawrect(double x, double y, double w, double h); |
||||
|
void drawlinerect(double x, double y, double w, double h); |
||||
|
void drawpoly(double x, double y, double sides, double radius, double rotation); |
||||
|
void drawlinepoly(double x, double y, double sides, double radius, double rotation); |
||||
|
void drawtriangle(double x1, double y1, double x2, double y2, double x3, double y3); |
||||
|
void drawimage(double x, double y, char* image, double size, double rotation); |
||||
|
void drawflush(struct MindustryObject display); |
||||
|
void end(); |
||||
|
// unit commands (not complete; don't know how to return multiple values) |
||||
|
void ubind(char* type); |
||||
|
void unit_move(double x, double y); |
||||
|
void unit_idle(); |
||||
|
void unit_stop(); |
||||
|
void unit_approach(double x, double y, double radius); |
||||
|
void unit_boost(double enable); |
||||
|
void unit_pathfind(); |
||||
|
void unit_target(double x, double y, double shoot); |
||||
|
void unit_targetp(double unit, double shoot); |
||||
|
void unit_item_drop(struct MindustryObject obj, double amount); |
||||
|
void unit_item_take(struct MindustryObject obj, char* item, double amount); |
||||
|
void unit_pay_drop(); |
||||
|
void unit_pay_take(double takeUnits); |
||||
|
void unit_mine(double x, double y); |
||||
|
void unit_flag(double value); |
||||
|
void unit_build(double x, double y, char* block, double rotation, char* configure); |
||||
|
double unit_within(double x, double y, double radius); |
||||
|
MindustryObject unit_radar(char* target1, char* target2, char* target3, char* sort, double order); |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue