[VER] 0.3.7-alpha

[Fix] 修复科学表达式数字解析的问题(Lexer引起) 由 Satklomi发现,感谢
[Feat] 增加Compiler相关定义,将开发BytecodeVM
[Tip] Evaluator进入Bug fix阶段,新功能延缓开发。转向VM
This commit is contained in:
2026-01-14 17:28:38 +08:00
parent 1ccc63419d
commit e28921ae02
30 changed files with 737 additions and 276 deletions

View File

@@ -0,0 +1,386 @@
#pragma once
#include "Value/interface.hpp"
#include <Value/Type.hpp>
#include <algorithm>
#include <unordered_map>
#include <iostream>
#include <memory>
#include <Context/context_forward.hpp>
#include <Core/fig_string.hpp>
#include <Value/value.hpp>
#include <Value/VariableSlot.hpp>
namespace Fig
{
struct ImplRecord
{
TypeInfo interfaceType;
TypeInfo structType;
std::unordered_map<FString, Function> implMethods;
};
class Context : public std::enable_shared_from_this<Context>
{
private:
FString scopeName;
std::unordered_map<FString, std::shared_ptr<VariableSlot>> variables;
std::unordered_map<std::size_t, Function> functions;
std::unordered_map<std::size_t, FString> functionNames;
// implRegistry <Struct, ordered list of ImplRecord>
std::unordered_map<TypeInfo, std::vector<ImplRecord>, TypeInfoHash> implRegistry;
public:
ContextPtr parent;
Context(const Context &) = default;
Context(const FString &name, ContextPtr p = nullptr) : scopeName(name), parent(p) {}
void setParent(ContextPtr _parent) { parent = _parent; }
void setScopeName(FString _name) { scopeName = std::move(_name); }
FString getScopeName() const { return scopeName; }
void merge(const Context &c)
{
variables.insert(c.variables.begin(), c.variables.end());
functions.insert(c.functions.begin(), c.functions.end());
functionNames.insert(c.functionNames.begin(), c.functionNames.end());
implRegistry.insert(c.implRegistry.begin(), c.implRegistry.end());
// structTypeNames.insert(c.structTypeNames.begin(),
// c.structTypeNames.end());
}
std::unordered_map<size_t, Function> getFunctions() const { return functions; }
std::shared_ptr<VariableSlot> get(const FString &name)
{
auto it = variables.find(name);
if (it != variables.end()) return it->second;
if (parent) return parent->get(name);
throw RuntimeError(FString(std::format("Variable '{}' not defined", name.toBasicString())));
}
AccessModifier getAccessModifier(const FString &name)
{
if (variables.contains(name)) { return variables[name]->am; }
else if (parent != nullptr) { return parent->getAccessModifier(name); }
else
{
throw RuntimeError(FString(std::format("Variable '{}' not defined", name.toBasicString())));
}
}
bool isVariableMutable(const FString &name)
{
AccessModifier am = getAccessModifier(name); // may throw
return !isAccessConst(am);
}
bool isVariablePublic(const FString &name)
{
AccessModifier am = getAccessModifier(name); // may throw
return isAccessPublic(am);
}
void set(const FString &name, ObjectPtr value)
{
if (variables.contains(name))
{
if (!isVariableMutable(name))
{
throw RuntimeError(FString(std::format("Variable '{}' is immutable", name.toBasicString())));
}
variables[name]->value = value;
}
else if (parent != nullptr) { parent->set(name, value); }
else
{
throw RuntimeError(FString(std::format("Variable '{}' not defined", name.toBasicString())));
}
}
void _update(const FString &name, ObjectPtr value)
{
if (variables.contains(name)) { variables[name]->value = value; }
else if (parent != nullptr) { parent->_update(name, value); }
else
{
throw RuntimeError(FString(std::format("Variable '{}' not defined", name.toBasicString())));
}
}
void def(const FString &name,
const TypeInfo &ti,
AccessModifier am,
const ObjectPtr &value = Object::getNullInstance())
{
if (containsInThisScope(name))
{
throw RuntimeError(
FString(std::format("Variable '{}' already defined in this scope", name.toBasicString())));
}
variables[name] = std::make_shared<VariableSlot>(name, value, ti, am);
if (ti == ValueType::Function and value->getTypeInfo() == ValueType::Function)
{
auto &fn = value->as<Function>();
functions[fn.id] = fn;
functionNames[fn.id] = name;
}
// if (ti == ValueType::StructType)
// {
// auto &st = value->as<StructType>();
// structTypeNames[st.id] = name;
// }
}
void
defReference(const FString &name, const TypeInfo &ti, AccessModifier am, std::shared_ptr<VariableSlot> target)
{
if (containsInThisScope(name))
{
throw RuntimeError(
FString(std::format("Variable '{}' already defined in this scope", name.toBasicString())));
}
variables[name] = std::make_shared<VariableSlot>(
name,
target->value,
ti,
am,
true,
target
);
}
std::optional<Function> getFunction(std::size_t id)
{
auto it = functions.find(id);
if (it != functions.end()) { return it->second; }
else if (parent) { return parent->getFunction(id); }
else
{
return std::nullopt;
}
}
std::optional<FString> getFunctionName(std::size_t id)
{
auto it = functionNames.find(id);
if (it != functionNames.end()) { return it->second; }
else if (parent) { return parent->getFunctionName(id); }
else
{
return std::nullopt;
}
}
// std::optional<FString> getStructName(std::size_t id)
// {
// auto it = structTypeNames.find(id);
// if (it != structTypeNames.end())
// {
// return it->second;
// }
// else if (parent)
// {
// return parent->getStructName(id);
// }
// else
// {
// return std::nullopt;
// }
// }
bool contains(const FString &name)
{
if (variables.contains(name)) { return true; }
else if (parent != nullptr) { return parent->contains(name); }
return false;
}
bool containsInThisScope(const FString &name) const { return variables.contains(name); }
TypeInfo getTypeInfo(const FString &name) { return get(name)->declaredType; }
bool isInFunctionContext()
{
ContextPtr ctx = shared_from_this();
while (ctx)
{
if (ctx->getScopeName().find(u8"<Function ") == 0) { return true; }
ctx = ctx->parent;
}
return false;
}
bool isInLoopContext()
{
ContextPtr ctx = shared_from_this();
while (ctx)
{
if (ctx->getScopeName().find(u8"<While ") == 0 or ctx->getScopeName().find(u8"<For ") == 0)
{
return true;
}
ctx = ctx->parent;
}
return false;
}
bool hasImplRegisted(const TypeInfo &structType, const TypeInfo &interfaceType) const
{
auto it = implRegistry.find(structType);
if (it != implRegistry.end())
{
for (auto &r : it->second)
{
if (r.interfaceType == interfaceType) return true;
}
}
return parent && parent->hasImplRegisted(structType, interfaceType);
}
std::optional<ImplRecord> getImplRecord(const TypeInfo &structType, const TypeInfo &interfaceType) const
{
auto it = implRegistry.find(structType);
if (it != implRegistry.end())
{
for (auto &r : it->second)
{
if (r.interfaceType == interfaceType) return r;
}
}
if (parent) return parent->getImplRecord(structType, interfaceType);
return std::nullopt;
}
void setImplRecord(const TypeInfo &structType, const TypeInfo &interfaceType, const ImplRecord &record)
{
auto &list = implRegistry[structType];
for (auto &r : list)
{
if (r.interfaceType == interfaceType) return;
}
list.push_back(record); // order is the level
}
bool hasMethodImplemented(const TypeInfo &structType, const FString &functionName) const
{
auto it = implRegistry.find(structType);
if (it != implRegistry.end())
{
for (auto &record : it->second)
{
if (record.implMethods.contains(functionName)) return true;
}
}
return parent && parent->hasMethodImplemented(structType, functionName);
}
bool hasDefaultImplementedMethod(const TypeInfo &structType, const FString &functionName) const
{
auto it = implRegistry.find(structType);
if (it == implRegistry.end()) return false;
std::vector<TypeInfo> implementedInterfaces;
for (auto &record : it->second) implementedInterfaces.push_back(record.interfaceType);
for (auto &[_, slot] : variables)
{
if (!slot->value->is<InterfaceType>()) continue;
InterfaceType &interface = slot->value->as<InterfaceType>();
bool implemented = std::any_of(implementedInterfaces.begin(),
implementedInterfaces.end(),
[&](const TypeInfo &ti) { return ti == interface.type; });
if (!implemented) continue;
for (auto &method : interface.methods)
{
if (method.name == functionName && method.hasDefaultBody()) return true;
}
}
return false;
}
Function getDefaultImplementedMethod(const TypeInfo &structType, const FString &functionName)
{
// O(N²)
// SLOW
// wwww (sad)
// huh
// just let it be SLOW
// i dont give a fuck
auto it = implRegistry.find(structType);
if (it == implRegistry.end()) assert(false);
std::vector<TypeInfo> implementedInterfaces;
for (auto &record : it->second) implementedInterfaces.push_back(record.interfaceType);
for (auto &[_, slot] : variables)
{
if (!slot->value->is<InterfaceType>()) continue;
InterfaceType &interface = slot->value->as<InterfaceType>();
bool implemented = std::any_of(implementedInterfaces.begin(),
implementedInterfaces.end(),
[&](const TypeInfo &ti) { return ti == interface.type; });
if (!implemented) continue;
for (auto &method : interface.methods)
{
if (method.name == functionName)
{
if (!method.hasDefaultBody()) assert(false);
return Function(
method.paras, TypeInfo(method.returnType), method.defaultBody, shared_from_this());
}
}
}
assert(false);
return Function(); // ignore warning
}
const Function &getImplementedMethod(const TypeInfo &structType, const FString &functionName) const
{
auto it = implRegistry.find(structType);
if (it != implRegistry.end())
{
for (auto &record : it->second)
{
auto fit = record.implMethods.find(functionName);
if (fit != record.implMethods.end()) return fit->second;
}
}
if (parent) return parent->getImplementedMethod(structType, functionName);
assert(false); // not found
throw ""; // ignore warning
}
void printStackTrace(std::ostream &os = std::cerr, int indent = 0) const
{
const Context *ctx = this;
std::vector<const Context *> chain;
while (ctx)
{
chain.push_back(ctx);
ctx = ctx->parent.get();
}
os << "[STACK TRACE]\n";
for (int i = static_cast<int>(chain.size()) - 1; i >= 0; --i)
{
os << " #" << (chain.size() - 1 - i) << " " << chain[i]->scopeName.toBasicString() << "\n";
}
}
};
}; // namespace Fig

