Search in sources :

Example 11 with Token

use of priv.bajdcc.LALR1.syntax.token.Token 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)

Example 12 with Token

use of priv.bajdcc.LALR1.syntax.token.Token in project jMiniLang by bajdcc.

the class ExpBinop method genCode.

@Override
public void genCode(ICodegen codegen) {
    if (token.kToken == TokenType.OPERATOR && token.object == OperatorType.DOT) {
        codegen.genCode(RuntimeInst.iopena);
        leftOperand.genCode(codegen);
        codegen.genCode(RuntimeInst.ipusha);
        rightOperand.genCode(codegen);
        codegen.genCode(RuntimeInst.ipusha);
        codegen.genCode(RuntimeInst.ipush, codegen.genDataRef("g_get_property"));
        codegen.genCode(RuntimeInst.icallx);
        return;
    }
    if (token.kToken == TokenType.OPERATOR) {
        OperatorType op = (OperatorType) token.object;
        if (TokenTools.isAssignment(op)) {
            RuntimeInst ins = TokenTools.op2ins(token);
            ExpValue left = (ExpValue) leftOperand;
            if (ins == RuntimeInst.ice) {
                rightOperand.genCode(codegen);
                codegen.genCode(RuntimeInst.ipush, codegen.genDataRef(left.getToken().object));
                codegen.genCode(RuntimeInst.istore);
                return;
            }
            leftOperand.genCode(codegen);
            rightOperand.genCode(codegen);
            codegen.genCode(ins);
            codegen.genCode(RuntimeInst.ipush, codegen.genDataRef(left.getToken().object));
            codegen.genCode(RuntimeInst.istore);
            return;
        } else if (op == OperatorType.COLON) {
            rightOperand.genCode(codegen);
            leftOperand.genCode(codegen);
            return;
        }
    }
    RuntimeInst inst = TokenTools.op2ins(token);
    leftOperand.genCode(codegen);
    RuntimeInstUnary jmp = null;
    switch(inst) {
        case iandl:
            jmp = codegen.genCode(RuntimeInst.ijfx, -1);
            break;
        case iorl:
            jmp = codegen.genCode(RuntimeInst.ijtx, -1);
            break;
        default:
            break;
    }
    rightOperand.genCode(codegen);
    codegen.genCode(inst);
    if (jmp != null) {
        jmp.op1 = codegen.getCodeIndex();
    }
}
Also used : RuntimeInstUnary(priv.bajdcc.LALR1.grammar.runtime.RuntimeInstUnary) RuntimeInst(priv.bajdcc.LALR1.grammar.runtime.RuntimeInst) OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Example 13 with Token

use of priv.bajdcc.LALR1.syntax.token.Token in project jMiniLang by bajdcc.

the class ModuleTask method util_calc.

