def visitVar_parens(self, ctx: parse.Var_parensContext): """var_parens (the ambiguous y(x+1) structure) var LPAREN expr RPAREN (CARET tex_symb)?""" # tex_symb is present if the var_parens structure is raised to a power: # for example y(x+1)^2 var = self.visit(ctx.var()) var_val = self.state.get(var.name) expr = self.visit(ctx.expr()) exponent = ctx.tex_symb() if isinstance(var_val, UserDefinedFunc) or var_val is None: # the variable is a function, so call it if exponent is not None: return Expression(op.pow, FunctionCall(var.name, [expr]), self.visit(exponent)) else: return FunctionCall(var.name, [expr]) else: # otherwise this is a multiplication if exponent is not None: # x((y+1)^z) return Expression( op.mul, var, Expression(op.pow, expr, self.visit(exponent))) else: return Expression(op.mul, var, expr)
def visitVar_pow_expr(self, ctx: parse.Var_pow_exprContext): """var_pow_expr var (CARET tex_symb)?""" exponent = ctx.tex_symb() if exponent is None: return self.visit(ctx.var()) return Expression(op.pow, self.visit(ctx.var()), self.visit(exponent))
def visitIme_mult(self, ctx: parse.Ime_multContext): """implicit_mult_expr_mult implicit_mult_expr implicit_mult_expr""" return Expression( op.mul, self.visit(ctx.implicit_mult_expr(0)), self.visit(ctx.implicit_mult_expr(1)), )
def visitAdd_expr_recurse(self, ctx: parse.Add_expr_recurseContext): """add_expr_recurse add_expr op=(PLUS | MINUS) add_expr""" return Expression( op.add if ctx.op.type == parse.PLUS else op.sub, self.visit(ctx.add_expr(0)), self.visit(ctx.add_expr(1)), )
def visitMult_sign(self, ctx: parse.Mult_signContext): """mult_sign sign=(PLUS | MINUS) mult_expr """ if ctx.sign.type == parse.MINUS: return Expression(op.mul, RealNumber(-1), self.visit(ctx.mult_expr())) else: return self.visit(ctx.mult_expr())
def visitParen_pow_expr(self, ctx: parse.Paren_pow_exprContext): """paren_pow_expr paren (CARET tex_symb)?""" exponent = ctx.tex_symb() if exponent is None: return self.visit(ctx.unit_paren()) return Expression(op.pow, self.visit(ctx.unit_paren()), self.visit(exponent))
def visitMult_expr_recurse(self, ctx: parse.Mult_expr_recurseContext): """mult_expr_recurse mult_expr op=(MULT | CMD_TIMES | CMD_CDOT | DIV | CMD_DIV) mult_expr """ return Expression( op.truediv if ctx.op.type in {parse.DIV, parse.CMD_DIV} else op.mul, self.visit(ctx.mult_expr(0)), self.visit(ctx.mult_expr(1)), )
def visitIme_var_unit_paren_left( self, ctx: parse.Ime_var_unit_paren_leftContext): """ime_var_unit_paren_left (var_pow_expr | paren_pow_expr) implicit_mult_expr""" if ctx.var_pow_expr(): left_expr = self.visit(ctx.var_pow_expr()) elif ctx.paren_pow_expr(): left_expr = self.visit(ctx.paren_pow_expr()) return Expression(op.mul, left_expr, self.visit(ctx.implicit_mult_expr()))
def visitIme_var_unit_paren_right( self, ctx: parse.Ime_var_unit_paren_rightContext): """ime_var_unit_paren_right implicit_mult_expr (var_pow_expr | paren_pow_expr)""" if ctx.var_pow_expr(): right_expr = self.visit(ctx.var_pow_expr()) elif ctx.paren_pow_expr(): right_expr = self.visit(ctx.paren_pow_expr()) return Expression(op.mul, self.visit(ctx.implicit_mult_expr()), right_expr)
def visitIme_left(self, ctx: parse.Ime_leftContext): """implicit_mult_expr_left left_implicit_pow_expr (implicit_mult_expr | var_pow_expr | paren_pow_expr)?""" if ctx.implicit_mult_expr(): return Expression( op.mul, self.visit(ctx.left_implicit_pow_expr()), self.visit(ctx.implicit_mult_expr()), ) if ctx.var_pow_expr(): return Expression( op.mul, self.visit(ctx.left_implicit_pow_expr()), self.visit(ctx.var_pow_expr()), ) if ctx.paren_pow_expr(): return Expression( op.mul, self.visit(ctx.left_implicit_pow_expr()), self.visit(ctx.paren_pow_expr()), ) else: return self.visit(ctx.left_implicit_pow_expr())
def visitFraction(self, ctx: parse.FractionContext): """fraction CMD_FRAC LCURLY expr RCURLY LCURLY expr RCURLY""" # use the floordiv // operator to represent a fraction return Expression(op.floordiv, self.visit(ctx.expr(0)), self.visit(ctx.expr(1)))
def visitPow_expr_recurse(self, ctx: parse.Pow_expr_recurseContext): """pow_expr_recurse pow_expr CARET tex_symb """ return Expression(op.pow, self.visit(ctx.pow_expr()), self.visit(ctx.tex_symb()))
def visitLeft_implicit_pow_expr_recurse( self, ctx: parse.Left_implicit_pow_expr_recurseContext): """left_implicit_pow_expr_recurse left_implicit_pow_expr CARET tex_symb """ return Expression(op.pow, self.visit(ctx.left_implicit_pow_expr()), self.visit(ctx.tex_symb()))
def visitIme_paren_paren(self, ctx: parse.Ime_paren_parenContext): """ime_paren_paren paren_pow_expr paren_pow_expr""" return Expression(op.mul, self.visit(ctx.paren_pow_expr(0)), self.visit(ctx.paren_pow_expr(1)))
def visitIme_var_var(self, ctx: parse.Ime_var_varContext): """ime_var_var var_pow_expr var_pow_expr""" return Expression(op.mul, self.visit(ctx.var_pow_expr(0)), self.visit(ctx.var_pow_expr(1)))