|
| 1 | +// 用 ts 重新实现一遍 calc |
| 2 | +enum TokenType { |
| 3 | + INTEGER, |
| 4 | + PLUS, |
| 5 | + MINUS |
| 6 | +} |
| 7 | + |
| 8 | +class Token { |
| 9 | + public constructor(public token_type: TokenType, public value: string | number) {} |
| 10 | +} |
| 11 | + |
| 12 | +class Interpreter { |
| 13 | + private text: string; |
| 14 | + private current_token : Token; |
| 15 | + private pos : number; |
| 16 | + private current_char : string; |
| 17 | + private number_reg: RegExp = new RegExp("^[0-9]$"); |
| 18 | + |
| 19 | + public constructor(text: string) { |
| 20 | + this.text = text; |
| 21 | + this.pos = 0; |
| 22 | + this.current_char = this.text.charAt(this.pos); |
| 23 | + } |
| 24 | + |
| 25 | + private throwError(msg: string) { |
| 26 | + throw new SyntaxError(msg); |
| 27 | + } |
| 28 | + |
| 29 | + private currentCharIsDigit() { |
| 30 | + return this.number_reg.test(this.current_char); |
| 31 | + } |
| 32 | + |
| 33 | + private advance() { |
| 34 | + this.pos += 1; |
| 35 | + if (this.pos < this.text.length) { |
| 36 | + this.current_char = this.text.charAt(this.pos); |
| 37 | + } else { |
| 38 | + this.current_char = null; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private getNumber() : number { |
| 43 | + let start_pos = this.pos; |
| 44 | + while (null != this.current_char && this.currentCharIsDigit()) { |
| 45 | + this.advance(); |
| 46 | + } |
| 47 | + let result = Number(this.text.substring(start_pos, this.pos)); |
| 48 | + return result; |
| 49 | + } |
| 50 | + |
| 51 | + private skipWhiteSpace() { |
| 52 | + while (this.current_char != null && this.current_char == " ") { |
| 53 | + this.advance(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private getNextToken() : Token { |
| 58 | + while (this.current_char != null) { |
| 59 | + if (this.current_char == " ") { |
| 60 | + this.skipWhiteSpace(); |
| 61 | + } else if (this.currentCharIsDigit()) { |
| 62 | + return new Token(TokenType.INTEGER, this.getNumber()); |
| 63 | + } else if (this.current_char == "+") { |
| 64 | + this.advance(); |
| 65 | + return new Token(TokenType.PLUS, "+"); |
| 66 | + } else if (this.current_char == "-") { |
| 67 | + this.advance(); |
| 68 | + return new Token(TokenType.MINUS, "-"); |
| 69 | + } else { |
| 70 | + this.throwError("term should be number or plus or minus"); |
| 71 | + } |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + private assertTokenType(type: TokenType) { |
| 77 | + if (this.current_token.token_type == type) { |
| 78 | + this.current_token = this.getNextToken(); |
| 79 | + return; |
| 80 | + } |
| 81 | + this.throwError(`current token should be ${type}`); |
| 82 | + } |
| 83 | + |
| 84 | + private getTerm() : number { |
| 85 | + let token = this.current_token; |
| 86 | + this.assertTokenType(TokenType.INTEGER); |
| 87 | + return token.value as number; |
| 88 | + } |
| 89 | + |
| 90 | + public interpreter() : number{ |
| 91 | + this.current_token = this.getNextToken(); |
| 92 | + let result = this.current_token.value as number; |
| 93 | + this.assertTokenType(TokenType.INTEGER); |
| 94 | + |
| 95 | + while (this.current_token != null && (this.current_token.token_type == TokenType.PLUS || this.current_token.token_type == TokenType.MINUS)) { |
| 96 | + if (this.current_token.token_type == TokenType.PLUS) { |
| 97 | + this.assertTokenType(TokenType.PLUS); |
| 98 | + result += this.getTerm(); |
| 99 | + } else if (this.current_token.token_type == TokenType.MINUS) { |
| 100 | + this.assertTokenType(TokenType.MINUS); |
| 101 | + result -= this.getTerm(); |
| 102 | + } else { |
| 103 | + this.throwError("Only support plus and minus"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return result; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +const test_string_1: string = "1 + 2"; |
| 112 | +const test_string_2: string = "1 + 2 + 3 - 5"; |
| 113 | +const test_string_3: string = "12 + 32 - 3 + 321" |
| 114 | + |
| 115 | +console.log(`${test_string_1} = ${new Interpreter(test_string_1).interpreter()}`); |
| 116 | +console.log(`${test_string_2} = ${new Interpreter(test_string_2).interpreter()}`); |
| 117 | +console.log(`${test_string_3} = ${new Interpreter(test_string_3).interpreter()}`); |
0 commit comments