def cb_op_plusminus(tokens): tokens = tokens[0] if len(tokens) == 3: # binary op assert isinstance(tokens[0], AstNode) assert isinstance(tokens[2], AstNode) op, args = tokens[1], [tokens[0], tokens[2]] elif len(tokens) > 3: args = [tokens.pop(0)] i = 0 while i < len(tokens): op = tokens[i] arg = tokens[i + 1] i += 2 if op == '-': arg = -arg elif op == '+': pass else: raise ValueError("Bad operator") args.append(arg) op = '+' else: raise ValueError("Parsing error") assert all(isinstance(arg, AstNode) for arg in args) result = AstOp(op, *args) return result
def cb_op_mul(tokens): tokens = tokens[0] assert len(tokens) == 3 assert isinstance(tokens[0], AstNode) assert isinstance(tokens[2], AstNode) # binary op op, args = tokens[1], [tokens[0], tokens[2]] result = AstOp(op, *args) return result
def merge_ops(tokens, op): args = [] if len(tokens) >= 3: args = [tokens.pop(0)] i = 0 while i < len(tokens): op_tmp = tokens[i] arg = tokens[i + 1] i += 2 if op_tmp != op: raise ValueError("Bad operator") args.append(arg) result = AstOp(op, *args) return result
def cb_postinc(tokens): assert len(tokens) == 1 result = AstMem(AstOp('postinc', tokens[0]), 32) return result
def cb_predec(tokens): assert len(tokens) == 1 result = AstMem(AstOp('predec', tokens[0]), 32) return result
def cb_op_div(tokens): tokens = tokens[0] assert len(tokens) == 3 assert tokens[1] == "/" result = AstOp("/", tokens[0], tokens[2]) return result
def cb_op_not(tokens): tokens = tokens[0] assert len(tokens) == 2 assert tokens[0] == "!" result = AstOp("!", tokens[1]) return result
def cb_deref_pinc(tokens): assert len(tokens) == 1 result = AstOp('autoinc', *tokens) return result