forked from PuqiAR/Fig-TreeWalker
[VER] 0.3.3-alpha
[FEAT] interface + impl 支持! Duck Typing + 严格的检查让语言健壮 [FEAT][IMPL] 增加辅助函数 isTypeMatch等 [IMPL] TypeInfo构造函数FString 现在 explicit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
#include <Ast/functionParameters.hpp>
|
||||
#include <Ast/Statements/FunctionDefSt.hpp>
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
/*
|
||||
|
||||
impl Readable for File
|
||||
{
|
||||
read() -> String
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
*/
|
||||
struct ImplementMethod
|
||||
{
|
||||
FString name;
|
||||
FunctionParameters paras;
|
||||
BlockStatement body;
|
||||
};
|
||||
|
||||
class ImplementAst final : public StatementAst
|
||||
{
|
||||
public:
|
||||
FString interfaceName;
|
||||
FString structName;
|
||||
|
||||
std::vector<ImplementMethod> methods;
|
||||
|
||||
ImplementAst()
|
||||
{
|
||||
type = AstType::ImplementSt;
|
||||
}
|
||||
|
||||
ImplementAst(FString _interfaceName, FString _structName, std::vector<ImplementMethod> _methods) :
|
||||
interfaceName(std::move(_interfaceName)), structName(std::move(_structName)), methods(std::move(_methods))
|
||||
{
|
||||
type = AstType::ImplementSt;
|
||||
}
|
||||
};
|
||||
|
||||
using Implement = std::shared_ptr<ImplementAst>;
|
||||
};
|
||||
59
src/Ast/Statements/InterfaceDefSt.hpp
Normal file
59
src/Ast/Statements/InterfaceDefSt.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/functionParameters.hpp>
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
/*
|
||||
|
||||
interface Readable
|
||||
{
|
||||
read() -> String
|
||||
{
|
||||
// default
|
||||
}
|
||||
|
||||
flush() -> Null; // non-default
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
struct InterfaceMethod
|
||||
{
|
||||
FString name;
|
||||
FunctionParameters paras;
|
||||
FString returnType;
|
||||
|
||||
BlockStatement defaultBody = nullptr; // nullptr is non-default func
|
||||
|
||||
bool hasDefaultBody() const
|
||||
{
|
||||
return defaultBody != nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
class InterfaceDefAst final : public StatementAst
|
||||
{
|
||||
public:
|
||||
FString name;
|
||||
std::vector<InterfaceMethod> methods;
|
||||
std::vector<FString> parents; // Feature, NOT NOW
|
||||
bool isPublic;
|
||||
|
||||
InterfaceDefAst()
|
||||
{
|
||||
type = AstType::InterfaceDefSt;
|
||||
}
|
||||
|
||||
InterfaceDefAst(FString _name, std::vector<InterfaceMethod> _methods, bool _isPublic) :
|
||||
name(std::move(_name)), methods(std::move(_methods)), isPublic(_isPublic)
|
||||
{
|
||||
type = AstType::InterfaceDefSt;
|
||||
}
|
||||
};
|
||||
|
||||
using InterfaceDef = std::shared_ptr<InterfaceDefAst>;
|
||||
};
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <Ast/Statements/IfSt.hpp>
|
||||
#include <Ast/Statements/ImplementSt.hpp>
|
||||
#include <Ast/Statements/ImportSt.hpp>
|
||||
#include <Ast/Statements/InterfaceDefSt.hpp>
|
||||
#include <Ast/Statements/FunctionDefSt.hpp>
|
||||
#include <Ast/Statements/ControlSt.hpp>
|
||||
#include <Ast/Statements/ExpressionStmt.hpp>
|
||||
|
||||
@@ -44,6 +44,7 @@ namespace Fig::Ast
|
||||
VarDefSt,
|
||||
FunctionDefSt,
|
||||
StructSt,
|
||||
InterfaceDefSt,
|
||||
ImplementSt,
|
||||
|
||||
IfSt,
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <Value/Type.hpp>
|
||||
#include <Core/fig_string.hpp>
|
||||
|
||||
#include <format>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
struct FunctionParameters // for define
|
||||
@@ -26,7 +28,6 @@ namespace Fig::Ast
|
||||
|
||||
FunctionParameters()
|
||||
{
|
||||
|
||||
}
|
||||
FunctionParameters(PosParasType _posParas, DefParasType _defParas)
|
||||
{
|
||||
@@ -43,5 +44,52 @@ namespace Fig::Ast
|
||||
{
|
||||
return posParas.size() + defParas.size();
|
||||
}
|
||||
|
||||
bool operator==(const FunctionParameters &other) const
|
||||
{
|
||||
return posParas == other.posParas && defParas == other.defParas && variadicPara == other.variadicPara && variadic == other.variadic;
|
||||
}
|
||||
|
||||
FString toString() const
|
||||
{
|
||||
if (variadic)
|
||||
{
|
||||
return FString(variadicPara + u8"...");
|
||||
}
|
||||
static const auto posParasToString = [this]() {
|
||||
FString out;
|
||||
for (auto &p : posParas)
|
||||
{
|
||||
out += p.first;
|
||||
if (!p.second.empty())
|
||||
{
|
||||
out += FString(u8":" + p.second);
|
||||
}
|
||||
out += u8",";
|
||||
}
|
||||
out.pop_back();
|
||||
return out;
|
||||
};
|
||||
static const auto defParasToString = [this]() {
|
||||
FString out;
|
||||
for (auto &p : defParas)
|
||||
{
|
||||
out += p.first;
|
||||
if (!p.second.first.empty())
|
||||
{
|
||||
out += FString(u8":" + p.second.first);
|
||||
}
|
||||
if (p.second.second != nullptr)
|
||||
{
|
||||
out += u8"=";
|
||||
out += p.second.second->toString();
|
||||
}
|
||||
out += u8",";
|
||||
}
|
||||
out.pop_back();
|
||||
return out;
|
||||
};
|
||||
return FString(std::format("{},{}", posParasToString().toBasicString(), defParasToString().toBasicString()));
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace Fig::Ast
|
||||
Reference in New Issue
Block a user