private static String util_calc(String expr) {
    try {
        priv.bajdcc.OP.grammar.Grammar grammar = new priv.bajdcc.OP.grammar.Grammar(expr);
        grammar.addTerminal("i", TokenType.INTEGER, null);
        grammar.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
        grammar.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
        grammar.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
        grammar.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
        grammar.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
        grammar.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
        String[] nons = new String[] { "E", "T", "F" };
        for (String non : nons) {
            grammar.addNonTerminal(non);
        }
        grammar.addPatternHandler("1", new IPatternHandler() {

            @Override
            public Object handle(List<Token> tokens, List<Object> symbols) {
                return Integer.parseInt(tokens.get(0).object.toString());
            }

            @Override
            public String getPatternName() {
                return "操作数转换";
            }
        });
        grammar.addPatternHandler("010", new IPatternHandler() {

            @Override
            public Object handle(List<Token> tokens, List<Object> symbols) {
                int lop = (int) symbols.get(0);
                int rop = (int) symbols.get(1);
                Token op = tokens.get(0);
                if (op.kToken == TokenType.OPERATOR) {
                    OperatorType kop = (OperatorType) op.object;
                    switch(kop) {
                        case PLUS:
                            return lop + rop;
                        case MINUS:
                            return lop - rop;
                        case TIMES:
                            return lop * rop;
                        case DIVIDE:
                            if (rop == 0) {
                                return lop;
                            } else {
                                return lop / rop;
                            }
                        default:
                            return 0;
                    }
                } else {
                    return 0;
                }
            }

            @Override
            public String getPatternName() {
                return "二元运算";
            }
        });
        grammar.addPatternHandler("101", new IPatternHandler() {

            @Override
            public Object handle(List<Token> tokens, List<Object> symbols) {
                Token ltok = tokens.get(0);
                Token rtok = tokens.get(1);
                Object exp = symbols.get(0);
                if (ltok.object == OperatorType.LPARAN && rtok.object == OperatorType.RPARAN) {
                    // 判断括号
                    return exp;
                }
                return null;
            }

            @Override
            public String getPatternName() {
                return "括号运算";
            }
        });
        grammar.infer("E -> E @PLUS T | E @MINUS T | T");
        grammar.infer("T -> T @TIMES F | T @DIVIDE F | F");
        grammar.infer("F -> @LPA E @RPA | @i");
        grammar.initialize("E");
        return String.valueOf(grammar.run());
    } catch (RegexException e) {
        logger.error("#CALC# Error: " + e.getPosition() + "," + e.getMessage());
    } catch (SyntaxException e) {
        logger.error("#CALC#Error: " + e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
    } catch (GrammarException e) {
        logger.error("#CALC#Error: " + e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
    }
    return "#CALC#Error";
}
Also used : Token(priv.bajdcc.util.lexer.token.Token) Grammar(priv.bajdcc.LALR1.grammar.Grammar) OperatorType(priv.bajdcc.util.lexer.token.OperatorType) SyntaxException(priv.bajdcc.OP.syntax.handler.SyntaxException) GrammarException(priv.bajdcc.OP.grammar.error.GrammarException) RegexException(priv.bajdcc.util.lexer.error.RegexException) IPatternHandler(priv.bajdcc.OP.grammar.handler.IPatternHandler)

Example 14 with Token

use of priv.bajdcc.LALR1.syntax.token.Token in project jMiniLang by bajdcc.

the class TestOperator method main.

public static void main(String[] args) {
    // System.out.println("Z -> `a`<,> | B | [`a` `b` Z B]");
    try {
        // Scanner scanner = new Scanner(System.in);
        // String expr = "( i )";
        // String expr = "((3))+4*5+66/(3-8)";
        // String expr = "(4 * 7 - 13) * (3 + 18 / 3 - 3)";
        String expr = "(5 + 5)";
        Semantic semantic = new Semantic(expr);
        semantic.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
        semantic.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
        semantic.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
        semantic.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
        semantic.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
        semantic.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
        semantic.addTerminal("INTEGER", TokenType.INTEGER, null);
        semantic.addNonTerminal("Z");
        semantic.addNonTerminal("E");
        semantic.addNonTerminal("T");
        semantic.addNonTerminal("F");
        semantic.addActionHandler("enter_paran", (indexed, manage, access, recorder) -> System.out.println("enter"));
        semantic.addActionHandler("leave_paran", (indexed, manage, access, recorder) -> System.out.println("leave"));
        semantic.addErrorHandler("lost_exp", (iterator, bag) -> {
            bag.bRead = false;
            bag.bPass = true;
            return "表达式不完整";
        });
        semantic.addErrorHandler("lost_exp_right", (iterator, bag) -> {
            bag.bRead = false;
            bag.bPass = true;
            return "缺少右括号";
        });
        ISemanticAnalyzer handleCopy = (indexed, query, recorder) -> indexed.get(0).object;
        ISemanticAnalyzer handleBinop = (indexed, query, recorder) -> {
            int lop = Integer.parseInt(indexed.get(0).object.toString());
            int rop = Integer.parseInt(indexed.get(2).object.toString());
            Token op = indexed.get(1).token;
            if (op.kToken == TokenType.OPERATOR) {
                OperatorType kop = (OperatorType) op.object;
                switch(kop) {
                    case PLUS:
                        return lop + rop;
                    case MINUS:
                        return lop - rop;
                    case TIMES:
                        return lop * rop;
                    case DIVIDE:
                        if (rop == 0) {
                            return lop;
                        } else {
                            return lop / rop;
                        }
                    default:
                        return 0;
                }
            } else {
                return 0;
            }
        };
        ISemanticAnalyzer handleValue = (indexed, query, recorder) -> indexed.get(0).token.object;
        // syntax.infer("E -> T `PLUS`<+> E | T `MINUS`<-> E | T");
        // syntax.infer("T -> F `TIMES`<*> T | F `DIVIDE`</> T | F");
        // syntax.infer("F -> `LPA`<(> E `RPA`<)>  | `SYMBOL`<i>");
        semantic.infer(handleCopy, "Z -> E[0]");
        semantic.infer(handleCopy, "E -> T[0]");
        semantic.infer(handleBinop, "E -> E[0] ( @PLUS[1]<+> | @MINUS[1]<-> ) T[2]{lost_exp}");
        semantic.infer(handleCopy, "T -> F[0]");
        semantic.infer(handleBinop, "T -> T[0] ( @TIMES[1]<*> | @DIVIDE[1]</> ) F[2]{lost_exp}");
        semantic.infer(handleValue, "F -> @INTEGER[0]<integer>");
        semantic.infer(handleCopy, "F -> @LPA#enter_paran#<(> E[0]{lost_exp} @RPA#leave_paran#{lost_exp_right}<)>");
        semantic.initialize("Z");
        // System.out.println(semantic.toString());
        // System.out.println(semantic.getNGAString());
        // System.out.println(semantic.getNPAString());
        // System.out.println(semantic.getInst());
        System.out.println(semantic.getTrackerError());
        System.out.println(semantic.getTokenList());
        System.out.println(semantic.getObject());
    // scanner.close();
    } catch (RegexException e) {
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    } catch (SyntaxException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    }
}
Also used : ISemanticAnalyzer(priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer) OperatorType(priv.bajdcc.util.lexer.token.OperatorType) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) Token(priv.bajdcc.util.lexer.token.Token) RegexException(priv.bajdcc.util.lexer.error.RegexException) Semantic(priv.bajdcc.LALR1.semantic.Semantic) TokenType(priv.bajdcc.util.lexer.token.TokenType) ISemanticAnalyzer(priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) Semantic(priv.bajdcc.LALR1.semantic.Semantic) RegexException(priv.bajdcc.util.lexer.error.RegexException) Token(priv.bajdcc.util.lexer.token.Token) OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Aggregations

Token (priv.bajdcc.util.lexer.token.Token)7 OperatorType (priv.bajdcc.util.lexer.token.OperatorType)5 RegexException (priv.bajdcc.util.lexer.error.RegexException)4 ExpValue (priv.bajdcc.LALR1.grammar.tree.ExpValue)3 RuleItem (priv.bajdcc.LALR1.syntax.rule.RuleItem)3 Scanner (java.util.Scanner)2 RuntimeInstUnary (priv.bajdcc.LALR1.grammar.runtime.RuntimeInstUnary)2 NGAEdge (priv.bajdcc.LALR1.syntax.automata.nga.NGAEdge)2 NGAStatus (priv.bajdcc.LALR1.syntax.automata.nga.NGAStatus)2 RuleExp (priv.bajdcc.LALR1.syntax.exp.RuleExp)2 TokenExp (priv.bajdcc.LALR1.syntax.exp.TokenExp)2 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)2 BreadthFirstSearch (priv.bajdcc.util.lexer.automata.BreadthFirstSearch)2 TokenType (priv.bajdcc.util.lexer.token.TokenType)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1