Search in sources :

Example 1 with Function

use of priv.bajdcc.LALR1.grammar.tree.Function in project jMiniLang by bajdcc.

the class ManageScopeSymbol method getFuncString.

public String getFuncString() {
    StringBuilder sb = new StringBuilder();
    sb.append("#### 过程表 ####");
    sb.append(System.lineSeparator());
    int i = 0;
    for (ArrayList<Function> funcs : funcMap.list) {
        for (Function func : funcs) {
            sb.append("----==== #").append(i).append(" ====----");
            sb.append(System.lineSeparator());
            sb.append(func.toString());
            sb.append(System.lineSeparator());
            sb.append(System.lineSeparator());
            i++;
        }
    }
    return sb.toString();
}
Also used : Function(priv.bajdcc.LALR1.grammar.tree.Function)

Example 2 with Function

use of priv.bajdcc.LALR1.grammar.tree.Function in project jMiniLang by bajdcc.

the class Semantic method run.

/**
 * 进行语义处理
 */
private void run() throws SyntaxException {
    if (!arrErrors.isEmpty()) {
        System.err.println(getTrackerError());
        throw new SyntaxException(SyntaxError.COMPILE_ERROR, arrErrors.get(0).position, "出现语法错误");
    }
    /* 规则集合 */
    ArrayList<RuleItem> items = npa.getRuleItems();
    /* 符号表查询接口 */
    IQuerySymbol query = getQuerySymbolService();
    /* 符号表管理接口 */
    IManageSymbol manage = getManageSymbolService();
    /* 语义错误处理接口 */
    ISemanticRecorder recorder = getSemanticRecorderService();
    /* 复制单词流 */
    ArrayList<Token> tokens = arrTokens.stream().map(Token::copy).collect(Collectors.toCollection(ArrayList::new));
    /* 运行时自动机 */
    SemanticMachine machine = new SemanticMachine(items, arrActions, tokens, query, manage, recorder, debug);
    /* 遍历所有指令 */
    arrInsts.forEach(machine::run);
    object = machine.getObject();
    if (object != null) {
        Function entry = (Function) object;
        manage.getManageScopeService().registerFunc(entry);
    }
}
Also used : Function(priv.bajdcc.LALR1.grammar.tree.Function) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuleItem(priv.bajdcc.LALR1.syntax.rule.RuleItem) IManageSymbol(priv.bajdcc.LALR1.grammar.symbol.IManageSymbol) Token(priv.bajdcc.util.lexer.token.Token) IQuerySymbol(priv.bajdcc.LALR1.grammar.symbol.IQuerySymbol) ISemanticRecorder(priv.bajdcc.LALR1.grammar.semantic.ISemanticRecorder)

Example 3 with Function

use of priv.bajdcc.LALR1.grammar.tree.Function in project jMiniLang by bajdcc.

the class Grammar method infer.

/**
 * 进行推导
 *
 * @throws SyntaxException 词法错误
 */
