def le(l, r): return BinaryOp(Token(TokenTypes.LESS_EQUAL, "<="), l, r)
def ge(l, r): return BinaryOp(Token(TokenTypes.GREATER_EQUAL, ">="), l, r)
def lt(l, r): return BinaryOp(Token(TokenTypes.LESS, "<"), l, r)
def gt(l, r): return BinaryOp(Token(TokenTypes.GREATER, ">"), l, r)
def _and(l, r): return BinaryOp(Token(TokenTypes.AND, "and"), l, r)
def _or(l, r): return BinaryOp(Token(TokenTypes.OR, "or"), l, r)
def _is_not(l, r): return BinaryOp(Token(TokenTypes.NOT_EQUAL, "is not"), l, r)
def t(s): return Token.from_str(s)
def mod(l, r): return BinaryOp(Token(TokenTypes.MOD, "%"), l, r)
def _is(l, r): return BinaryOp(Token(TokenTypes.EQUAL, "is"), l, r)
def mult(l, r): return BinaryOp(Token(TokenTypes.MULT, "*"), l, r)
def v(n): return Var(Token(TokenTypes.VAR, n))
def c(v): return Int(Token(TokenTypes.INT, v))
from littlepython.ast import Int, Var, BinaryOp, UnaryOp, Assign, ControlBlock, If, Block, GetArrayItem, SetArrayItem, \ ForLoop, Function, FunctionDef, FunctionSig, Call, Return from littlepython.tokenizer import Token, TokenTypes def t(s): return Token.from_str(s) # def iterator_to_generator(iterator): # return (i for i in iterator) ADD = Token(TokenTypes.ADD, "+") SUB = Token(TokenTypes.SUB, "-") DIV = Token(TokenTypes.DIV, "/") NOT = Token(TokenTypes.NOT, "not") EOF = Token(TokenTypes.EOF, None) def add(l, r=None): if r: return BinaryOp(ADD, l, r) return UnaryOp(ADD, l) def sub(l, r=None): if r: return BinaryOp(SUB, l, r) return UnaryOp(SUB, l)
def test_token_from_str_invalid(): with pytest.raises(ValueError) as err: Token.from_str("^") assert "Invalid input" in str(err.value)