View File

@@ -0,0 +1,9 @@
#pragma once
#include <memory>
namespace Fig
{
class Context;
using ContextPtr = std::shared_ptr<Context>;
};

View File

@@ -0,0 +1,143 @@
#pragma once
#include <Value/VariableSlot.hpp>
#include <Value/value.hpp>
namespace Fig
{
struct LvObject
{
enum class Kind
{
Variable,
ListElement,
MapElement,
StringElement,
} kind;
std::shared_ptr<VariableSlot> slot;
ObjectPtr value = nullptr;
size_t numIndex;
ObjectPtr mapIndex;
ContextPtr ctx;
LvObject(std::shared_ptr<VariableSlot> _slot, ContextPtr _ctx) :
slot(std::move(_slot)), ctx(_ctx)
{
kind = Kind::Variable;
}
LvObject(ObjectPtr _v, size_t _index, Kind _kind, ContextPtr _ctx) :
value(_v), numIndex(_index), ctx(_ctx)
{
kind = _kind;
}
LvObject(ObjectPtr _v, ObjectPtr _index, Kind _kind, ContextPtr _ctx) :
value(_v), mapIndex(_index), ctx(_ctx)
{
kind = _kind;
}
ObjectPtr get() const
{
if (kind == Kind::Variable)
{
auto s = resolve(slot);
return s->value;
}
else if (kind == Kind::ListElement)
{
List &list = value->as<List>();
if (numIndex >= list.size())
throw RuntimeError(FString(
std::format("Index {} out of range {}", numIndex, value->toString().toBasicString())));
return list.at(numIndex).value;
}
else if (kind == Kind::MapElement) // map
{
Map &map = value->as<Map>();
if (!map.contains(mapIndex))
throw RuntimeError(FString(
std::format("Key {} not found", mapIndex->toString().toBasicString())));
return map.at(mapIndex);
}
else
{
// string
FString &string = value->as<ValueType::StringClass>();
if (numIndex >= string.length())
throw RuntimeError(FString(
std::format("Index {} out of range {}", numIndex, value->toString().toBasicString())));
return std::make_shared<Object>(string.getRealChar(numIndex));
}
}
void set(const ObjectPtr &v)
{
if (kind == Kind::Variable)
{
auto s = resolve(slot);
if (!isTypeMatch(s->declaredType, v, ctx))
{
throw RuntimeError(
FString(
std::format("Variable `{}` expects type `{}`, but got '{}'",
s->name.toBasicString(),
s->declaredType.toString().toBasicString(),
v->getTypeInfo().toString().toBasicString())));
}
if (isAccessConst(s->am))
{
throw RuntimeError(FString(
std::format("Variable `{}` is immutable", s->name.toBasicString())));
}
s->value = v;
}
else if (kind == Kind::ListElement)
{
List &list = value->as<List>();
if (numIndex >= list.size())
throw RuntimeError(FString(
std::format("Index {} out of range", numIndex)));
list[numIndex] = v;
}
else if (kind == Kind::MapElement) // map
{
Map &map = value->as<Map>();
map[mapIndex] = v;
}
else if (kind == Kind::StringElement)
{
FString &string = value->as<ValueType::StringClass>();
if (numIndex >= string.length())
throw RuntimeError(FString(
std::format("Index {} out of range {}", numIndex, value->toString().toBasicString())));
if (v->getTypeInfo() != ValueType::String)
throw RuntimeError(FString(
std::format("Could not assign {} to sub string", v->toString().toBasicString())
));
const FString &strReplace = v->as<ValueType::StringClass>();
if (strReplace.length() > 1)
throw RuntimeError(FString(
std::format("Could not assign {} to sub string, expects length 1", v->toString().toBasicString())
));
string.realReplace(numIndex, strReplace);
}
}
FString name() const { return resolve(slot)->name; }
TypeInfo declaredType() const { return resolve(slot)->declaredType; }
AccessModifier access() const { return resolve(slot)->am; }
private:
std::shared_ptr<VariableSlot> resolve(std::shared_ptr<VariableSlot> s) const
{
while (s->isRef) s = s->refTarget;
return s;
}
};
} // namespace Fig

View File

