diff options
| Commit | 09e59ae226f2fcb469b944c815b1e1bf46d4c569 (patch) | |
|---|---|---|
| Author | 2018-11-25 17:49:14 +0200 | |
| Committer | 2018-11-25 17:49:14 +0200 | |
| Files | d8273d7a27cab059957720e06dc19c2c0fd728d1 /JSImpl | |
| IPL-09e59ae226f2fcb469b944c815b1e1bf46d4c569.tar IPL-09e59ae226f2fcb469b944c815b1e1bf46d4c569.tar.gz IPL-09e59ae226f2fcb469b944c815b1e1bf46d4c569.zip | ||
| Parent | 2425ccff65421e82dc406125c52b7bf804dcc918 (diff) | |
Implement comparison operators (#71)
* Implement comparison operators.
* Remove debug code.
* Undo test code.
3 files changed, 117 insertions, 6 deletions
| -rw-r--r-- | JSImpl/src/ASTInterpreter.cpp | 9 | ||||
| -rw-r--r-- | JSImpl/src/ByteCodeGenerator.cpp | 43 | ||||
| -rw-r--r-- | JSImpl/test/sprtTests.cpp | 71 |
diff --git a/JSImpl/src/ASTInterpreter.cpp b/JSImpl/src/ASTInterpreter.cpp index f7d1dc2..25f5c02 100644 --- a/JSImpl/src/ASTInterpreter.cpp +++ b/JSImpl/src/ASTInterpreter.cpp @@ -185,6 +185,15 @@ void ASTInterpreter::Visit(BinaryExpression* e) { case TokenType::Less: m_Evaluation.push_back(left < right); break; + case TokenType::LessEqual: + m_Evaluation.push_back(left <= right); + break; + case TokenType::Greater: + m_Evaluation.push_back(left > right); + break; + case TokenType::GreaterEqual: + m_Evaluation.push_back(left >= right); + break; case TokenType::Comma: m_Evaluation.push_back(right); break; diff --git a/JSImpl/src/ByteCodeGenerator.cpp b/JSImpl/src/ByteCodeGenerator.cpp index dc846f2..fd85326 100644 --- a/JSImpl/src/ByteCodeGenerator.cpp +++ b/JSImpl/src/ByteCodeGenerator.cpp @@ -53,7 +53,11 @@ private: SAVE,
RESTORE,
LESS,
+ LESSEQ,
+ GREATER,
+ GREATEREQ,
EQ,
+ NEQ,
LEQ,
GETG,
SETG,
@@ -282,11 +286,20 @@ void ByteCodeGenerator::Visit(BinaryExpression* e) case TokenType::Less:
PushInstruction(Instruction::Type::LESS, o, l, r);
return;
+ case TokenType::LessEqual:
+ PushInstruction(Instruction::Type::LESSEQ, o, l, r);
+ return;
+ case TokenType::Greater:
+ PushInstruction(Instruction::Type::GREATER, o, l, r);
+ return;
+ case TokenType::GreaterEqual:
+ PushInstruction(Instruction::Type::GREATEREQ, o, l, r);
+ break;
case TokenType::EqualEqual:
PushInstruction(Instruction::Type::EQ, o, l, r);
return;
case TokenType::BangEqual:
- NOT_IMPLEMENTED;
+ PushInstruction(Instruction::Type::NEQ,o,l,r);
break;
case TokenType::Equal:
@@ -378,8 +391,8 @@ void ByteCodeGenerator::Visit(UnaryExpression* e) PushInstruction(Instruction::Type::SUB, reg, reg, one);
}
return;
- default: - NOT_IMPLEMENTED; + default:
+ NOT_IMPLEMENTED;
break;
}
}
@@ -427,8 +440,8 @@ void ByteCodeGenerator::Visit(UnaryExpression* e) case TokenType::Bang:
NOT_IMPLEMENTED;
return;
- default: - NOT_IMPLEMENTED; + default:
+ NOT_IMPLEMENTED;
break;
}
}
@@ -532,11 +545,31 @@ IPLString ByteCodeGenerator::GetCode() + " r" + std::to_string(ResolveRegisterName(i.Args[1]))
+ " r" + std::to_string(ResolveRegisterName(i.Args[2])) + '\n';
break;
+ case ByteCodeGenerator::Instruction::LESSEQ:
+ result += "lesseq r" + std::to_string(ResolveRegisterName(i.Args[0]))
+ + " r" + std::to_string(ResolveRegisterName(i.Args[1]))
+ + " r" + std::to_string(ResolveRegisterName(i.Args[2])) + '\n';
+ break;
+ case ByteCodeGenerator::Instruction::GREATER:
+ result += "greater r" + std::to_string(ResolveRegisterName(i.Args[0]))
+ + " r" + std::to_string(ResolveRegisterName(i.Args[1]))
+ + " r" + std::to_string(ResolveRegisterName(i.Args[2])) + '\n';
+ break;
+ case ByteCodeGenerator::Instruction::GREATEREQ:
+ result += "greatereq r" + std::to_string(ResolveRegisterName(i.Args[0]))
+ + " r" + std::to_string(ResolveRegisterName(i.Args[1]))
+ + " r" + std::to_string(ResolveRegisterName(i.Args[2])) + '\n';
+ break;
case ByteCodeGenerator::Instruction::EQ:
result += "eq r" + std::to_string(ResolveRegisterName(i.Args[0]))
+ " r" + std::to_string(ResolveRegisterName(i.Args[1]))
+ " r" + std::to_string(ResolveRegisterName(i.Args[2])) + '\n';
break;
+ case ByteCodeGenerator::Instruction::NEQ:
+ result += "neq r" + std::to_string(ResolveRegisterName(i.Args[0]))
+ + " r" + std::to_string(ResolveRegisterName(i.Args[1]))
+ + " r" + std::to_string(ResolveRegisterName(i.Args[2])) + '\n';
+ break;
case ByteCodeGenerator::Instruction::LEQ:
result += "leq r" + std::to_string(ResolveRegisterName(i.Args[0]))
+ " r" + std::to_string(ResolveRegisterName(i.Args[1]))
diff --git a/JSImpl/test/sprtTests.cpp b/JSImpl/test/sprtTests.cpp index 8480554..a7b9e3b 100644 --- a/JSImpl/test/sprtTests.cpp +++ b/JSImpl/test/sprtTests.cpp @@ -127,7 +127,6 @@ TEST_F(SPRTTest, Less) ASSERT_EQ(Output.str(), "100"); } - TEST_F(SPRTTest, LessEq) { Spasm::byte bytecode[] = { @@ -145,6 +144,76 @@ TEST_F(SPRTTest, LessEq) ASSERT_EQ(Output.str(), "101"); } +TEST_F(SPRTTest, Greater) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::Greater, 3, 1, 2, + OpCodes::Greater, 4, 2, 1, + OpCodes::Greater, 5, 2, 2, + OpCodes::Print, 3, + OpCodes::Print, 4, + OpCodes::Print, 5, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "010"); +} + +TEST_F(SPRTTest, GreaterEq) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::GreaterEq, 3, 1, 2, + OpCodes::GreaterEq, 4, 2, 1, + OpCodes::GreaterEq, 5, 2, 2, + OpCodes::Print, 3, + OpCodes::Print, 4, + OpCodes::Print, 5, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "011"); +} + +TEST_F(SPRTTest, Equal) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::Const, 3, 6, + OpCodes::Equal, 4, 2, 1, + OpCodes::Equal, 5, 2, 2, + OpCodes::Equal, 6, 1, 3, + OpCodes::Print, 4, + OpCodes::Print, 5, + OpCodes::Print, 6, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "011"); +} + +TEST_F(SPRTTest, NotEqual) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::Const, 3, 6, + OpCodes::NotEqual, 4, 2, 1, + OpCodes::NotEqual, 5, 2, 2, + OpCodes::NotEqual, 6, 1, 3, + OpCodes::Print, 4, + OpCodes::Print, 5, + OpCodes::Print, 6, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "100"); +} + TEST_F(SPRTTest, Jump) { Spasm::byte bytecode[] = { |
