diff --git a/SMILEBASIC/main.d b/SMILEBASIC/main.d index c26e9be..1163682 100644 --- a/SMILEBASIC/main.d +++ b/SMILEBASIC/main.d @@ -3,9 +3,9 @@ import otya.smilebasic.parser; int main(string[] argv) { writeln("Hello D-World!"); - auto parser = new Parser("1+2"); - writeln(); + auto parser = new Parser("1+2*3+4"); writeln(parser.calc()); + writeln(1+2*3+4); readln(); return 0; } diff --git a/SMILEBASIC/node.d b/SMILEBASIC/node.d index 321c98f..7ff592c 100644 --- a/SMILEBASIC/node.d +++ b/SMILEBASIC/node.d @@ -29,11 +29,17 @@ class BinaryOperator : Expression Expression item1; TokenType operator; Expression item2; - this(Expression i1, TokenType o) + this(BinaryOperator bop) + { + this.type = NodeType.BinaryOperator; + this.item1 = bop.item1; + this.operator = bop.operator; + this.item2 = bop.item2; + } + this(Expression i1) { this.type = NodeType.BinaryOperator; this.item1 = i1; - this.operator = o; } this(Expression i1, TokenType o, Expression i2) { diff --git a/SMILEBASIC/parser.d b/SMILEBASIC/parser.d index e8de6b8..f9c984a 100644 --- a/SMILEBASIC/parser.d +++ b/SMILEBASIC/parser.d @@ -141,6 +141,7 @@ class Parser int calc() { auto exp = expression(); + writeln(); return calc(exp); } int calc(Expression exp) @@ -189,14 +190,20 @@ class Parser auto token = lex.front(); if(order == getOPRank(token.type)) { - BinaryOperator op = new BinaryOperator(exp, token.type); + BinaryOperator op = new BinaryOperator(exp); while(order == getOPRank(token.type)) { auto tt = token.type; + op.operator = token.type; lex.popFront(); op.item2 = term(order - 1, node); token = lex.front(); write(tt, " "); + if(order == getOPRank(token.type)) + { + BinaryOperator op2 = new BinaryOperator(op); + op.item1 = op2; + } stdout.flush(); } return op; @@ -221,3 +228,18 @@ class Parser return node; } } +void test(wstring exp, int result) +{ + auto parser = new Parser(exp); + assert(parser.calc() == result); +} +//D言語の式の評価結果と同じか検証 +void Test(const char[] V)() +{ + mixin("writeln(\""~V~"\",\" = \" ,"~V~");test(\""~V~"\", "~V~");"); +} +unittest +{ + Test!"2+20/5*3"(); + Test!"2-3*4-5"(); +}