diff --git a/SMILEBASIC/main.d b/SMILEBASIC/main.d index 1163682..bdd72d0 100644 --- a/SMILEBASIC/main.d +++ b/SMILEBASIC/main.d @@ -3,9 +3,10 @@ import otya.smilebasic.parser; int main(string[] argv) { writeln("Hello D-World!"); - auto parser = new Parser("1+2*3+4"); + auto parser = new Parser("1+2*(3+4)"); writeln(parser.calc()); writeln(1+2*3+4); + writeln(1+2*(3+4)); readln(); return 0; } diff --git a/SMILEBASIC/parser.d b/SMILEBASIC/parser.d index f9c984a..7763e2a 100644 --- a/SMILEBASIC/parser.d +++ b/SMILEBASIC/parser.d @@ -21,6 +21,8 @@ class Lexical table['-'] = TokenType.Minus; table['*'] = TokenType.Mul; table['/'] = TokenType.Div; + table['('] = TokenType.LParen; + table[')'] = TokenType.RParen; } bool empty() { @@ -140,6 +142,7 @@ class Parser } int calc() { + lex.popFront(); auto exp = expression(); writeln(); return calc(exp); @@ -177,7 +180,6 @@ class Parser Expression expression() { Expression node = null; - lex.popFront(); return term(8, node); } Expression term(int order, Expression node) @@ -221,10 +223,19 @@ class Parser stdout.flush(); node = new Constant(Value(token.value.integerValue)); break; + case TokenType.LParen: + lex.popFront(); + node = expression(); + token = lex.front(); + if(token.type != TokenType.RParen) + //error + {} + break; default: return node; } - if(!lex.empty())lex.popFront(); + if(!lex.empty()) + lex.popFront(); return node; } } diff --git a/SMILEBASIC/token.d b/SMILEBASIC/token.d index 4370c1d..f7252e8 100644 --- a/SMILEBASIC/token.d +++ b/SMILEBASIC/token.d @@ -16,6 +16,8 @@ enum TokenType Xor, Print, If, + LParen, + RParen, } struct Token