private void infer() throws SyntaxException {
    /* 起始符号就是main函数 */
    infer(handler.getSemanticHandler("main"), "program -> stmt_list[0]{lost_stmt}");
    /* Block语句 */
    infer(handler.getSemanticHandler("block"), "block -> @LBR#do_enter_scope# [stmt_list[0]] @RBR#do_leave_scope#{lost_rbr}");
    infer(handler.getSemanticHandler("block_stmt"), "block_stmt -> block[0]");
    /* 当前块(Block)全部由语句组成 */
    infer(handler.getSemanticHandler("stmt_list"), "stmt_list -> stmt[0]{lost_stmt} [stmt_list[1]]");
    /* 语句分为变量定义(赋值)、调用语句 */
    infer(handler.getSemanticHandler("copy"), "stmt_exp -> var[0] | call[0] | cycle_ctrl[0] | exp[0]");
    infer(handler.getSemanticHandler("copy"), "stmt -> stmt_stmt[0] | stmt_ctrl[0] | block_stmt[0]");
    infer(handler.getSemanticHandler("stmt_exp"), "stmt_stmt -> [stmt_exp[0]] @SEMI{lost_semi}");
    infer(handler.getSemanticHandler("copy"), "stmt_ctrl -> ret[0] | port[0] | if[0] | for[0] | foreach[0] | while[0] | try[0] | throw[0]");
    /* 返回语句 */
    infer(handler.getSemanticHandler("return"), "ret -> (@YIELD[1] | @RETURN) [exp[0]] @SEMI{lost_semi}");
    /* 变量定义(赋值)语句(由于支持Lambda,函数定义皆为Lambda形式) */
    infer(handler.getSemanticHandler("var"), "var -> (@VARIABLE[11] | @LET[12]) @ID[0]#declear_variable#{lost_token} @ASSIGN{lost_assign} (func[1]{lost_func} | exp[2]{lost_exp})");
    /* 类属性赋值语句 */
    infer(handler.getSemanticHandler("set"), "set -> @SET exp[3]{lost_exp} @PROPERTY{lost_property} exp[4]{lost_exp} @ASSIGN{lost_assign} exp[2]{lost_exp}");
    /* 类方法调用语句 */
    infer(handler.getSemanticHandler("invoke"), "invoke -> @INVOKE[0] exp[1]{lost_exp} @PROPERTY{lost_property} exp[2]{lost_exp} [@PROPERTY] @LPA{lost_lpa} [exp_list[3]] @RPA{lost_rpa}");
    /* 导入与导出语句 */
    infer(handler.getSemanticHandler("port"), "port -> (@IMPORT[1] | @EXPORT[2]) @LITERAL[0]{lost_string} @SEMI{lost_semi}");
    /* 表达式(算符文法) */
    ISemanticAnalyzer exp_handler = handler.getSemanticHandler("exp");
    infer(exp_handler, "exp -> exp01[0]");
    infer(exp_handler, "exp01 -> [exp01[1] (@EQ_ASSIGN[2] | @ADD_ASSIGN[2] | @SUB_ASSIGN[2] | @MUL_ASSIGN[2] | @DIV_ASSIGN[2] | @AND_ASSIGN[2] | @OR_ASSIGN[2] | @XOR_ASSIGN[2] | @MOD_ASSIGN[2])] exp0[0]");
    infer(exp_handler, "exp0 -> exp1[0] [@QUERY[4] exp0[6] @COLON[5]{lost_colon} exp0[7]]");
    infer(exp_handler, "exp1 -> [exp1[1] (@AND_OP[2] | @OR_OP[2])] exp2[0]");
    infer(exp_handler, "exp2 -> [exp2[1] (@OR[2] | @XOR[2] | @AND[2])] exp3[0]");
    infer(exp_handler, "exp3 -> [exp3[1] (@EQ_OP[2] | @NE_OP[2])] exp4[0]");
    infer(exp_handler, "exp4 -> [exp4[1] (@LT[2] | @GT[2] | @LE_OP[2] | @GE_OP[2])] exp5[0]");
    infer(exp_handler, "exp5 -> [exp5[1] (@LEFT_OP[2] | @RIGHT_OP[2])] exp6[0]");
    infer(exp_handler, "exp6 -> [exp6[1] (@ADD[2] | @SUB[2])] exp7[0]");
    infer(exp_handler, "exp7 -> [exp7[1] (@MUL[2] | @DIV[2] | @MOD[2])] exp8[0]");
    infer(exp_handler, "exp8 -> (@NOT_OP[3] | @NOT[3]) exp8[1] | exp9[0]");
    infer(exp_handler, "exp9 -> (@INC_OP[3] | @DEC_OP[3]) exp9[1] | exp9[1] (@INC_OP[3] | @DEC_OP[3]) | exp10[0]");
    infer(exp_handler, "exp10 -> [exp10[1] @DOT[2]] type[0]");
    /* 调用语句 */
    infer(handler.getSemanticHandler("call_exp"), "call -> @CALL (@LPA{lost_lpa} func[0]{lost_call} @RPA{lost_rpa} | @ID[1]{lost_call}) @LPA{lost_lpa} [exp_list[2]] @RPA{lost_rpa}");
    /* 函数定义 */
    infer(handler.getSemanticHandler("token_list"), "var_list -> @ID[0]#declear_param# [@COMMA var_list[1]]");
    infer(handler.getSemanticHandler("exp_list"), "exp_list -> exp[0] [@COMMA exp_list[1]]");
    infer(handler.getSemanticHandler("token_list"), "doc_list -> @LITERAL[0] [@COMMA doc_list[1]]");
    /* 函数主体 */
    infer(handler.getSemanticHandler("func"), "func -> (@FUNCTION[10]#func_clearargs# | @YIELD#func_clearargs#) [@LSQ doc_list[0]{lost_doc} @RSQ] (@ID[1]#predeclear_funcname#{lost_func_name} | @NOT[1]#predeclear_funcname#{lost_func_name}) @LPA{lost_lpa} [var_list[2]] @RPA{lost_rpa} (@PTR_OP#do_enter_scope#{lost_func_body} exp[3]#do_leave_scope#{lost_exp} | block[4]{lost_func_body})");
    /* 匿名主体 */
    infer(handler.getSemanticHandler("lambda"), "lambda -> @LAMBDA[1]#lambda# @LPA{lost_lpa} [var_list[2]] @RPA{lost_rpa} (@PTR_OP{lost_func_body} exp[3]{lost_exp} | block[4]{lost_func_body})");
    /* 基本数据类型 */
    infer(handler.getSemanticHandler("type"), "type -> @ID[0] [(@LPA[3] [exp_list[4]] @RPA{lost_rpa}) | (@LSQ exp[5]{lost_exp} @RSQ{lost_rpa})] | @INTEGER[0] | @DECIMAL[0] | @LITERAL[0] | @CHARACTER[0] | @BOOLEAN[0] | @LPA exp[1]{lost_exp} @RPA{lost_rpa} | call[1] | lambda[2] | set[1] | invoke[1] | array[1] | map[1]");
    /* 条件语句 */
    infer(handler.getSemanticHandler("if"), "if -> @IF @LPA{lost_lpa} exp[0]{lost_exp} @RPA{lost_rpa} block[1]{lost_block} [@ELSE (block[2]{lost_block} | if[3]{lost_block})]");
    /* 循环语句 */
    infer(handler.getSemanticHandler("for"), "for -> @FOR#do_enter_cycle# @LPA{lost_lpa} [var[0]] @SEMI{lost_semi} [exp[1]] @SEMI{lost_semi} [exp[2] | var[2]] @RPA{lost_rpa} block[3]{lost_block}");
    infer(handler.getSemanticHandler("while"), "while -> @WHILE#do_enter_cycle# @LPA{lost_lpa} exp[0] @RPA{lost_rpa} block[1]{lost_block}");
    /* 循环语句 */
    infer(handler.getSemanticHandler("foreach"), "foreach -> @FOREACH#do_enter_cycle# @LPA{lost_lpa} @VARIABLE{lost_var} @ID[0]#declear_variable#{lost_token} @COLON{lost_colon} exp[1]{lost_exp} @RPA{lost_rpa} block[2]{lost_block}");
    /* 循环控制语句 */
    infer(handler.getSemanticHandler("cycle"), "cycle_ctrl -> @BREAK[0] | @CONTINUE[0]");
    /* 数组初始化 */
    infer(handler.getSemanticHandler("array"), "array -> @LSQ [exp_list[0]] @RSQ{lost_rsq}");
    /* 字典初始化 */
    infer(handler.getSemanticHandler("map_list"), "map_list -> @LITERAL[1] @COLON[3]{lost_colon} exp[2]{lost_exp} [@COMMA map_list[0]]");
    infer(handler.getSemanticHandler("map"), "map -> @LBR [map_list[0]] @RBR{lost_rbr}");
    /* 异常处理 */
    infer(handler.getSemanticHandler("try"), "try -> @TRY block[1]{lost_block} @CATCH{lost_catch} [@LPA{lost_lpa} @ID[0]#declear_param#{lost_token} @RPA{lost_rpa}] block[2]#clear_catch#{lost_block}");
    infer(handler.getSemanticHandler("throw"), "throw -> @THROW exp[0]{lost_exp} @SEMI{lost_semi}");
    initialize("program");
}
Also used : ISemanticAnalyzer(priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)