@@ -0,0 +1,92 @@
#pragma once
#include <Core/fig_string.hpp>
#include <variant>
#include <map>
namespace Fig
{
class TypeInfo final
{
private:
size_t id;
public:
friend class TypeInfoHash;
FString name;
FString toString() const
{
return name;
}
static std::map<FString, size_t> typeMap;
static size_t getID(FString _name)
{
return typeMap.at(_name);
}
size_t getInstanceID() const
{
return id;
}
TypeInfo();
explicit TypeInfo(const FString &_name, bool reg = false);
TypeInfo(const TypeInfo &other) = default;
bool operator==(const TypeInfo &other) const
{
return id == other.id;
}
};
class TypeInfoHash
{
public:
std::size_t operator()(const TypeInfo &ti) const
{
return std::hash<size_t>{}(ti.id);
}
};
// class Value;
namespace ValueType
{
extern const TypeInfo Any;
extern const TypeInfo Null;
extern const TypeInfo Int;
extern const TypeInfo String;
extern const TypeInfo Bool;
extern const TypeInfo Double;
extern const TypeInfo Function;
extern const TypeInfo StructType;
extern const TypeInfo StructInstance;
extern const TypeInfo List;
extern const TypeInfo Map;
extern const TypeInfo Module;
extern const TypeInfo InterfaceType;
using IntClass = int64_t;
using DoubleClass = double;
using BoolClass = bool;
using NullClass = std::monostate;
using StringClass = FString;
}; // namespace ValueType
}; // namespace Fig
namespace std
{
template <>
struct hash<Fig::TypeInfo>
{
size_t operator()(const Fig::TypeInfo &t)
{
return std::hash<size_t>{}(t.getInstanceID());
}
};
};

View File

@@ -0,0 +1,22 @@
#pragma once
#include <Ast/AccessModifier.hpp>
#include <Core/fig_string.hpp>
#include <Value/Type.hpp>
#include <memory>
namespace Fig
{
class Object;
using ObjectPtr = std::shared_ptr<Object>;
struct VariableSlot
{
FString name;
ObjectPtr value;
TypeInfo declaredType;
AccessModifier am;
bool isRef = false;
std::shared_ptr<VariableSlot> refTarget;
};
}

View File

@@ -0,0 +1,67 @@
#pragma once
#include <Ast/functionParameters.hpp>
#include <Context/context_forward.hpp>
#include <atomic>
#include <functional>
#include <memory>
#include <vector>
namespace Fig
{
class Object;
class Function
{
public:
std::size_t id;
Ast::FunctionParameters paras;
TypeInfo retType;
Ast::BlockStatement body;
bool isBuiltin = false;
std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)> builtin;
int builtinParamCount = -1;
std::shared_ptr<Context> closureContext;
// ===== Constructors =====
Function() :
id(nextId()) {}
Function(Ast::FunctionParameters _paras, TypeInfo _retType, Ast::BlockStatement _body, ContextPtr _closureContext) :
id(nextId()), // 分配唯一 ID
paras(std::move(_paras)),
retType(std::move(_retType)),
body(std::move(_body)),
closureContext(std::move(_closureContext))
{
}
Function(std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)> fn, int argc) :
id(nextId()), isBuiltin(true), builtin(fn), builtinParamCount(argc) {}
// ===== Copy / Move =====
Function(const Function &other) = default;
Function(Function &&) noexcept = default;
Function &operator=(const Function &) = default;
Function &operator=(Function &&) noexcept = default;
// ===== Comparison =====
bool operator==(const Function &other) const noexcept
{
return id == other.id;
}
bool operator!=(const Function &other) const noexcept
{
return !(*this == other);
}
private:
static std::size_t nextId()
{
static std::atomic<std::size_t> counter{1};
return counter++;
}
};
} // namespace Fig

View File

@@ -0,0 +1,20 @@
#pragma once
#include <Ast/Statements/InterfaceDefSt.hpp>
#include <Value/Type.hpp>
#include <vector>
namespace Fig
{
struct InterfaceType
{
TypeInfo type;
std::vector<Ast::InterfaceMethod> methods;
bool operator==(const InterfaceType &other) const
{
return type == other.type; // only compare type info (chain -> typeinfo.id)
}
};
}

View File

@@ -0,0 +1,27 @@
#pragma once
#include <Core/fig_string.hpp>
#include <Context/context_forward.hpp>
namespace Fig
{
struct Module
{
FString name;
ContextPtr ctx;
Module() = default;
Module(FString n, ContextPtr c) :
name(std::move(n)),
ctx(std::move(c))
{
}
bool operator==(const Module &o) const noexcept
{
return name == o.name;
}
};
};

View File

@@ -0,0 +1,32 @@
#pragma once
#include <Context/context_forward.hpp>
#include <Value/Type.hpp>
namespace Fig
{
struct StructInstance
{
TypeInfo parentType;
ContextPtr localContext;
// ===== Constructors =====
StructInstance(TypeInfo _parentType, ContextPtr _localContext) :
parentType(_parentType), localContext(std::move(_localContext)) {}
StructInstance(const StructInstance &other) = default;
StructInstance(StructInstance &&) noexcept = default;
StructInstance &operator=(const StructInstance &) = default;
StructInstance &operator=(StructInstance &&) noexcept = default;
// ===== Comparison =====
bool operator==(const StructInstance &other) const noexcept
{
return parentType == other.parentType && localContext == other.localContext;
}
bool operator!=(const StructInstance &other) const noexcept
{
return !(*this == other);
}
};
} // namespace Fig

View File

@@ -0,0 +1,80 @@
#pragma once
#include <Core/fig_string.hpp>
#include <Ast/Statements/StructDefSt.hpp>
#include <Value/Type.hpp>
#include <Context/context_forward.hpp>
#include <atomic>
#include <vector>
namespace Fig
{
struct Field
{
AccessModifier am;
FString name;
TypeInfo type;
Ast::Expression defaultValue;
Field(AccessModifier _am, FString _name, TypeInfo _type, Ast::Expression _defaultValue) :
am(_am), name(std::move(_name)), type(std::move(_type)), defaultValue(std::move(_defaultValue)) {}
bool isPublic() const
{
return am == AccessModifier::Public || am == AccessModifier::PublicConst;
}
bool isConst() const
{
return am == AccessModifier::Const || am == AccessModifier::PublicConst;
}
};
struct StructType
{
TypeInfo type;
ContextPtr defContext; // 定义时的上下文
std::vector<Field> fields;
bool builtin = false;
// ===== Constructors =====
StructType(TypeInfo _type, ContextPtr _defContext, std::vector<Field> _fields, bool _builtin = false) :
type(std::move(_type)), defContext(std::move(_defContext)), fields(std::move(_fields)), builtin(_builtin) {}
StructType(const StructType &other) = default;
StructType(StructType &&) noexcept = default;
StructType &operator=(const StructType &) = default;
StructType &operator=(StructType &&) noexcept = default;
// ===== Comparison =====
bool operator==(const StructType &other) const noexcept
{
return type == other.type;
}
bool operator!=(const StructType &other) const noexcept
{
return !(*this == other);
}
private:
static std::size_t nextId()
{
static std::atomic<std::size_t> counter{1};
return counter++;
}
};
} // namespace Fig
namespace std
{
template <>
struct hash<Fig::Field>
{
size_t operator()(const Fig::Field &f)
{
return std::hash<Fig::FString>{}(f.name);
}
};
}; // namespace std

View File

