コード例 #1
0
ファイル: cocc_module.py プロジェクト: hxmwr/coc-lang
 def __init__(self, module, decr_set, name, base_cls_type, gtp_name_list):
     if gtp_name_list:
         assert "native" in decr_set
     self.module = module
     self.decr_set = decr_set
     self.name = name
     self.base_cls_type = base_cls_type
     self.gtp_name_list = gtp_name_list
     self.construct_method = []
     self.destruct_method = None
     self.attr_map = cocc_common.OrderedDict()
     self.method_map = cocc_common.OrderedDict()
コード例 #2
0
ファイル: cocc_module.py プロジェクト: hxmwr/coc-lang
def _parse_arg_map(token_list, dep_module_set):
    arg_map = cocc_common.OrderedDict()
    if token_list.peek().is_sym(")"):
        return arg_map
    while True:
        if token_list.peek().is_reserved("ref"):
            token_list.pop()
            is_ref = True
        else:
            is_ref = False
        type = cocc_type.parse_type(token_list, dep_module_set, is_ref = is_ref)
        if type.name == "void":
            type.token.syntax_err("参数类型不可为void")
        t, name = token_list.pop_name()
        if name in arg_map:
            t.syntax_err("参数名重定义")
        if name in dep_module_set:
            t.syntax_err("参数名和导入模块名冲突")
        arg_map[name] = type
        t = token_list.peek()
        if t.is_sym(","):
            token_list.pop_sym(",")
            continue
        if t.is_sym(")"):
            return arg_map
        t.syntax_err("需要','或')'")
コード例 #3
0
ファイル: cocc_module.py プロジェクト: hxmwr/coc-lang
 def __init__(self, gcls, gtp_list):
     assert "native" in gcls.decr_set
     assert gtp_list and len(gcls.gtp_name_list) == len(gtp_list)
     self.gtp_map = dict(zip(gcls.gtp_name_list, gtp_list))
     self.module = gcls.module
     self.decr_set = gcls.decr_set
     self.gcls = gcls
     self.name = gcls.name + "<%s>" % ", ".join([str(gtp) for gtp in gtp_list])
     self.base_cls_type = None if gcls.base_cls_type is None else gcls.base_cls_type.to_gcls_inst_type(self.gtp_map)
     self.construct_method = [method.to_gcls_inst_method(self) for method in gcls.construct_method]
     self.destruct_method = None if gcls.destruct_method is None else gcls.destruct_method.to_gcls_inst_method(self)
     self.attr_map = cocc_common.OrderedDict()
     for name, attr in gcls.attr_map.iteritems():
         assert name == attr.name
         self.attr_map[name] = attr.to_gcls_inst_attr(self)
     self.method_map = cocc_common.OrderedDict()
     for name, method_list in gcls.method_map.iteritems():
         self.method_map[name] = ml = []
         for method in method_list:
             assert name == method.name
             ml.append(method.to_gcls_inst_method(self))
コード例 #4
0
ファイル: cocc_stmt.py プロジェクト: hxmwr/coc-lang
def _parse_for_prefix(token_list, var_map_list, cls, module):
    token_list.pop_sym("(")

    for_var_map = cocc_common.OrderedDict()
    tp = cocc_type.try_parse_type(token_list, module)
    if tp is None:
        #第一部分为表达式列表
        init_expr_list = []
        if not token_list.peek().is_sym(";"):
            init_expr_list += _parse_expr_list_with_se(token_list, var_map_list + (for_var_map,), cls, module)
    else:
        #第一部分为若干变量定义
        init_expr_list = []
        while True:
            t, name = token_list.pop_name()
            if name in module.dep_module_set:
                t.syntax_err("变量名和导入模块名重复")
            token_list.pop_sym("=")
            expr = cocc_expr.parse_expr(token_list, var_map_list + (for_var_map,), cls, module, tp)
            for_var_map[name] = tp
            init_expr_list.append(expr)
            if token_list.peek().is_sym(";"):
                break
            token_list.pop_sym(",")
    token_list.pop_sym(";")

    if token_list.peek().is_sym(";"):
        #没有第二部分
        judge_expr = None
    else:
        judge_expr = cocc_expr.parse_expr(token_list, var_map_list + (for_var_map,), cls, module, cocc_type.BOOL_TYPE)
    token_list.pop_sym(";")

    loop_expr_list = []
    if not token_list.peek().is_sym(")"):
        loop_expr_list += _parse_expr_list_with_se(token_list, var_map_list + (for_var_map,), cls, module)

    token_list.pop_sym(")")

    return for_var_map, init_expr_list, judge_expr, loop_expr_list
