Skip to content

Commit

Permalink
support enum as string
Browse files Browse the repository at this point in the history
  • Loading branch information
chloro-pn committed Jun 15, 2024
1 parent 18e04d2 commit 597c502
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* [done] lambda捕获列表支持move
* [done] 内置/注册函数支持赋值给callable【目前不支持内置函数返回类型,因为有些内置函数是模板化的,其类型根据输入参数确定】
* [done] 支持enum
- enum的字面量格式应该是什么样的?
- enum的字面量格式如下:
- [done] enum type:enum_item
* [done] 优化内置方法的错误提示
* [done] 支持变量引用(不涉及类型系统)
Expand All @@ -31,7 +31,7 @@
* [done] 支持比较运算符 < , <=, >, >=
* 支持新的数据类型 Map
* 支持函数重载
* 支持注册函数的包管理
* [done] 支持注册函数的包管理
* [done] Output to json format
* for语句支持变量定义
* [done] 重构ValueCategory处理系统(bug: lambda捕获的ref变量不应该和callable保持一致的ValueCategory,它应该总是LValue)
Expand Down
3 changes: 3 additions & 0 deletions include/wamon/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ inline bool IsStructOrEnumType(const std::unique_ptr<Type>& type) {
return type->IsBasicType() && !IsBuiltInType(type);
}

class PackageUnit;
bool IsEnumType(const std::unique_ptr<Type>& type, const PackageUnit&);

namespace detail {

inline std::vector<std::string> GetBuiltInTypesWithoutVoid() {
Expand Down
8 changes: 8 additions & 0 deletions src/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ static void register_buildin_operator_handles(std::unordered_map<std::string, Op
tmp->SetValue(AsListVariable(v1)->get_string_only_for_byte_list());
return tmp;
}
// enum item to string
if (IsEnumType(from_type, interpreter.GetPackageUnit()) && IsStringType(to_type)) {
auto enum_def = interpreter.GetPackageUnit().FindEnum(from_type->GetTypeInfo());
assert(enum_def != nullptr);
const auto& enum_item = AsEnumVariable(v1)->GetEnumItem();
std::string enum_str = enum_def->GetEnumName() + ":" + enum_item;
return std::make_shared<StringVariable>(enum_str, Variable::ValueCategory::RValue, "");
}
// struct to struct trait
auto v = VariableFactory(to_type, Variable::ValueCategory::RValue, "", interpreter);
v->ConstructByFields({v1});
Expand Down
7 changes: 7 additions & 0 deletions src/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,11 @@ void CheckCanConstructBy(const PackageUnit& pu, const std::unique_ptr<Type>& var

} // namespace detail

bool IsEnumType(const std::unique_ptr<Type>& type, const PackageUnit& pu) {
if (IsStructOrEnumType(type) == false) {
return false;
}
return pu.FindEnum(type->GetTypeInfo()) != nullptr;
}

} // namespace wamon
3 changes: 3 additions & 0 deletions src/type_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ std::unique_ptr<Type> CheckAndGetAsResultType(std::unique_ptr<Type> lt, std::uni
if (TypeFactory<std::vector<unsigned char>>::Get()->GetTypeInfo() == lt->GetTypeInfo() && IsStringType(rt)) {
return rt;
}
if (IsEnumType(lt, pu) && IsStringType(rt)) {
return rt;
}
std::string reason;
if (CheckTraitConstraint(pu, rt, lt, reason)) {
return rt;
Expand Down
14 changes: 14 additions & 0 deletions test/interpreter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,16 @@ TEST(interpreter, operator) {
let v : double = 3.9;
return v as int;
}
enum Color {
Red;
Blue;
}
func as_test_5() -> string {
let v : Color = enum Color:Blue;
return v as string;
}
)";
wamon::PackageUnit pu;
auto tokens = scan.Scan(str);
Expand Down Expand Up @@ -629,6 +639,10 @@ TEST(interpreter, operator) {
EXPECT_EQ(ret->GetTypeInfo(), "int");
EXPECT_EQ(wamon::AsIntVariable(ret)->GetValue(), 3);

ret = interpreter.CallFunctionByName("main$as_test_5", {});
EXPECT_EQ(ret->GetTypeInfo(), "string");
EXPECT_EQ(wamon::AsStringVariable(ret)->GetValue(), "main$Color:Blue");

ret = interpreter.ExecExpression(tc, "main", "0X30 as int");
EXPECT_EQ(ret->GetTypeInfo(), "int");
EXPECT_EQ(wamon::AsIntVariable(ret)->GetValue(), 48);
Expand Down

0 comments on commit 597c502

Please sign in to comment.