@@ -0,0 +1,138 @@
#include <Value/Type.hpp>
#include <Value/value.hpp>
#include <Context/context.hpp>
// #include <iostream>
namespace Fig
{
std::map<FString, size_t> TypeInfo::typeMap = {};
TypeInfo::TypeInfo() : // only allow use in evaluate time !! <---- dynamic type system requirement
id(1), name(FString(u8"Any"))
{
}
TypeInfo::TypeInfo(const FString &_name, bool reg)
{
static size_t id_count = 0;
name = _name;
// std::cerr << "TypeInfo constructor called for type name: " << name.toBasicString() << "\n";
if (reg)
{
typeMap[name] = ++id_count;
id = id_count;
}
else
{
if (!typeMap.contains(_name))
{
throw RuntimeError(FString(std::format(
"No type named '{}'",
_name.toBasicString())));
// *this = ValueType::String;
}
id = typeMap.at(name); // may throw
}
}
size_t ValueKeyHash::operator()(const ValueKey &key) const
{
{
ObjectPtr value = key.value;
const TypeInfo &type = value->getTypeInfo();
if (type == ValueType::Int)
{
return std::hash<ValueType::IntClass>{}(value->as<ValueType::IntClass>());
}
if (type == ValueType::Double)
{
return std::hash<ValueType::DoubleClass>{}(value->as<ValueType::DoubleClass>());
}
if (type == ValueType::String)
{
return std::hash<ValueType::StringClass>{}(value->as<ValueType::StringClass>());
}
if (type == ValueType::Bool)
{
return std::hash<ValueType::BoolClass>{}(value->as<ValueType::BoolClass>());
}
if (type == ValueType::StructType)
{
auto HashFields = [](std::vector<Field> fields) {
size_t r = 0;
for (auto &f : fields)
{
r += std::hash<Field>{}(f);
}
return r;
};
const StructType &st = value->as<StructType>();
return std::hash<TypeInfo>{}(st.type) + HashFields(st.fields);
}
if (type == ValueType::StructInstance)
{
const StructInstance &si = value->as<StructInstance>();
return std::hash<TypeInfo>{}(si.parentType) + std::hash<uint64_t>{}(reinterpret_cast<uint64_t>(std::addressof(*si.localContext)));
}
assert(false);
throw ""; // ignore warning
}
}
FString prettyType(std::shared_ptr<const Object> obj)
{
auto t = obj->getTypeInfo();
if (t == ValueType::StructInstance)
return obj->as<StructInstance>().parentType.toString();
return t.toString();
}
const TypeInfo ValueType::Any(FString(u8"Any"), true); // id: 1
const TypeInfo ValueType::Null(FString(u8"Null"), true); // id: 2
const TypeInfo ValueType::Int(FString(u8"Int"), true); // id: 3
const TypeInfo ValueType::String(FString(u8"String"), true); // id: 4
const TypeInfo ValueType::Bool(FString(u8"Bool"), true); // id: 5
const TypeInfo ValueType::Double(FString(u8"Double"), true); // id: 6
const TypeInfo ValueType::Function(FString(u8"Function"), true); // id: 7
const TypeInfo ValueType::StructType(FString(u8"StructType"), true); // id: 8
const TypeInfo ValueType::StructInstance(FString(u8"StructInstance"), true); // id: 9
const TypeInfo ValueType::List(FString(u8"List"), true); // id: 10
const TypeInfo ValueType::Map(FString(u8"Map"), true); // id: 11
const TypeInfo ValueType::Module(FString(u8"Module"), true); // id: 12
const TypeInfo ValueType::InterfaceType(FString(u8"InterfaceType"), true); // id: 13
bool implements(const TypeInfo &structType, const TypeInfo &interfaceType, ContextPtr ctx)
{
return ctx->hasImplRegisted(structType, interfaceType);
}
bool isTypeMatch(const TypeInfo &expected, ObjectPtr obj, ContextPtr ctx)
{
if (expected == ValueType::Any)
return true;
TypeInfo actual = obj->getTypeInfo();
if (obj->is<StructInstance>())
{
const StructInstance &si = obj->as<StructInstance>();
if (si.parentType == expected)
{
return true;
}
if (implements(si.parentType, expected, ctx))
{
return true;
}
return false;
}
else
{
return expected == actual;
}
}
} // namespace Fig

View File

