feat: 重构编译器以支持函数定义和调用,添加新的字节码以支持函数调用

另外,我很高兴地宣布,fib(40) 递归法 在我的平台, i5-13490f,只需要 6600ms, fib(30) 56ms
这是历史性的一刻!
This commit is contained in:
2026-03-07 00:34:52 +08:00
parent 1fe9ccf7ea
commit 6dbecbbdc0
13 changed files with 477 additions and 127 deletions

View File

@@ -282,7 +282,8 @@ namespace Fig
}
TypeInfo *valueType = stmt->value->resolvedType;
if (currentReturnType != typeCtx.GetAny() && currentReturnType != valueType)
if (!currentReturnType->isAny() && !valueType->isAny() && currentReturnType != valueType)
{
return std::unexpected(Error(ErrorType::TypeError,
std::format("return type mismatch: expects '{}', got `{}`",
@@ -500,6 +501,36 @@ namespace Fig
return {};
}
Result<void, Error> Analyzer::analyzeCallExpr(CallExpr *expr)
{
auto calleeRes = analyzeExpr(expr->callee);
if (!calleeRes)
{
return calleeRes;
}
if (expr->callee->resolvedType != typeCtx.GetAny()
&& expr->callee->resolvedType != typeCtx.GetFunction())
{
return std::unexpected(Error(ErrorType::TypeError,
std::format("object `{}` is not callable", expr->callee->toString()),
"none",
makeSourceLocation(expr->callee)));
}
for (auto *arg : expr->args.args)
{
auto argRes = analyzeExpr(arg);
if (!argRes)
{
return argRes;
}
}
expr->resolvedType = typeCtx.GetAny();
return {};
}
Result<void, Error> Analyzer::analyzeStmt(Stmt *stmt)
{
if (!stmt)
@@ -596,10 +627,17 @@ namespace Fig
return {};
}
case AstType::IdentiExpr: return analyzeIdentiExpr(static_cast<IdentiExpr *>(expr));
case AstType::IdentiExpr: {
return analyzeIdentiExpr(static_cast<IdentiExpr *>(expr));
}
case AstType::InfixExpr:
case AstType::InfixExpr: {
return analyzeInfixExpr(static_cast<InfixExpr *>(expr));
}
case AstType::CallExpr: {
return analyzeCallExpr(static_cast<CallExpr *>(expr));
}
// TODO: PrefixExpr (前缀), CallExpr (函数调用), MemberExpr (属性访问)