単体テストの追加
This commit is contained in:
@@ -3,9 +3,9 @@ import otya.smilebasic.parser;
|
|||||||
int main(string[] argv)
|
int main(string[] argv)
|
||||||
{
|
{
|
||||||
writeln("Hello D-World!");
|
writeln("Hello D-World!");
|
||||||
auto parser = new Parser("1+2");
|
auto parser = new Parser("1+2*3+4");
|
||||||
writeln();
|
|
||||||
writeln(parser.calc());
|
writeln(parser.calc());
|
||||||
|
writeln(1+2*3+4);
|
||||||
readln();
|
readln();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,11 +29,17 @@ class BinaryOperator : Expression
|
|||||||
Expression item1;
|
Expression item1;
|
||||||
TokenType operator;
|
TokenType operator;
|
||||||
Expression item2;
|
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.type = NodeType.BinaryOperator;
|
||||||
this.item1 = i1;
|
this.item1 = i1;
|
||||||
this.operator = o;
|
|
||||||
}
|
}
|
||||||
this(Expression i1, TokenType o, Expression i2)
|
this(Expression i1, TokenType o, Expression i2)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -141,6 +141,7 @@ class Parser
|
|||||||
int calc()
|
int calc()
|
||||||
{
|
{
|
||||||
auto exp = expression();
|
auto exp = expression();
|
||||||
|
writeln();
|
||||||
return calc(exp);
|
return calc(exp);
|
||||||
}
|
}
|
||||||
int calc(Expression exp)
|
int calc(Expression exp)
|
||||||
@@ -189,14 +190,20 @@ class Parser
|
|||||||
auto token = lex.front();
|
auto token = lex.front();
|
||||||
if(order == getOPRank(token.type))
|
if(order == getOPRank(token.type))
|
||||||
{
|
{
|
||||||
BinaryOperator op = new BinaryOperator(exp, token.type);
|
BinaryOperator op = new BinaryOperator(exp);
|
||||||
while(order == getOPRank(token.type))
|
while(order == getOPRank(token.type))
|
||||||
{
|
{
|
||||||
auto tt = token.type;
|
auto tt = token.type;
|
||||||
|
op.operator = token.type;
|
||||||
lex.popFront();
|
lex.popFront();
|
||||||
op.item2 = term(order - 1, node);
|
op.item2 = term(order - 1, node);
|
||||||
token = lex.front();
|
token = lex.front();
|
||||||
write(tt, " ");
|
write(tt, " ");
|
||||||
|
if(order == getOPRank(token.type))
|
||||||
|
{
|
||||||
|
BinaryOperator op2 = new BinaryOperator(op);
|
||||||
|
op.item1 = op2;
|
||||||
|
}
|
||||||
stdout.flush();
|
stdout.flush();
|
||||||
}
|
}
|
||||||
return op;
|
return op;
|
||||||
@@ -221,3 +228,18 @@ class Parser
|
|||||||
return node;
|
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"();
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user