@@ -0,0 +1,717 @@
#pragma once
#include <Value/function.hpp>
#include <Value/interface.hpp>
#include <Value/structType.hpp>
#include <Value/structInstance.hpp>
#include <Value/Type.hpp>
#include <Value/valueError.hpp>
#include <Value/module.hpp>
#include <memory>
#include <variant>
#include <cmath>
#include <string>
#include <format>
#include <functional>
#include <unordered_map>
namespace Fig
{
inline bool isDoubleInteger(ValueType::DoubleClass d)
{
return std::floor(d) == d;
}
inline bool isNumberExceededIntLimit(ValueType::DoubleClass d)
{
static constexpr auto intMaxAsDouble =
static_cast<ValueType::DoubleClass>(std::numeric_limits<ValueType::IntClass>::max());
static constexpr auto intMinAsDouble =
static_cast<ValueType::DoubleClass>(std::numeric_limits<ValueType::IntClass>::min());
return d > intMaxAsDouble || d < intMinAsDouble;
}
class Object;
using ObjectPtr = std::shared_ptr<Object>;
FString prettyType(std::shared_ptr<const Object> obj);
bool operator==(const Object &, const Object &);
struct Element
{
ObjectPtr value;
Element(ObjectPtr _value) :
value(_value) {}
bool operator==(const Element &other) const
{
return *value == *other.value;
}
void deepCopy(const Element &e)
{
value = std::make_shared<Object>(*e.value);
}
};
using List = std::vector<Element>;
struct ValueKey
{
ObjectPtr value;
ValueKey(ObjectPtr _value) :
value(_value) {}
void deepCopy(const ValueKey &vk) { value = std::make_shared<Object>(*vk.value); }
};
struct ValueKeyHash
{
size_t operator()(const ValueKey &key) const;
};
using Map = std::unordered_map<ValueKey, ObjectPtr, ValueKeyHash>;
bool isTypeMatch(const TypeInfo &, ObjectPtr, ContextPtr);
bool implements(const TypeInfo &, const TypeInfo &, ContextPtr);
class Object : public std::enable_shared_from_this<Object>
{
public:
using VariantType = std::variant<
ValueType::NullClass,
ValueType::IntClass,
ValueType::DoubleClass,
ValueType::StringClass,
ValueType::BoolClass,
Function,
StructType,
StructInstance,
List,
Map,
Module,
InterfaceType>;
std::unordered_map<TypeInfo,
std::unordered_map<FString,
std::function<ObjectPtr(std::vector<ObjectPtr>)>>,
TypeInfoHash>
memberTypeFunctions{
{ValueType::Null, {}},
{ValueType::Int, {}},
{ValueType::Double, {}},
{ValueType::String, {
{u8"length", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 0)
throw RuntimeError(FString(
std::format("`length` expects 0 arguments, {} got", args.size())));
const FString &str = as<ValueType::StringClass>();
return std::make_shared<Object>(static_cast<ValueType::IntClass>(str.length()));
}},
{u8"replace", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 2)
throw RuntimeError(FString(
std::format("`replace` expects 2 arguments, {} got", args.size())));
FString &str = as<ValueType::StringClass>();
ObjectPtr arg1 = args[0];
ObjectPtr arg2 = args[1];
if (!arg1->is<ValueType::IntClass>())
{
throw RuntimeError(FString(
"`replace` arg 1 expects type Int"));
}
if (!arg2->is<ValueType::StringClass>())
{
throw RuntimeError(FString(
"`replace` arg 2 expects type String"));
}
str.realReplace(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::StringClass>());
return Object::getNullInstance();
}},
{u8"erase", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 2)
throw RuntimeError(FString(
std::format("`erase` expects 2 arguments, {} got", args.size())));
FString &str = as<ValueType::StringClass>();
ObjectPtr arg1 = args[0];
ObjectPtr arg2 = args[1];
if (!arg1->is<ValueType::IntClass>())
{
throw RuntimeError(FString(
"`erase` arg 1 expects type Int"));
}
if (!arg2->is<ValueType::IntClass>())
{
throw RuntimeError(FString(
"`erase` arg 2 expects type Int"));
}
ValueType::IntClass index = arg1->as<ValueType::IntClass>();
ValueType::IntClass n = arg2->as<ValueType::IntClass>();
if (index < 0 || n < 0)
{
throw RuntimeError(FString("`erase`: index and n must greater or equal to 0"));
}
if (index + n > str.length())
{
throw RuntimeError(FString("`erase`: length is not long enough to erase"));
}
str.realErase(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::IntClass>());
return Object::getNullInstance();
}},
{u8"insert", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 2)
throw RuntimeError(FString(
std::format("`insert` expects 2 arguments, {} got", args.size())));
FString &str = as<ValueType::StringClass>();
ObjectPtr arg1 = args[0];
ObjectPtr arg2 = args[1];
if (!arg1->is<ValueType::IntClass>())
{
throw RuntimeError(FString(
"`insert` arg 1 expects type Int"));
}
if (!arg2->is<ValueType::StringClass>())
{
throw RuntimeError(FString(
"`insert` arg 2 expects type String"));
}
str.realInsert(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::StringClass>());
return Object::getNullInstance();
}},
}},
{ValueType::Function, {}},
{ValueType::StructType, {}},
{ValueType::StructInstance, {}},
{ValueType::List, {
{u8"length", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 0)
throw RuntimeError(FString(
std::format("`length` expects 0 arguments, {} got", args.size())));
const List &list = as<List>();
return std::make_shared<Object>(static_cast<ValueType::IntClass>(list.size()));
}},
{u8"get", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(FString(
std::format("`get` expects 1 arguments, {} got", args.size())));
ObjectPtr arg = args[0];
if (arg->getTypeInfo() != ValueType::Int)
throw RuntimeError(FString(
std::format("`get` argument 1 expects Int, {} got", arg->getTypeInfo().toString().toBasicString())));
ValueType::IntClass i = arg->as<ValueType::IntClass>();
const List &list = as<List>();
if (i >= list.size())
return Object::getNullInstance();
return list[i].value;
}},
{u8"push", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(FString(
std::format("`push` expects 1 arguments, {} got", args.size())));
ObjectPtr arg = args[0];
List &list = as<List>();
list.push_back(arg);
return Object::getNullInstance();
}},
}},
{ValueType::Map, {
{u8"get", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(FString(
std::format("`get` expects 1 arguments, {} got", args.size())));
ObjectPtr index = args[0];
const Map &map = as<Map>();
if (!map.contains(index))
return Object::getNullInstance();
return map.at(index);
}},
{u8"contains", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(FString(
std::format("`contains` expects 1 arguments, {} got", args.size())));
ObjectPtr index = args[0];
const Map &map = as<Map>();
return std::make_shared<Object>(
map.contains(index));
}},
}},
{ValueType::Module, {}},
{ValueType::InterfaceType, {}},
};
std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash> memberTypeFunctionsParas{
{ValueType::Null, {}},
{ValueType::Int, {}},
{ValueType::Double, {}},
{ValueType::String, {
{u8"length", 0},
{u8"replace", 2},
{u8"erase", 2},
{u8"insert", 2},
}},
{ValueType::Function, {}},
{ValueType::StructType, {}},
{ValueType::StructInstance, {}},
{ValueType::List, {{u8"length", 0}, {u8"get", 1}, {u8"push", 1}}},
{ValueType::Map, {
{u8"get", 1},
{u8"contains", 1},
}},
{ValueType::Module, {}},
{ValueType::InterfaceType, {}},
};
bool hasMemberFunction(const FString &name) const
{
return memberTypeFunctions.at(getTypeInfo()).contains(name);
}
std::function<ObjectPtr(std::vector<ObjectPtr>)> getMemberFunction(const FString &name) const
{
return memberTypeFunctions.at(getTypeInfo()).at(name);
}
int getMemberFunctionParaCount(const FString &name) const
{
return memberTypeFunctionsParas.at(getTypeInfo()).at(name);
}
VariantType data;
Object() :
data(ValueType::NullClass{}) {}
Object(const ValueType::NullClass &n) :
data(n) {}
Object(const ValueType::IntClass &i) :
data(i) {}
explicit Object(const ValueType::DoubleClass &d) :
data(d) {}
Object(const ValueType::StringClass &s) :
data(s) {}
Object(const ValueType::BoolClass &b) :
data(b) {}
Object(const Function &f) :
data(f) {}
Object(const StructType &s) :
data(s) {}
Object(const StructInstance &s) :
data(s) {}
Object(const List &l) :
data(l) {}
Object(const Map &m) :
data(m) {}
Object(const Module &m) :
data(m) {}
Object(const InterfaceType &i) :
data(i) {}
Object(const Object &) = default;
Object(Object &&) noexcept = default;
Object &operator=(const Object &) = default;
Object &operator=(Object &&) noexcept = default;
static Object defaultValue(TypeInfo ti)
{
if (ti == ValueType::Int)
return Object(ValueType::IntClass(0));
else if (ti == ValueType::Double)
return Object(ValueType::DoubleClass(0.0));
else if (ti == ValueType::String)
return Object(ValueType::StringClass(u8""));
else if (ti == ValueType::Bool)
return Object(ValueType::BoolClass(false));
else if (ti == ValueType::List)
return Object(List{});
else if (ti == ValueType::Map)
return Object(Map{});
else
return *getNullInstance();
}
template <typename T>
bool is() const
{
return std::holds_alternative<T>(data);
}
template <typename T>
T &as()
{
return std::get<T>(data);
}
template <typename T>
const T &as() const
{
return std::get<T>(data);
}
static std::shared_ptr<Object> getNullInstance()
{
static std::shared_ptr<Object> n = std::make_shared<Object>(ValueType::NullClass{});
return n;
}
static std::shared_ptr<Object> getTrueInstance()
{
static std::shared_ptr<Object> t = std::make_shared<Object>(true);
return t;
}
static std::shared_ptr<Object> getFalseInstance()
{
static std::shared_ptr<Object> f = std::make_shared<Object>(false);
return f;
}
TypeInfo getTypeInfo() const
{
return std::visit([](auto &&val) -> TypeInfo {
using T = std::decay_t<decltype(val)>;
if constexpr (std::is_same_v<T, ValueType::NullClass>)
return ValueType::Null;
else if constexpr (std::is_same_v<T, ValueType::IntClass>)
return ValueType::Int;
else if constexpr (std::is_same_v<T, ValueType::DoubleClass>)
return ValueType::Double;
else if constexpr (std::is_same_v<T, ValueType::StringClass>)
return ValueType::String;
else if constexpr (std::is_same_v<T, ValueType::BoolClass>)
return ValueType::Bool;
else if constexpr (std::is_same_v<T, Function>)
return ValueType::Function;
else if constexpr (std::is_same_v<T, StructType>)
return ValueType::StructType;
else if constexpr (std::is_same_v<T, StructInstance>)
return ValueType::StructInstance;
else if constexpr (std::is_same_v<T, List>)
return ValueType::List;
else if constexpr (std::is_same_v<T, Map>)
return ValueType::Map;
else if constexpr (std::is_same_v<T, Module>)
return ValueType::Module;
else if constexpr (std::is_same_v<T, InterfaceType>)
return ValueType::InterfaceType;
else
return ValueType::Any;
},
data);
}
bool isNull() const { return is<ValueType::NullClass>(); }
bool isNumeric() const { return is<ValueType::IntClass>() || is<ValueType::DoubleClass>(); }
ValueType::DoubleClass getNumericValue() const
{
if (is<ValueType::IntClass>())
return static_cast<ValueType::DoubleClass>(as<ValueType::IntClass>());
else if (is<ValueType::DoubleClass>())
return as<ValueType::DoubleClass>();
else
throw RuntimeError(u8"getNumericValue: Not a numeric value");
}
FString toStringIO() const
{
if (is<ValueType::StringClass>()) return as<ValueType::StringClass>();
return toString();
}
FString toString() const
{
if (is<ValueType::NullClass>()) return FString(u8"null");
if (is<ValueType::IntClass>()) return FString(std::to_string(as<ValueType::IntClass>()));
if (is<ValueType::DoubleClass>()) return FString(std::format("{}", as<ValueType::DoubleClass>()));
if (is<ValueType::StringClass>()) return FString(u8"\"" + as<ValueType::StringClass>() + u8"\"");
if (is<ValueType::BoolClass>()) return as<ValueType::BoolClass>() ? FString(u8"true") : FString(u8"false");
if (is<Function>())
return FString(std::format("<Function '{}' at {:p}>",
as<Function>().id,
static_cast<const void *>(&as<Function>())));
if (is<StructType>())
return FString(std::format("<StructType '{}' at {:p}>",
as<StructType>().type.toString().toBasicString(),
static_cast<const void *>(&as<StructType>())));
if (is<StructInstance>())
return FString(std::format("<StructInstance '{}' at {:p}>",
as<StructInstance>().parentType.toString().toBasicString(),
static_cast<const void *>(&as<StructInstance>())));
if (is<List>())
{
FString output(u8"[");
const List &list = as<List>();
bool first_flag = true;
for (auto &ele : list)
{
if (!first_flag)
output += u8", ";
output += ele.value->toString();
first_flag = false;
}
output += u8"]";
return output;
}
if (is<Map>())
{
FString output(u8"{");
const Map &map = as<Map>();
bool first_flag = true;
for (auto &[key, value] : map)
{
if (!first_flag)
output += u8", ";
output += key.value->toString() + FString(u8" : ") + value->toString();
first_flag = false;
}
output += u8"}";
return output;
}
if (is<Module>())
{
return FString(std::format(
"<Module '{}' at {:p}>",
as<Module>().name.toBasicString(),
static_cast<const void *>(&as<Module>())));
}
if (is<InterfaceType>())
{
return FString(std::format(
"<InterfaceType '{}' at {:p}>",
as<InterfaceType>().type.toString().toBasicString(),
static_cast<const void *>(&as<InterfaceType>())));
}
return FString(u8"<error>");
}
private:
static std::string makeTypeErrorMessage(const char *prefix, const char *op,
const Object &lhs, const Object &rhs)
{
auto lhs_type = lhs.getTypeInfo().name.toBasicString();
auto rhs_type = rhs.getTypeInfo().name.toBasicString();
return std::format("{}: {} '{}' {}", prefix, lhs_type, op, rhs_type);
}
public:
// math
friend Object operator+(const Object &lhs, const Object &rhs)
{
if (lhs.isNull() || rhs.isNull())
throw ValueError(FString(makeTypeErrorMessage("Cannot add", "+", lhs, rhs)));
if (lhs.isNumeric() && rhs.isNumeric())
{
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
auto result = lhs.getNumericValue() + rhs.getNumericValue();
if (bothInt && !isNumberExceededIntLimit(result))
return Object(static_cast<ValueType::IntClass>(result));
return Object(result);
}
if (lhs.is<ValueType::StringClass>() && rhs.is<ValueType::StringClass>())
return Object(FString(lhs.as<ValueType::StringClass>() + rhs.as<ValueType::StringClass>()));
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "+", lhs, rhs)));
}
friend Object operator-(const Object &lhs, const Object &rhs)
{
if (lhs.isNull() || rhs.isNull())
throw ValueError(FString(makeTypeErrorMessage("Cannot subtract", "-", lhs, rhs)));
if (lhs.isNumeric() && rhs.isNumeric())
{
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
auto result = lhs.getNumericValue() - rhs.getNumericValue();
if (bothInt && !isNumberExceededIntLimit(result))
return Object(static_cast<ValueType::IntClass>(result));
return Object(result);
}
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "-", lhs, rhs)));
}
friend Object operator*(const Object &lhs, const Object &rhs)
{
if (lhs.isNull() || rhs.isNull())
throw ValueError(FString(makeTypeErrorMessage("Cannot multiply", "*", lhs, rhs)));
if (lhs.isNumeric() && rhs.isNumeric())
{
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
auto result = lhs.getNumericValue() * rhs.getNumericValue();
if (bothInt && !isNumberExceededIntLimit(result))
return Object(static_cast<ValueType::IntClass>(result));
return Object(result);
}
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "*", lhs, rhs)));
}
friend Object operator/(const Object &lhs, const Object &rhs)
{
if (lhs.isNull() || rhs.isNull())
throw ValueError(FString(makeTypeErrorMessage("Cannot divide", "/", lhs, rhs)));
if (lhs.isNumeric() && rhs.isNumeric())
{
auto rnv = rhs.getNumericValue();
if (rnv == 0)
throw ValueError(FString(makeTypeErrorMessage("Division by zero", "/", lhs, rhs)));
// bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
auto result = lhs.getNumericValue() / rnv;
// if (bothInt && !isNumberExceededIntLimit(result))
// return Object(static_cast<ValueType::IntClass>(result));
// int / int maybe decimals
// DO NOT convert it to INT
return Object(result);
}
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "/", lhs, rhs)));
}
friend Object operator%(const Object &lhs, const Object &rhs)
{
if (lhs.isNull() || rhs.isNull())
throw ValueError(FString(makeTypeErrorMessage("Cannot modulo", "%", lhs, rhs)));
if (lhs.isNumeric() && rhs.isNumeric())
{
auto rnv = rhs.getNumericValue();
if (rnv == 0)
throw ValueError(FString(makeTypeErrorMessage("Modulo by zero", "/", lhs, rhs)));
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
auto result = std::fmod(lhs.getNumericValue(), rnv);
if (bothInt && !isNumberExceededIntLimit(result))
return Object(static_cast<ValueType::IntClass>(result));
return Object(result);
}
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "%", lhs, rhs)));
}
// logic
friend Object operator&&(const Object &lhs, const Object &rhs)
{
if (!lhs.is<ValueType::BoolClass>() || !rhs.is<ValueType::BoolClass>())
throw ValueError(FString(makeTypeErrorMessage("Logical AND requires bool", "&&", lhs, rhs)));
return Object(lhs.as<ValueType::BoolClass>() && rhs.as<ValueType::BoolClass>());
}
friend Object operator||(const Object &lhs, const Object &rhs)
{
if (!lhs.is<ValueType::BoolClass>() || !rhs.is<ValueType::BoolClass>())
throw ValueError(FString(makeTypeErrorMessage("Logical OR requires bool", "||", lhs, rhs)));
return Object(lhs.as<ValueType::BoolClass>() || rhs.as<ValueType::BoolClass>());
}
friend Object operator!(const Object &v)
{
if (!v.is<ValueType::BoolClass>())
throw ValueError(FString(std::format("Logical NOT requires bool: '{}'", v.getTypeInfo().name.toBasicString())));
return Object(!v.as<ValueType::BoolClass>());
}
friend Object operator-(const Object &v)
{
if (v.isNull())
throw ValueError(FString(u8"Unary minus cannot be applied to null"));
if (v.is<ValueType::IntClass>())
return Object(-v.as<ValueType::IntClass>());
if (v.is<ValueType::DoubleClass>())
return Object(-v.as<ValueType::DoubleClass>());
throw ValueError(FString(std::format("Unary minus requires int or double: '{}'", v.getTypeInfo().name.toBasicString())));
}
friend Object operator~(const Object &v)
{
if (!v.is<ValueType::IntClass>())
throw ValueError(FString(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
return Object(~v.as<ValueType::IntClass>());
}
// comparison
friend bool operator==(const Object &lhs, const Object &rhs) { return lhs.data == rhs.data; }
friend bool operator!=(const Object &lhs, const Object &rhs) { return !(lhs == rhs); }
friend bool operator<(const Object &lhs, const Object &rhs)
{
if (lhs.isNumeric() && rhs.isNumeric()) return lhs.getNumericValue() < rhs.getNumericValue();
if (lhs.is<ValueType::StringClass>() && rhs.is<ValueType::StringClass>())
return lhs.as<ValueType::StringClass>() < rhs.as<ValueType::StringClass>();
throw ValueError(FString(makeTypeErrorMessage("Unsupported comparison", "<", lhs, rhs)));
}
friend bool operator<=(const Object &lhs, const Object &rhs) { return lhs == rhs || lhs < rhs; }
friend bool operator>(const Object &lhs, const Object &rhs)
{
if (lhs.isNumeric() && rhs.isNumeric()) return lhs.getNumericValue() > rhs.getNumericValue();
if (lhs.is<ValueType::StringClass>() && rhs.is<ValueType::StringClass>())
return lhs.as<ValueType::StringClass>() > rhs.as<ValueType::StringClass>();
throw ValueError(FString(makeTypeErrorMessage("Unsupported comparison", ">", lhs, rhs)));
}
friend bool operator>=(const Object &lhs, const Object &rhs) { return lhs == rhs || lhs > rhs; }
// bitwise
friend Object bit_and(const Object &lhs, const Object &rhs)
{
if (!lhs.is<ValueType::IntClass>() || !rhs.is<ValueType::IntClass>())
throw ValueError(FString(makeTypeErrorMessage("Bitwise AND requires int", "&", lhs, rhs)));
return Object(lhs.as<ValueType::IntClass>() & rhs.as<ValueType::IntClass>());
}
friend Object bit_or(const Object &lhs, const Object &rhs)
{
if (!lhs.is<ValueType::IntClass>() || !rhs.is<ValueType::IntClass>())
throw ValueError(FString(makeTypeErrorMessage("Bitwise OR requires int", "|", lhs, rhs)));
return Object(lhs.as<ValueType::IntClass>() | rhs.as<ValueType::IntClass>());
}
friend Object bit_xor(const Object &lhs, const Object &rhs)
{
if (!lhs.is<ValueType::IntClass>() || !rhs.is<ValueType::IntClass>())
throw ValueError(FString(makeTypeErrorMessage("Bitwise XOR requires int", "^", lhs, rhs)));
return Object(lhs.as<ValueType::IntClass>() ^ rhs.as<ValueType::IntClass>());
}
friend Object bit_not(const Object &v)
{
if (!v.is<ValueType::IntClass>())
throw ValueError(FString(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
return Object(~v.as<ValueType::IntClass>());
}
friend Object shift_left(const Object &lhs, const Object &rhs)
{
if (!lhs.is<ValueType::IntClass>() || !rhs.is<ValueType::IntClass>())
throw ValueError(FString(makeTypeErrorMessage("Shift left requires int", "<<", lhs, rhs)));
return Object(lhs.as<ValueType::IntClass>() << rhs.as<ValueType::IntClass>());
}
friend Object shift_right(const Object &lhs, const Object &rhs)
{
if (!lhs.is<ValueType::IntClass>() || !rhs.is<ValueType::IntClass>())
throw ValueError(FString(makeTypeErrorMessage("Shift right requires int", ">>", lhs, rhs)));
return Object(lhs.as<ValueType::IntClass>() >> rhs.as<ValueType::IntClass>());
}
friend Object power(const Object &base, const Object &exp)
{
if (base.isNull() || exp.isNull())
throw ValueError(FString(makeTypeErrorMessage("Cannot exponentiate", "**", base, exp)));
if (base.isNumeric() && exp.isNumeric())
{
bool bothInt = base.is<ValueType::IntClass>() && exp.is<ValueType::IntClass>();
auto result = std::pow(base.getNumericValue(), exp.getNumericValue());
if (bothInt && !isNumberExceededIntLimit(result))
return Object(static_cast<ValueType::IntClass>(result));
return Object(result);
}
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "**", base, exp)));
}
};
using ObjectPtr = std::shared_ptr<Object>;
using RvObject = ObjectPtr;
inline bool operator==(const ValueKey &l, const ValueKey &r)
{
return *l.value == *r.value;
}
} // namespace Fig

