Remove interpreter
This commit is contained in:
@@ -13,7 +13,6 @@ set(LIB_SOURCES
|
|||||||
src/lexer.cpp
|
src/lexer.cpp
|
||||||
src/parser.cpp
|
src/parser.cpp
|
||||||
src/value.cpp
|
src/value.cpp
|
||||||
src/interpreter.cpp
|
|
||||||
src/state.cpp
|
src/state.cpp
|
||||||
src/chunk.cpp
|
src/chunk.cpp
|
||||||
src/compiler.cpp
|
src/compiler.cpp
|
||||||
@@ -27,7 +26,6 @@ set(LIB_HEADERS
|
|||||||
src/parser.h
|
src/parser.h
|
||||||
src/ast.h
|
src/ast.h
|
||||||
src/value.h
|
src/value.h
|
||||||
src/interpreter.h
|
|
||||||
src/state.h
|
src/state.h
|
||||||
src/chunk.h
|
src/chunk.h
|
||||||
src/compiler.h
|
src/compiler.h
|
||||||
|
|||||||
@@ -4,13 +4,10 @@
|
|||||||
|
|
||||||
namespace camellya {
|
namespace camellya {
|
||||||
|
|
||||||
State::State(ExecutionMode mode)
|
State::State()
|
||||||
: execution_mode_(mode),
|
: vm_(std::make_unique<VM>()), compiler_(std::make_unique<Compiler>()) {}
|
||||||
interpreter_(std::make_unique<Interpreter>()),
|
|
||||||
vm_(std::make_unique<VM>()),
|
|
||||||
compiler_(std::make_unique<Compiler>()) {}
|
|
||||||
|
|
||||||
bool State::do_string(const std::string& script) {
|
bool State::do_string(const std::string &script) {
|
||||||
try {
|
try {
|
||||||
Lexer lexer(script);
|
Lexer lexer(script);
|
||||||
auto tokens = lexer.tokenize();
|
auto tokens = lexer.tokenize();
|
||||||
@@ -23,13 +20,13 @@ bool State::do_string(const std::string& script) {
|
|||||||
last_error_.clear();
|
last_error_.clear();
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception &e) {
|
||||||
last_error_ = e.what();
|
last_error_ = e.what();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool State::do_file(const std::string& filename) {
|
bool State::do_file(const std::string &filename) {
|
||||||
std::ifstream file(filename);
|
std::ifstream file(filename);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
last_error_ = "Failed to open file: " + filename;
|
last_error_ = "Failed to open file: " + filename;
|
||||||
@@ -41,37 +38,24 @@ bool State::do_file(const std::string& filename) {
|
|||||||
return do_string(buffer.str());
|
return do_string(buffer.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void State::register_function(const std::string& name, NativeFunction func) {
|
void State::register_function(const std::string &name, NativeFunction func) {
|
||||||
auto func_value = std::make_shared<FunctionValue>(name, func);
|
auto func_value = std::make_shared<FunctionValue>(name, func);
|
||||||
|
|
||||||
if (execution_mode_ == ExecutionMode::INTERPRETER) {
|
|
||||||
interpreter_->global_environment->define(name, func_value);
|
|
||||||
} else {
|
|
||||||
vm_->register_native_function(name, func);
|
vm_->register_native_function(name, func);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ValuePtr State::get_global(const std::string& name) {
|
ValuePtr State::get_global(const std::string &name) {
|
||||||
if (execution_mode_ == ExecutionMode::INTERPRETER) {
|
|
||||||
return interpreter_->global_environment->get(name);
|
|
||||||
} else {
|
|
||||||
return vm_->get_global(name);
|
return vm_->get_global(name);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void State::set_global(const std::string& name, ValuePtr value) {
|
void State::set_global(const std::string &name, ValuePtr value) {
|
||||||
if (execution_mode_ == ExecutionMode::INTERPRETER) {
|
|
||||||
interpreter_->global_environment->define(name, value);
|
|
||||||
} else {
|
|
||||||
vm_->set_global(name, value);
|
vm_->set_global(name, value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void State::push_number(double value) {
|
void State::push_number(double value) {
|
||||||
stack_.push_back(std::make_shared<NumberValue>(value));
|
stack_.push_back(std::make_shared<NumberValue>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void State::push_string(const std::string& value) {
|
void State::push_string(const std::string &value) {
|
||||||
stack_.push_back(std::make_shared<StringValue>(value));
|
stack_.push_back(std::make_shared<StringValue>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,13 +63,9 @@ void State::push_bool(bool value) {
|
|||||||
stack_.push_back(std::make_shared<BoolValue>(value));
|
stack_.push_back(std::make_shared<BoolValue>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void State::push_nil() {
|
void State::push_nil() { stack_.push_back(std::make_shared<NilValue>()); }
|
||||||
stack_.push_back(std::make_shared<NilValue>());
|
|
||||||
}
|
|
||||||
|
|
||||||
void State::push_value(ValuePtr value) {
|
void State::push_value(ValuePtr value) { stack_.push_back(std::move(value)); }
|
||||||
stack_.push_back(std::move(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
double State::to_number(int index) {
|
double State::to_number(int index) {
|
||||||
ValuePtr val = get_stack_value(index);
|
ValuePtr val = get_stack_value(index);
|
||||||
@@ -108,9 +88,7 @@ bool State::to_bool(int index) {
|
|||||||
return is_truthy(val);
|
return is_truthy(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
ValuePtr State::to_value(int index) {
|
ValuePtr State::to_value(int index) { return get_stack_value(index); }
|
||||||
return get_stack_value(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
void State::set_top(int index) {
|
void State::set_top(int index) {
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
@@ -150,12 +128,7 @@ ValuePtr State::get_stack_value(int index) {
|
|||||||
return stack_[index];
|
return stack_[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool State::execute_program(const Program& program) {
|
bool State::execute_program(const Program &program) {
|
||||||
if (execution_mode_ == ExecutionMode::INTERPRETER) {
|
|
||||||
// Use tree-walking interpreter
|
|
||||||
interpreter_->execute(program);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
// Use VM
|
// Use VM
|
||||||
auto chunk = compiler_->compile(program);
|
auto chunk = compiler_->compile(program);
|
||||||
if (!chunk) {
|
if (!chunk) {
|
||||||
@@ -168,7 +141,6 @@ bool State::execute_program(const Program& program) {
|
|||||||
last_error_ = vm_->get_error();
|
last_error_ = vm_->get_error();
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace camellya
|
} // namespace camellya
|
||||||
|
|||||||
37
src/state.h
37
src/state.h
@@ -1,51 +1,40 @@
|
|||||||
#ifndef CAMELLYA_STATE_H
|
#ifndef CAMELLYA_STATE_H
|
||||||
#define CAMELLYA_STATE_H
|
#define CAMELLYA_STATE_H
|
||||||
|
|
||||||
|
#include "compiler.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "interpreter.h"
|
|
||||||
#include "vm.h"
|
|
||||||
#include "compiler.h"
|
|
||||||
#include "value.h"
|
#include "value.h"
|
||||||
#include <string>
|
#include "vm.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace camellya {
|
namespace camellya {
|
||||||
|
|
||||||
// Execution mode
|
|
||||||
enum class ExecutionMode {
|
|
||||||
INTERPRETER, // Tree-walking interpreter
|
|
||||||
VM // Bytecode VM
|
|
||||||
};
|
|
||||||
|
|
||||||
// Main state class - similar to lua_State
|
// Main state class - similar to lua_State
|
||||||
class State {
|
class State {
|
||||||
public:
|
public:
|
||||||
State(ExecutionMode mode = ExecutionMode::VM);
|
State();
|
||||||
~State() = default;
|
~State() = default;
|
||||||
|
|
||||||
// Set execution mode
|
|
||||||
void set_execution_mode(ExecutionMode mode) { execution_mode_ = mode; }
|
|
||||||
ExecutionMode get_execution_mode() const { return execution_mode_; }
|
|
||||||
|
|
||||||
// Execute script from string
|
// Execute script from string
|
||||||
bool do_string(const std::string& script);
|
bool do_string(const std::string &script);
|
||||||
|
|
||||||
// Execute script from file
|
// Execute script from file
|
||||||
bool do_file(const std::string& filename);
|
bool do_file(const std::string &filename);
|
||||||
|
|
||||||
// Register native function
|
// Register native function
|
||||||
void register_function(const std::string& name, NativeFunction func);
|
void register_function(const std::string &name, NativeFunction func);
|
||||||
|
|
||||||
// Get global variable
|
// Get global variable
|
||||||
ValuePtr get_global(const std::string& name);
|
ValuePtr get_global(const std::string &name);
|
||||||
|
|
||||||
// Set global variable
|
// Set global variable
|
||||||
void set_global(const std::string& name, ValuePtr value);
|
void set_global(const std::string &name, ValuePtr value);
|
||||||
|
|
||||||
// Stack operations (Lua-like API)
|
// Stack operations (Lua-like API)
|
||||||
void push_number(double value);
|
void push_number(double value);
|
||||||
void push_string(const std::string& value);
|
void push_string(const std::string &value);
|
||||||
void push_bool(bool value);
|
void push_bool(bool value);
|
||||||
void push_nil();
|
void push_nil();
|
||||||
void push_value(ValuePtr value);
|
void push_value(ValuePtr value);
|
||||||
@@ -60,11 +49,9 @@ public:
|
|||||||
void pop(int n = 1);
|
void pop(int n = 1);
|
||||||
|
|
||||||
// Error handling
|
// Error handling
|
||||||
const std::string& get_error() const { return last_error_; }
|
const std::string &get_error() const { return last_error_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ExecutionMode execution_mode_;
|
|
||||||
std::unique_ptr<Interpreter> interpreter_;
|
|
||||||
std::unique_ptr<VM> vm_;
|
std::unique_ptr<VM> vm_;
|
||||||
std::unique_ptr<Compiler> compiler_;
|
std::unique_ptr<Compiler> compiler_;
|
||||||
std::vector<ValuePtr> stack_;
|
std::vector<ValuePtr> stack_;
|
||||||
@@ -73,7 +60,7 @@ private:
|
|||||||
ValuePtr get_stack_value(int index);
|
ValuePtr get_stack_value(int index);
|
||||||
|
|
||||||
// Helper for execution
|
// Helper for execution
|
||||||
bool execute_program(const Program& program);
|
bool execute_program(const Program &program);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace camellya
|
} // namespace camellya
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <catch2/catch_test_macros.hpp>
|
|
||||||
#include <catch2/benchmark/catch_benchmark.hpp>
|
|
||||||
#include "camellya.h"
|
#include "camellya.h"
|
||||||
|
#include <catch2/benchmark/catch_benchmark.hpp>
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ using namespace camellya;
|
|||||||
|
|
||||||
TEST_CASE("basic arithmetic", "[script]") {
|
TEST_CASE("basic arithmetic", "[script]") {
|
||||||
State state;
|
State state;
|
||||||
const char* script = R"(
|
const char *script = R"(
|
||||||
var x = 10;
|
var x = 10;
|
||||||
var y = 20;
|
var y = 20;
|
||||||
var z = x + y;
|
var z = x + y;
|
||||||
@@ -27,7 +27,7 @@ TEST_CASE("basic arithmetic", "[script]") {
|
|||||||
|
|
||||||
TEST_CASE("basic function", "[script][func]") {
|
TEST_CASE("basic function", "[script][func]") {
|
||||||
State state;
|
State state;
|
||||||
const char* script = R"(
|
const char *script = R"(
|
||||||
func add(number x, number y) -> number {
|
func add(number x, number y) -> number {
|
||||||
return x + y;
|
return x + y;
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ TEST_CASE("basic function", "[script][func]") {
|
|||||||
|
|
||||||
TEST_CASE("list indexing is 0-based", "[list]") {
|
TEST_CASE("list indexing is 0-based", "[list]") {
|
||||||
State state;
|
State state;
|
||||||
const char* script = R"(
|
const char *script = R"(
|
||||||
var numbers = [10, 20, 30];
|
var numbers = [10, 20, 30];
|
||||||
)";
|
)";
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ TEST_CASE("list indexing is 0-based", "[list]") {
|
|||||||
|
|
||||||
TEST_CASE("class init is called on declaration", "[class][init]") {
|
TEST_CASE("class init is called on declaration", "[class][init]") {
|
||||||
State state;
|
State state;
|
||||||
const char* script = R"(
|
const char *script = R"(
|
||||||
class Person {
|
class Person {
|
||||||
var age : number;
|
var age : number;
|
||||||
var name : string;
|
var name : string;
|
||||||
@@ -125,8 +125,7 @@ TEST_CASE("class init is called on declaration", "[class][init]") {
|
|||||||
|
|
||||||
TEST_CASE("interpreter performance: simple loop", "[perf][script]") {
|
TEST_CASE("interpreter performance: simple loop", "[perf][script]") {
|
||||||
State state;
|
State state;
|
||||||
State state_vm(ExecutionMode::VM);
|
const char *script = R"(
|
||||||
const char* script = R"(
|
|
||||||
func sum_to(number n) -> number {
|
func sum_to(number n) -> number {
|
||||||
var s = 0;
|
var s = 0;
|
||||||
for (var i = 0; i < n; i = i + 1) {
|
for (var i = 0; i < n; i = i + 1) {
|
||||||
@@ -149,23 +148,11 @@ TEST_CASE("interpreter performance: simple loop", "[perf][script]") {
|
|||||||
auto r_num = std::dynamic_pointer_cast<NumberValue>(r_val);
|
auto r_num = std::dynamic_pointer_cast<NumberValue>(r_val);
|
||||||
REQUIRE(r_num->value == 499500.0);
|
REQUIRE(r_num->value == 499500.0);
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("loop break", "[script][loop]") {
|
TEST_CASE("loop break", "[script][loop]") {
|
||||||
State state;
|
State state;
|
||||||
const char* script = R"(
|
const char *script = R"(
|
||||||
var sum = 0;
|
var sum = 0;
|
||||||
for (var i = 0; i < 10; i = i + 1) {
|
for (var i = 0; i < 10; i = i + 1) {
|
||||||
if (i == 5) {
|
if (i == 5) {
|
||||||
@@ -184,7 +171,7 @@ TEST_CASE("loop break", "[script][loop]") {
|
|||||||
|
|
||||||
TEST_CASE("loop continue", "[script][loop]") {
|
TEST_CASE("loop continue", "[script][loop]") {
|
||||||
State state;
|
State state;
|
||||||
const char* script = R"(
|
const char *script = R"(
|
||||||
var sum = 0;
|
var sum = 0;
|
||||||
for (var i = 0; i < 5; i = i + 1) {
|
for (var i = 0; i < 5; i = i + 1) {
|
||||||
if (i == 2) {
|
if (i == 2) {
|
||||||
@@ -203,7 +190,7 @@ TEST_CASE("loop continue", "[script][loop]") {
|
|||||||
|
|
||||||
TEST_CASE("while break and continue", "[script][loop]") {
|
TEST_CASE("while break and continue", "[script][loop]") {
|
||||||
State state;
|
State state;
|
||||||
const char* script = R"(
|
const char *script = R"(
|
||||||
var i = 0;
|
var i = 0;
|
||||||
var sum = 0;
|
var sum = 0;
|
||||||
while (i < 10) {
|
while (i < 10) {
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#include <catch2/catch_test_macros.hpp>
|
|
||||||
#include "src/camellya.h"
|
#include "src/camellya.h"
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
using namespace camellya;
|
using namespace camellya;
|
||||||
|
|
||||||
TEST_CASE("VM - Basic arithmetic", "[vm]") {
|
TEST_CASE("VM - Basic arithmetic", "[vm]") {
|
||||||
State state(ExecutionMode::VM);
|
State state;
|
||||||
|
|
||||||
SECTION("Addition") {
|
SECTION("Addition") {
|
||||||
REQUIRE(state.do_string("var x = 10 + 20;"));
|
REQUIRE(state.do_string("var x = 10 + 20;"));
|
||||||
@@ -37,7 +37,7 @@ TEST_CASE("VM - Basic arithmetic", "[vm]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("VM - Variables and assignment", "[vm]") {
|
TEST_CASE("VM - Variables and assignment", "[vm]") {
|
||||||
State state(ExecutionMode::VM);
|
State state;
|
||||||
|
|
||||||
SECTION("Variable declaration and initialization") {
|
SECTION("Variable declaration and initialization") {
|
||||||
REQUIRE(state.do_string("var a = 42;"));
|
REQUIRE(state.do_string("var a = 42;"));
|
||||||
@@ -57,19 +57,20 @@ TEST_CASE("VM - Variables and assignment", "[vm]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("VM - String operations", "[vm]") {
|
TEST_CASE("VM - String operations", "[vm]") {
|
||||||
State state(ExecutionMode::VM);
|
State state;
|
||||||
|
|
||||||
SECTION("String concatenation") {
|
SECTION("String concatenation") {
|
||||||
REQUIRE(state.do_string(R"(var greeting = "Hello" + " " + "World";)"));
|
REQUIRE(state.do_string(R"(var greeting = "Hello" + " " + "World";)"));
|
||||||
auto greeting = state.get_global("greeting");
|
auto greeting = state.get_global("greeting");
|
||||||
REQUIRE(greeting != nullptr);
|
REQUIRE(greeting != nullptr);
|
||||||
REQUIRE(greeting->type() == Type::STRING);
|
REQUIRE(greeting->type() == Type::STRING);
|
||||||
REQUIRE(std::dynamic_pointer_cast<StringValue>(greeting)->value == "Hello World");
|
REQUIRE(std::dynamic_pointer_cast<StringValue>(greeting)->value ==
|
||||||
|
"Hello World");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("VM - Comparison operators", "[vm]") {
|
TEST_CASE("VM - Comparison operators", "[vm]") {
|
||||||
State state(ExecutionMode::VM);
|
State state;
|
||||||
|
|
||||||
SECTION("Equality") {
|
SECTION("Equality") {
|
||||||
REQUIRE(state.do_string("var eq = 10 == 10;"));
|
REQUIRE(state.do_string("var eq = 10 == 10;"));
|
||||||
@@ -91,7 +92,7 @@ TEST_CASE("VM - Comparison operators", "[vm]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("VM - Lists", "[vm]") {
|
TEST_CASE("VM - Lists", "[vm]") {
|
||||||
State state(ExecutionMode::VM);
|
State state;
|
||||||
|
|
||||||
SECTION("Create list") {
|
SECTION("Create list") {
|
||||||
REQUIRE(state.do_string("var numbers = [1, 2, 3, 4, 5];"));
|
REQUIRE(state.do_string("var numbers = [1, 2, 3, 4, 5];"));
|
||||||
@@ -113,7 +114,7 @@ TEST_CASE("VM - Lists", "[vm]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("VM - Maps", "[vm]") {
|
TEST_CASE("VM - Maps", "[vm]") {
|
||||||
State state(ExecutionMode::VM);
|
State state;
|
||||||
|
|
||||||
SECTION("Create map") {
|
SECTION("Create map") {
|
||||||
REQUIRE(state.do_string(R"(var person = {"name": "Alice", "age": "30"};)"));
|
REQUIRE(state.do_string(R"(var person = {"name": "Alice", "age": "30"};)"));
|
||||||
@@ -134,7 +135,7 @@ TEST_CASE("VM - Maps", "[vm]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("VM - If statements", "[vm]") {
|
TEST_CASE("VM - If statements", "[vm]") {
|
||||||
State state(ExecutionMode::VM);
|
State state;
|
||||||
|
|
||||||
SECTION("If branch taken") {
|
SECTION("If branch taken") {
|
||||||
REQUIRE(state.do_string(R"(
|
REQUIRE(state.do_string(R"(
|
||||||
@@ -162,7 +163,7 @@ TEST_CASE("VM - If statements", "[vm]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("VM - While loops", "[vm]") {
|
TEST_CASE("VM - While loops", "[vm]") {
|
||||||
State state(ExecutionMode::VM);
|
State state;
|
||||||
|
|
||||||
SECTION("While loop") {
|
SECTION("While loop") {
|
||||||
REQUIRE(state.do_string(R"(
|
REQUIRE(state.do_string(R"(
|
||||||
@@ -179,7 +180,7 @@ TEST_CASE("VM - While loops", "[vm]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("VM - Native functions", "[vm]") {
|
TEST_CASE("VM - Native functions", "[vm]") {
|
||||||
State state(ExecutionMode::VM);
|
State state;
|
||||||
|
|
||||||
SECTION("len function") {
|
SECTION("len function") {
|
||||||
REQUIRE(state.do_string(R"(
|
REQUIRE(state.do_string(R"(
|
||||||
@@ -190,80 +191,3 @@ TEST_CASE("VM - Native functions", "[vm]") {
|
|||||||
REQUIRE(std::dynamic_pointer_cast<NumberValue>(size)->value == 4.0);
|
REQUIRE(std::dynamic_pointer_cast<NumberValue>(size)->value == 4.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("VM vs Interpreter - Same results", "[vm][interpreter]") {
|
|
||||||
const char* script = R"(
|
|
||||||
var x = 10;
|
|
||||||
var y = 20;
|
|
||||||
var sum = x + y;
|
|
||||||
var product = x * y;
|
|
||||||
)";
|
|
||||||
|
|
||||||
State vm_state(ExecutionMode::VM);
|
|
||||||
State interp_state(ExecutionMode::INTERPRETER);
|
|
||||||
|
|
||||||
REQUIRE(vm_state.do_string(script));
|
|
||||||
REQUIRE(interp_state.do_string(script));
|
|
||||||
|
|
||||||
auto vm_sum = vm_state.get_global("sum");
|
|
||||||
auto interp_sum = interp_state.get_global("sum");
|
|
||||||
REQUIRE(std::dynamic_pointer_cast<NumberValue>(vm_sum)->value ==
|
|
||||||
std::dynamic_pointer_cast<NumberValue>(interp_sum)->value);
|
|
||||||
|
|
||||||
auto vm_product = vm_state.get_global("product");
|
|
||||||
auto interp_product = interp_state.get_global("product");
|
|
||||||
REQUIRE(std::dynamic_pointer_cast<NumberValue>(vm_product)->value ==
|
|
||||||
std::dynamic_pointer_cast<NumberValue>(interp_product)->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("class init is called on declaration", "[vm][class][init]") {
|
|
||||||
State state(ExecutionMode::VM);
|
|
||||||
const char* script = R"(
|
|
||||||
class Person {
|
|
||||||
var age : number;
|
|
||||||
var name : string;
|
|
||||||
|
|
||||||
func init() -> nil {
|
|
||||||
age = 18;
|
|
||||||
name = "Default";
|
|
||||||
}
|
|
||||||
|
|
||||||
func getAge() -> number {
|
|
||||||
return this.age;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var p : Person;
|
|
||||||
var a = p.getAge();
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto ret = state.do_string(script);
|
|
||||||
if(!ret) {
|
|
||||||
REQUIRE(state.get_error() == "");
|
|
||||||
}
|
|
||||||
|
|
||||||
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 age_val = instance->get("age");
|
|
||||||
auto name_val = instance->get("name");
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user