Example 4 with Function

use of priv.bajdcc.LALR1.grammar.tree.Function in project jMiniLang by bajdcc.

the class Codegen method gencode.

/**
 * 产生中间代码
 */
public void gencode() {
    genCode(RuntimeInst.iopena);
    genCodeWithFuncWriteBack(RuntimeInst.ipush, 0);
    genCode(RuntimeInst.icall);
    genCode(RuntimeInst.ipop);
    genCode(RuntimeInst.ihalt);
    genCode(RuntimeInst.inop);
    for (Function func : funcMap.list) {
        if (func.isExtern()) {
            info.addExports(func.getRealName(), data.getCodeIndex());
            info.addExternalFunc(func.getRealName(), new CodegenFuncDoc(func.getDoc()));
        }
        func.genCode(this);
    }
}
Also used : Function(priv.bajdcc.LALR1.grammar.tree.Function)

Example 5 with Function

use of priv.bajdcc.LALR1.grammar.tree.Function in project jMiniLang by bajdcc.

the class ManageScopeSymbol method getFuncByName.

@Override
public Function getFuncByName(String name) {
    for (int i = funcScope.size() - 1; i >= 0; i--) {
        HashMap<String, Function> f = funcScope.get(i);
        Function f1 = f.get(name);
        if (f1 != null)
            return f1;
        for (Function func : f.values()) {
            String funcName = func.getRealName();
            if (funcName != null && funcName.equals(name)) {
                return func;
            }
        }
    }
    return null;
}
Also used : Function(priv.bajdcc.LALR1.grammar.tree.Function)

Aggregations

Function (priv.bajdcc.LALR1.grammar.tree.Function)4 ISemanticRecorder (priv.bajdcc.LALR1.grammar.semantic.ISemanticRecorder)1 IManageSymbol (priv.bajdcc.LALR1.grammar.symbol.IManageSymbol)1 IQuerySymbol (priv.bajdcc.LALR1.grammar.symbol.IQuerySymbol)1 ISemanticAnalyzer (priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)1 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)1 RuleItem (priv.bajdcc.LALR1.syntax.rule.RuleItem)1 Token (priv.bajdcc.util.lexer.token.Token)1