v0.4.3-alpha
[Fix] 修复builtin type的方法可能会导致 bad_variant_access的问题。现在Function严格区分3中函数:
Normal, Builtin, MemberType
[Feat] 增加了 Repl! 通过 Fig -r/--repl进入。目前必须写分号,且异常产生时因为获取不到源文件会有意想不到的问题 :(
[Impl] 精简了Context类,减少内存占用。当然,一些内置函数名获取不了了
后续的 Fig lang standard (目前还没有)中内置函数(MemberType/Builtin)严格无异常抛出
[Feat] 写了个Simple Compiler 和 Bytecode VM作为测试。性能拉跨。优化目前停止
[...] 剩下的忘了~
This commit is contained in:
@@ -131,9 +131,9 @@ namespace Fig::Ast
|
||||
return FString(std::format("<Base Ast '{}' at {}:{}>", typeName().toBasicString(), aai.line, aai.column));
|
||||
}
|
||||
|
||||
AstAddressInfo getAAI() { return aai; }
|
||||
AstAddressInfo getAAI() const { return aai; }
|
||||
|
||||
AstType getType() { return type; }
|
||||
AstType getType() const { return type; }
|
||||
};
|
||||
|
||||
class StatementAst : public _AstBase
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
|
||||
从树遍历到虚拟机!
|
||||
|
||||
*/
|
||||
|
||||
#include "Core/fig_string.hpp"
|
||||
#include "Utils/magic_enum/magic_enum.hpp"
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
#include <string_view>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
using OpCodeType = uint8_t;
|
||||
enum class Bytecode : OpCodeType
|
||||
{
|
||||
HALT = 0x01, // 程序终止,后跟 8 位退出码
|
||||
|
||||
POP = 0x10,
|
||||
|
||||
LOAD_NULL = 0x20,
|
||||
LOAD_TRUE = 0x21,
|
||||
LOAD_FALSE = 0x22,
|
||||
LOAD_CON8 = 0x23, // 跟 8 位索引 (0 - 255)
|
||||
LOAD_CON16 = 0x24, // 跟 16 位索引 (255 - 65535)
|
||||
LOAD_CON32 = 0x25, // 跟 32 位索引 (65536 - 2^32-1)
|
||||
|
||||
ADD = 0x30, // +
|
||||
SUB = 0x31, // -
|
||||
MUL = 0x32, // *
|
||||
DIV = 0x33, // /
|
||||
MOD = 0x34, // %
|
||||
|
||||
AND = 0x40, // &
|
||||
OR = 0x41, // |
|
||||
XOR = 0x42, // ~
|
||||
NOT = 0x43, // ! (not)
|
||||
|
||||
EQ = 0x50, // ==
|
||||
GT = 0x51, // >
|
||||
GTEQ = 0x52, // >=
|
||||
LT = 0x53, // <
|
||||
LTEQ = 0x54, // <=
|
||||
|
||||
JUMP16 = 0x60, // 跟 16 位索引 无条件
|
||||
JUMP32 = 0x61, // 跟 32 位索引 无条件
|
||||
|
||||
JUMP16_IF_TRUE = 0x62, // 跟 16 位索引 栈顶为真跳转
|
||||
JUMP32_IF_TRUE = 0x63, // 跟 32 位索引 栈顶为真跳转
|
||||
|
||||
JUMP16_IF_FALSE = 0x64, // 跟 16 位索引 栈顶为假跳转
|
||||
JUMP32_IF_FALSE = 0x65, // 跟 32 位索引 栈顶为假跳转
|
||||
|
||||
LOAD_LOCAL16 = 0x70, // 后跟 16 位索引
|
||||
LOAD_LOCAL32 = 0x71, // 后跟 32 位索引
|
||||
};
|
||||
|
||||
inline FString bytecode2string(Bytecode code)
|
||||
{
|
||||
const std::string_view &name = magic_enum::enum_name(code);
|
||||
return FString(FStringView(name));
|
||||
}
|
||||
|
||||
inline FString reverseCompile(const std::vector<uint8_t> &src)
|
||||
{
|
||||
assert(src.size() >= 1);
|
||||
|
||||
FString result;
|
||||
|
||||
using enum Bytecode;
|
||||
for (size_t i = 0; i < src.size();)
|
||||
{
|
||||
Bytecode code = Bytecode(src[i]);
|
||||
switch (code)
|
||||
{
|
||||
case HALT: {
|
||||
uint8_t quitCode = src[++i];
|
||||
result += FString(std::format("HALT {}", static_cast<uint8_t>(quitCode))) + u8"\n";
|
||||
break;
|
||||
}
|
||||
case LOAD_CON8: {
|
||||
uint8_t id = src[++i];
|
||||
result += FString(std::format("LOAD_CON8 {}", static_cast<uint8_t>(id))) + u8"\n";
|
||||
break;
|
||||
}
|
||||
case LOAD_CON16:
|
||||
case JUMP16:
|
||||
case JUMP16_IF_TRUE:
|
||||
case JUMP16_IF_FALSE:
|
||||
case LOAD_LOCAL16: {
|
||||
uint8_t high = src[++i];
|
||||
uint8_t low = src[++i];
|
||||
int32_t id = (high << 8) | low;
|
||||
result += FString(std::format("{} {}", bytecode2string(code).toBasicString(), id))
|
||||
+ u8"\n";
|
||||
break;
|
||||
}
|
||||
|
||||
case LOAD_CON32:
|
||||
case JUMP32:
|
||||
case JUMP32_IF_TRUE:
|
||||
case JUMP32_IF_FALSE:
|
||||
case LOAD_LOCAL32: {
|
||||
uint32_t b0 = static_cast<uint32_t>(src[++i]);
|
||||
uint32_t b1 = static_cast<uint32_t>(src[++i]);
|
||||
uint32_t b2 = static_cast<uint32_t>(src[++i]);
|
||||
uint32_t b3 = static_cast<uint32_t>(src[++i]);
|
||||
|
||||
uint32_t id = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
|
||||
result += FString(std::format("{} {}", bytecode2string(code).toBasicString(), id))
|
||||
+ u8"\n";
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
result += bytecode2string(code) + u8"\n";
|
||||
}
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}; // namespace Fig
|
||||
17
src/Bytecode/CallFrame.hpp
Normal file
17
src/Bytecode/CallFrame.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <Bytecode/CompiledFunction.hpp>
|
||||
#include <Bytecode/Instruction.hpp>
|
||||
#include <Bytecode/Chunk.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct CallFrame
|
||||
{
|
||||
uint64_t ip; // 函数第一个指令 index
|
||||
uint64_t base; // 第一个参数在栈中位置偏移量
|
||||
|
||||
CompiledFunction fn; // 编译过的函数体
|
||||
};
|
||||
|
||||
};
|
||||
25
src/Bytecode/Chunk.hpp
Normal file
25
src/Bytecode/Chunk.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <Core/fig_string.hpp>
|
||||
#include <Bytecode/Instruction.hpp>
|
||||
#include <Evaluator/Value/value.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct ChunkAddressInfo
|
||||
{
|
||||
FString sourcePath;
|
||||
std::vector<FString> sourceLines;
|
||||
};
|
||||
|
||||
struct Chunk
|
||||
{
|
||||
Instructions ins; // vector<Instruction>
|
||||
std::vector<Object> constants; // 常量池
|
||||
|
||||
std::vector<InstructionAddressInfo> instructions_addr; // 下标和ins对齐,表示每个Instruction对应的地址
|
||||
ChunkAddressInfo addr; // 代码块独立Addr
|
||||
};
|
||||
};
|
||||
22
src/Bytecode/CompileError.hpp
Normal file
22
src/Bytecode/CompileError.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <Error/error.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
class CompileError : public AddressableError
|
||||
{
|
||||
using AddressableError::AddressableError;
|
||||
|
||||
virtual FString toString() const override
|
||||
{
|
||||
std::string msg = std::format("[CompileError] {} in [{}] {}",
|
||||
this->message.toBasicString(),
|
||||
this->src_loc.file_name(),
|
||||
this->src_loc.function_name());
|
||||
return FString(msg);
|
||||
}
|
||||
|
||||
virtual FString getErrorType() const override { return FString(u8"CompileError"); }
|
||||
};
|
||||
};
|
||||
21
src/Bytecode/CompiledFunction.hpp
Normal file
21
src/Bytecode/CompiledFunction.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <Bytecode/Instruction.hpp>
|
||||
#include <Bytecode/Chunk.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct CompiledFunction
|
||||
{
|
||||
|
||||
Chunk chunk; // 函数代码块
|
||||
|
||||
FString name; // 函数名
|
||||
uint64_t posArgCount; // 位置参数数量
|
||||
uint64_t defArgCount; // 默认参数数量
|
||||
bool variadicPara; // 可变参数(是:最后为可变,否:不可变)
|
||||
|
||||
uint64_t localCount; // 局部变量数量(不包括参数)
|
||||
uint64_t slotCount; // = 总参数数量 + 局部变量数量
|
||||
};
|
||||
};
|
||||
69
src/Bytecode/Instruction.hpp
Normal file
69
src/Bytecode/Instruction.hpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
using u8 = uint8_t;
|
||||
enum class OpCode : u8
|
||||
{
|
||||
HALT = 0, // 程序结束
|
||||
RETURN,
|
||||
|
||||
LOAD_LOCAL,
|
||||
LOAD_CONST,
|
||||
|
||||
STORE_LOCAL,
|
||||
|
||||
LT,
|
||||
LTET,
|
||||
GT,
|
||||
GTET,
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
|
||||
JUMP, // + 64 offset (int64_t)
|
||||
JUMP_IF_FALSE, // + 64 offset (int64_t)
|
||||
|
||||
CALL,
|
||||
};
|
||||
|
||||
static constexpr int MAX_LOCAL_COUNT = UINT64_MAX;
|
||||
static constexpr int MAX_CONSTANT_COUNT = UINT64_MAX;
|
||||
static constexpr int MAX_FUNCTION_ARG_COUNT = UINT64_MAX;
|
||||
|
||||
inline OpCode getLastOpCode()
|
||||
{
|
||||
return OpCode::RETURN;
|
||||
}
|
||||
|
||||
struct InstructionAddressInfo
|
||||
{
|
||||
size_t line, column;
|
||||
};
|
||||
|
||||
using ProgramCounter = uint64_t;
|
||||
using InstructionPoint = uint64_t;
|
||||
|
||||
class Instruction
|
||||
{
|
||||
public:
|
||||
OpCode code;
|
||||
|
||||
int64_t operand;
|
||||
|
||||
Instruction(OpCode _code) : code(_code) {}
|
||||
|
||||
Instruction(OpCode _code, int64_t _operand)
|
||||
{
|
||||
code = _code;
|
||||
operand = _operand;
|
||||
}
|
||||
};
|
||||
|
||||
using Instructions = std::vector<Instruction>;
|
||||
}; // namespace Fig
|
||||
107
src/Bytecode/vm_test_main.cpp
Normal file
107
src/Bytecode/vm_test_main.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include <Bytecode/Chunk.hpp>
|
||||
#include <Bytecode/Instruction.hpp>
|
||||
#include <Bytecode/CompiledFunction.hpp>
|
||||
#include <VirtualMachine/VirtualMachine.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
using namespace Fig;
|
||||
|
||||
int main()
|
||||
{
|
||||
/*
|
||||
func fib(x)
|
||||
{
|
||||
if x <= 1
|
||||
{
|
||||
return x;
|
||||
}
|
||||
return fib(x - 1) + fib(x - 2);
|
||||
}
|
||||
*/
|
||||
|
||||
// ---------------- fib ----------------
|
||||
|
||||
Instructions fib_ins{
|
||||
/* 0 */ {OpCode::LOAD_LOCAL, 0}, // x
|
||||
/* 1 */ {OpCode::LOAD_CONST, 0}, // 1
|
||||
/* 2 */ {OpCode::LTET}, // x <= 1
|
||||
/* 3 */ {OpCode::JUMP_IF_FALSE, 2}, // false -> jump to 6
|
||||
|
||||
/* 4 */ {OpCode::LOAD_LOCAL, 0}, // return x
|
||||
/* 5 */ {OpCode::RETURN},
|
||||
|
||||
/* 6 */ {OpCode::LOAD_LOCAL, 0}, // x
|
||||
/* 7 */ {OpCode::LOAD_CONST, 0}, // 1
|
||||
/* 8 */ {OpCode::SUB}, // x - 1
|
||||
/* 9 */ {OpCode::LOAD_CONST, 2}, // fib
|
||||
/* 10 */ {OpCode::CALL, 1}, // fib(x-1)
|
||||
|
||||
/* 11 */ {OpCode::LOAD_LOCAL, 0}, // x
|
||||
/* 12 */ {OpCode::LOAD_CONST, 1}, // 2
|
||||
/* 13 */ {OpCode::SUB}, // x - 2
|
||||
/* 14 */ {OpCode::LOAD_CONST, 2}, // fib
|
||||
/* 15 */ {OpCode::CALL, 1}, // fib(x-2)
|
||||
|
||||
/* 16 */ {OpCode::ADD},
|
||||
/* 17 */ {OpCode::RETURN},
|
||||
};
|
||||
|
||||
std::vector<Object> fib_consts{
|
||||
Object((int64_t) 1), // 0
|
||||
Object((int64_t) 2), // 1
|
||||
Object(), // 2 fib (回填)
|
||||
};
|
||||
|
||||
CompiledFunction fib_fn{
|
||||
{},
|
||||
u8"fib",
|
||||
1, // posArgCount
|
||||
0,
|
||||
false,
|
||||
0, // localCount
|
||||
1 // slotCount = 参数 x
|
||||
};
|
||||
|
||||
// fib 自引用
|
||||
fib_consts[2] = Object(Function(&fib_fn));
|
||||
|
||||
Chunk fib_chunk{fib_ins, fib_consts, {}, ChunkAddressInfo{}};
|
||||
|
||||
fib_fn.chunk = fib_chunk;
|
||||
|
||||
// ---------------- main ----------------
|
||||
|
||||
Instructions main_ins{
|
||||
{OpCode::LOAD_CONST, 0}, // 30
|
||||
{OpCode::LOAD_CONST, 1}, // fib
|
||||
{OpCode::CALL, 1},
|
||||
{OpCode::RETURN},
|
||||
};
|
||||
|
||||
std::vector<Object> main_consts{
|
||||
Object((int64_t)251), // 0
|
||||
Object(Function(&fib_fn)), // 1
|
||||
};
|
||||
|
||||
Chunk main_chunk{main_ins, main_consts, {}, ChunkAddressInfo{}};
|
||||
|
||||
CompiledFunction main_fn{main_chunk, u8"main", 0, 0, false, 0, 0};
|
||||
|
||||
CallFrame entry{.ip = 0, .base = 0, .fn = main_fn};
|
||||
|
||||
VirtualMachine vm(entry);
|
||||
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
|
||||
auto start = Clock::now();
|
||||
Object result = vm.Execute();
|
||||
auto end = Clock::now();
|
||||
|
||||
auto duration_secs = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
|
||||
auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
||||
|
||||
std::cout << result.toString().toBasicString() << "\n";
|
||||
std::cout << "cost: " << duration_secs << "s. " << duration_ms << "ms" << "\n";
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
#include <Ast/astBase.hpp>
|
||||
#include <Compiler/Compiler.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
void Compiler::compile(Ast::Statement stmt)
|
||||
{
|
||||
using enum Ast::AstType;
|
||||
using namespace Ast;
|
||||
Ast::AstType type = stmt->getType();
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case VarDefSt: {
|
||||
auto vd = std::static_pointer_cast<VarDefAst>(stmt);
|
||||
const FString name = vd->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}; // namespace Fig
|
||||
@@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Ast/astBase.hpp"
|
||||
#include <Ast/ast.hpp>
|
||||
#include <Bytecode/Bytecode.hpp>
|
||||
#include <VMValue/VMValue.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
class Compiler
|
||||
{
|
||||
private:
|
||||
std::vector<Ast::Statement> source;
|
||||
std::vector<OpCodeType> output; // std::vector<uint8_t>
|
||||
|
||||
std::vector<Value> constants;
|
||||
public:
|
||||
std::vector<OpCodeType> getOutput() const { return output; }
|
||||
std::vector<Value> getConstantPool() const { return constants; }
|
||||
|
||||
Compiler() {}
|
||||
Compiler(std::vector<Ast::Statement> _source) : source(std::move(_source)) {}
|
||||
|
||||
void compile_expr(Ast::Expression);
|
||||
|
||||
void compile(Ast::Statement);
|
||||
|
||||
void CompileAll();
|
||||
};
|
||||
}; // namespace Fig
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cstdint>
|
||||
#include <string_view>
|
||||
|
||||
#define __FCORE_VERSION "0.4.2-alpha"
|
||||
#define __FCORE_VERSION "0.4.3-alpha"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define __FCORE_PLATFORM "Windows"
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace Fig
|
||||
|
||||
FString operator+(const FString &x)
|
||||
{
|
||||
return FString(toBasicString() + x.toBasicString());
|
||||
return FString(static_cast<std::u8string>(*this) + static_cast<std::u8string>(x));
|
||||
}
|
||||
FString operator+(const char8_t *c)
|
||||
{
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "Evaluator/Value/function.hpp"
|
||||
#include <Ast/Statements/InterfaceDefSt.hpp>
|
||||
#include <Evaluator/Value/interface.hpp>
|
||||
#include <Evaluator/Value/Type.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <unordered_map>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
@@ -30,8 +32,8 @@ namespace Fig
|
||||
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;
|
||||
// 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;
|
||||
@@ -51,14 +53,24 @@ namespace Fig
|
||||
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::unordered_map<size_t, Function> getFunctions() const
|
||||
{
|
||||
std::unordered_map<size_t, Function> result;
|
||||
for (auto &[name, slot] : variables)
|
||||
{
|
||||
if (slot->declaredType == ValueType::Function)
|
||||
{
|
||||
const Function &fn = slot->value->as<Function>();
|
||||
result[fn.id] = fn;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::shared_ptr<VariableSlot> get(const FString &name)
|
||||
{
|
||||
@@ -122,12 +134,6 @@ namespace Fig
|
||||
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>();
|
||||
@@ -144,26 +150,19 @@ namespace Fig
|
||||
}
|
||||
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
|
||||
for (auto &[name, slot] : variables)
|
||||
{
|
||||
return std::nullopt;
|
||||
if (slot->declaredType == ValueType::Function)
|
||||
{
|
||||
const Function &fn = slot->value->as<Function>();
|
||||
if (fn.id == id) { return name; }
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
// std::optional<FString> getStructName(std::size_t id)
|
||||
// {
|
||||
// auto it = structTypeNames.find(id);
|
||||
|
||||
@@ -11,27 +11,27 @@ namespace Fig
|
||||
{
|
||||
case AstType::ValueExpr: {
|
||||
auto val = std::static_pointer_cast<Ast::ValueExprAst>(exp);
|
||||
assert(val != nullptr);
|
||||
|
||||
return val->val;
|
||||
}
|
||||
case AstType::VarExpr: {
|
||||
auto varExpr = std::static_pointer_cast<Ast::VarExprAst>(exp);
|
||||
assert(varExpr != nullptr);
|
||||
|
||||
return evalVarExpr(varExpr, ctx).get(); // LvObject -> RvObject
|
||||
}
|
||||
case AstType::BinaryExpr: {
|
||||
auto bin = std::static_pointer_cast<Ast::BinaryExprAst>(exp);
|
||||
assert(bin != nullptr);
|
||||
|
||||
return evalBinary(bin, ctx);
|
||||
}
|
||||
case AstType::UnaryExpr: {
|
||||
auto un = std::static_pointer_cast<Ast::UnaryExprAst>(exp);
|
||||
assert(un != nullptr);
|
||||
|
||||
return evalUnary(un, ctx);
|
||||
}
|
||||
case AstType::TernaryExpr: {
|
||||
auto te = std::static_pointer_cast<Ast::TernaryExprAst>(exp);
|
||||
assert(te != nullptr);
|
||||
|
||||
return evalTernary(te, ctx);
|
||||
}
|
||||
case AstType::MemberExpr:
|
||||
@@ -39,7 +39,6 @@ namespace Fig
|
||||
|
||||
case AstType::FunctionCall: {
|
||||
auto fnCall = std::static_pointer_cast<Ast::FunctionCallExpr>(exp);
|
||||
assert(fnCall != nullptr);
|
||||
|
||||
Ast::Expression callee = fnCall->callee;
|
||||
ObjectPtr fnObj = eval(callee, ctx);
|
||||
@@ -58,19 +57,19 @@ namespace Fig
|
||||
auto fnNameOpt = ctx->getFunctionName(fnId);
|
||||
if (!fnNameOpt && fn.closureContext) fnNameOpt = fn.closureContext->getFunctionName(fnId);
|
||||
|
||||
const FString &fnName = (fnNameOpt ? *fnNameOpt : u8"<anonymous>");
|
||||
const FString &fnName = (fnNameOpt ? *fnNameOpt : u8"<anonymous> or builtin-type member function");
|
||||
|
||||
return evalFunctionCall(fn, fnCall->arg, fnName, ctx);
|
||||
}
|
||||
case AstType::FunctionLiteralExpr: {
|
||||
auto fnLiteral = std::static_pointer_cast<Ast::FunctionLiteralExprAst>(exp);
|
||||
assert(fnLiteral != nullptr);
|
||||
|
||||
|
||||
Ast::BlockStatement body = nullptr;
|
||||
if (fnLiteral->isExprMode())
|
||||
{
|
||||
Ast::Expression exprBody = fnLiteral->getExprBody();
|
||||
assert(exprBody != nullptr);
|
||||
|
||||
|
||||
const Ast::AstAddressInfo &aai = exprBody->getAAI();
|
||||
Ast::Return st = std::make_shared<Ast::ReturnSt>(exprBody);
|
||||
@@ -83,7 +82,7 @@ namespace Fig
|
||||
else
|
||||
{
|
||||
body = fnLiteral->getBlockBody();
|
||||
assert(body != nullptr);
|
||||
|
||||
}
|
||||
Function fn(fnLiteral->paras, ValueType::Any, body, ctx
|
||||
/*
|
||||
@@ -94,13 +93,13 @@ namespace Fig
|
||||
}
|
||||
case AstType::InitExpr: {
|
||||
auto initExpr = std::static_pointer_cast<Ast::InitExprAst>(exp);
|
||||
assert(initExpr != nullptr);
|
||||
|
||||
return evalInitExpr(initExpr, ctx);
|
||||
}
|
||||
|
||||
case AstType::ListExpr: {
|
||||
auto lstExpr = std::static_pointer_cast<Ast::ListExprAst>(exp);
|
||||
assert(lstExpr != nullptr);
|
||||
|
||||
|
||||
List list;
|
||||
for (auto &exp : lstExpr->val) { list.push_back(eval(exp, ctx)); }
|
||||
@@ -109,7 +108,7 @@ namespace Fig
|
||||
|
||||
case AstType::MapExpr: {
|
||||
auto mapExpr = std::static_pointer_cast<Ast::MapExprAst>(exp);
|
||||
assert(mapExpr != nullptr);
|
||||
|
||||
|
||||
Map map;
|
||||
for (auto &[key, value] : mapExpr->val) { map[eval(key, ctx)] = eval(value, ctx); }
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "Evaluator/Value/value.hpp"
|
||||
#include <Evaluator/Value/LvObject.hpp>
|
||||
#include <Evaluator/Value/IntPool.hpp>
|
||||
#include <Evaluator/evaluator.hpp>
|
||||
@@ -84,11 +85,19 @@ namespace Fig
|
||||
}
|
||||
case Operator::And: {
|
||||
ObjectPtr lhs = eval(lexp, ctx);
|
||||
if (lhs->is<bool>() && !isBoolObjectTruthy(lhs))
|
||||
{
|
||||
return Object::getFalseInstance(); // short-circuit
|
||||
}
|
||||
ObjectPtr rhs = eval(rexp, ctx);
|
||||
return std::make_shared<Object>(*lhs && *rhs);
|
||||
};
|
||||
case Operator::Or: {
|
||||
ObjectPtr lhs = eval(lexp, ctx);
|
||||
if (lhs->is<bool>() && isBoolObjectTruthy(lhs))
|
||||
{
|
||||
return Object::getTrueInstance();
|
||||
}
|
||||
ObjectPtr rhs = eval(rexp, ctx);
|
||||
return std::make_shared<Object>(*lhs || *rhs);
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <Evaluator/Value/function.hpp>
|
||||
#include <Evaluator/Value/LvObject.hpp>
|
||||
#include <Evaluator/evaluator.hpp>
|
||||
#include <Evaluator/evaluator_error.hpp>
|
||||
@@ -11,7 +12,7 @@ namespace Fig
|
||||
{
|
||||
const Function &fnStruct = fn;
|
||||
Ast::FunctionCallArgs evaluatedArgs;
|
||||
if (fnStruct.isBuiltin)
|
||||
if (fnStruct.type == Function::Builtin || fnStruct.type == Function::MemberType)
|
||||
{
|
||||
for (const auto &argExpr : fnArgs.argv) { evaluatedArgs.argv.push_back(eval(argExpr, ctx)); }
|
||||
if (fnStruct.builtinParamCount != -1 && fnStruct.builtinParamCount != evaluatedArgs.getLength())
|
||||
@@ -23,8 +24,15 @@ namespace Fig
|
||||
evaluatedArgs.getLength()),
|
||||
fnArgs.argv.back());
|
||||
}
|
||||
if (fnStruct.type == Function::Builtin)
|
||||
{
|
||||
return fnStruct.builtin(evaluatedArgs.argv);
|
||||
}
|
||||
else
|
||||
{
|
||||
return fnStruct.mtFn(nullptr, evaluatedArgs.argv); // wrapped member type function (`this` provided by evalMemberExpr)
|
||||
}
|
||||
}
|
||||
|
||||
// check argument, all types of parameters
|
||||
Ast::FunctionParameters fnParas = fnStruct.paras;
|
||||
@@ -126,7 +134,7 @@ namespace Fig
|
||||
paramName = fnParas.defParas[defParamIndex].first;
|
||||
paramType = TypeInfo(fnParas.defParas[defParamIndex].second.first);
|
||||
}
|
||||
AccessModifier argAm = AccessModifier::Const;
|
||||
AccessModifier argAm = AccessModifier::Normal;
|
||||
newContext->def(paramName, paramType, argAm, evaluatedArgs.argv[j]);
|
||||
}
|
||||
goto ExecuteBody;
|
||||
@@ -138,7 +146,7 @@ namespace Fig
|
||||
{
|
||||
list.push_back(eval(exp, ctx)); // eval arguments in current scope
|
||||
}
|
||||
newContext->def(fnParas.variadicPara, ValueType::List, AccessModifier::Const, std::make_shared<Object>(list));
|
||||
newContext->def(fnParas.variadicPara, ValueType::List, AccessModifier::Normal, std::make_shared<Object>(list));
|
||||
goto ExecuteBody;
|
||||
}
|
||||
|
||||
@@ -172,4 +180,4 @@ namespace Fig
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
};
|
||||
}; // namespace Fig
|
||||
@@ -288,11 +288,13 @@ namespace Fig
|
||||
[&argName](const Field &f) { return f.name == argName; });
|
||||
if (fieldIt == structT.fields.end())
|
||||
{
|
||||
throw EvaluatorError(u8"StructFieldNotFoundError",
|
||||
std::format("Field '{}' not found in structure '{}'",
|
||||
argName.toBasicString(),
|
||||
structName.toBasicString()),
|
||||
initExpr);
|
||||
// throw EvaluatorError(u8"StructFieldNotFoundError",
|
||||
// std::format("Field '{}' not found in structure '{}'",
|
||||
// argName.toBasicString(),
|
||||
// structName.toBasicString()),
|
||||
// initExpr);
|
||||
initExpr->initMode = Positional;
|
||||
return evalInitExpr(initExpr, ctx);
|
||||
}
|
||||
const Field &field = *fieldIt;
|
||||
if (!isTypeMatch(field.type, argVal, ctx))
|
||||
|
||||
@@ -35,7 +35,11 @@ namespace Fig
|
||||
{
|
||||
return LvObject(std::make_shared<VariableSlot>(
|
||||
member,
|
||||
std::make_shared<Object>(Function(baseVal->getMemberFunction(member),
|
||||
std::make_shared<Object>(Function(
|
||||
[baseVal, member](ObjectPtr self, std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
if (self) { return baseVal->getMemberFunction(member)(self, args); }
|
||||
return baseVal->getMemberFunction(member)(baseVal, args);
|
||||
},
|
||||
baseVal->getMemberFunctionParaCount(member))),
|
||||
ValueType::Function,
|
||||
AccessModifier::PublicConst),
|
||||
@@ -105,7 +109,7 @@ namespace Fig
|
||||
}
|
||||
LvObject Evaluator::evalIndexExpr(Ast::IndexExpr ie, ContextPtr ctx)
|
||||
{
|
||||
LvObject base = evalLv(ie->base, ctx);
|
||||
RvObject base = eval(ie->base, ctx);
|
||||
RvObject index = eval(ie->index, ctx);
|
||||
|
||||
const TypeInfo &type = base.get()->getTypeInfo();
|
||||
@@ -125,12 +129,12 @@ namespace Fig
|
||||
{
|
||||
throw EvaluatorError(
|
||||
u8"IndexOutOfRangeError",
|
||||
std::format("Index {} out of list `{}` range", indexVal, base.get()->toString().toBasicString()),
|
||||
std::format("Index {} out of list `{}` range", indexVal, base->toString().toBasicString()),
|
||||
ie->index);
|
||||
}
|
||||
return LvObject(base.get(), indexVal, LvObject::Kind::ListElement, ctx);
|
||||
return LvObject(base, indexVal, LvObject::Kind::ListElement, ctx);
|
||||
}
|
||||
else if (type == ValueType::Map) { return LvObject(base.get(), index, LvObject::Kind::MapElement, ctx); }
|
||||
else if (type == ValueType::Map) { return LvObject(base, index, LvObject::Kind::MapElement, ctx); }
|
||||
else if (type == ValueType::String)
|
||||
{
|
||||
if (index->getTypeInfo() != ValueType::Int)
|
||||
@@ -140,22 +144,22 @@ namespace Fig
|
||||
std::format("Type `String` indices must be `Int`, got '{}'", prettyType(index).toBasicString()),
|
||||
ie->index);
|
||||
}
|
||||
FString &string = base.get()->as<ValueType::StringClass>();
|
||||
FString &string = base->as<ValueType::StringClass>();
|
||||
ValueType::IntClass indexVal = index->as<ValueType::IntClass>();
|
||||
if (indexVal >= string.length())
|
||||
{
|
||||
throw EvaluatorError(
|
||||
u8"IndexOutOfRangeError",
|
||||
std::format("Index {} out of string `{}` range", indexVal, base.get()->toString().toBasicString()),
|
||||
std::format("Index {} out of string `{}` range", indexVal, base->toString().toBasicString()),
|
||||
ie->index);
|
||||
}
|
||||
return LvObject(base.get(), indexVal, LvObject::Kind::StringElement, ctx);
|
||||
return LvObject(base, indexVal, LvObject::Kind::StringElement, ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw EvaluatorError(
|
||||
u8"NoSubscriptableError",
|
||||
std::format("`{}` object is not subscriptable", base.declaredType().toString().toBasicString()),
|
||||
std::format("`{}` object is not subscriptable", base->getTypeInfo().toString().toBasicString()),
|
||||
ie->base);
|
||||
}
|
||||
}
|
||||
@@ -167,17 +171,17 @@ namespace Fig
|
||||
{
|
||||
case AstType::VarExpr: {
|
||||
Ast::VarExpr var = std::static_pointer_cast<Ast::VarExprAst>(exp);
|
||||
assert(var != nullptr);
|
||||
|
||||
return evalVarExpr(var, ctx);
|
||||
}
|
||||
case AstType::MemberExpr: {
|
||||
Ast::MemberExpr me = std::static_pointer_cast<Ast::MemberExprAst>(exp);
|
||||
assert(me != nullptr);
|
||||
|
||||
return evalMemberExpr(me, ctx);
|
||||
}
|
||||
case AstType::IndexExpr: {
|
||||
Ast::IndexExpr ie = std::static_pointer_cast<Ast::IndexExprAst>(exp);
|
||||
assert(ie != nullptr);
|
||||
|
||||
return evalIndexExpr(ie, ctx);
|
||||
}
|
||||
default: {
|
||||
@@ -188,4 +192,4 @@ namespace Fig
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}; // namespace Fig
|
||||
@@ -13,12 +13,11 @@ namespace Fig
|
||||
{
|
||||
case ImportSt: {
|
||||
auto i = std::static_pointer_cast<Ast::ImportSt>(stmt);
|
||||
assert(i != nullptr);
|
||||
return evalImportSt(i, ctx);
|
||||
}
|
||||
case VarDefSt: {
|
||||
auto varDef = std::static_pointer_cast<Ast::VarDefAst>(stmt);
|
||||
assert(varDef != nullptr);
|
||||
|
||||
|
||||
if (ctx->containsInThisScope(varDef->name))
|
||||
{
|
||||
@@ -63,7 +62,7 @@ namespace Fig
|
||||
|
||||
case FunctionDefSt: {
|
||||
auto fnDef = std::static_pointer_cast<Ast::FunctionDefSt>(stmt);
|
||||
assert(fnDef != nullptr);
|
||||
|
||||
|
||||
const FString &fnName = fnDef->name;
|
||||
if (ctx->containsInThisScope(fnName))
|
||||
@@ -90,7 +89,7 @@ namespace Fig
|
||||
|
||||
case StructSt: {
|
||||
auto stDef = std::static_pointer_cast<Ast::StructDefSt>(stmt);
|
||||
assert(stDef != nullptr);
|
||||
|
||||
|
||||
if (ctx->containsInThisScope(stDef->name))
|
||||
{
|
||||
@@ -149,7 +148,7 @@ namespace Fig
|
||||
|
||||
case InterfaceDefSt: {
|
||||
auto ifd = std::static_pointer_cast<Ast::InterfaceDefAst>(stmt);
|
||||
assert(ifd != nullptr);
|
||||
|
||||
|
||||
const FString &interfaceName = ifd->name;
|
||||
|
||||
@@ -170,7 +169,7 @@ namespace Fig
|
||||
|
||||
case ImplementSt: {
|
||||
auto ip = std::static_pointer_cast<Ast::ImplementAst>(stmt);
|
||||
assert(ip != nullptr);
|
||||
|
||||
|
||||
TypeInfo structType(ip->structName);
|
||||
TypeInfo interfaceType(ip->interfaceName);
|
||||
@@ -407,7 +406,7 @@ namespace Fig
|
||||
|
||||
case TrySt: {
|
||||
auto tryst = std::static_pointer_cast<Ast::TrySt>(stmt);
|
||||
assert(tryst != nullptr);
|
||||
|
||||
|
||||
ContextPtr tryCtx = std::make_shared<Context>(
|
||||
FString(std::format("<Try at {}:{}>", tryst->getAAI().line, tryst->getAAI().column)), ctx);
|
||||
@@ -446,7 +445,7 @@ namespace Fig
|
||||
|
||||
case ThrowSt: {
|
||||
auto ts = std::static_pointer_cast<Ast::ThrowSt>(stmt);
|
||||
assert(ts != nullptr);
|
||||
|
||||
|
||||
ObjectPtr value = eval(ts->value, ctx);
|
||||
if (value->is<ValueType::NullClass>())
|
||||
@@ -458,7 +457,7 @@ namespace Fig
|
||||
|
||||
case ReturnSt: {
|
||||
auto returnSt = std::static_pointer_cast<Ast::ReturnSt>(stmt);
|
||||
assert(returnSt != nullptr);
|
||||
|
||||
|
||||
ObjectPtr returnValue = Object::getNullInstance(); // default is null
|
||||
if (returnSt->retValue) returnValue = eval(returnSt->retValue, ctx);
|
||||
@@ -491,14 +490,12 @@ namespace Fig
|
||||
|
||||
case ExpressionStmt: {
|
||||
auto exprStmt = std::static_pointer_cast<Ast::ExpressionStmtAst>(stmt);
|
||||
assert(exprStmt != nullptr);
|
||||
|
||||
return StatementResult::normal(eval(exprStmt->exp, ctx));
|
||||
}
|
||||
|
||||
case BlockStatement: {
|
||||
auto block = std::static_pointer_cast<Ast::BlockStatementAst>(stmt);
|
||||
assert(block != nullptr);
|
||||
|
||||
|
||||
ContextPtr blockCtx = std::make_shared<Context>(
|
||||
FString(std::format("<Block at {}:{}>", block->getAAI().line, block->getAAI().column)), ctx);
|
||||
|
||||
@@ -30,6 +30,11 @@ namespace Fig
|
||||
if (val >= CACHE_MIN && val <= CACHE_MAX) { return cache[val - CACHE_MIN]; }
|
||||
return std::make_shared<Object>(val);
|
||||
}
|
||||
Object createIntCopy(ValueType::IntClass val) const
|
||||
{
|
||||
if (val >= CACHE_MIN && val <= CACHE_MAX) { return *cache[val - CACHE_MIN]; }
|
||||
return Object(val);
|
||||
}
|
||||
|
||||
static const IntPool &getInstance()
|
||||
{
|
||||
|
||||
@@ -11,51 +11,91 @@
|
||||
namespace Fig
|
||||
{
|
||||
class Object;
|
||||
|
||||
class Function
|
||||
{
|
||||
public:
|
||||
std::size_t id;
|
||||
|
||||
enum FnType
|
||||
{
|
||||
Normal,
|
||||
Builtin,
|
||||
MemberType
|
||||
} type;
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
Ast::FunctionParameters paras;
|
||||
TypeInfo retType;
|
||||
Ast::BlockStatement body;
|
||||
|
||||
bool isBuiltin = false;
|
||||
Ast::BlockStatement body;
|
||||
};
|
||||
std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)> builtin;
|
||||
std::function<std::shared_ptr<Object>(std::shared_ptr<Object>,
|
||||
const std::vector<std::shared_ptr<Object>> &)>
|
||||
mtFn;
|
||||
};
|
||||
|
||||
int builtinParamCount = -1;
|
||||
|
||||
std::shared_ptr<Context> closureContext;
|
||||
|
||||
// ===== Constructors =====
|
||||
Function() :
|
||||
id(nextId()) {}
|
||||
Function() : id(nextId()) {}
|
||||
|
||||
Function(Ast::FunctionParameters _paras, TypeInfo _retType, Ast::BlockStatement _body, ContextPtr _closureContext) :
|
||||
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))
|
||||
{
|
||||
type = Normal;
|
||||
}
|
||||
|
||||
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) {}
|
||||
id(nextId()), type(Builtin), builtin(fn), builtinParamCount(argc)
|
||||
{
|
||||
type = Builtin;
|
||||
}
|
||||
|
||||
Function(std::function<std::shared_ptr<Object>(std::shared_ptr<Object>,
|
||||
const std::vector<std::shared_ptr<Object>> &)> fn,
|
||||
int argc) :
|
||||
id(nextId()), type(MemberType), mtFn(fn), builtinParamCount(argc)
|
||||
{
|
||||
type = MemberType;
|
||||
}
|
||||
|
||||
// ===== Copy / Move =====
|
||||
Function(const Function &other) = default;
|
||||
Function(Function &&) noexcept = default;
|
||||
Function &operator=(const Function &) = default;
|
||||
Function &operator=(Function &&) noexcept = default;
|
||||
Function(const Function &other)
|
||||
{
|
||||
copyFrom(other);
|
||||
}
|
||||
Function &operator=(const Function &other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
destroy();
|
||||
copyFrom(other);
|
||||
}
|
||||
return *this;
|
||||
};
|
||||
|
||||
~Function()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
// ===== Comparison =====
|
||||
bool operator==(const Function &other) const noexcept
|
||||
{
|
||||
return id == other.id;
|
||||
}
|
||||
bool operator!=(const Function &other) const noexcept
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
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()
|
||||
@@ -63,5 +103,43 @@ namespace Fig
|
||||
static std::atomic<std::size_t> counter{1};
|
||||
return counter++;
|
||||
}
|
||||
void destroy()
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case Normal:
|
||||
paras.~FunctionParameters();
|
||||
retType.~TypeInfo();
|
||||
body.~shared_ptr();
|
||||
break;
|
||||
case Builtin: builtin.~function(); break;
|
||||
case MemberType: mtFn.~function(); break;
|
||||
}
|
||||
}
|
||||
|
||||
void copyFrom(const Function &other)
|
||||
{
|
||||
type = other.type;
|
||||
id = nextId(); // 每个复制都生成新的ID
|
||||
builtinParamCount = other.builtinParamCount;
|
||||
closureContext = other.closureContext;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case Normal:
|
||||
new (¶s) Ast::FunctionParameters(other.paras);
|
||||
new (&retType) TypeInfo(other.retType);
|
||||
new (&body) Ast::BlockStatement(other.body);
|
||||
break;
|
||||
case Builtin:
|
||||
new (&builtin) std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)>(
|
||||
other.builtin);
|
||||
break;
|
||||
case MemberType:
|
||||
new (&mtFn) std::function<std::shared_ptr<Object>(
|
||||
std::shared_ptr<Object>, const std::vector<std::shared_ptr<Object>> &)>(other.mtFn);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace Fig
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include "Core/fig_string.hpp"
|
||||
#include <Evaluator/Value/function.hpp>
|
||||
#include <Evaluator/Value/interface.hpp>
|
||||
#include <Evaluator/Value/structType.hpp>
|
||||
@@ -8,6 +9,8 @@
|
||||
#include <Evaluator/Value/module.hpp>
|
||||
#include <Evaluator/Value/value_forward.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
#include <cmath>
|
||||
@@ -41,26 +44,18 @@ namespace Fig
|
||||
struct Element
|
||||
{
|
||||
ObjectPtr value;
|
||||
Element(ObjectPtr _value) :
|
||||
value(_value) {}
|
||||
Element(ObjectPtr _value) : value(_value) {}
|
||||
|
||||
bool operator==(const Element &other) const
|
||||
{
|
||||
return *value == *other.value;
|
||||
}
|
||||
bool operator==(const Element &other) const { return *value == *other.value; }
|
||||
|
||||
void deepCopy(const Element &e)
|
||||
{
|
||||
value = std::make_shared<Object>(*e.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) {}
|
||||
ValueKey(ObjectPtr _value) : value(_value) {}
|
||||
|
||||
void deepCopy(const ValueKey &vk) { value = std::make_shared<Object>(*vk.value); }
|
||||
};
|
||||
@@ -74,11 +69,12 @@ namespace Fig
|
||||
bool isTypeMatch(const TypeInfo &, ObjectPtr, ContextPtr);
|
||||
bool implements(const TypeInfo &, const TypeInfo &, ContextPtr);
|
||||
|
||||
using BuiltinTypeMemberFn = std::function<ObjectPtr(ObjectPtr, std::vector<ObjectPtr>)>;
|
||||
|
||||
class Object : public std::enable_shared_from_this<Object>
|
||||
{
|
||||
public:
|
||||
using VariantType = std::variant<
|
||||
ValueType::NullClass,
|
||||
using VariantType = std::variant<ValueType::NullClass,
|
||||
ValueType::IntClass,
|
||||
ValueType::DoubleClass,
|
||||
ValueType::StringClass,
|
||||
@@ -91,58 +87,59 @@ namespace Fig
|
||||
Module,
|
||||
InterfaceType>;
|
||||
|
||||
std::unordered_map<TypeInfo,
|
||||
std::unordered_map<FString,
|
||||
std::function<ObjectPtr(std::vector<ObjectPtr>)>>,
|
||||
TypeInfoHash>
|
||||
|
||||
|
||||
static std::unordered_map<TypeInfo, std::unordered_map<FString, BuiltinTypeMemberFn>, TypeInfoHash> getMemberTypeFunctions()
|
||||
{
|
||||
static const std::unordered_map<TypeInfo, std::unordered_map<FString, BuiltinTypeMemberFn>, TypeInfoHash>
|
||||
memberTypeFunctions{
|
||||
{ValueType::Null, {}},
|
||||
{ValueType::Int, {}},
|
||||
{ValueType::Double, {}},
|
||||
{ValueType::String, {
|
||||
{u8"length", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
{ValueType::String,
|
||||
{
|
||||
{u8"length",
|
||||
[](ObjectPtr object, 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>();
|
||||
throw RuntimeError(
|
||||
FString(std::format("`length` expects 0 arguments, {} got", args.size())));
|
||||
const FString &str = object->as<ValueType::StringClass>();
|
||||
return std::make_shared<Object>(static_cast<ValueType::IntClass>(str.length()));
|
||||
}},
|
||||
{u8"replace", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
{u8"replace",
|
||||
[](ObjectPtr object, 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>();
|
||||
throw RuntimeError(
|
||||
FString(std::format("`replace` expects 2 arguments, {} got", args.size())));
|
||||
FString &str = object->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"));
|
||||
throw RuntimeError(FString("`replace` arg 1 expects type Int"));
|
||||
}
|
||||
if (!arg2->is<ValueType::StringClass>())
|
||||
{
|
||||
throw RuntimeError(FString(
|
||||
"`replace` arg 2 expects type String"));
|
||||
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 {
|
||||
{u8"erase",
|
||||
[](ObjectPtr object, 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>();
|
||||
throw RuntimeError(
|
||||
FString(std::format("`erase` expects 2 arguments, {} got", args.size())));
|
||||
FString &str = object->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"));
|
||||
throw RuntimeError(FString("`erase` arg 1 expects type Int"));
|
||||
}
|
||||
if (!arg2->is<ValueType::IntClass>())
|
||||
{
|
||||
throw RuntimeError(FString(
|
||||
"`erase` arg 2 expects type Int"));
|
||||
throw RuntimeError(FString("`erase` arg 2 expects type Int"));
|
||||
}
|
||||
ValueType::IntClass index = arg1->as<ValueType::IntClass>();
|
||||
ValueType::IntClass n = arg2->as<ValueType::IntClass>();
|
||||
@@ -157,22 +154,21 @@ namespace Fig
|
||||
str.realErase(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::IntClass>());
|
||||
return Object::getNullInstance();
|
||||
}},
|
||||
{u8"insert", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
{u8"insert",
|
||||
[](ObjectPtr object, 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>();
|
||||
throw RuntimeError(
|
||||
FString(std::format("`insert` expects 2 arguments, {} got", args.size())));
|
||||
FString &str = object->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"));
|
||||
throw RuntimeError(FString("`insert` arg 1 expects type Int"));
|
||||
}
|
||||
if (!arg2->is<ValueType::StringClass>())
|
||||
{
|
||||
throw RuntimeError(FString(
|
||||
"`insert` arg 2 expects type String"));
|
||||
throw RuntimeError(FString("`insert` arg 2 expects type String"));
|
||||
}
|
||||
str.realInsert(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::StringClass>());
|
||||
return Object::getNullInstance();
|
||||
@@ -181,67 +177,81 @@ namespace Fig
|
||||
{ValueType::Function, {}},
|
||||
{ValueType::StructType, {}},
|
||||
{ValueType::StructInstance, {}},
|
||||
{ValueType::List, {
|
||||
{u8"length", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
{ValueType::List,
|
||||
{
|
||||
{u8"length",
|
||||
[](ObjectPtr object, 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>();
|
||||
throw RuntimeError(
|
||||
FString(std::format("`length` expects 0 arguments, {} got", args.size())));
|
||||
const List &list = object->as<List>();
|
||||
return std::make_shared<Object>(static_cast<ValueType::IntClass>(list.size()));
|
||||
}},
|
||||
{u8"get", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
{u8"get",
|
||||
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
if (args.size() != 1)
|
||||
throw RuntimeError(FString(
|
||||
std::format("`get` expects 1 arguments, {} got", args.size())));
|
||||
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())));
|
||||
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();
|
||||
const List &list = object->as<List>();
|
||||
if (i >= list.size()) return Object::getNullInstance();
|
||||
return list[i].value;
|
||||
}},
|
||||
{u8"push", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
{u8"push",
|
||||
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
if (args.size() != 1)
|
||||
throw RuntimeError(FString(
|
||||
std::format("`push` expects 1 arguments, {} got", args.size())));
|
||||
throw RuntimeError(
|
||||
FString(std::format("`push` expects 1 arguments, {} got", args.size())));
|
||||
ObjectPtr arg = args[0];
|
||||
List &list = as<List>();
|
||||
List &list = object->as<List>();
|
||||
list.push_back(arg);
|
||||
return Object::getNullInstance();
|
||||
}},
|
||||
}},
|
||||
{ValueType::Map, {
|
||||
{u8"get", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
{ValueType::Map,
|
||||
{
|
||||
{u8"get",
|
||||
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
if (args.size() != 1)
|
||||
throw RuntimeError(FString(
|
||||
std::format("`get` expects 1 arguments, {} got", args.size())));
|
||||
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();
|
||||
const Map &map = object->as<Map>();
|
||||
if (!map.contains(index)) return Object::getNullInstance();
|
||||
return map.at(index);
|
||||
}},
|
||||
{u8"contains", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
{u8"contains",
|
||||
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
if (args.size() != 1)
|
||||
throw RuntimeError(FString(
|
||||
std::format("`contains` expects 1 arguments, {} got", args.size())));
|
||||
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));
|
||||
const Map &map = object->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{
|
||||
return memberTypeFunctions;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash> getMemberTypeFunctionsParas()
|
||||
{
|
||||
static const std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash>
|
||||
memberTypeFunctionsParas{
|
||||
{ValueType::Null, {}},
|
||||
{ValueType::Int, {}},
|
||||
{ValueType::Double, {}},
|
||||
{ValueType::String, {
|
||||
{ValueType::String,
|
||||
{
|
||||
{u8"length", 0},
|
||||
{u8"replace", 2},
|
||||
{u8"erase", 2},
|
||||
@@ -251,54 +261,45 @@ namespace Fig
|
||||
{ValueType::StructType, {}},
|
||||
{ValueType::StructInstance, {}},
|
||||
{ValueType::List, {{u8"length", 0}, {u8"get", 1}, {u8"push", 1}}},
|
||||
{ValueType::Map, {
|
||||
{ValueType::Map,
|
||||
{
|
||||
{u8"get", 1},
|
||||
{u8"contains", 1},
|
||||
}},
|
||||
{ValueType::Module, {}},
|
||||
{ValueType::InterfaceType, {}},
|
||||
};
|
||||
return memberTypeFunctionsParas;
|
||||
}
|
||||
|
||||
bool hasMemberFunction(const FString &name) const
|
||||
{
|
||||
return memberTypeFunctions.at(getTypeInfo()).contains(name);
|
||||
return getMemberTypeFunctions().at(getTypeInfo()).contains(name);
|
||||
}
|
||||
std::function<ObjectPtr(std::vector<ObjectPtr>)> getMemberFunction(const FString &name) const
|
||||
BuiltinTypeMemberFn getMemberFunction(const FString &name) const
|
||||
{
|
||||
return memberTypeFunctions.at(getTypeInfo()).at(name);
|
||||
return getMemberTypeFunctions().at(getTypeInfo()).at(name);
|
||||
}
|
||||
int getMemberFunctionParaCount(const FString &name) const
|
||||
{
|
||||
return memberTypeFunctionsParas.at(getTypeInfo()).at(name);
|
||||
return getMemberTypeFunctionsParas().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() : 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;
|
||||
@@ -359,7 +360,8 @@ namespace Fig
|
||||
|
||||
TypeInfo getTypeInfo() const
|
||||
{
|
||||
return std::visit([](auto &&val) -> TypeInfo {
|
||||
return std::visit(
|
||||
[](auto &&val) -> TypeInfo {
|
||||
using T = std::decay_t<decltype(val)>;
|
||||
|
||||
if constexpr (std::is_same_v<T, ValueType::NullClass>)
|
||||
@@ -427,13 +429,12 @@ namespace Fig
|
||||
{
|
||||
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::DoubleClass>()) return FString(std::format("{:g}", 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>())));
|
||||
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(),
|
||||
@@ -449,8 +450,7 @@ namespace Fig
|
||||
bool first_flag = true;
|
||||
for (auto &ele : list)
|
||||
{
|
||||
if (!first_flag)
|
||||
output += u8", ";
|
||||
if (!first_flag) output += u8", ";
|
||||
output += ele.value->toString();
|
||||
first_flag = false;
|
||||
}
|
||||
@@ -464,8 +464,7 @@ namespace Fig
|
||||
bool first_flag = true;
|
||||
for (auto &[key, value] : map)
|
||||
{
|
||||
if (!first_flag)
|
||||
output += u8", ";
|
||||
if (!first_flag) output += u8", ";
|
||||
output += key.value->toString() + FString(u8" : ") + value->toString();
|
||||
first_flag = false;
|
||||
}
|
||||
@@ -474,15 +473,13 @@ namespace Fig
|
||||
}
|
||||
if (is<Module>())
|
||||
{
|
||||
return FString(std::format(
|
||||
"<Module '{}' at {:p}>",
|
||||
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}>",
|
||||
return FString(std::format("<InterfaceType '{}' at {:p}>",
|
||||
as<InterfaceType>().type.toString().toBasicString(),
|
||||
static_cast<const void *>(&as<InterfaceType>())));
|
||||
}
|
||||
@@ -490,8 +487,8 @@ namespace Fig
|
||||
}
|
||||
|
||||
private:
|
||||
static std::string makeTypeErrorMessage(const char *prefix, const char *op,
|
||||
const Object &lhs, const Object &rhs)
|
||||
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();
|
||||
@@ -508,8 +505,7 @@ namespace Fig
|
||||
{
|
||||
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
|
||||
auto result = lhs.getNumericValue() + rhs.getNumericValue();
|
||||
if (bothInt)
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
if (bothInt) return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(result);
|
||||
}
|
||||
if (lhs.is<ValueType::StringClass>() && rhs.is<ValueType::StringClass>())
|
||||
@@ -525,8 +521,7 @@ namespace Fig
|
||||
{
|
||||
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
|
||||
auto result = lhs.getNumericValue() - rhs.getNumericValue();
|
||||
if (bothInt)
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
if (bothInt) return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(result);
|
||||
}
|
||||
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "-", lhs, rhs)));
|
||||
@@ -540,18 +535,14 @@ namespace Fig
|
||||
{
|
||||
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
|
||||
auto result = lhs.getNumericValue() * rhs.getNumericValue();
|
||||
if (bothInt)
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
if (bothInt) return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(result);
|
||||
}
|
||||
if (lhs.is<ValueType::StringClass>() && rhs.is<ValueType::IntClass>())
|
||||
{
|
||||
FString result;
|
||||
const FString &l = lhs.as<ValueType::StringClass>();
|
||||
for (size_t i=0; i < rhs.getNumericValue(); ++i)
|
||||
{
|
||||
result += l;
|
||||
}
|
||||
for (size_t i = 0; i < rhs.getNumericValue(); ++i) { result += l; }
|
||||
return Object(result);
|
||||
}
|
||||
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "*", lhs, rhs)));
|
||||
@@ -564,8 +555,7 @@ namespace Fig
|
||||
if (lhs.isNumeric() && rhs.isNumeric())
|
||||
{
|
||||
auto rnv = rhs.getNumericValue();
|
||||
if (rnv == 0)
|
||||
throw ValueError(FString(makeTypeErrorMessage("Division by zero", "/", lhs, rhs)));
|
||||
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)
|
||||
@@ -597,8 +587,7 @@ namespace Fig
|
||||
if (lhs.isNumeric() && rhs.isNumeric())
|
||||
{
|
||||
auto rnv = rhs.getNumericValue();
|
||||
if (rnv == 0)
|
||||
throw ValueError(FString(makeTypeErrorMessage("Modulo by zero", "/", lhs, rhs)));
|
||||
if (rnv == 0) throw ValueError(FString(makeTypeErrorMessage("Modulo by zero", "/", lhs, rhs)));
|
||||
auto result = std::fmod(lhs.getNumericValue(), rnv);
|
||||
return Object(result);
|
||||
}
|
||||
@@ -623,25 +612,25 @@ namespace Fig
|
||||
friend Object operator!(const Object &v)
|
||||
{
|
||||
if (!v.is<ValueType::BoolClass>())
|
||||
throw ValueError(FString(std::format("Logical NOT requires bool: '{}'", v.getTypeInfo().name.toBasicString())));
|
||||
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())));
|
||||
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())));
|
||||
throw ValueError(
|
||||
FString(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
|
||||
return Object(~v.as<ValueType::IntClass>());
|
||||
}
|
||||
|
||||
@@ -690,7 +679,8 @@ namespace Fig
|
||||
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())));
|
||||
throw ValueError(
|
||||
FString(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
|
||||
return Object(~v.as<ValueType::IntClass>());
|
||||
}
|
||||
|
||||
@@ -716,14 +706,19 @@ namespace Fig
|
||||
{
|
||||
bool bothInt = base.is<ValueType::IntClass>() && exp.is<ValueType::IntClass>();
|
||||
auto result = std::pow(base.getNumericValue(), exp.getNumericValue());
|
||||
if (bothInt)
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
if (bothInt) return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(result);
|
||||
}
|
||||
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "**", base, exp)));
|
||||
}
|
||||
};
|
||||
|
||||
inline bool isBoolObjectTruthy(ObjectPtr obj)
|
||||
{
|
||||
assert(obj->is<bool>());
|
||||
return obj->as<bool>();
|
||||
}
|
||||
|
||||
using RvObject = ObjectPtr;
|
||||
|
||||
inline bool operator==(const ValueKey &l, const ValueKey &r)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
#include <Ast/Expressions/InitExpr.hpp>
|
||||
#include <Ast/Statements/ImplementSt.hpp>
|
||||
#include <Ast/Statements/InterfaceDefSt.hpp>
|
||||
|
||||
62
src/IR/IR.hpp
Normal file
62
src/IR/IR.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <Core/fig_string.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
namespace Fig::IR
|
||||
{
|
||||
using Reg = uint16_t;
|
||||
using Label = uint32_t;
|
||||
|
||||
constexpr Reg INVALID_REG = 0xFFFF;
|
||||
|
||||
enum class Op : uint8_t
|
||||
{
|
||||
// ---- control ----
|
||||
Nop,
|
||||
Jmp,
|
||||
Br, // conditional branch
|
||||
Ret,
|
||||
Call,
|
||||
|
||||
// ---- arithmetic ----
|
||||
Add,
|
||||
Sub,
|
||||
Mul,
|
||||
Div,
|
||||
|
||||
// ---- compare ----
|
||||
Lt,
|
||||
Le,
|
||||
Gt,
|
||||
Ge,
|
||||
Eq,
|
||||
|
||||
// ---- data ----
|
||||
LoadImm, // immediate -> reg
|
||||
Mov,
|
||||
};
|
||||
|
||||
struct Inst
|
||||
{
|
||||
Op op;
|
||||
|
||||
Reg dst; // 结果寄存器
|
||||
Reg a; // operand a
|
||||
Reg b; // operand b
|
||||
|
||||
int64_t imm; // immediate / jump offset
|
||||
};
|
||||
|
||||
struct Function
|
||||
{
|
||||
FString name;
|
||||
|
||||
uint16_t paramCount;
|
||||
uint16_t localCount; // 不含参数
|
||||
uint16_t regCount; // param + locals + temps
|
||||
|
||||
std::vector<Inst> code;
|
||||
};
|
||||
};
|
||||
74
src/IR/IRInterpreter.hpp
Normal file
74
src/IR/IRInterpreter.hpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
|
||||
#include <IR/IR.hpp>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace Fig::IR
|
||||
{
|
||||
struct VirtualMachine
|
||||
{
|
||||
std::vector<Function *> functions;
|
||||
|
||||
int64_t execute(Function *fn, const int64_t *args)
|
||||
{
|
||||
assert(fn != nullptr);
|
||||
std::vector<int64_t> regs(fn->regCount);
|
||||
// load params
|
||||
|
||||
size_t ip = 0;
|
||||
|
||||
while (ip < fn->code.size())
|
||||
{
|
||||
const Inst &ins = fn->code[ip++];
|
||||
|
||||
switch (ins.op)
|
||||
{
|
||||
case Op::Nop: break;
|
||||
|
||||
case Op::LoadImm: regs[ins.dst] = ins.imm; break;
|
||||
|
||||
case Op::Mov: regs[ins.dst] = regs[ins.a]; break;
|
||||
|
||||
case Op::Add: regs[ins.dst] = regs[ins.a] + regs[ins.b]; break;
|
||||
|
||||
case Op::Sub: regs[ins.dst] = regs[ins.a] - regs[ins.b]; break;
|
||||
|
||||
case Op::Mul: regs[ins.dst] = regs[ins.a] * regs[ins.b]; break;
|
||||
|
||||
case Op::Div: regs[ins.dst] = regs[ins.a] / regs[ins.b]; break;
|
||||
|
||||
case Op::Lt: regs[ins.dst] = regs[ins.a] < regs[ins.b]; break;
|
||||
|
||||
case Op::Le: regs[ins.dst] = regs[ins.a] <= regs[ins.b]; break;
|
||||
|
||||
case Op::Gt: regs[ins.dst] = regs[ins.a] > regs[ins.b]; break;
|
||||
|
||||
case Op::Ge: regs[ins.dst] = regs[ins.a] >= regs[ins.b]; break;
|
||||
|
||||
case Op::Eq: regs[ins.dst] = regs[ins.a] == regs[ins.b]; break;
|
||||
|
||||
case Op::Jmp: ip = static_cast<size_t>(static_cast<int64_t>(ip) + ins.imm); break;
|
||||
|
||||
case Op::Br:
|
||||
if (regs[ins.a] == 0) { ip = static_cast<size_t>(static_cast<int64_t>(ip) + ins.imm); }
|
||||
break;
|
||||
|
||||
case Op::Call: {
|
||||
// currently supports 1-arg call via reg a
|
||||
Function *callee = functions.at(static_cast<size_t>(ins.imm));
|
||||
int64_t arg0 = regs[ins.a];
|
||||
int64_t ret = execute(callee, &arg0);
|
||||
regs[ins.dst] = ret;
|
||||
break;
|
||||
}
|
||||
|
||||
case Op::Ret: return regs[ins.a];
|
||||
}
|
||||
}
|
||||
|
||||
// unreachable normally
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}; // namespace Fig::IR
|
||||
14
src/IR/ir_test_main.cpp
Normal file
14
src/IR/ir_test_main.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <IR/IRInterpreter.hpp>
|
||||
#include <IR/IR.hpp>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace Fig;
|
||||
IR::VirtualMachine vm;
|
||||
|
||||
using namespace IR;
|
||||
IR::Inst fib_ir[] = {
|
||||
|
||||
};
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <Token/token.hpp>
|
||||
#include <Lexer/lexer.hpp>
|
||||
|
||||
#include <Core/fig_string.hpp>
|
||||
#include <Utils/utils.hpp>
|
||||
|
||||
#if 0
|
||||
@@ -201,6 +200,11 @@ namespace Fig
|
||||
next();
|
||||
str += u8"\"";
|
||||
}
|
||||
else if (ec == U'r')
|
||||
{
|
||||
next();
|
||||
str += u8"\r";
|
||||
}
|
||||
else if (ec == U'\'')
|
||||
{
|
||||
next();
|
||||
|
||||
65
src/Repl/Repl.cpp
Normal file
65
src/Repl/Repl.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "Ast/astBase.hpp"
|
||||
#include "Error/error.hpp"
|
||||
#include "Error/errorLog.hpp"
|
||||
#include <Core/fig_string.hpp>
|
||||
#include <Repl/Repl.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
void Repl::Start() const
|
||||
{
|
||||
ostream << getPrompt() << "\n";
|
||||
|
||||
const FString &sourcePath = u8"<stdin>";
|
||||
const std::vector<FString> &sourceLines{};
|
||||
|
||||
Evaluator evaluator;
|
||||
|
||||
evaluator.CreateGlobalContext();
|
||||
evaluator.RegisterBuiltinsValue();
|
||||
evaluator.SetSourcePath(sourcePath);
|
||||
evaluator.SetSourceLines(sourceLines);
|
||||
|
||||
while (true)
|
||||
{
|
||||
ostream << "\r\n>>";
|
||||
const FString &line = readline();
|
||||
|
||||
if (line.empty())
|
||||
{
|
||||
ostream << Object::getNullInstance()->toString().toBasicString();
|
||||
continue;
|
||||
}
|
||||
if (line == u8"!exit")
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Lexer lexer(line, sourcePath, sourceLines);
|
||||
Parser parser(lexer, sourcePath, sourceLines);
|
||||
|
||||
std::vector<AstBase> program;
|
||||
try
|
||||
{
|
||||
program = parser.parseAll();
|
||||
|
||||
StatementResult sr = evaluator.Run(program);
|
||||
ObjectPtr result = sr.result;
|
||||
ostream << result->toString().toBasicString() << '\n';
|
||||
}
|
||||
catch (AddressableError &e)
|
||||
{
|
||||
ostream << "Oops!\n";
|
||||
ErrorLog::logAddressableError(e);
|
||||
ostream << "\n";
|
||||
}
|
||||
catch (UnaddressableError &e)
|
||||
{
|
||||
ostream << "Oops!\n";
|
||||
ErrorLog::logUnaddressableError(e);
|
||||
ostream << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace Fig
|
||||
54
src/Repl/Repl.hpp
Normal file
54
src/Repl/Repl.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#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>
|
||||
#include <Core/runtimeTime.hpp>
|
||||
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
class Repl
|
||||
{
|
||||
private:
|
||||
std::istream &istream;
|
||||
std::ostream &ostream;
|
||||
|
||||
public:
|
||||
Repl(std::istream &_istream = std::cin, std::ostream &_ostream = std::cout) :
|
||||
istream(_istream), ostream(_ostream)
|
||||
{
|
||||
}
|
||||
|
||||
FString readline() const
|
||||
{
|
||||
std::string buf;
|
||||
std::getline(istream, buf);
|
||||
|
||||
return FString(buf);
|
||||
}
|
||||
|
||||
static std::string getPrompt()
|
||||
{
|
||||
static const std::string prompt = std::format("Fig {} ({})[{} {}-bit on `{}`]\n",
|
||||
Core::VERSION,
|
||||
Core::COMPILE_TIME,
|
||||
Core::COMPILER,
|
||||
Core::ARCH,
|
||||
Core::PLATFORM)
|
||||
+ "Type '!exit' to exit\n" + "feel free to type!";
|
||||
return prompt;
|
||||
}
|
||||
|
||||
void Start() const;
|
||||
};
|
||||
|
||||
}; // namespace Fig
|
||||
@@ -1,29 +0,0 @@
|
||||
#include <VMValue/VMValue.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
FString Value::toString() const
|
||||
{
|
||||
if (static_cast<int>(type) > static_cast<int>(Double))
|
||||
{
|
||||
return heap->toString();
|
||||
}
|
||||
switch (type)
|
||||
{
|
||||
case Null:
|
||||
return u8"null";
|
||||
case Bool:
|
||||
return (b ? u8"true" : u8"false");
|
||||
case Int:
|
||||
return FString(std::to_string(i));
|
||||
case Double:
|
||||
return FString(std::to_string(d));
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,91 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
value system for virtual machine
|
||||
not Evaluator value
|
||||
*/
|
||||
|
||||
#include <Core/fig_string.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct HeapObject;
|
||||
|
||||
struct Value
|
||||
{
|
||||
struct NullObject {};
|
||||
|
||||
enum _Type : uint8_t
|
||||
{
|
||||
Null = 0,
|
||||
Bool,
|
||||
Int,
|
||||
Double,
|
||||
|
||||
/* Complex types, alloc in heap*/
|
||||
String,
|
||||
// Function,
|
||||
// StructType,
|
||||
// StructInstance,
|
||||
// List,
|
||||
// Map,
|
||||
// Module,
|
||||
// InterfaceType
|
||||
} type;
|
||||
|
||||
union
|
||||
{
|
||||
NullObject n;
|
||||
bool b;
|
||||
int64_t i;
|
||||
double d;
|
||||
|
||||
HeapObject *heap;
|
||||
};
|
||||
|
||||
Value()
|
||||
{
|
||||
type = Null;
|
||||
n = NullObject();
|
||||
}
|
||||
|
||||
Value(bool _b)
|
||||
{
|
||||
type = Bool;
|
||||
b = _b;
|
||||
}
|
||||
|
||||
Value(int64_t _i)
|
||||
{
|
||||
type = Int;
|
||||
i = _i;
|
||||
}
|
||||
|
||||
Value(double _d)
|
||||
{
|
||||
type = Double;
|
||||
d = _d;
|
||||
}
|
||||
|
||||
FString toString() const;
|
||||
};
|
||||
|
||||
struct HeapObject
|
||||
{
|
||||
Value::_Type type; // > 3
|
||||
|
||||
virtual FString toString() const = 0;
|
||||
};
|
||||
|
||||
struct String : HeapObject
|
||||
{
|
||||
FString value;
|
||||
|
||||
virtual FString toString() const override
|
||||
{
|
||||
return value;
|
||||
}
|
||||
};
|
||||
};
|
||||
225
src/VirtualMachine/VirtualMachine.cpp
Normal file
225
src/VirtualMachine/VirtualMachine.cpp
Normal file
@@ -0,0 +1,225 @@
|
||||
#include <Evaluator/Value/value.hpp>
|
||||
#include <Evaluator/Value/Type.hpp>
|
||||
|
||||
#include <Bytecode/Instruction.hpp>
|
||||
#include <Bytecode/CompiledFunction.hpp>
|
||||
#include <VirtualMachine/VirtualMachine.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
Object VirtualMachine::Execute()
|
||||
{
|
||||
while (currentFrame->ip < currentFrame->fn.chunk.ins.size())
|
||||
{
|
||||
Instruction ins = currentFrame->fn.chunk.ins[currentFrame->ip++];
|
||||
|
||||
switch (ins.code)
|
||||
{
|
||||
case OpCode::HALT: {
|
||||
return *Object::getNullInstance();
|
||||
}
|
||||
case OpCode::RETURN: {
|
||||
const Object &ret = pop();
|
||||
|
||||
uint64_t base = currentFrame->base;
|
||||
popFrame();
|
||||
|
||||
if (frames.empty())
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
stack.resize(base); // 清除函数的临时值
|
||||
push(ret);
|
||||
break;
|
||||
}
|
||||
case OpCode::LOAD_LOCAL: {
|
||||
uint64_t operand = static_cast<uint64_t>(ins.operand);
|
||||
// LOCAL编号都为正数
|
||||
|
||||
push(stack[currentFrame->base + operand]);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::LOAD_CONST: {
|
||||
uint64_t operand = static_cast<uint64_t>(ins.operand);
|
||||
// CONST编号都为正数
|
||||
|
||||
push(currentFrame->fn.chunk.constants[operand]);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::STORE_LOCAL: {
|
||||
uint64_t operand = static_cast<uint64_t>(ins.operand);
|
||||
// LOCAL编号都为正数
|
||||
|
||||
stack[currentFrame->base + operand] = pop();
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::LT: {
|
||||
const Object &rhs = pop();
|
||||
const Object &lhs = pop();
|
||||
|
||||
Object result = lhs < rhs;
|
||||
push(result);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::LTET: {
|
||||
const Object &rhs = pop();
|
||||
const Object &lhs = pop();
|
||||
|
||||
Object result = lhs <= rhs;
|
||||
push(result);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::GT: {
|
||||
const Object &rhs = pop();
|
||||
const Object &lhs = pop();
|
||||
|
||||
Object result = lhs > rhs;
|
||||
push(result);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::GTET: {
|
||||
const Object &rhs = pop();
|
||||
const Object &lhs = pop();
|
||||
|
||||
Object result = lhs >= rhs;
|
||||
push(result);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::ADD: {
|
||||
const Object &rhs = pop();
|
||||
const Object &lhs = pop();
|
||||
|
||||
if (lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>())
|
||||
{
|
||||
ValueType::IntClass result = lhs.as<ValueType::IntClass>() + rhs.as<ValueType::IntClass>();
|
||||
push(Object(result));
|
||||
break;
|
||||
}
|
||||
|
||||
Object result = lhs + rhs;
|
||||
push(result);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::SUB: {
|
||||
const Object &rhs = pop();
|
||||
const Object &lhs = pop();
|
||||
|
||||
if (lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>())
|
||||
{
|
||||
ValueType::IntClass result = lhs.as<ValueType::IntClass>() - rhs.as<ValueType::IntClass>();
|
||||
push(Object(result));
|
||||
break;
|
||||
}
|
||||
|
||||
Object result = lhs - rhs;
|
||||
push(result);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::MUL: {
|
||||
const Object &rhs = pop();
|
||||
const Object &lhs = pop();
|
||||
|
||||
if (lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>())
|
||||
{
|
||||
ValueType::IntClass result = lhs.as<ValueType::IntClass>() * rhs.as<ValueType::IntClass>();
|
||||
push(Object(result));
|
||||
break;
|
||||
}
|
||||
|
||||
Object result = lhs * rhs;
|
||||
push(result);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::DIV: {
|
||||
const Object &rhs = pop();
|
||||
const Object &lhs = pop();
|
||||
|
||||
if (lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>())
|
||||
{
|
||||
ValueType::DoubleClass result = (double)lhs.as<ValueType::IntClass>() / (double)rhs.as<ValueType::IntClass>();
|
||||
push(Object(result));
|
||||
break;
|
||||
}
|
||||
|
||||
Object result = lhs / rhs;
|
||||
push(result);
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::JUMP: {
|
||||
int64_t target = ins.operand;
|
||||
currentFrame->ip += target;
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::JUMP_IF_FALSE: {
|
||||
const Object &cond = pop();
|
||||
|
||||
if (!cond.is<bool>())
|
||||
{
|
||||
throw RuntimeError(
|
||||
FString(u8"Condition must be boolean!")
|
||||
);
|
||||
}
|
||||
if (!cond.as<bool>())
|
||||
{
|
||||
// cond is falsity
|
||||
int64_t target = ins.operand;
|
||||
currentFrame->ip += target;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case OpCode::CALL:
|
||||
{
|
||||
uint16_t argCount = static_cast<uint16_t>(ins.operand); // number of max arg is UINT16_MAX
|
||||
|
||||
const Object &obj = stack.back();
|
||||
if (!obj.is<Function>())
|
||||
{
|
||||
throw RuntimeError(FString(std::format("{} is not callable", obj.toString().toBasicString())));
|
||||
}
|
||||
|
||||
// const Function &fn_obj = obj.as<Function>();
|
||||
// assert(fn_obj.isCompiled && "function must be compiled");
|
||||
|
||||
CompiledFunction fn;
|
||||
|
||||
assert(stack.size() >= argCount && "stack does not have enough arguments");
|
||||
assert(fn.slotCount >= argCount && "slotCount < argCount");
|
||||
|
||||
uint64_t base = stack.size() - 1 - argCount;
|
||||
|
||||
pop(); // pop function
|
||||
|
||||
for (int64_t i = 0; i < fn.slotCount - argCount; ++i)
|
||||
{
|
||||
push(*Object::getNullInstance());
|
||||
}
|
||||
|
||||
CallFrame newFrame
|
||||
{
|
||||
0,
|
||||
base, // 参数已经加载到stack, base为第一个参数
|
||||
fn
|
||||
};
|
||||
|
||||
addFrame(newFrame);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return *Object::getNullInstance();
|
||||
}
|
||||
}; // namespace Fig
|
||||
75
src/VirtualMachine/VirtualMachine.hpp
Normal file
75
src/VirtualMachine/VirtualMachine.hpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include <Evaluator/Value/value.hpp>
|
||||
#include <Bytecode/CallFrame.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
class VirtualMachine
|
||||
{
|
||||
private:
|
||||
std::vector<CallFrame> frames;
|
||||
CallFrame *currentFrame;
|
||||
|
||||
std::vector<Object> stack;
|
||||
Object *stack_top;
|
||||
|
||||
public:
|
||||
void Clean()
|
||||
{
|
||||
frames.clear();
|
||||
stack.clear();
|
||||
|
||||
currentFrame = nullptr;
|
||||
stack_top = nullptr;
|
||||
}
|
||||
|
||||
void addFrame(const CallFrame &_frame)
|
||||
{
|
||||
frames.push_back(_frame);
|
||||
currentFrame = &frames.back();
|
||||
}
|
||||
|
||||
CallFrame popFrame()
|
||||
{
|
||||
assert((!frames.empty()) && "frames is empty!");
|
||||
|
||||
CallFrame back = *currentFrame;
|
||||
frames.pop_back();
|
||||
currentFrame = (frames.empty() ? nullptr : &frames.back());
|
||||
|
||||
return back;
|
||||
}
|
||||
|
||||
void push(const Object &_object)
|
||||
{
|
||||
stack.push_back(_object);
|
||||
stack_top = &stack.back();
|
||||
}
|
||||
|
||||
void nextIns(CallFrame &frame) const
|
||||
{
|
||||
frame.ip += 1;
|
||||
}
|
||||
|
||||
Object pop()
|
||||
{
|
||||
assert((!stack.empty()) && "stack is empty!");
|
||||
|
||||
Object back = *stack_top;
|
||||
stack.pop_back();
|
||||
stack_top = (stack.empty() ? nullptr : &stack.back());
|
||||
|
||||
return back;
|
||||
}
|
||||
|
||||
VirtualMachine(const CallFrame &_frame)
|
||||
{
|
||||
addFrame(_frame);
|
||||
}
|
||||
|
||||
Object Execute();
|
||||
};
|
||||
};
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
|
||||
███████████ █████ █████ ██████████ ███████████ █████ █████████ █████ █████████ ██████ █████ █████████ █████ █████ █████████ █████████ ██████████
|
||||
░█░░░███░░░█░░███ ░░███ ░░███░░░░░█ ░░███░░░░░░█░░███ ███░░░░░███ ░░███ ███░░░░░███ ░░██████ ░░███ ███░░░░░███░░███ ░░███ ███░░░░░███ ███░░░░░███░░███░░░░░█
|
||||
░ ░███ ░ ░███ ░███ ░███ █ ░ ░███ █ ░ ░███ ███ ░░░ ░███ ░███ ░███ ░███░███ ░███ ███ ░░░ ░███ ░███ ░███ ░███ ███ ░░░ ░███ █ ░
|
||||
░███ ░███████████ ░██████ ░███████ ░███ ░███ ░███ ░███████████ ░███░░███░███ ░███ ░███ ░███ ░███████████ ░███ ░██████
|
||||
░███ ░███░░░░░███ ░███░░█ ░███░░░█ ░███ ░███ █████ ░███ ░███░░░░░███ ░███ ░░██████ ░███ █████ ░███ ░███ ░███░░░░░███ ░███ █████ ░███░░█
|
||||
░███ ░███ ░███ ░███ ░ █ ░███ ░ ░███ ░░███ ░░███ ░███ █ ░███ ░███ ░███ ░░█████ ░░███ ░░███ ░███ ░███ ░███ ░███ ░░███ ░░███ ░███ ░ █
|
||||
█████ █████ █████ ██████████ █████ █████ ░░█████████ ███████████ █████ █████ █████ ░░█████ ░░█████████ ░░████████ █████ █████ ░░█████████ ██████████
|
||||
░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
|
||||
|
||||
Copyright (C) 2020-2026 PuqiAR
|
||||
|
||||
This software is licensed under the MIT License. See LICENSE for details.
|
||||
*/
|
||||
|
||||
|
||||
// DO NOT USE CLANG-FORMAT FOR THIS FILE
|
||||
|
||||
#include <Bytecode/Bytecode.hpp>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace Fig;
|
||||
|
||||
|
||||
std::vector<OpCodeType> src;
|
||||
|
||||
{
|
||||
using enum Bytecode;
|
||||
src = {
|
||||
0x21, // LOAD_TRUE
|
||||
0x23, 0x7f, // LOAD_CON8 0x7f
|
||||
0x24, 0x12, 0x34, // LOAD_CON16 0x1234
|
||||
0x25, 0x12, 0x34, 0x56, 0x78, // LOAD_CON32 0x12345678
|
||||
0x63, 0x12, 0x34, 0x56, 0x78 // JUMP32_IF_TRUE
|
||||
};
|
||||
|
||||
FString r = reverseCompile(src);
|
||||
std::cout << r.toBasicString() << std::endl;
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,7 @@ This software is licensed under the MIT License. See LICENSE.txt for details.
|
||||
#include <Utils/utils.hpp>
|
||||
#include <Error/errorLog.hpp>
|
||||
#include <Core/runtimeTime.hpp>
|
||||
#include <Repl/Repl.hpp>
|
||||
|
||||
static size_t addressableErrorCount = 0;
|
||||
static size_t unaddressableErrorCount = 0;
|
||||
@@ -54,6 +55,10 @@ int main(int argc, char **argv)
|
||||
program.add_argument("source")
|
||||
.help("source file to be interpreted")
|
||||
.default_value(std::string(""));
|
||||
program.add_argument("-r", "--repl")
|
||||
.help("start repl")
|
||||
.default_value(false)
|
||||
.implicit_value(true);
|
||||
// program.add_argument("-v", "--version")
|
||||
// .help("get the version of Fig Interpreter")
|
||||
// .default_value(false)
|
||||
@@ -74,6 +79,14 @@ int main(int argc, char **argv)
|
||||
// std::print("Fig Interpreter version {}\n", Fig::Core::VERSION);
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
if (program.get<bool>("--repl"))
|
||||
{
|
||||
Fig::Repl repl;
|
||||
repl.Start();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
Fig::FString sourcePath(program.get<std::string>("source"));
|
||||
if (sourcePath.empty())
|
||||
{
|
||||
60
xmake.lua
60
xmake.lua
@@ -3,8 +3,6 @@ add_rules("plugin.compile_commands.autoupdate", {outputdir = ".vscode"})
|
||||
|
||||
set_policy("run.autobuild", false)
|
||||
|
||||
target("Fig")
|
||||
set_kind("binary")
|
||||
set_languages("c++23")
|
||||
|
||||
add_ldflags("-static", {force = true})
|
||||
@@ -36,48 +34,32 @@ target("Fig")
|
||||
add_files("src/Module/builtins.cpp")
|
||||
|
||||
add_files("src/Evaluator/Value/value.cpp")
|
||||
add_files("src/Evaluator/Core/*.cpp")
|
||||
add_files("src/Evaluator/evaluator.cpp")
|
||||
add_files("src/Evaluator/main.cpp")
|
||||
|
||||
add_includedirs("src")
|
||||
-- add_includedirs("src/Evaluator")
|
||||
|
||||
set_warnings("all")
|
||||
|
||||
add_defines("__FCORE_COMPILE_TIME=\"" .. os.date("%Y-%m-%d %H:%M:%S") .. "\"")
|
||||
|
||||
-- -- Bytecode VM target
|
||||
-- target("Figc")
|
||||
-- set_kind("binary")
|
||||
-- set_languages("c++23")
|
||||
target("Fig")
|
||||
set_kind("binary")
|
||||
|
||||
-- add_ldflags("-static", {force = true})
|
||||
-- if is_plat("linux") then
|
||||
-- -- Linux: clang + libc++
|
||||
-- set_toolchains("clang")
|
||||
-- add_cxxflags("-stdlib=libc++")
|
||||
-- add_ldflags("-stdlib=libc++")
|
||||
-- elseif is_plat("windows") then
|
||||
-- -- 1. CI cross (Linux -> Windows)
|
||||
-- -- 2. local dev (Windows + llvm-mingw)
|
||||
-- set_toolchains("mingw") -- llvm-mingw
|
||||
-- add_ldflags("-Wl,--stack,268435456")
|
||||
-- -- set_toolchains("clang")
|
||||
-- -- static lib
|
||||
-- -- add_ldflags("-target x86_64-w64-mingw32", "-static")
|
||||
-- -- add_cxxflags("-stdlib=libc++")
|
||||
-- -- add_ldflags("-stdlib=libc++")
|
||||
-- end
|
||||
add_files("src/Evaluator/Core/*.cpp")
|
||||
add_files("src/VirtualMachine/VirtualMachine.cpp")
|
||||
add_files("src/Evaluator/evaluator.cpp")
|
||||
add_files("src/Repl/Repl.cpp")
|
||||
add_files("src/main.cpp")
|
||||
|
||||
-- add_includedirs("src/Evaluator")
|
||||
-- add_files("src/Evaluator/Value/value.cpp")
|
||||
-- add_files("src/VirtualMachine/main.cpp")
|
||||
-- add_files("src/Core/warning.cpp")
|
||||
-- add_files("src/Lexer/lexer.cpp")
|
||||
-- add_files("src/Parser/parser.cpp")
|
||||
set_warnings("all")
|
||||
|
||||
-- add_includedirs("src")
|
||||
-- set_warnings("all")
|
||||
target("vm_test_main")
|
||||
set_kind("binary")
|
||||
|
||||
-- add_defines("__FCORE_COMPILE_TIME=\"" .. os.date("%Y-%m-%d %H:%M:%S") .. "\"")
|
||||
add_files("src/VirtualMachine/VirtualMachine.cpp")
|
||||
add_files("src/Bytecode/vm_test_main.cpp")
|
||||
|
||||
set_warnings("all")
|
||||
|
||||
target("ir_test_main")
|
||||
set_kind("binary")
|
||||
|
||||
add_files("src/IR/ir_test_main.cpp")
|
||||
|
||||
set_warnings("all")
|
||||
Reference in New Issue
Block a user