Search in sources :

Example 1 with ISemanticRecorder

use of priv.bajdcc.LALR1.grammar.semantic.ISemanticRecorder in project jMiniLang by bajdcc.

the class TokenTools method sinop.

/**
 * 单目运算
 *
 * @param recorder 错误记录
 * @param exp      表达式
 * @return 运算是否合法
 */
public static boolean sinop(ISemanticRecorder recorder, ExpSinop exp) {
    ExpValue value = (ExpValue) exp.getOperand();
    Token token = value.getToken();
    OperatorType type = (OperatorType) exp.getToken().object;
    if (sin(type, token)) {
        return true;
    }
    recorder.add(SemanticError.INVALID_OPERATOR, token);
    return false;
}
Also used : ExpValue(priv.bajdcc.LALR1.grammar.tree.ExpValue) Token(priv.bajdcc.util.lexer.token.Token) OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Example 2 with ISemanticRecorder

use of priv.bajdcc.LALR1.grammar.semantic.ISemanticRecorder in project jMiniLang by bajdcc.

the class TokenTools method triop.

/**
 * 三目运算(当前只有一种形式)
 *
 * @param recorder 错误记录
 * @param exp      表达式
 * @return 运算是否合法
 */
public static int triop(ISemanticRecorder recorder, ExpTriop exp) {
    ExpValue firstValue = (ExpValue) exp.getFirstOperand();
    Token firstToken = exp.getFirstToken();
    Token secondToken = exp.getSecondToken();
    int branch = tri(firstToken, secondToken, firstValue.getToken());
    if (branch != 0) {
        return branch;
    }
    recorder.add(SemanticError.INVALID_OPERATOR, firstToken);
    return 0;
}
Also used : ExpValue(priv.bajdcc.LALR1.grammar.tree.ExpValue) Token(priv.bajdcc.util.lexer.token.Token)

Example 3 with ISemanticRecorder

use of priv.bajdcc.LALR1.grammar.semantic.ISemanticRecorder 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 4 with ISemanticRecorder

use of priv.bajdcc.LALR1.grammar.semantic.ISemanticRecorder in project jMiniLang by bajdcc.

the class TokenTools method binop.

/**
 * 双目运算
 *
 * @param recorder 错误记录
 * @param exp      表达式
 * @return 运算是否合法
 */
public static boolean binop(ISemanticRecorder recorder, ExpBinop exp) {
    ExpValue leftValue = (ExpValue) exp.getLeftOperand();
    ExpValue rightValue = (ExpValue) exp.getRightOperand();
    Token token = exp.getToken();
    Token leftToken = leftValue.getToken();
    Token rightToken = rightValue.getToken();
    OperatorType type = (OperatorType) token.object;
    if (bin(type, leftToken, rightToken)) {
        return true;
    }
    recorder.add(SemanticError.INVALID_OPERATOR, token);
    return false;
}
Also used : ExpValue(priv.bajdcc.LALR1.grammar.tree.ExpValue) Token(priv.bajdcc.util.lexer.token.Token) OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Aggregations

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