挺大的改动。增加 as运算符,转换不了抛出 TypeError。import语法更新。修复try一点错误。现在表达式运算返回ExprResult。通过3个宏实现简便错误传播与解包 unwrap
This commit is contained in:
@@ -31,6 +31,27 @@ public interface Error
|
||||
getErrorMessage() -> String;
|
||||
}
|
||||
|
||||
public struct TypeError
|
||||
{
|
||||
public msg: String;
|
||||
}
|
||||
|
||||
impl Error for TypeError
|
||||
{
|
||||
toString()
|
||||
{
|
||||
return "TypeError: " + getErrorMessage();
|
||||
}
|
||||
getErrorClass()
|
||||
{
|
||||
return "TypeError";
|
||||
}
|
||||
getErrorMessage()
|
||||
{
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
// Operation interface
|
||||
|
||||
public interface Operation
|
||||
|
||||
@@ -3,7 +3,12 @@
|
||||
Library/std/std.fig
|
||||
*/
|
||||
|
||||
import io; // link std.io
|
||||
import value; // link std.type
|
||||
import math; // link std.math
|
||||
import tester; // link std.tester
|
||||
import io as std_io;
|
||||
import value as std_value;
|
||||
import math as std_math;
|
||||
import test as std_test;
|
||||
|
||||
public const io := std_io; // link std.io
|
||||
public const value := std_value; // link std.type
|
||||
public const math := std_math; // link std.math
|
||||
public const test := std_test; // link std.test
|
||||
@@ -1,13 +1,42 @@
|
||||
#include "Ast/Expressions/BinaryExpr.hpp"
|
||||
#include "Ast/Expressions/FunctionCall.hpp"
|
||||
#include "Ast/Expressions/ValueExpr.hpp"
|
||||
#include "Ast/Expressions/VarExpr.hpp"
|
||||
#include "Ast/Statements/ControlSt.hpp"
|
||||
#include "Ast/astBase.hpp"
|
||||
#include "Ast/functionParameters.hpp"
|
||||
#include <Evaluator/Context/context.hpp>
|
||||
#include <Core/fig_string.hpp>
|
||||
#include <Ast/AccessModifier.hpp>
|
||||
#include <Evaluator/Value/structType.hpp>
|
||||
#include <Evaluator/Value/value.hpp>
|
||||
#include <Module/builtins.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <print>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <chrono>
|
||||
#include <numeric>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Fig::Builtins
|
||||
{
|
||||
const TypeInfo &getErrorInterfaceTypeInfo()
|
||||
{
|
||||
static const TypeInfo ErrorInterfaceTypeInfo(u8"Error", true);
|
||||
return ErrorInterfaceTypeInfo;
|
||||
}
|
||||
const TypeInfo &getTypeErrorStructTypeInfo()
|
||||
{
|
||||
static const TypeInfo TypeErrorStructTypeInfo(u8"TypeError", true);
|
||||
return TypeErrorStructTypeInfo;
|
||||
}
|
||||
const TypeInfo &getOperationInterfaceTypeInfo()
|
||||
{
|
||||
static const TypeInfo OperationInterfaceTypeInfo(u8"Operation", true);
|
||||
return OperationInterfaceTypeInfo;
|
||||
}
|
||||
const std::unordered_map<FString, ObjectPtr> &getBuiltinValues()
|
||||
{
|
||||
static const std::unordered_map<FString, ObjectPtr> builtinValues = {
|
||||
@@ -28,7 +57,12 @@ namespace Fig::Builtins
|
||||
Ast::FunctionParameters({}, {}),
|
||||
std::make_shared<Ast::VarExprAst>(u8"String"),
|
||||
nullptr)}))},
|
||||
{u8"Operation", std::make_shared<Object>(InterfaceType(getOperationInterfaceTypeInfo(), {}))},
|
||||
{u8"TypeError", std::make_shared<Object>(StructType(
|
||||
getTypeErrorStructTypeInfo(),
|
||||
std::make_shared<Context>(u8"<Built-in `TypeError`>"),
|
||||
{Field(AccessModifier::Public, u8"msg", ValueType::String, nullptr)}
|
||||
))},
|
||||
{u8"Operation", std::make_shared<Object>(InterfaceType(getOperationInterfaceTypeInfo(), {}))},
|
||||
|
||||
{u8"Any", std::make_shared<Object>(StructType(ValueType::Any, nullptr, {}, true))},
|
||||
{u8"Int", std::make_shared<Object>(StructType(ValueType::Int, nullptr, {}, true))},
|
||||
@@ -119,8 +153,8 @@ namespace Fig::Builtins
|
||||
}},
|
||||
{u8"__fvalue_type",
|
||||
[](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
return std::make_shared<Object>(args[0]->getTypeInfo().toString());
|
||||
}},
|
||||
return std::make_shared<Object>(args[0]->getTypeInfo().toString());
|
||||
}},
|
||||
{u8"__fvalue_int_parse",
|
||||
[](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
FString str = args[0]->as<ValueType::StringClass>();
|
||||
@@ -380,4 +414,4 @@ namespace Fig::Builtins
|
||||
};
|
||||
return builtinFunctions;
|
||||
}
|
||||
}
|
||||
} // namespace Fig::Builtins
|
||||
@@ -26,12 +26,9 @@ namespace Fig
|
||||
}
|
||||
*/
|
||||
|
||||
inline static const TypeInfo &getErrorInterfaceTypeInfo()
|
||||
{
|
||||
static const TypeInfo ErrorInterfaceTypeInfo(u8"Error", true);
|
||||
return ErrorInterfaceTypeInfo;
|
||||
}
|
||||
const TypeInfo &getErrorInterfaceTypeInfo();
|
||||
|
||||
const TypeInfo &getTypeErrorStructTypeInfo();
|
||||
/*
|
||||
interface Operation
|
||||
{
|
||||
@@ -40,11 +37,7 @@ namespace Fig
|
||||
|
||||
*/
|
||||
|
||||
inline static const TypeInfo &getOperationInterfaceTypeInfo()
|
||||
{
|
||||
static const TypeInfo OperationInterfaceTypeInfo(u8"Operation", true);
|
||||
return OperationInterfaceTypeInfo;
|
||||
}
|
||||
const TypeInfo &getOperationInterfaceTypeInfo();
|
||||
|
||||
const std::unordered_map<FString, ObjectPtr> &getBuiltinValues();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user