View File

@@ -0,0 +1,17 @@
#pragma once
#include <Error/error.hpp>
namespace Fig
{
class ValueError : public UnaddressableError
{
public:
using UnaddressableError::UnaddressableError;
virtual FString toString() const override
{
std::string msg = std::format("[ValueError] {} in [{}] {}", std::string(this->message.begin(), this->message.end()), this->src_loc.file_name(), this->src_loc.function_name());
return FString(msg);
}
};
};

View File

@@ -573,6 +573,12 @@ namespace Fig
for (const auto &stmt : fnStruct.body->stmts)
{
StatementResult sr = evalStatement(stmt, newContext);
if (sr.isError())
{
throw EvaluatorError(u8"UncaughtExceptionError",
std::format("Uncaught exception: {}", sr.result->toString().toBasicString()),
stmt);
}
if (!sr.isNormal())
{
retVal = sr.result;
@@ -683,20 +689,14 @@ namespace Fig
}
case AstType::InitExpr: {
auto initExpr = std::dynamic_pointer_cast<Ast::InitExprAst>(exp);
if (!ctx->contains(initExpr->structName))
{
throw EvaluatorError(
u8"StructNotFoundError",
std::format("Structure type '{}' not found", initExpr->structName.toBasicString()),
initExpr);
}
ObjectPtr structTypeVal = ctx->get(initExpr->structName)->value;
LvObject structeLv = evalLv(initExpr->structe, ctx);
ObjectPtr structTypeVal = structeLv.get();
const FString &structName = structeLv.name();
if (!structTypeVal->is<StructType>())
{
throw EvaluatorError(
u8"NotAStructTypeError",
std::format("'{}' is not a structure type", initExpr->structName.toBasicString()),
initExpr);
throw EvaluatorError(u8"NotAStructTypeError",
std::format("'{}' is not a structure type", structName.toBasicString()),
initExpr);
}
const StructType &structT = structTypeVal->as<StructType>();
@@ -717,7 +717,8 @@ namespace Fig
}
// default value
if (argSize == 0) {
if (argSize == 0)
{
if (type == ValueType::Any || type == ValueType::Null || type == ValueType::Function)
{
throw EvaluatorError(
@@ -824,7 +825,7 @@ namespace Fig
{
throw EvaluatorError(u8"StructInitArgumentMismatchError",
std::format("Structure '{}' expects {} to {} fields, but {} were provided",
initExpr->structName.toBasicString(),
structName.toBasicString(),
minArgs,
maxArgs,
initExpr->args.size()),
@@ -837,7 +838,7 @@ namespace Fig
evaluatedArgs.push_back({argName, eval(argExpr, ctx)});
}
ContextPtr instanceCtx = std::make_shared<Context>(
FString(std::format("<StructInstance {}>", initExpr->structName.toBasicString())), ctx);
FString(std::format("<StructInstance {}>", structName.toBasicString())), ctx);
/*
3 ways of calling constructor
.1 Person {"Fig", 1, "IDK"};
@@ -869,7 +870,7 @@ namespace Fig
u8"StructFieldTypeMismatchError",
std::format(
"In structure '{}', field '{}' expects type '{}', but got type '{}'",
initExpr->structName.toBasicString(),
structName.toBasicString(),
fieldName.toBasicString(),
expectedType.toString().toBasicString(),
prettyType(defaultVal).toBasicString()),
@@ -886,7 +887,7 @@ namespace Fig
throw EvaluatorError(
u8"StructFieldTypeMismatchError",
std::format("In structure '{}', field '{}' expects type '{}', but got type '{}'",
initExpr->structName.toBasicString(),
structName.toBasicString(),
fieldName.toBasicString(),
expectedType.toString().toBasicString(),
prettyType(argVal).toBasicString()),
@@ -907,7 +908,7 @@ namespace Fig
throw EvaluatorError(u8"StructFieldRedeclarationError",
std::format("Field '{}' already initialized in structure '{}'",
fieldName.toBasicString(),
initExpr->structName.toBasicString()),
structName.toBasicString()),
initExpr);
}
if (i + 1 > got)
@@ -925,7 +926,7 @@ namespace Fig
u8"StructFieldTypeMismatchError",
std::format(
"In structure '{}', field '{}' expects type '{}', but got type '{}'",
initExpr->structName.toBasicString(),
structName.toBasicString(),
fieldName.toBasicString(),
expectedType.toString().toBasicString(),
prettyType(defaultVal).toBasicString()),
@@ -941,7 +942,7 @@ namespace Fig
throw EvaluatorError(
u8"StructFieldTypeMismatchError",
std::format("In structure '{}', field '{}' expects type '{}', but got type '{}'",
initExpr->structName.toBasicString(),
structName.toBasicString(),
fieldName.toBasicString(),
field.type.toString().toBasicString(),
prettyType(argVal).toBasicString()),

189
src/Evaluator/main.cpp Normal file
View File

@@ -0,0 +1,189 @@
/*
███████████ █████ █████ ██████████ ███████████ █████ █████████ █████ █████████ ██████ █████ █████████ █████ █████ █████████ █████████ ██████████
░█░░░███░░░█░░███ ░░███ ░░███░░░░░█ ░░███░░░░░░█░░███ ███░░░░░███ ░░███ ███░░░░░███ ░░██████ ░░███ ███░░░░░███░░███ ░░███ ███░░░░░███ ███░░░░░███░░███░░░░░█
░ ░███ ░ ░███ ░███ ░███ █ ░ ░███ █ ░ ░███ ███ ░░░ ░███ ░███ ░███ ░███░███ ░███ ███ ░░░ ░███ ░███ ░███ ░███ ███ ░░░ ░███ █ ░
░███ ░███████████ ░██████ ░███████ ░███ ░███ ░███ ░███████████ ░███░░███░███ ░███ ░███ ░███ ░███████████ ░███ ░██████
░███ ░███░░░░░███ ░███░░█ ░███░░░█ ░███ ░███ █████ ░███ ░███░░░░░███ ░███ ░░██████ ░███ █████ ░███ ░███ ░███░░░░░███ ░███ █████ ░███░░█
░███ ░███ ░███ ░███ ░ █ ░███ ░ ░███ ░░███ ░░███ ░███ █ ░███ ░███ ░███ ░░█████ ░░███ ░░███ ░███ ░███ ░███ ░███ ░░███ ░░███ ░███ ░ █
█████ █████ █████ ██████████ █████ █████ ░░█████████ ███████████ █████ █████ █████ ░░█████ ░░█████████ ░░████████ █████ █████ ░░█████████ ██████████
░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
.---.
. __.....__ .--. | | _..._ __.....__
.'| .-'' '. _.._ |__| .--./) | | .' '. .--./) .--./) .-'' '.
.| < | / .-''"'-. `. .' .._|.--. /.''\\ | | . .-. . /.''\\ /.''\\ / .-''"'-. `.
.' |_ | | / /________\ \ | ' | || | | | | | __ | ' ' || | | | __ | | | |/ /________\ \
.' || | .'''-. | | __| |__ | | \`-' / | | .:--.'. | | | | \`-' / _ _ .:--.'. \`-' / | |
'--. .-'| |/.'''. \\ .-------------' |__ __| | | /("'` | |/ | \ | | | | | /("'` | ' / | / | \ | /("'` \ .-------------'
| | | / | | \ '-.____...---. | | | | \ '---. | |`" __ | | | | | | \ '---. .' | .' | `" __ | | \ '---. \ '-.____...---.
| | | | | | `. .' | | |__| /'""'.\ | | .'.''| | | | | | /'""'.\ / | / | .'.''| | /'""'.\ `. .'
| '.'| | | | `''-...... -' | | || || '---'/ / | |_| | | | || ||| `'. | / / | |_|| || `''-...... -'
| / | '. | '. | | \'. __// \ \._,\ '/| | | | \'. __// ' .'| '/\ \._,\ '/\'. __//
`'-' '---' '---' |_| `'---' `--' `" '--' '--' `'---' `-' `--' `--' `" `'---'
Copyright (C) 2020-2026 PuqiAR
This software is licensed under the MIT License. See LICENSE.txt for details.
*/
#include <Utils/argparse/argparse.hpp>
#include <print>
#include <fstream>
#include <Core/core.hpp>
#include <Lexer/lexer.hpp>
#include <Parser/parser.hpp>
#include <Evaluator/evaluator.hpp>
#include <Utils/AstPrinter.hpp>
#include <Utils/utils.hpp>
#include <Error/errorLog.hpp>
static size_t addressableErrorCount = 0;
static size_t unaddressableErrorCount = 0;
int main(int argc, char **argv)
{
argparse::ArgumentParser program("Fig Interpreter", Fig::Core::VERSION.data());
program.add_argument("source")
.help("source file to be interpreted")
.default_value(std::string(""));
// program.add_argument("-v", "--version")
// .help("get the version of Fig Interpreter")
// .default_value(false)
// .implicit_value(true);
// interpreter
try
{
program.parse_args(argc, argv);
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
return 1;
}
// if (program.get<bool>("--version"))
// {
// std::print("Fig Interpreter version {}\n", Fig::Core::VERSION);
// return 0;
// }
Fig::FString sourcePath(program.get<std::string>("source"));
if (sourcePath.empty())
{
std::cerr << "No source file provided.\n";
return 1;
}
std::ifstream file(sourcePath.toBasicString());
if (!file.is_open())
{
std::cerr << "Could not open file: " << sourcePath.toBasicString() << '\n';
return 1;
}
std::string source((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
Fig::Lexer lexer((Fig::FString(source)));
// Token tok;
// while ((tok = lexer.nextToken()).getType() != TokenType::EndOfFile)
// {
// std::println("{}", tok.toString().toBasicString());
// }
Fig::Parser parser(lexer);
std::vector<Fig::Ast::AstBase> asts;
std::vector<FString> sourceLines = Fig::Utils::splitSource(Fig::FString(source));
try
{
asts = parser.parseAll();
}
catch (const Fig::AddressableError &e)
{
addressableErrorCount++;
ErrorLog::logAddressableError(e, sourcePath, sourceLines);
return 1;
}
catch (const Fig::UnaddressableError &e)
{
unaddressableErrorCount++;
ErrorLog::logUnaddressableError(e);
return 1;
}
catch (const std::exception &e)
{
std::cerr << "uncaught exception of: " << e.what() << '\n';
return 1;
}
// AstPrinter printer;
// std::print("<Debug> AST:\n");
// for (const auto &node : ast)
// {
// printer.print(node);
// }
Fig::Evaluator evaluator;
evaluator.SetSourcePath(sourcePath);
evaluator.CreateGlobalContext();
evaluator.RegisterBuiltinsValue();
try
{
evaluator.Run(asts);
}
catch (const Fig::AddressableError &e)
{
addressableErrorCount++;
ErrorLog::logAddressableError(e, sourcePath, sourceLines);
evaluator.printStackTrace();
return 1;
}
catch (const Fig::UnaddressableError &e)
{
unaddressableErrorCount++;
ErrorLog::logUnaddressableError(e);
evaluator.printStackTrace();
return 1;
}
catch (const std::exception &e)
{
std::cerr << "uncaught exception of: " << e.what() << '\n';
evaluator.printStackTrace();
return 1;
}
// try
// {
// std::vector<Fig::Ast> ast = parser.parseAll();
// AstPrinter printer;
// std::print("<Debug> AST:\n");
// for (const auto &node : ast)
// {
// printer.print(node);
// }
// Fig::Evaluator evaluator(ast);
// evaluator.run();
// }
// catch (const Fig::AddressableError &e)
// {
// std::cerr << e.what() << '\n';
// return 1;
// }
// catch (const Fig::UnaddressableError &e)
// {
// std::cerr << e.what() << '\n';
// return 1;
// }
// catch (const std::exception &e)
// {
// std::cerr << e.what() << '\n';
// return 1;
// }
}