def link(self, curr_module, module_map): if "__init__" not in [name for name, arg_count in self.method_map]: #增加默认构造函数 self.method_map[("__init__", 0)] = (_Method("__init__", [], set(), [], self.name_token)) self.attr_set = larc_common.OrderedSet() for method in self.method_map.itervalues(): method.link(curr_module, module_map, self)
def __init__(self, name): self.name = name if name == "Exception": self.base_class_module = None self.base_class_name = None self.base_class = None else: self.base_class_module = "*" self.base_class_name = "Exception" self.base_class = builtin_class_map["Exception"] self.method_map = larc_common.OrderedDict() self.method_map[("__init__", 0)] = None self.method_map[("__init__", 1)] = None self.attr_set = larc_common.OrderedSet()
def _parse_arg_set(token_list, dep_module_set): arg_set = larc_common.OrderedSet() if token_list.peek().is_sym(")"): return arg_set while True: t, name = token_list.pop_name() if name in arg_set: t.syntax_err("参数名重定义") if name in dep_module_set: t.syntax_err("参数名和导入模块名冲突") arg_set.add(name) t = token_list.peek() if t.is_sym(","): token_list.pop_sym(",") continue if t.is_sym(")"): return arg_set t.syntax_err("需要','或')'")
def parse_for_prefix(token_list, module, cls, var_set_list, non_local_var_used_map): token_list.pop_sym("(") for_var_set = larc_common.OrderedSet() if token_list.peek().is_reserved("var"): token_list.pop() t = token_list.peek() var_name = parse_var_name(token_list) for vn in iter_var_name(var_name): if vn in module.dep_module_set: t.syntax_err("变量名'%s'与导入模块重名" % vn) for var_set in var_set_list + (for_var_set, ): if vn in var_set: t.syntax_err("变量名'%s'重定义" % vn) for_var_set.add(vn) lvalue = larc_expr.var_name_to_expr(var_name) else: lvalue = larc_expr.parse_expr(token_list, module, cls, var_set_list, non_local_var_used_map) token_list.pop_sym(":") iter_obj = larc_expr.parse_expr(token_list, module, cls, var_set_list, non_local_var_used_map) token_list.pop_sym(")") return for_var_set, lvalue, iter_obj
def __init__(self, module, name, is_native): self.module = module self.name = name self.is_native = is_native self.attr_set = larc_common.OrderedSet() self.method_map = larc_common.OrderedDict()
def parse_stmt_list(token_list, module, cls, var_set_list, loop_deep): assert var_set_list stmt_list = _StmtList(var_set_list[-1]) non_local_var_used_map = larc_common.OrderedDict() while token_list: if token_list.peek().is_sym("}"): break #解析语句 t = token_list.pop() if t.is_sym(";"): t.warning("空语句") continue if t.is_sym("{"): stmt = _Stmt("block", stmt_list=parse_stmt_list( token_list, module, cls, var_set_list + (larc_common.OrderedSet(), ), loop_deep)) token_list.pop_sym("}") stmt_list.append(stmt) continue if t.is_reserved("var"): def add_to_curr_var_set(t, vn): if vn in module.dep_module_set: t.syntax_err("变量名'%s'与导入模块重名" % vn) for var_set in var_set_list: if vn in var_set: t.syntax_err("变量名'%s'重定义" % vn) if vn in non_local_var_used_map: non_local_var_used_map[vn].syntax_err("局部变量在定义之前使用") var_set_list[-1].add(vn) for t, var_name, expr in parse_var_define(token_list, module, cls, var_set_list, non_local_var_used_map): for vn in iter_var_name(var_name): add_to_curr_var_set(t, vn) stmt_list.append(_Stmt("var", name=var_name, expr=expr)) continue if t.is_reserved and t.value in ("break", "continue"): if loop_deep == 0: t.syntax_err("循环外的'%s'" % t.value) token_list.pop_sym(";") stmt_list.append(_Stmt(t.value)) continue if t.is_reserved("return"): expr = larc_expr.parse_expr(token_list, module, cls, var_set_list, non_local_var_used_map) token_list.pop_sym(";") stmt_list.append(_Stmt("return", expr=expr)) continue if t.is_reserved("for"): for_var_set, lvalue, iter_obj = parse_for_prefix( token_list, module, cls, var_set_list, non_local_var_used_map) token_list.pop_sym("{") for_stmt_list = parse_stmt_list( token_list, module, cls, var_set_list + (for_var_set.copy(), ), loop_deep + 1) token_list.pop_sym("}") stmt_list.append( _Stmt("for", var_set=for_var_set, lvalue=lvalue, iter_obj=iter_obj, stmt_list=for_stmt_list)) continue if t.is_reserved("while"): token_list.pop_sym("(") expr = larc_expr.parse_expr(token_list, module, cls, var_set_list, non_local_var_used_map) token_list.pop_sym(")") token_list.pop_sym("{") while_stmt_list = parse_stmt_list( token_list, module, cls, var_set_list + (larc_common.OrderedDict(), ), loop_deep + 1) token_list.pop_sym("}") stmt_list.append( _Stmt("while", expr=expr, stmt_list=while_stmt_list)) continue if t.is_reserved("do"): token_list.pop_sym("{") do_stmt_list = parse_stmt_list( token_list, module, cls, var_set_list + (larc_common.OrderedDict(), ), loop_deep + 1) token_list.pop_sym("}") t = token_list.pop() if not t.is_reserved("while"): t.syntax_err("需要'while'") token_list.pop_sym("(") expr = larc_expr.parse_expr(token_list, module, cls, var_set_list, non_local_var_used_map) token_list.pop_sym(")") token_list.pop_sym(";") stmt_list.append(_Stmt("do", expr=expr, stmt_list=do_stmt_list)) continue if t.is_reserved("if"): if_expr_list = [] if_stmt_list_list = [] else_stmt_list = None while True: token_list.pop_sym("(") expr = larc_expr.parse_expr(token_list, module, cls, var_set_list, non_local_var_used_map) token_list.pop_sym(")") token_list.pop_sym("{") if_stmt_list = parse_stmt_list( token_list, module, cls, var_set_list + (larc_common.OrderedDict(), ), loop_deep) token_list.pop_sym("}") if_expr_list.append(expr) if_stmt_list_list.append(if_stmt_list) if not token_list.peek().is_reserved("else"): break token_list.pop() t = token_list.pop() if t.is_reserved("if"): continue if not t.is_sym("{"): t.syntax_err("需要'{'") else_stmt_list = parse_stmt_list( token_list, module, cls, var_set_list + (larc_common.OrderedDict(), ), loop_deep) token_list.pop_sym("}") break stmt_list.append( _Stmt("if", if_expr_list=if_expr_list, if_stmt_list_list=if_stmt_list_list, else_stmt_list=else_stmt_list)) continue if t.is_sym and t.value in larc_token.INC_DEC_SYM_SET: inc_dec_op = t.value t = token_list.peek() lvalue = larc_expr.parse_expr(token_list, module, cls, var_set_list, non_local_var_used_map) if not lvalue.is_lvalue: t.syntax_err("非左值表达式不能做'%s'操作" % inc_dec_op) if lvalue.op in ("[:]", "tuple", "list"): t.syntax_err("分片和解包左值表达式不能做'%s'操作" % inc_dec_op) stmt_list.append(_Stmt(inc_dec_op, lvalue=lvalue)) token_list.pop_sym(";") continue #todo: try catch finally throw assert #剩下的就是表达式和赋值了 token_list.revert() expr = larc_expr.parse_expr(token_list, module, cls, var_set_list, non_local_var_used_map) if token_list.peek().is_sym(";"): #表达式 token_list.pop_sym(";") stmt_list.append(_Stmt("expr", expr=expr)) continue t = token_list.peek() if t.is_sym and t.value in larc_token.ASSIGN_SYM_SET: #赋值 assign_sym = t.value lvalue = expr if not lvalue.is_lvalue: t.syntax_err("赋值操作'%s'左边非左值表达式" % assign_sym) if assign_sym != "=": #增量赋值 if lvalue.op in ("[:]", "tuple", "list"): t.syntax_err("分片和解包左值表达式无法增量赋值") token_list.pop_sym(assign_sym) expr = larc_expr.parse_expr(token_list, module, cls, var_set_list, non_local_var_used_map) token_list.pop_sym(";") stmt_list.append(_Stmt(assign_sym, lvalue=lvalue, expr=expr)) continue t.syntax_err() return stmt_list