Browse Source

Remove hardcoded instructions and start on means to read builtin.c for instructions

dynamic
Raphael Roberts 5 years ago
parent
commit
bb4698792c
  1. 54
      c2logic/instruction_parser.py
  2. 105
      c2logic/instructions.py
  3. 2
      include/builtins.c
  4. 47
      include/builtins_new.h

54
c2logic/instruction_parser.py

@ -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)

105
c2logic/instructions.py

@ -88,111 +88,12 @@ class Goto(Instruction):
def __str__(self): def __str__(self):
return f"jump {self.func_start + self.offset} {JumpCondition.always}" return f"jump {self.func_start + self.offset} {JumpCondition.always}"
class Print(Instruction):
def __init__(self, val: str):
self.val = val
def __str__(self):
return f"print {self.val}"
class PrintFlush(Instruction):
def __init__(self, message: str):
self.message = message
def __str__(self):
return f"printflush {self.message}"
class Radar(Instruction):
def __init__(
self, dest: str, src: str, target1: str, target2: str, target3: str, sort: str, index: str
):
self.src = src
self.dest = dest
self.target1 = target1
self.target2 = target2
self.target3 = target3
self.sort = sort
self.index = index
def __str__(self):
return f"radar {self.target1} {self.target2} {self.target3} {self.sort} {self.src} {self.index} {self.dest}"
class Sensor(Instruction):
def __init__(self, dest: str, src: str, prop: str):
self.dest = dest
self.src = src
self.prop = prop
def __str__(self):
return f"sensor {self.dest} {self.src} @{self.prop}"
class Enable(Instruction):
def __init__(self, obj: str, enabled: str):
self.obj = obj
self.enabled = enabled
def __str__(self):
return f"control enabled {self.obj} {self.enabled} 0 0 0"
class Shoot(Instruction):
def __init__(self, obj: str, x: str, y: str, shoot: str):
self.obj = obj
self.x = x
self.y = y
self.shoot = shoot
def __str__(self):
return f"control shoot {self.obj} {self.x} {self.y} {self.shoot} 0"
class GetLink(Instruction):
def __init__(self, dest: str, index: str):
self.dest = dest
self.index = index
def __str__(self):
return f"getlink {self.dest} {self.index}"
class Read(Instruction):
def __init__(self, dest: str, src: str, index: str):
self.dest = dest
self.src = src
self.index = index
def __str__(self):
return f"read {self.dest} {self.src} {self.index}"
class Write(Instruction):
def __init__(self, src: str, dest: str, index: str):
self.dest = dest
self.src = src
self.index = index
def __str__(self):
return f"write {self.src} {self.dest} {self.index}"
class Draw(Instruction):
def __init__(self, cmd: str, *args):
self.cmd = cmd
self.args = args
def __str__(self):
args = list(self.args) + ['0'] * (6 - len(self.args))
return f"draw {self.cmd} {' '.join(args)}"
class DrawFlush(Instruction):
def __init__(self, display: str):
self.display = display
def __str__(self):
return f"drawflush {self.display}"
class End(Instruction):
def __str__(self):
return "end"
class RawAsm(Instruction): class RawAsm(Instruction):
def __init__(self, code: str): def __init__(self, code: str):
self.code = code self.code = code
def __str__(self): def __str__(self):
return self.code return self.code
class ParsedInstruction(Instruction):
pass

2
include/builtins.c

@ -129,6 +129,6 @@ void unit_build(double x, double y, char* block, double rotation, char* configur
double unit_within(double x, double y, double radius) { double unit_within(double x, double y, double radius) {
asm("ucontrol within {x} {y} {radius} {dest} 0"); asm("ucontrol within {x} {y} {radius} {dest} 0");
} }
MindustryObject unit_radar(char* target1, char* target2, char* target3, char* sort, double order) {
struct MindustryObject unit_radar(char* target1, char* target2, char* target3, char* sort, double order) {
asm("uradar {target1} {target2} {target3} {sort} 0 {order} {dest}"); asm("uradar {target1} {target2} {target3} {sort} 0 {order} {dest}");
} }

47
include/builtins_new.h

@ -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);
Loading…
Cancel
Save