Remove interpreter
This commit is contained in:
@@ -1,82 +1,82 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/benchmark/catch_benchmark.hpp>
|
||||
#include "camellya.h"
|
||||
#include <catch2/benchmark/catch_benchmark.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
using namespace camellya;
|
||||
|
||||
TEST_CASE("basic arithmetic", "[script]") {
|
||||
State state;
|
||||
const char* script = R"(
|
||||
State state;
|
||||
const char *script = R"(
|
||||
var x = 10;
|
||||
var y = 20;
|
||||
var z = x + y;
|
||||
)";
|
||||
|
||||
REQUIRE(state.do_string(script));
|
||||
REQUIRE(state.do_string(script));
|
||||
|
||||
auto z = state.get_global("z");
|
||||
REQUIRE(z);
|
||||
REQUIRE(z->type() == Type::NUMBER);
|
||||
auto z = state.get_global("z");
|
||||
REQUIRE(z);
|
||||
REQUIRE(z->type() == Type::NUMBER);
|
||||
|
||||
auto nz = std::dynamic_pointer_cast<NumberValue>(z);
|
||||
REQUIRE(nz);
|
||||
REQUIRE(nz->value == 30.0);
|
||||
auto nz = std::dynamic_pointer_cast<NumberValue>(z);
|
||||
REQUIRE(nz);
|
||||
REQUIRE(nz->value == 30.0);
|
||||
}
|
||||
|
||||
TEST_CASE("basic function", "[script][func]") {
|
||||
State state;
|
||||
const char* script = R"(
|
||||
State state;
|
||||
const char *script = R"(
|
||||
func add(number x, number y) -> number {
|
||||
return x + y;
|
||||
}
|
||||
var z = add(10, 20);
|
||||
)";
|
||||
|
||||
REQUIRE(state.do_string(script));
|
||||
REQUIRE(state.do_string(script));
|
||||
|
||||
auto z = state.get_global("z");
|
||||
REQUIRE(z);
|
||||
REQUIRE(z->type() == Type::NUMBER);
|
||||
auto z = state.get_global("z");
|
||||
REQUIRE(z);
|
||||
REQUIRE(z->type() == Type::NUMBER);
|
||||
|
||||
auto nz = std::dynamic_pointer_cast<NumberValue>(z);
|
||||
REQUIRE(nz);
|
||||
REQUIRE(nz->value == 30.0);
|
||||
auto nz = std::dynamic_pointer_cast<NumberValue>(z);
|
||||
REQUIRE(nz);
|
||||
REQUIRE(nz->value == 30.0);
|
||||
}
|
||||
|
||||
TEST_CASE("list indexing is 0-based", "[list]") {
|
||||
State state;
|
||||
const char* script = R"(
|
||||
State state;
|
||||
const char *script = R"(
|
||||
var numbers = [10, 20, 30];
|
||||
)";
|
||||
|
||||
REQUIRE(state.do_string(script));
|
||||
REQUIRE(state.do_string(script));
|
||||
|
||||
auto list_val = state.get_global("numbers");
|
||||
REQUIRE(list_val);
|
||||
REQUIRE(list_val->type() == Type::LIST);
|
||||
auto list_val = state.get_global("numbers");
|
||||
REQUIRE(list_val);
|
||||
REQUIRE(list_val->type() == Type::LIST);
|
||||
|
||||
auto list = std::dynamic_pointer_cast<ListValue>(list_val);
|
||||
REQUIRE(list);
|
||||
REQUIRE(list->size() == 3);
|
||||
auto list = std::dynamic_pointer_cast<ListValue>(list_val);
|
||||
REQUIRE(list);
|
||||
REQUIRE(list->size() == 3);
|
||||
|
||||
auto first = list->get(0);
|
||||
auto third = list->get(2);
|
||||
auto first = list->get(0);
|
||||
auto third = list->get(2);
|
||||
|
||||
REQUIRE(first->type() == Type::NUMBER);
|
||||
REQUIRE(third->type() == Type::NUMBER);
|
||||
REQUIRE(first->type() == Type::NUMBER);
|
||||
REQUIRE(third->type() == Type::NUMBER);
|
||||
|
||||
auto first_n = std::dynamic_pointer_cast<NumberValue>(first);
|
||||
auto third_n = std::dynamic_pointer_cast<NumberValue>(third);
|
||||
auto first_n = std::dynamic_pointer_cast<NumberValue>(first);
|
||||
auto third_n = std::dynamic_pointer_cast<NumberValue>(third);
|
||||
|
||||
REQUIRE(first_n->value == 10.0);
|
||||
REQUIRE(third_n->value == 30.0);
|
||||
REQUIRE(first_n->value == 10.0);
|
||||
REQUIRE(third_n->value == 30.0);
|
||||
}
|
||||
|
||||
TEST_CASE("class init is called on declaration", "[class][init]") {
|
||||
State state;
|
||||
const char* script = R"(
|
||||
State state;
|
||||
const char *script = R"(
|
||||
class Person {
|
||||
var age : number;
|
||||
var name : string;
|
||||
@@ -95,38 +95,37 @@ TEST_CASE("class init is called on declaration", "[class][init]") {
|
||||
var a = p.getAge();
|
||||
)";
|
||||
|
||||
REQUIRE(state.do_string(script));
|
||||
REQUIRE(state.do_string(script));
|
||||
|
||||
auto p_val = state.get_global("p");
|
||||
REQUIRE(p_val);
|
||||
REQUIRE(p_val->type() == Type::INSTANCE);
|
||||
auto p_val = state.get_global("p");
|
||||
REQUIRE(p_val);
|
||||
REQUIRE(p_val->type() == Type::INSTANCE);
|
||||
|
||||
auto instance = std::dynamic_pointer_cast<InstanceValue>(p_val);
|
||||
REQUIRE(instance);
|
||||
auto instance = std::dynamic_pointer_cast<InstanceValue>(p_val);
|
||||
REQUIRE(instance);
|
||||
|
||||
auto age_val = instance->get("age");
|
||||
auto name_val = instance->get("name");
|
||||
auto age_val = instance->get("age");
|
||||
auto name_val = instance->get("name");
|
||||
|
||||
REQUIRE(age_val->type() == Type::NUMBER);
|
||||
REQUIRE(name_val->type() == Type::STRING);
|
||||
REQUIRE(age_val->type() == Type::NUMBER);
|
||||
REQUIRE(name_val->type() == Type::STRING);
|
||||
|
||||
auto age_num = std::dynamic_pointer_cast<NumberValue>(age_val);
|
||||
auto name_str = std::dynamic_pointer_cast<StringValue>(name_val);
|
||||
auto age_num = std::dynamic_pointer_cast<NumberValue>(age_val);
|
||||
auto name_str = std::dynamic_pointer_cast<StringValue>(name_val);
|
||||
|
||||
REQUIRE(age_num->value == 18.0);
|
||||
REQUIRE(name_str->value == "Default");
|
||||
REQUIRE(age_num->value == 18.0);
|
||||
REQUIRE(name_str->value == "Default");
|
||||
|
||||
auto a_val = state.get_global("a");
|
||||
REQUIRE(a_val);
|
||||
REQUIRE(a_val->type() == Type::NUMBER);
|
||||
auto a_num = std::dynamic_pointer_cast<NumberValue>(a_val);
|
||||
REQUIRE(a_num->value == 18.0);
|
||||
auto a_val = state.get_global("a");
|
||||
REQUIRE(a_val);
|
||||
REQUIRE(a_val->type() == Type::NUMBER);
|
||||
auto a_num = std::dynamic_pointer_cast<NumberValue>(a_val);
|
||||
REQUIRE(a_num->value == 18.0);
|
||||
}
|
||||
|
||||
TEST_CASE("interpreter performance: simple loop", "[perf][script]") {
|
||||
State state;
|
||||
State state_vm(ExecutionMode::VM);
|
||||
const char* script = R"(
|
||||
State state;
|
||||
const char *script = R"(
|
||||
func sum_to(number n) -> number {
|
||||
var s = 0;
|
||||
for (var i = 0; i < n; i = i + 1) {
|
||||
@@ -137,35 +136,23 @@ TEST_CASE("interpreter performance: simple loop", "[perf][script]") {
|
||||
var r = sum_to(1000);
|
||||
)";
|
||||
|
||||
BENCHMARK("sum_to(1000)") {
|
||||
if (!state.do_string(script)) {
|
||||
auto last_error = state.get_error();
|
||||
REQUIRE(last_error.empty());
|
||||
}
|
||||
|
||||
auto r_val = state.get_global("r");
|
||||
REQUIRE(r_val);
|
||||
REQUIRE(r_val->type() == Type::NUMBER);
|
||||
auto r_num = std::dynamic_pointer_cast<NumberValue>(r_val);
|
||||
REQUIRE(r_num->value == 499500.0);
|
||||
};
|
||||
BENCHMARK("sum_to(1000)") {
|
||||
if (!state.do_string(script)) {
|
||||
auto last_error = state.get_error();
|
||||
REQUIRE(last_error.empty());
|
||||
}
|
||||
|
||||
BENCHMARK("state_vm sum_to(1000)") {
|
||||
if (!state_vm.do_string(script)) {
|
||||
auto last_error = state_vm.get_error();
|
||||
REQUIRE(last_error.empty());
|
||||
}
|
||||
auto r_val = state_vm.get_global("r");
|
||||
REQUIRE(r_val);
|
||||
REQUIRE(r_val->type() == Type::NUMBER);
|
||||
auto r_num = std::dynamic_pointer_cast<NumberValue>(r_val);
|
||||
REQUIRE(r_num->value == 499500.0);
|
||||
};
|
||||
auto r_val = state.get_global("r");
|
||||
REQUIRE(r_val);
|
||||
REQUIRE(r_val->type() == Type::NUMBER);
|
||||
auto r_num = std::dynamic_pointer_cast<NumberValue>(r_val);
|
||||
REQUIRE(r_num->value == 499500.0);
|
||||
};
|
||||
}
|
||||
|
||||
TEST_CASE("loop break", "[script][loop]") {
|
||||
State state;
|
||||
const char* script = R"(
|
||||
State state;
|
||||
const char *script = R"(
|
||||
var sum = 0;
|
||||
for (var i = 0; i < 10; i = i + 1) {
|
||||
if (i == 5) {
|
||||
@@ -175,16 +162,16 @@ TEST_CASE("loop break", "[script][loop]") {
|
||||
}
|
||||
)";
|
||||
|
||||
REQUIRE(state.do_string(script));
|
||||
auto sum_val = state.get_global("sum");
|
||||
REQUIRE(sum_val);
|
||||
auto sum_num = std::dynamic_pointer_cast<NumberValue>(sum_val);
|
||||
REQUIRE(sum_num->value == 10.0); // 0+1+2+3+4 = 10
|
||||
REQUIRE(state.do_string(script));
|
||||
auto sum_val = state.get_global("sum");
|
||||
REQUIRE(sum_val);
|
||||
auto sum_num = std::dynamic_pointer_cast<NumberValue>(sum_val);
|
||||
REQUIRE(sum_num->value == 10.0); // 0+1+2+3+4 = 10
|
||||
}
|
||||
|
||||
TEST_CASE("loop continue", "[script][loop]") {
|
||||
State state;
|
||||
const char* script = R"(
|
||||
State state;
|
||||
const char *script = R"(
|
||||
var sum = 0;
|
||||
for (var i = 0; i < 5; i = i + 1) {
|
||||
if (i == 2) {
|
||||
@@ -194,16 +181,16 @@ TEST_CASE("loop continue", "[script][loop]") {
|
||||
}
|
||||
)";
|
||||
|
||||
REQUIRE(state.do_string(script));
|
||||
auto sum_val = state.get_global("sum");
|
||||
REQUIRE(sum_val);
|
||||
auto sum_num = std::dynamic_pointer_cast<NumberValue>(sum_val);
|
||||
REQUIRE(sum_num->value == 8.0); // 0+1+3+4 = 8
|
||||
REQUIRE(state.do_string(script));
|
||||
auto sum_val = state.get_global("sum");
|
||||
REQUIRE(sum_val);
|
||||
auto sum_num = std::dynamic_pointer_cast<NumberValue>(sum_val);
|
||||
REQUIRE(sum_num->value == 8.0); // 0+1+3+4 = 8
|
||||
}
|
||||
|
||||
TEST_CASE("while break and continue", "[script][loop]") {
|
||||
State state;
|
||||
const char* script = R"(
|
||||
State state;
|
||||
const char *script = R"(
|
||||
var i = 0;
|
||||
var sum = 0;
|
||||
while (i < 10) {
|
||||
@@ -218,16 +205,16 @@ TEST_CASE("while break and continue", "[script][loop]") {
|
||||
}
|
||||
)";
|
||||
|
||||
REQUIRE(state.do_string(script));
|
||||
auto sum_val = state.get_global("sum");
|
||||
REQUIRE(sum_val);
|
||||
auto sum_num = std::dynamic_pointer_cast<NumberValue>(sum_val);
|
||||
REQUIRE(sum_num->value == 12.0);
|
||||
// 1st iter: i=1, sum=1
|
||||
// 2nd iter: i=2, sum=1+2=3
|
||||
// 3rd iter: i=3, continue
|
||||
// 4th iter: i=4, sum=3+4=7
|
||||
// 5th iter: i=5, sum=7+5=12
|
||||
// 6th iter: i=6, break
|
||||
// Result should be 12.0
|
||||
REQUIRE(state.do_string(script));
|
||||
auto sum_val = state.get_global("sum");
|
||||
REQUIRE(sum_val);
|
||||
auto sum_num = std::dynamic_pointer_cast<NumberValue>(sum_val);
|
||||
REQUIRE(sum_num->value == 12.0);
|
||||
// 1st iter: i=1, sum=1
|
||||
// 2nd iter: i=2, sum=1+2=3
|
||||
// 3rd iter: i=3, continue
|
||||
// 4th iter: i=4, sum=3+4=7
|
||||
// 5th iter: i=5, sum=7+5=12
|
||||
// 6th iter: i=6, break
|
||||
// Result should be 12.0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user