From 3c9c1dd2aa7c69c92f696b95aff99ea9043896f6 Mon Sep 17 00:00:00 2001 From: otya128 Date: Wed, 3 Jun 2015 21:18:55 +0900 Subject: [PATCH] =?UTF-8?q?=E6=8B=AC=E5=BC=A7=E3=81=AE=E5=AE=9F=EF=BF=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SMILEBASIC/main.d | 3 ++- SMILEBASIC/parser.d | 15 +++++++++++++-- SMILEBASIC/token.d | 2 ++ 3 files changed, 17 insertions(+), 3 deletions(-) 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