Skip to content

Commit

Permalink
delete variable's inplace name
Browse files Browse the repository at this point in the history
  • Loading branch information
chloro-pn committed Jul 28, 2024
1 parent 2e1379a commit 971a354
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 153 deletions.
8 changes: 4 additions & 4 deletions example/interpreter_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main() {
wamon::Interpreter ip(pu);
// 利用VariableFactory
// API构造变量对象,需要指定类型、值型别、名字[为空则代表匿名]和packageunit,packageunit是当指定类型中包含结构体类型时从该包中查找对应的类型定义。
auto v = wamon::VariableFactory(wamon::TypeFactory<int>::Get(), wamon::Variable::ValueCategory::RValue, "", ip);
auto v = wamon::VariableFactory(wamon::TypeFactory<int>::Get(), wamon::Variable::ValueCategory::RValue, ip);
// 利用AsXXXVariable系列API将变量转化为对应类型并进行赋值。
wamon::AsIntVariable(v)->SetValue(10);
// 填充函数参数,这里的设计还不优雅,需要进行类型转换std::unique_ptr -> std::shared_ptr
Expand All @@ -73,11 +73,11 @@ int main() {
// 利用TypeFactory API构造结构体对应的类型并指定其所属的Package
auto struct_type = wamon::TypeFactory<WAMON_STRUCT_TYPE("main$test2")>::Get();
// 构造该类型的变量
v = wamon::VariableFactory(struct_type, wamon::Variable::ValueCategory::RValue, "", ip);
v = wamon::VariableFactory(struct_type, wamon::Variable::ValueCategory::RValue, ip);
auto v2 = std::shared_ptr<wamon::Variable>(std::move(v));
wamon::AsStructVariable(v2)->DefaultConstruct();
auto string_v =
wamon::VariableFactory(wamon::TypeFactory<std::string>::Get(), wamon::Variable::ValueCategory::RValue, "", ip);
wamon::VariableFactory(wamon::TypeFactory<std::string>::Get(), wamon::Variable::ValueCategory::RValue, ip);
wamon::AsStringVariable(string_v)->SetValue("world");
// 结构体成员变量赋值
wamon::AsStructVariable(v2)->UpdateDataMemberByName("b", std::move(string_v));
Expand All @@ -87,7 +87,7 @@ int main() {
// 利用FindVariableById API获取全局变量,注意,不要忘记包前缀修饰
v2 = ip.FindVariableById("main$v2");

v = wamon::VariableFactory(wamon::TypeFactory<int>::Get(), wamon::Variable::ValueCategory::RValue, "", ip);
v = wamon::VariableFactory(wamon::TypeFactory<int>::Get(), wamon::Variable::ValueCategory::RValue, ip);
wamon::AsIntVariable(v)->SetValue(10);
// 利用CallCallable API调用Callable对象
v2 = ip.CallCallable(v2, {std::move(v)});
Expand Down
4 changes: 2 additions & 2 deletions example/lambda.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ int main() {
}
Interpreter ip(pu);
std::vector<std::shared_ptr<Variable>> params;
params.push_back(VariableFactory(TypeFactory<int>::Get(), Variable::ValueCategory::RValue, "", ip));
params.push_back(VariableFactory(TypeFactory<int>::Get(), Variable::ValueCategory::RValue, "", ip));
params.push_back(VariableFactory(TypeFactory<int>::Get(), Variable::ValueCategory::RValue, ip));
params.push_back(VariableFactory(TypeFactory<int>::Get(), Variable::ValueCategory::RValue, ip));
AsIntVariable(params[0])->SetValue(2);
AsIntVariable(params[1])->SetValue(3);
auto ret = ip.CallCallable(ip.FindVariableById("main$v"), std::move(params));
Expand Down
6 changes: 3 additions & 3 deletions example/register_cpp_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ auto my_cpp_func_check(const wamon::PackageUnit&, const std::vector<std::unique_
auto my_cpp_func(wamon::Interpreter&, std::vector<std::shared_ptr<wamon::Variable>>&& params)
-> std::shared_ptr<wamon::Variable> {
auto len = wamon::AsStringVariable(params[0])->GetValue().size();
return std::make_shared<wamon::IntVariable>(static_cast<int>(len), wamon::Variable::ValueCategory::RValue, "");
return std::make_shared<wamon::IntVariable>(static_cast<int>(len), wamon::Variable::ValueCategory::RValue);
}

auto my_type_cpp_func(wamon::Interpreter&, std::vector<std::shared_ptr<wamon::Variable>>&& params)
-> std::shared_ptr<wamon::Variable> {
return std::make_shared<wamon::IntVariable>(12138, wamon::Variable::ValueCategory::RValue, "");
return std::make_shared<wamon::IntVariable>(12138, wamon::Variable::ValueCategory::RValue);
}

int main() {
Expand Down Expand Up @@ -58,7 +58,7 @@ int main() {

wamon::Interpreter ip(package_unit);
auto string_v =
wamon::VariableFactory(wamon::TypeFactory<std::string>::Get(), wamon::Variable::ValueCategory::RValue, "", ip);
wamon::VariableFactory(wamon::TypeFactory<std::string>::Get(), wamon::Variable::ValueCategory::RValue, ip);
wamon::AsStringVariable(string_v)->SetValue("hello");

auto ret = ip.CallFunctionByName("main$call_cpp_function", {std::move(string_v)});
Expand Down
10 changes: 5 additions & 5 deletions include/wamon/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class StringIteralExpr : public Expression {
void SetStringIter(const std::string& str) { str_ = str; }

std::shared_ptr<Variable> Calculate(Interpreter& interpreter) override {
return std::make_shared<StringVariable>(str_, Variable::ValueCategory::RValue, "");
return std::make_shared<StringVariable>(str_, Variable::ValueCategory::RValue);
}

private:
Expand All @@ -210,7 +210,7 @@ class IntIteralExpr : public Expression {
void SetIntIter(const int64_t& n) { num_ = n; }

std::shared_ptr<Variable> Calculate(Interpreter& interpreter) override {
return std::make_shared<IntVariable>(static_cast<int>(num_), Variable::ValueCategory::RValue, "");
return std::make_shared<IntVariable>(static_cast<int>(num_), Variable::ValueCategory::RValue);
}

private:
Expand All @@ -223,7 +223,7 @@ class DoubleIteralExpr : public Expression {

std::shared_ptr<Variable> Calculate(Interpreter& interpreter) override {
// not implemented now
return std::make_shared<DoubleVariable>(d_, Variable::ValueCategory::RValue, "");
return std::make_shared<DoubleVariable>(d_, Variable::ValueCategory::RValue);
}

private:
Expand All @@ -235,7 +235,7 @@ class BoolIteralExpr : public Expression {
void SetBoolIter(bool b) { b_ = b; }

std::shared_ptr<Variable> Calculate(Interpreter& interpreter) override {
return std::make_shared<BoolVariable>(b_, Variable::ValueCategory::RValue, "");
return std::make_shared<BoolVariable>(b_, Variable::ValueCategory::RValue);
}

private:
Expand All @@ -248,7 +248,7 @@ class ByteIteralExpr : public Expression {

std::shared_ptr<Variable> Calculate(Interpreter& interpreter) override {
// not implemented now
return std::make_shared<ByteVariable>(byte_, Variable::ValueCategory::RValue, "");
return std::make_shared<ByteVariable>(byte_, Variable::ValueCategory::RValue);
}

private:
Expand Down
6 changes: 0 additions & 6 deletions include/wamon/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ struct RuntimeContext {
symbol_table_.insert({tmp_name, variable});
}

template <bool check_value_category = true>
void RegisterVariable(const std::shared_ptr<Variable>& variable) {
std::string name = variable->GetName();
RegisterVariable<check_value_category>(variable, name);
}

std::shared_ptr<Variable> FindVariable(const std::string& id_name) {
auto it = symbol_table_.find(id_name);
if (it == symbol_table_.end()) {
Expand Down
12 changes: 6 additions & 6 deletions include/wamon/to_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace wamon {
* API : ToVar
*
* ********************************************************************/
#define WAMON_TO_VAR(basic_type, transform_type) \
if constexpr (std::is_same_v<type, basic_type>) { \
auto ret = VariableFactory(TypeFactory<basic_type>::Get(), Variable::ValueCategory::LValue, ""); \
As##transform_type##Variable(ret)->SetValue(std::forward<T>(v)); \
return ret; \
#define WAMON_TO_VAR(basic_type, transform_type) \
if constexpr (std::is_same_v<type, basic_type>) { \
auto ret = VariableFactory(TypeFactory<basic_type>::Get(), Variable::ValueCategory::LValue); \
As##transform_type##Variable(ret)->SetValue(std::forward<T>(v)); \
return ret; \
}

inline std::shared_ptr<Variable> ToVar(std::string_view strv) {
auto ret = VariableFactory(TypeFactory<std::string>::Get(), Variable::ValueCategory::LValue, "");
auto ret = VariableFactory(TypeFactory<std::string>::Get(), Variable::ValueCategory::LValue);
AsStringVariable(ret)->SetValue(strv);
return ret;
}
Expand Down
31 changes: 11 additions & 20 deletions include/wamon/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class EnumDef;

/*
* Variable为运行时变量类型的基类,每个Variable一定包含一个type成员标识其类型
* 以及一个变量名字,当其为空的时候标识匿名变量
* Variable本身不包含名字,其名字需要被使用者持有
* 每个Variable都具有值型别(like c++),左值或者右值
*/
class Variable {
Expand All @@ -24,8 +24,7 @@ class Variable {
RValue,
};

explicit Variable(std::unique_ptr<Type>&& t, ValueCategory vc, const std::string& name = "")
: type_(std::move(t)), name_(name), vc_(vc) {}
explicit Variable(std::unique_ptr<Type>&& t, ValueCategory vc) : type_(std::move(t)), vc_(vc) {}

virtual void ConstructByFields(const std::vector<std::shared_ptr<Variable>>& fields) = 0;

Expand All @@ -46,10 +45,6 @@ class Variable {

virtual std::unique_ptr<Type> GetType() const { return type_->Clone(); }

void SetName(const std::string& name) { name_ = name; }

const std::string& GetName() const { return name_; }

ValueCategory GetValueCategory() const { return vc_; }

virtual void ChangeTo(ValueCategory vc) { vc_ = vc; }
Expand All @@ -66,7 +61,6 @@ class Variable {

private:
std::unique_ptr<Type> type_;
std::string name_;

protected:
ValueCategory vc_;
Expand All @@ -79,16 +73,16 @@ std::shared_ptr<Variable> VariableMoveOrCopy(const std::shared_ptr<Variable>& v)
class PackageUnit;
class Interpreter;
std::shared_ptr<Variable> VariableFactory(const std::unique_ptr<Type>& type, Variable::ValueCategory vc,
const std::string& name, Interpreter* ip = nullptr);
Interpreter* ip = nullptr);

std::shared_ptr<Variable> VariableFactory(const std::unique_ptr<Type>& type, Variable::ValueCategory vc,
const std::string& name, Interpreter& ip);
Interpreter& ip);

class Interpreter;

class StructVariable : public Variable {
public:
StructVariable(const StructDef* sd, ValueCategory vc, Interpreter& i, const std::string& name);
StructVariable(const StructDef* sd, ValueCategory vc, Interpreter& i);

std::string GetTypeInfo() const override;

Expand Down Expand Up @@ -145,7 +139,7 @@ inline StructVariable* AsStructVariable(const std::shared_ptr<Variable>& v) {

class EnumVariable : public Variable {
public:
EnumVariable(const EnumDef* def, ValueCategory vc, const std::string& name);
EnumVariable(const EnumDef* def, ValueCategory vc);

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

Expand Down Expand Up @@ -173,14 +167,13 @@ inline EnumVariable* AsEnumVariable(const std::shared_ptr<Variable>& v) { return

class CompoundVariable : public Variable {
public:
CompoundVariable(std::unique_ptr<Type>&& t, ValueCategory vc, const std::string& name)
: Variable(std::move(t), vc, name) {}
CompoundVariable(std::unique_ptr<Type>&& t, ValueCategory vc) : Variable(std::move(t), vc) {}
};

class PointerVariable : public CompoundVariable {
public:
PointerVariable(std::unique_ptr<Type>&& hold_type, ValueCategory vc, const std::string& name)
: CompoundVariable(std::make_unique<PointerType>(std::move(hold_type)), vc, name) {}
PointerVariable(std::unique_ptr<Type>&& hold_type, ValueCategory vc)
: CompoundVariable(std::make_unique<PointerType>(std::move(hold_type)), vc) {}

std::shared_ptr<Variable> GetHoldVariable() { return obj_.lock(); }

Expand Down Expand Up @@ -230,9 +223,8 @@ class FunctionVariable : public CompoundVariable {
std::shared_ptr<Variable> v;
};

FunctionVariable(std::vector<std::unique_ptr<Type>>&& param, std::unique_ptr<Type>&& ret, ValueCategory vc,
const std::string& name)
: CompoundVariable(std::make_unique<FuncType>(std::move(param), std::move(ret)), vc, name) {}
FunctionVariable(std::vector<std::unique_ptr<Type>>&& param, std::unique_ptr<Type>&& ret, ValueCategory vc)
: CompoundVariable(std::make_unique<FuncType>(std::move(param), std::move(ret)), vc) {}

void SetFuncName(const std::string& func_name) { func_name_ = func_name; }

Expand Down Expand Up @@ -277,7 +269,6 @@ class FunctionVariable : public CompoundVariable {
capture_variables_.push_back(each);
} else {
auto tmp = each.v->Clone();
tmp->SetName(each.v->GetName());
tmp->ChangeTo(vc_);
capture_variables_.push_back({each.is_ref, tmp});
}
Expand Down
5 changes: 2 additions & 3 deletions include/wamon/variable_bool.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace wamon {

class BoolVariable : public Variable {
public:
BoolVariable(bool v, ValueCategory vc, const std::string& name)
: Variable(TypeFactory<bool>::Get(), vc, name), value_(v) {}
BoolVariable(bool v, ValueCategory vc) : Variable(TypeFactory<bool>::Get(), vc), value_(v) {}

bool GetValue() const { return value_; }

Expand All @@ -28,7 +27,7 @@ class BoolVariable : public Variable {
void DefaultConstruct() override { value_ = true; }

std::shared_ptr<Variable> Clone() override {
return std::make_shared<BoolVariable>(GetValue(), ValueCategory::RValue, "");
return std::make_shared<BoolVariable>(GetValue(), ValueCategory::RValue);
}

bool Compare(const std::shared_ptr<Variable>& other) override {
Expand Down
5 changes: 2 additions & 3 deletions include/wamon/variable_byte.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ inline void byte_to_string(unsigned char c, char (&buf)[4]) {

class ByteVariable : public Variable {
public:
ByteVariable(unsigned char v, ValueCategory vc, const std::string& name)
: Variable(TypeFactory<unsigned char>::Get(), vc, name), value_(v) {}
ByteVariable(unsigned char v, ValueCategory vc) : Variable(TypeFactory<unsigned char>::Get(), vc), value_(v) {}

unsigned char GetValue() const { return value_; }

Expand All @@ -45,7 +44,7 @@ class ByteVariable : public Variable {
void DefaultConstruct() override { value_ = 0; }

std::shared_ptr<Variable> Clone() override {
return std::make_shared<ByteVariable>(GetValue(), ValueCategory::RValue, "");
return std::make_shared<ByteVariable>(GetValue(), ValueCategory::RValue);
}

bool Compare(const std::shared_ptr<Variable>& other) override {
Expand Down
5 changes: 2 additions & 3 deletions include/wamon/variable_double.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace wamon {

class DoubleVariable : public Variable {
public:
DoubleVariable(double v, ValueCategory vc, const std::string& name)
: Variable(TypeFactory<double>::Get(), vc, name), value_(v) {}
DoubleVariable(double v, ValueCategory vc) : Variable(TypeFactory<double>::Get(), vc), value_(v) {}

double GetValue() const { return value_; }

Expand All @@ -28,7 +27,7 @@ class DoubleVariable : public Variable {
void DefaultConstruct() override { value_ = 0.0; }

std::shared_ptr<Variable> Clone() override {
return std::make_shared<DoubleVariable>(GetValue(), ValueCategory::RValue, "");
return std::make_shared<DoubleVariable>(GetValue(), ValueCategory::RValue);
}

bool Compare(const std::shared_ptr<Variable>& other) override {
Expand Down
5 changes: 2 additions & 3 deletions include/wamon/variable_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace wamon {

class IntVariable : public Variable {
public:
IntVariable(int v, ValueCategory vc, const std::string& name)
: Variable(TypeFactory<int>::Get(), vc, name), value_(v) {}
IntVariable(int v, ValueCategory vc) : Variable(TypeFactory<int>::Get(), vc), value_(v) {}

int GetValue() const { return value_; }

Expand All @@ -28,7 +27,7 @@ class IntVariable : public Variable {
void DefaultConstruct() override { value_ = 0; }

std::shared_ptr<Variable> Clone() override {
return std::make_shared<IntVariable>(GetValue(), ValueCategory::RValue, "");
return std::make_shared<IntVariable>(GetValue(), ValueCategory::RValue);
}

bool Compare(const std::shared_ptr<Variable>& other) override {
Expand Down
6 changes: 3 additions & 3 deletions include/wamon/variable_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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),
ListVariable(std::unique_ptr<Type>&& element_type, ValueCategory vc)
: CompoundVariable(std::make_unique<ListType>(element_type->Clone()), vc),
element_type_(std::move(element_type)) {}

void PushBack(std::shared_ptr<Variable> element);
Expand All @@ -25,7 +25,7 @@ class ListVariable : public CompoundVariable {
elements_.resize(new_size);
} else {
for (size_t i = 0; i < (new_size - old_size); ++i) {
auto v = VariableFactory(element_type_, vc_, "", ip);
auto v = VariableFactory(element_type_, vc_, ip);
v->DefaultConstruct();
elements_.push_back(std::move(v));
}
Expand Down
7 changes: 3 additions & 4 deletions include/wamon/variable_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace wamon {

class StringVariable : public Variable {
public:
StringVariable(const std::string& v, ValueCategory vc, const std::string& name)
: Variable(TypeFactory<std::string>::Get(), vc, name), value_(v) {}
StringVariable(const std::string& v, ValueCategory vc) : Variable(TypeFactory<std::string>::Get(), vc), value_(v) {}

const std::string& GetValue() const { return value_; }

Expand Down Expand Up @@ -36,9 +35,9 @@ class StringVariable : public Variable {
std::shared_ptr<Variable> Clone() override {
std::shared_ptr<StringVariable> ret;
if (IsRValue()) {
ret = std::make_shared<StringVariable>(std::move(value_), ValueCategory::RValue, "");
ret = std::make_shared<StringVariable>(std::move(value_), ValueCategory::RValue);
} else {
ret = std::make_shared<StringVariable>(GetValue(), ValueCategory::RValue, "");
ret = std::make_shared<StringVariable>(GetValue(), ValueCategory::RValue);
}
return ret;
}
Expand Down
Loading

0 comments on commit 971a354

Please sign in to comment.