コード例 #5
0
ファイル: cocc_module.py プロジェクト: hxmwr/coc-lang
    def _parse_text(self, token_list):
        self.dep_module_set = set()
        import_end = False
        self.class_map = cocc_common.OrderedDict()
        self.gcls_inst_map = cocc_common.OrderedDict()
        self.typedef_map = cocc_common.OrderedDict()
        self.func_map = cocc_common.OrderedDict()
        self.global_var_map = cocc_common.OrderedDict()
        while token_list:
            #解析import
            t = token_list.peek()
            if t.is_reserved("import"):
                #import
                if import_end:
                    t.syntax_err("import必须在模块代码最前面")
                self._parse_import(token_list)
                continue
            import_end = True

            #解析修饰
            decr_set = _parse_decr_set(token_list)

            #解析各种定义
            t = token_list.peek()
            if t.is_reserved("class"):
                #解析类
                if decr_set - set(["public", "native", "final"]):
                    t.syntax_err("类只能用public、native和final修饰")
                self._parse_class(decr_set, token_list)
                continue

            if t.is_reserved("typedef"):
                #解析typedef
                if decr_set - set(["public"]):
                    t.syntax_err("typedef只能用public修饰")
                self._parse_typedef(decr_set, token_list)
                continue

            #可能是函数或全局变量
            type = cocc_type.parse_type(token_list, self.dep_module_set)
            t, name = token_list.pop_name()
            self._check_redefine(t, name, token_list.peek().is_sym("("))
            t, sym = token_list.pop_sym()
            if sym == "(":
                #函数
                if decr_set - set(["public", "native"]):
                    t.syntax_err("函数只能用public和native修饰")
                self._parse_func(decr_set, type, name, token_list)
                continue
            if sym in (";", "=", ","):
                #全局变量
                if decr_set - set(["public", "native", "final"]):
                    t.syntax_err("全局变量只能用public、native和final修饰")
                if type.name == "void":
                    t.syntax_err("变量类型不可为void")
                while True:
                    if sym == "=":
                        if "native" in decr_set:
                            t.syntax_err("不能初始化native全局变量")
                        expr_token_list, sym = _parse_expr_token_list(token_list)
                    else:
                        if "native" not in decr_set:
                            t.syntax_err("非native全局变量必须显式初始化")
                        expr_token_list = None
                    self.global_var_map[name] = _GlobalVar(name, self, decr_set, type, expr_token_list)
                    if sym == ";":
                        break
                    #定义了多个变量,继续解析
                    assert sym == ","
                    t, name = token_list.pop_name()
                    self._check_redefine(t, name)
                    t, sym = token_list.pop_sym()
                    if sym not in (";", "=", ","):
                        t.syntax_err()
                continue
            t.syntax_err()
コード例 #6
0
ファイル: cocc_module.py プロジェクト: hxmwr/coc-lang
#coding=gbk

"""
编译模块
"""

import os
import cocc_common
import cocc_token
import cocc_type
import cocc_stmt
import cocc_expr

builtins_module = None
module_map = cocc_common.OrderedDict()

def _parse_decr_set(token_list):
    decr_set = set()
    while True:
        t = token_list.peek()
        for decr in "public", "protected", "private", "native", "final", "abstract":
            if t.is_reserved(decr):
                if decr in decr_set:
                    t.syntax_err("重复的修饰'%s'" % decr)
                decr_set.add(decr)
                if len(decr_set & set(["public", "protected", "private"])) > 1:
                    t.syntax_err("同时存在多个权限修饰")
                token_list.pop()
                break
        else:
            return decr_set
