|
| 1 | +package compiler.infix; |
| 2 | + |
| 3 | +import compiler.expr.*; |
| 4 | +import compiler.expr.ast.ASTNode; |
| 5 | +import compiler.expr.ast.BinOp; |
| 6 | +import compiler.expr.ast.IntNode; |
| 7 | +import compiler.expr.ast.UnaryNode; |
| 8 | +import io.study.exception.StdException; |
| 9 | + |
| 10 | +import java.util.LinkedHashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.Callable; |
| 13 | +import java.util.function.Function; |
| 14 | + |
| 15 | +/** |
| 16 | + * pratt parser |
| 17 | + * http://www.craftinginterpreters.com/compiling-expressions.html#parsing-infix-expressions |
| 18 | + */ |
| 19 | +public class Program { |
| 20 | + Map<TokenType,ParseRule> parseRuleMap = new LinkedHashMap<>(); |
| 21 | + Lexer lexer = null; |
| 22 | + Token currentToken; |
| 23 | + Token prevToken; |
| 24 | + |
| 25 | + public void advance(){ |
| 26 | + prevToken = currentToken; |
| 27 | + currentToken = lexer.getNextToken(); |
| 28 | + } |
| 29 | + public void consume(TokenType tokenType){ |
| 30 | + if(tokenType != currentToken.getTokenType()){ |
| 31 | + throw new SyntaxException("invalid token:"+currentToken.getTokenType()+",expect is :"+tokenType); |
| 32 | + } |
| 33 | + advance(); |
| 34 | + } |
| 35 | + public Program(Lexer lexer){ |
| 36 | + this.lexer = lexer; |
| 37 | + advance(); |
| 38 | + } |
| 39 | + |
| 40 | + public Object parsePrecedence(Precedence precedence){ |
| 41 | + advance(); |
| 42 | + Function prefixRule = parseRuleMap.get(prevToken.getTokenType()).getPrefix(); |
| 43 | + if(prefixRule == null){ |
| 44 | + throw new StdException("expect expression"); |
| 45 | + } |
| 46 | + Object left = prefixRule.apply(null); |
| 47 | + while(precedence.ordinal() < parseRuleMap.get(currentToken.getTokenType()).precedence.ordinal()){ |
| 48 | + advance(); |
| 49 | + Function infix = parseRuleMap.get(prevToken.getTokenType()).infix; |
| 50 | + left = infix.apply(left); |
| 51 | + } |
| 52 | + return left; |
| 53 | + } |
| 54 | + public Object grouping(Object param){ |
| 55 | + expression(); |
| 56 | + consume(TokenType.RIGHT_PARA); |
| 57 | + return null; |
| 58 | + } |
| 59 | + public Object unary(Object param){ |
| 60 | + return new UnaryNode((ASTNode) parsePrecedence(Precedence.UNARY)); |
| 61 | + } |
| 62 | + public Object expression(){ |
| 63 | + return parsePrecedence(Precedence.ASSIGNMENT); |
| 64 | + } |
| 65 | + |
| 66 | + public Object binary(Object left){ |
| 67 | + TokenType prevTokenType = prevToken.getTokenType(); |
| 68 | + ParseRule parseRule = parseRuleMap.get(prevTokenType); |
| 69 | + Object right = parsePrecedence(Precedence.values()[parseRule.precedence.ordinal()+1]); |
| 70 | + return new BinOp((ASTNode) left,new Token(prevTokenType), (ASTNode) right); |
| 71 | + } |
| 72 | + public Object number(){ |
| 73 | + return new IntNode(new Token(TokenType.INTEGER,prevToken.getValue())); |
| 74 | + } |
| 75 | + public Object number(Object object){ |
| 76 | + return number(); |
| 77 | + } |
| 78 | + public void init(){ |
| 79 | + parseRuleMap.put(TokenType.LEFT_PARA,new ParseRule(this::grouping,null,Precedence.NONE)); |
| 80 | + parseRuleMap.put(TokenType.PLUS,new ParseRule(null,this::binary,Precedence.TERM)); |
| 81 | + parseRuleMap.put(TokenType.SUB,new ParseRule(this::unary,this::binary,Precedence.TERM)); |
| 82 | + parseRuleMap.put(TokenType.INTEGER,new ParseRule(this::number,null,Precedence.NONE)); |
| 83 | + parseRuleMap.put(TokenType.EOF,new ParseRule(null,null,Precedence.NONE)); |
| 84 | + } |
| 85 | + public static void main(String[] args){ |
| 86 | + Program program = new Program(new Lexer("-1+2+3")); |
| 87 | + program.init(); |
| 88 | + Object ret = program.expression(); |
| 89 | + System.out.println(ret); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | +} |
0 commit comments