Search in sources :

Example 1 with OperatorType

use of priv.bajdcc.util.lexer.token.OperatorType 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 OperatorType

use of priv.bajdcc.util.lexer.token.OperatorType in project jMiniLang by bajdcc.

the class ExpSinop method genCode.

@Override
public void genCode(ICodegen codegen) {
    operand.genCode(codegen);
    codegen.genCode(TokenTools.op2ins(token));
    OperatorType type = (OperatorType) token.object;
    if (type == OperatorType.PLUS_PLUS || type == OperatorType.MINUS_MINUS) {
        ExpValue value = (ExpValue) operand;
        codegen.genCode(RuntimeInst.ipush, codegen.genDataRef(value.getToken().object));
        codegen.genCode(RuntimeInst.istore);
    }
}
Also used : OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Example 3 with OperatorType

use of priv.bajdcc.util.lexer.token.OperatorType in project jMiniLang by bajdcc.

the class TestGrammar 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);
        Grammar grammar = new Grammar("3 - (28 / (4 * 7)) * (2 + 4) + 5");
        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");
        System.out.println(grammar.getPrecedenceString());
        System.out.println(grammar.toString());
        grammar.run();
        System.out.println(grammar.getTokenString());
    // 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();
    } catch (GrammarException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    }
}
Also used : Token(priv.bajdcc.util.lexer.token.Token) Grammar(priv.bajdcc.OP.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 4 with OperatorType

use of priv.bajdcc.util.lexer.token.OperatorType in project jMiniLang by bajdcc.

the class TestGrammar2 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);
        Grammar grammar = new Grammar("!3 - (28 / (!4 * 7)) * (2 + 4) + 5");
        grammar.addTerminal("i", TokenType.INTEGER, null);
        grammar.addTerminal("NEGATIVE", TokenType.OPERATOR, OperatorType.LOGICAL_NOT);
        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", "G" };
        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.addPatternHandler("10", new IPatternHandler() {

            @Override
            public Object handle(List<Token> tokens, List<Object> symbols) {
                Token unary = tokens.get(0);
                int op = (int) symbols.get(0);
                if (unary.object == OperatorType.LOGICAL_NOT) {
                    // 判断取反
                    return -op;
                }
                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 -> @NEGATIVE G | G");
        grammar.infer("G -> @LPA E @RPA | @i");
        grammar.initialize("E");
        System.out.println(grammar.getPrecedenceString());
        System.out.println(grammar.toString());
        grammar.run();
        System.out.println(grammar.getTokenString());
    // 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();
    } catch (GrammarException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    }
}
Also used : Token(priv.bajdcc.util.lexer.token.Token) Grammar(priv.bajdcc.OP.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 5 with OperatorType

use of priv.bajdcc.util.lexer.token.OperatorType in project jMiniLang by bajdcc.

the class OperatorTokenizer method getToken.

/*
	 * (非 Javadoc)
	 * 
	 * @see
	 * priv.bajdcc.lexer.algorithm.ITokenAlgorithm#getToken(java.lang.String,
	 * priv.bajdcc.lexer.token.Token)
	 */
@Override
public Token getToken(String string, Token token, IRegexStringIterator iterator) {
    token.kToken = TokenType.OPERATOR;
    OperatorType op = hashOperator.get(string);
    if (op == OperatorType.MINUS) {
        if (iterator.available()) {
            char ch = iterator.current();
            if (ch == '.' || Character.isDigit(ch)) {
                // 判定是数字
                return null;
            }
        }
    }
    token.object = op;
    return token;
}
Also used : OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Aggregations

OperatorType (priv.bajdcc.util.lexer.token.OperatorType)12 Token (priv.bajdcc.util.lexer.token.Token)6 RegexException (priv.bajdcc.util.lexer.error.RegexException)4 GrammarException (priv.bajdcc.OP.grammar.error.GrammarException)3 IPatternHandler (priv.bajdcc.OP.grammar.handler.IPatternHandler)3 SyntaxException (priv.bajdcc.OP.syntax.handler.SyntaxException)3 ExpValue (priv.bajdcc.LALR1.grammar.tree.ExpValue)2 Grammar (priv.bajdcc.OP.grammar.Grammar)2 Grammar (priv.bajdcc.LALR1.grammar.Grammar)1 RuntimeInst (priv.bajdcc.LALR1.grammar.runtime.RuntimeInst)1 RuntimeInstUnary (priv.bajdcc.LALR1.grammar.runtime.RuntimeInstUnary)1 Semantic (priv.bajdcc.LALR1.semantic.Semantic)1 ISemanticAnalyzer (priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)1 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)1 MetaType (priv.bajdcc.util.lexer.token.MetaType)1 TokenType (priv.bajdcc.util.lexer.token.TokenType)1