From e475cfad32b31bb569dac23ab076ef125f365a11 Mon Sep 17 00:00:00 2001 From: Larry Xue Date: Sun, 23 Aug 2020 15:02:48 -0400 Subject: [PATCH] fix clangd --- c2logic/compiler.py | 10 ++++++---- c2logic/instructions.py | 17 ++++++++++++----- compile_flags.txt | 2 +- examples/control_flow.c | 5 ++++- examples/funcs.c | 2 +- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/c2logic/compiler.py b/c2logic/compiler.py index deb4669..74e935c 100644 --- a/c2logic/compiler.py +++ b/c2logic/compiler.py @@ -1,5 +1,5 @@ from pycparser.c_ast import Compound, Constant, DeclList, Enum, FileAST, FuncDecl, Struct, TypeDecl -from .instructions import BinaryOp, Enable, JumpCondition, Print, PrintFlush, Radar, RawAsm, RelativeJump, Sensor, Shoot, UnaryOp, Instruction, Set, Noop +from .instructions import BinaryOp, Enable, End, JumpCondition, Print, PrintFlush, Radar, RawAsm, RelativeJump, Sensor, Shoot, UnaryOp, Instruction, Set, Noop from pycparser import c_ast, parse_file from dataclasses import dataclass @@ -193,7 +193,7 @@ class Compiler(c_ast.NodeVisitor): self.visit(node.iftrue) #jump over else body from end of if body if node.iffalse is not None: - self.push(RelativeJump(None, JumpCondition("==", "0", "0"))) + self.push(RelativeJump(None, JumpCondition.always)) cond_jump_offset2 = len(self.curr_function.instructions) - 1 self.curr_function.instructions[cond_jump_offset].offset = len( self.curr_function.instructions @@ -205,11 +205,12 @@ class Compiler(c_ast.NodeVisitor): ) def visit_Break(self, node): - self.push(RelativeJump(None, JumpCondition("==", "0", "0"))) + self.push(RelativeJump(None, JumpCondition.always)) self.loop_end_jumps.append(len(self.curr_function.instructions) - 1) def visit_FuncCall(self, node): name = node.name.name + #TODO avoid duplication in psuedo-function calls if name == "_asm": arg = node.args.exprs[0] if not isinstance(arg, Constant) or arg.type != "string": @@ -283,7 +284,8 @@ class Compiler(c_ast.NodeVisitor): else: break self.push(Shoot(*args)) #pylint: disable=no-value-for-parameter - + elif name == "end": + self.push(End()) else: raise NotImplementedError(node) diff --git a/c2logic/instructions.py b/c2logic/instructions.py index 5105d94..9e5bf24 100644 --- a/c2logic/instructions.py +++ b/c2logic/instructions.py @@ -1,3 +1,4 @@ +from dataclasses import dataclass from .operations import binary_ops, condition_ops, unary_ops class Instruction: @@ -34,19 +35,21 @@ class UnaryOp(Instruction): def __str__(self): return f"uop {unary_ops[self.op]} {self.src} {self.dest}" +@dataclass class JumpCondition: - def __init__(self, op: str, left, right): - self.op = op - self.left = left - self.right = right + op: str + left: str + right: str @classmethod def from_binaryop(cls, binop: BinaryOp): - return JumpCondition(binop.op, binop.left, binop.right) + return cls(binop.op, binop.left, binop.right) def __str__(self): return f"{condition_ops[self.op]} {self.left} {self.right}" +JumpCondition.always = JumpCondition("==", "0", "0") + class RelativeJump(Instruction): def __init__(self, offset: int, cond: JumpCondition): self.offset = offset @@ -110,6 +113,10 @@ class Shoot(Instruction): def __str__(self): return f"control shoot {self.obj} {self.x} {self.y} {self.shoot} 0" +class End(Instruction): + def __str__(self): + return "end" + class RawAsm(Instruction): def __init__(self, code: str): self.code = code diff --git a/compile_flags.txt b/compile_flags.txt index c313eec..8667f37 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,4 +1,4 @@ -I ./include -Wall -Wextra --std=c11 \ No newline at end of file +-Wno-main-return-type \ No newline at end of file diff --git a/examples/control_flow.c b/examples/control_flow.c index e039739..c221017 100644 --- a/examples/control_flow.c +++ b/examples/control_flow.c @@ -1,10 +1,13 @@ -#include "mindustry.h" +#include "../include/mindustry.h" extern struct MindustryObject message1; void main(void) { double i = 0; while (i < 10) { printd(i); i++; + if (i == 4) { + break; + } } print("\n"); i = 0; diff --git a/examples/funcs.c b/examples/funcs.c index 55defce..43c3193 100644 --- a/examples/funcs.c +++ b/examples/funcs.c @@ -1,4 +1,4 @@ -#include "mindustry.h" +#include "../include/mindustry.h" extern struct MindustryObject message1; extern struct MindustryObject swarmer1; extern struct MindustryObject conveyor1;