def ezhil_reverse(*args): if (len(args) != 1): raise Exception('One argument alone expected to reverse function') if (isinstance(args[0], str) or isinstance(args[0], unicode)): return String(args[0][::-1]) #string-reverse return list.reverse(args[0])
def line(p): function = p[1] if function.gettokentype() == 'SET_INT': return SetVariable(p[2].value, p[1].value, Integer(p[3].value)) elif function.gettokentype() == 'SET_FLOAT': return SetVariable(p[2].value, p[1].value, Float(p[3].value)) elif function.gettokentype() == 'SET_CHAR': return SetVariable(p[2].value, p[1].value, Char(p[3].value)) elif function.gettokentype() == 'SET_STRING': return SetVariable(p[2].value, p[1].value, String(p[3].value)) elif function.gettokentype() == 'SET_BOOL': return SetVariable(p[2].value, p[1].value, Bool(p[3].value)) elif function.gettokentype() == 'SET_ARRAY': type = p[2].gettokentype() if type == 'SET_INT': return SetArray(p[4], 0, p[3].value) elif type == 'SET_FLOAT': return SetArray(p[4], 0.0, p[3].value) elif type == 'SET_STRING': return SetArray(p[4], "a", p[3].value)
def factor(self): self.dbg_msg("factor") tok = self.peek() if tok.kind == Token.LPAREN: lparen_tok = self.dequeue() val = self.expr() if self.dequeue().kind != Token.RPAREN: raise SyntaxError("Missing Parens") elif tok.kind == Token.NUMBER: tok_num = self.dequeue() [l, c] = tok_num.get_line_col() val = Number(tok.val, l, c, self.debug) elif tok.kind == Token.ID: tok_id = self.dequeue() [l, c] = tok_id.get_line_col() val = Identifier(tok.val, l, c, self.debug) ptok = self.peek() self.dbg_msg("factor: " + str(ptok) + " / " + str(tok)) if (ptok.kind == Token.LPAREN): ## function call [l, c] = ptok.get_line_col() vallist = self.valuelist() val = ExprCall(val, vallist, l, c, self.debug) elif (ptok.kind == Token.LSQRBRACE): ## array type val = None raise ParseException("arrays not implemented" + str(ptok)) elif tok.kind == Token.STRING: str_tok = self.dequeue() [l, c] = str_tok.get_line_col() val = String(tok.val, l, c, self.debug) else: raise ParseException("Expected Number, found something " + str(tok)) self.dbg_msg("factor-returning: " + str(val)) return val
def SPRINTF(*args): opstr = Interpreter.SPRINTF_worker(*args) return String(opstr)
def INPUT(args): op = (raw_input(args)) if (isinstance(op, int) or isinstance(op, float)): return Number(0.0 + op) return String(op)
def RAWINPUT(args): op = raw_input(args) return String(op)
def file_read(*args): assert (len(args) == 1) assert (hasattr(args[0], 'read')) return String(args[0].read())
def box_y(p): return String(p[0].getstr())
def origem(p): return String(p[0].getstr())
def factor(self): self.dbg_msg("factor") tok = self.peek() if tok.kind == EzhilToken.LPAREN: lparen_tok = self.dequeue() val = self.expr() if self.dequeue().kind != EzhilToken.RPAREN: raise SyntaxError("Missing Parens " + str(self.last_token())) elif tok.kind == EzhilToken.NUMBER: tok_num = self.dequeue() [l, c] = tok_num.get_line_col() val = Number(tok.val, l, c, self.debug) elif tok.kind == EzhilToken.LOGICAL_NOT: tok_not = self.dequeue() [l, c] = tok_not.get_line_col() val = UnaryExpr(self.expr(), tok_not, l, c, self.debug) self.dbg_msg("completed parsing unary expression" + str(val)) elif tok.kind == EzhilToken.ID: tok_id = self.dequeue() [l, c] = tok_id.get_line_col() val = Identifier(tok.val, l, c, self.debug) ptok = self.peek() self.dbg_msg("factor: " + str(ptok) + " / " + str(tok)) if (ptok.kind == EzhilToken.LPAREN): ## function call [l, c] = ptok.get_line_col() vallist = self.valuelist() val = ExprCall(val, vallist, l, c, self.debug) elif (ptok.kind == EzhilToken.LSQRBRACE): ## indexing a array type variable or ID [l, c] = ptok.get_line_col() ## replace with a call to __getitem__ exp = self.factor() if (hasattr(exp, '__getitem__')): VL2 = ValueList([val, exp[0]], l, c, self.debug) else: # when exp is a expression VL2 = ValueList([val, exp], l, c, self.debug) val = ExprCall(Identifier("__getitem__", l, c), VL2, l, c, self.debug) for itr in range(1, len(exp)): VL2 = ValueList([val, exp[itr]], l, c, self.debug) val = ExprCall(Identifier("__getitem__", l, c), VL2, l, c, self.debug) #raise ParseException("array indexing implemented"+str(ptok)); elif (ptok.kind == EzhilToken.LCURLBRACE): val = None raise ParseException("dictionary indexing implemented" + str(ptok)) elif tok.kind == EzhilToken.STRING: str_tok = self.dequeue() [l, c] = str_tok.get_line_col() val = String(tok.val, l, c, self.debug) elif tok.kind in EzhilToken.ADDSUB: unop = self.dequeue() [l, c] = unop.get_line_col() val = Expr(Number(0), unop, self.term(), l, c, self.debug) elif tok.kind == EzhilToken.LCURLBRACE: # creating a list/dictionary expression dict_start = self.dequeue() val = Dict() while (True): if (self.peek().kind == EzhilToken.RCURLBRACE): break exprkey = self.expr() tok_colon = self.match(EzhilToken.COLON) exprval = self.expr() val.update({exprkey: exprval}) if self.debug: print(self.peek().__class__, self.peek()) if (self.peek().kind == EzhilToken.RCURLBRACE): break else: assert (self.peek().kind == EzhilToken.COMMA) self.dequeue() assert (self.peek().kind == EzhilToken.RCURLBRACE) list_end = self.dequeue() elif tok.kind == EzhilToken.LSQRBRACE: # creating a list/array expression list_start = self.dequeue() val = Array() while (True): if (self.peek().kind == EzhilToken.RSQRBRACE): break exprval = self.expr() val.append(exprval) if self.debug: print(self.peek().__class__, self.peek()) if (self.peek().kind == EzhilToken.RSQRBRACE): break else: assert (self.peek().kind == EzhilToken.COMMA) self.dequeue() assert (self.peek().kind == EzhilToken.RSQRBRACE) list_end = self.dequeue() else: raise ParseException("Expected Number, found something " + str(tok)) self.dbg_msg("factor-returning: " + str(val)) return val
def string(p): return String(p[1])
def expression_string(p): # p is a list of the pieces matched by the right hand side of the # rule return String(str(p[0].getstr()))
def handleString(p): return String(p[0].value)
def factor(self): self.dbg_msg("factor") tok = self.peek() if tok.kind == EzhilToken.LPAREN: lparen_tok = self.dequeue() val = self.expr() if self.dequeue().kind != EzhilToken.RPAREN: raise SyntaxError("Missing Parens " + str(self.last_token())) elif tok.kind == EzhilToken.NUMBER: tok_num = self.dequeue() [l, c] = tok_num.get_line_col() val = Number(tok.val, l, c, self.debug) elif tok.kind == EzhilToken.LOGICAL_NOT: tok_not = self.dequeue() [l, c] = tok_not.get_line_col() val = UnaryExpr(self.expr(), tok_not, l, c, self.debug) self.dbg_msg("completed parsing unary expression" + str(val)) elif tok.kind == EzhilToken.ID: tok_id = self.dequeue() [l, c] = tok_id.get_line_col() val = Identifier(tok.val, l, c, self.debug) ptok = self.peek() self.dbg_msg("factor: " + str(ptok) + " / " + str(tok)) if (ptok.kind == EzhilToken.LPAREN): ## function call [l, c] = ptok.get_line_col() vallist = self.valuelist() val = ExprCall(val, vallist, l, c, self.debug) elif (ptok.kind == EzhilToken.LSQRBRACE): ## indexing a array type variable or ID val = None raise ParseException("arrays not implemented" + str(ptok)) elif tok.kind == EzhilToken.STRING: str_tok = self.dequeue() [l, c] = str_tok.get_line_col() val = String(tok.val, l, c, self.debug) elif tok.kind in EzhilToken.ADDSUB: unop = self.dequeue() [l, c] = unop.get_line_col() val = Expr(Number(0), unop, self.term(), l, c, self.debug) elif tok.kind == EzhilToken.LSQRBRACE: # creating a list/array expression list_start = self.dequeue() val = Array() while (True): exprval = self.expr() val.append(exprval) if self.debug: print(self.peek().__class__, self.peek()) if (self.peek().kind == EzhilToken.RSQRBRACE): break else: assert (self.peek().kind == EzhilToken.COMMA) self.dequeue() assert (self.peek().kind == EzhilToken.RSQRBRACE) list_end = self.dequeue() else: raise ParseException("Expected Number, found something " + str(tok)) self.dbg_msg("factor-returning: " + str(val)) return val
def sel_col(p): return String(p[0].getstr())
def string(p): return String(p[0].value)
def strings(p): return String(p[0].value[1:-1])