Skip to content

Commit

Permalink
refactor and split variable_list
Browse files Browse the repository at this point in the history
  • Loading branch information
chloro-pn committed Jul 8, 2024
1 parent f4ac97f commit 4936668
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 195 deletions.
1 change: 1 addition & 0 deletions example/merge_sort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "wamon/scanner.h"
#include "wamon/type_checker.h"
#include "wamon/variable.h"
#include "wamon/variable_list.h"

int main() {
std::string script = R"(
Expand Down
6 changes: 3 additions & 3 deletions include/wamon/inner_type_method.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace wamon {

class PackageUnit;
class Interpreter;

class InnerTypeMethod {
public:
Expand All @@ -21,8 +21,8 @@ class InnerTypeMethod {

using CheckType = std::function<std::unique_ptr<Type>(const std::unique_ptr<Type>& builtin_type,
const std::vector<std::unique_ptr<Type>>& params_type)>;
using HandleType = std::function<std::shared_ptr<Variable>(
std::shared_ptr<Variable>& obj, std::vector<std::shared_ptr<Variable>>&&, const PackageUnit&)>;
using HandleType = std::function<std::shared_ptr<Variable>(std::shared_ptr<Variable>& obj,
std::vector<std::shared_ptr<Variable>>&&, Interpreter&)>;

std::string GetHandleId(const std::unique_ptr<Type>& builtintype, const std::string& method_name) const {
std::string handle_id;
Expand Down
2 changes: 1 addition & 1 deletion include/wamon/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class Interpreter {
std::vector<std::shared_ptr<Variable>>&& params) {
auto method = InnerTypeMethod::Instance().Get(obj, method_name);
EnterContext<RuntimeContextType::Method>(obj->GetTypeInfo() + "::" + method_name);
auto ret = method(obj, std::move(params), pu_);
auto ret = method(obj, std::move(params), *this);
LeaveContext();
return ret->IsRValue() ? std::move(ret) : ret->Clone();
}
Expand Down
118 changes: 0 additions & 118 deletions include/wamon/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,124 +223,6 @@ inline PointerVariable* AsPointerVariable(const std::shared_ptr<Variable>& v) {
return static_cast<PointerVariable*>(v.get());
}

class ListVariable : public CompoundVariable {
public:
ListVariable(std::unique_ptr<Type>&& element_type, ValueCategory vc, Interpreter& ip, const std::string& name)
: CompoundVariable(std::make_unique<ListType>(element_type->Clone()), vc, name),
element_type_(std::move(element_type)),
ip_(ip) {}

void PushBack(std::shared_ptr<Variable> element);

void PopBack();

size_t Size() const { return elements_.size(); }

void Resize(size_t new_size) {
size_t old_size = Size();
if (new_size == old_size) {
return;
} else if (new_size < old_size) {
elements_.resize(new_size);
} else {
for (size_t i = 0; i < (new_size - old_size); ++i) {
auto v = VariableFactory(element_type_, vc_, "", ip_);
v->DefaultConstruct();
elements_.push_back(std::move(v));
}
}
}

void Clear() { elements_.clear(); }

void Erase(size_t index) {
if (index >= elements_.size()) {
throw WamonException("List.Erase error, index out of range : {} >= {}", index, elements_.size());
}
auto it = elements_.begin();
std::advance(it, index);
elements_.erase(it);
}

void Insert(size_t index, std::shared_ptr<Variable> v) {
if (index > Size()) {
throw WamonException("List.Insert error, index = {}, size = {}", index, Size());
}

std::shared_ptr<Variable> tmp;
if (v->IsRValue()) {
tmp = v;
} else {
tmp = v->Clone();
}
tmp->ChangeTo(vc_);
auto it = elements_.begin();
std::advance(it, index);
elements_.insert(it, std::move(tmp));
}

std::shared_ptr<Variable> at(size_t i) {
if (i >= Size()) {
throw WamonException("ListVariable.at error, index {} out of range", i);
}
return elements_[i];
}

std::string get_string_only_for_byte_list();

void ConstructByFields(const std::vector<std::shared_ptr<Variable>>& fields) override;

void DefaultConstruct() override;

std::shared_ptr<Variable> Clone() override;

bool Compare(const std::shared_ptr<Variable>& other) override {
check_compare_type_match(other);
ListVariable* list_v = static_cast<ListVariable*>(other.get());
if (elements_.size() != list_v->elements_.size()) {
return false;
}
for (size_t i = 0; i < elements_.size(); ++i) {
if (elements_[i]->Compare((list_v->elements_)[i]) == false) {
return false;
}
}
return true;
}

void Assign(const std::shared_ptr<Variable>& other) override {
check_compare_type_match(other);
ListVariable* list_v = static_cast<ListVariable*>(other.get());
elements_.clear();
for (size_t i = 0; i < list_v->elements_.size(); ++i) {
// PushBack函数会处理值型别
PushBack(list_v->elements_[i]);
}
}

void ChangeTo(ValueCategory vc) override {
vc_ = vc;
for (auto& each : elements_) {
each->ChangeTo(vc);
}
}

nlohmann::json Print() override {
nlohmann::json list;
for (size_t i = 0; i < elements_.size(); ++i) {
list.push_back(elements_[i]->Print());
}
return list;
}

private:
std::unique_ptr<Type> element_type_;
std::vector<std::shared_ptr<Variable>> elements_;
Interpreter& ip_;
};

inline ListVariable* AsListVariable(const std::shared_ptr<Variable>& v) { return static_cast<ListVariable*>(v.get()); }

class FunctionVariable : public CompoundVariable {
public:
struct capture_item {
Expand Down
124 changes: 124 additions & 0 deletions include/wamon/variable_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#pragma once

#include "wamon/interpreter.h"
#include "wamon/variable.h"

namespace wamon {

class ListVariable : public CompoundVariable {
public:
ListVariable(std::unique_ptr<Type>&& element_type, ValueCategory vc, const std::string& name)
: CompoundVariable(std::make_unique<ListType>(element_type->Clone()), vc, name),
element_type_(std::move(element_type)) {}

void PushBack(std::shared_ptr<Variable> element);

void PopBack();

size_t Size() const { return elements_.size(); }

void Resize(size_t new_size, Interpreter& ip) {
size_t old_size = Size();
if (new_size == old_size) {
return;
} else if (new_size < old_size) {
elements_.resize(new_size);
} else {
for (size_t i = 0; i < (new_size - old_size); ++i) {
auto v = VariableFactory(element_type_, vc_, "", ip);
v->DefaultConstruct();
elements_.push_back(std::move(v));
}
}
}

void Clear() { elements_.clear(); }

void Erase(size_t index) {
if (index >= elements_.size()) {
throw WamonException("List.Erase error, index out of range : {} >= {}", index, elements_.size());
}
auto it = elements_.begin();
std::advance(it, index);
elements_.erase(it);
}

void Insert(size_t index, std::shared_ptr<Variable> v) {
if (index > Size()) {
throw WamonException("List.Insert error, index = {}, size = {}", index, Size());
}

std::shared_ptr<Variable> tmp;
if (v->IsRValue()) {
tmp = v;
} else {
tmp = v->Clone();
}
tmp->ChangeTo(vc_);
auto it = elements_.begin();
std::advance(it, index);
elements_.insert(it, std::move(tmp));
}

std::shared_ptr<Variable> at(size_t i) {
if (i >= Size()) {
throw WamonException("ListVariable.at error, index {} out of range", i);
}
return elements_[i];
}

std::string get_string_only_for_byte_list();

void ConstructByFields(const std::vector<std::shared_ptr<Variable>>& fields) override;

void DefaultConstruct() override;

std::shared_ptr<Variable> Clone() override;

bool Compare(const std::shared_ptr<Variable>& other) override {
check_compare_type_match(other);
ListVariable* list_v = static_cast<ListVariable*>(other.get());
if (elements_.size() != list_v->elements_.size()) {
return false;
}
for (size_t i = 0; i < elements_.size(); ++i) {
if (elements_[i]->Compare((list_v->elements_)[i]) == false) {
return false;
}
}
return true;
}

void Assign(const std::shared_ptr<Variable>& other) override {
check_compare_type_match(other);
ListVariable* list_v = static_cast<ListVariable*>(other.get());
elements_.clear();
for (size_t i = 0; i < list_v->elements_.size(); ++i) {
// PushBack函数会处理值型别
PushBack(list_v->elements_[i]);
}
}

void ChangeTo(ValueCategory vc) override {
vc_ = vc;
for (auto& each : elements_) {
each->ChangeTo(vc);
}
}

nlohmann::json Print() override {
nlohmann::json list;
for (size_t i = 0; i < elements_.size(); ++i) {
list.push_back(elements_[i]->Print());
}
return list;
}

private:
std::unique_ptr<Type> element_type_;
std::vector<std::shared_ptr<Variable>> elements_;
};

inline ListVariable* AsListVariable(const std::shared_ptr<Variable>& v) { return static_cast<ListVariable*>(v.get()); }

} // namespace wamon
2 changes: 1 addition & 1 deletion src/builtin_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include <iostream>
#include <string>

#include "nlohmann/json.hpp"
#include "wamon/ast.h"
#include "wamon/exception.h"
#include "wamon/interpreter.h"
#include "wamon/package_unit.h"
#include "wamon/type_checker.h"
#include "wamon/variable.h"
#include "wamon/variable_list.h"

namespace wamon {

Expand Down
Loading

0 comments on commit 4936668

Please sign in to comment.