コード例 #7
0
ファイル: cocc_stmt.py プロジェクト: hxmwr/coc-lang
def parse_stmt_list(token_list, module, cls, var_map_list, loop_deep, ret_type):
    assert var_map_list
    stmt_list = _StmtList(var_map_list[-1])
    while True:
        if token_list.peek().is_sym("}"):
            break

        t = token_list.pop()
        if t.is_sym(";"):
            continue
        if t.is_sym("{"):
            #新代码块
            stmt_list.append(_Stmt("block", stmt_list = parse_stmt_list(token_list, module, cls, var_map_list + (cocc_common.OrderedDict(),),
                                                                        loop_deep, ret_type)))
            token_list.pop_sym("}")
            continue
        if t.is_reserved and t.value in ("break", "continue"):
            if loop_deep == 0:
                t.syntax_err("循环外的'%s'" % t.value)
            stmt_list.append(_Stmt(t.value))
            continue
        if t.is_reserved("return"):
            expr = _parse_return(token_list, ret_type, var_map_list, cls, module)
            stmt_list.append(_Stmt("return", expr = expr))
            continue
        if t.is_reserved("for"):
            for_var_map, init_expr_list, judge_expr, loop_expr_list = _parse_for_prefix(token_list, var_map_list, cls, module)
            token_list.pop_sym("{")
            for_stmt_list = parse_stmt_list(token_list, module, cls, var_map_list + (for_var_map.copy(),), loop_deep + 1, ret_type)
            token_list.pop_sym("}")
            stmt_list.append(_Stmt("for", for_var_map = for_var_map, init_expr_list = init_expr_list, judge_expr = judge_expr,
                                   loop_expr_list = loop_expr_list, stmt_list = for_stmt_list))
            continue
        if t.is_reserved("while"):
            token_list.pop_sym("(")
            expr = cocc_expr.parse_expr(token_list, var_map_list, cls, module, cocc_type.BOOL_TYPE)
            token_list.pop_sym(")")
            token_list.pop_sym("{")
            while_stmt_list = parse_stmt_list(token_list, module, cls, var_map_list + (cocc_common.OrderedDict(),), loop_deep + 1, ret_type)
            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_map_list + (cocc_common.OrderedDict(),), loop_deep + 1, ret_type)
            token_list.pop_sym("}")
            t = token_list.pop()
            if not t.is_reserved("while"):
                t.syntax_err("需要'while'")
            token_list.pop_sym("(")
            expr = cocc_expr.parse_expr(token_list, var_map_list, cls, module, cocc_type.BOOL_TYPE)
            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 = cocc_expr.parse_expr(token_list, var_map_list, cls, module, cocc_type.BOOL_TYPE)
                token_list.pop_sym(")")
                token_list.pop_sym("{")
                if_stmt_list = parse_stmt_list(token_list, module, cls, var_map_list + (cocc_common.OrderedDict(),), loop_deep, ret_type)
                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_map_list + (cocc_common.OrderedDict(),), loop_deep, ret_type)
                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

        token_list.revert()
        t = token_list.peek()
        tp = cocc_type.try_parse_type(token_list, module)
        if tp is not None:
            #变量定义
            if tp.is_void:
                t.syntax_err("变量类型不能为void")
            while True:
                t, name = token_list.pop_name()
                if name in module.dep_module_set:
                    t.syntax_err("变量名和导入模块重名")
                for var_map in var_map_list:
                    if name in var_map:
                        t.syntax_err("变量名重定义")
                var_map_list[-1][name] = tp
                token_list.pop_sym("=")
                expr = cocc_expr.parse_expr(token_list, var_map_list, cls, module, tp)
                stmt_list.append(_Stmt("var", name = name, expr = expr))
                if token_list.peek().is_sym(";"):
                    break
                token_list.pop_sym(",")
            token_list.pop_sym(";")
            continue

        #表达式
        expr = _parse_expr_with_se(token_list, var_map_list, cls, module)
        stmt_list.append(_Stmt("expr", expr = expr))
        token_list.pop_sym(";")

    return stmt_list