0f635ccf2b
- 更新了类型系统,新增了类型并优化了结构。 - 引入了基类型和派生类,用于函数、结构体和接口类型。 - 实现了类型上下文,用于管理内置类型和类型解析。 - 添加了诊断类,用于收集和报告警告和错误。 - 通过改进错误处理增强了虚拟机执行,以应对递归限制问题。 - 实现了反汇编器,将字节码转换为代码,以改善调试和分析。 - 添加了新的抽象语法树节点,用于成员表达式、对象初始化、接口和结构体定义。 - 引入了语义错误测试,包括重定义、未声明的变量和无效的结构字段。
74 lines
2.3 KiB
C++
74 lines
2.3 KiB
C++
/*!
|
|
@file src/Parser/TypeExprParser.cpp
|
|
@brief 类型表达式解析器实现:支持泛型与空安全
|
|
@author PuqiAR (im@puqiar.top)
|
|
@date 2026-03-08
|
|
*/
|
|
|
|
#include <Parser/Parser.hpp>
|
|
|
|
namespace Fig
|
|
{
|
|
// 解析基础命名类型与泛型: List<Int>
|
|
Result<TypeExpr *, Error> Parser::parseNamedTypeExpr()
|
|
{
|
|
StateProtector p(this, {State::ParsingNamedTypeExpr});
|
|
SourceLocation location = makeSourceLocation(currentToken());
|
|
|
|
DynArray<String> path;
|
|
while (true)
|
|
{
|
|
const Token &tok = consumeToken();
|
|
const String &name = srcManager.GetSub(tok.index, tok.length);
|
|
path.push_back(name);
|
|
|
|
if (match(TokenType::Dot))
|
|
{
|
|
if (!currentToken().isIdentifier())
|
|
return std::unexpected(makeUnexpectTokenError("Type", "identifier", currentToken()));
|
|
}
|
|
else break;
|
|
}
|
|
|
|
DynArray<TypeExpr *> arguments;
|
|
if (match(TokenType::Less)) // `<`
|
|
{
|
|
while (true)
|
|
{
|
|
auto result = parseTypeExpr();
|
|
if (!result) return std::unexpected(result.error());
|
|
arguments.push_back(*result);
|
|
|
|
if (match(TokenType::Greater)) break; // `>`
|
|
if (!match(TokenType::Comma))
|
|
return std::unexpected(makeUnexpectTokenError("TypeArgs", "'>' or ','", currentToken()));
|
|
}
|
|
}
|
|
|
|
return arena.Allocate<NamedTypeExpr>(path, arguments, location);
|
|
}
|
|
|
|
// 解析主入口: 处理 `?` 后缀
|
|
Result<TypeExpr *, Error> Parser::parseTypeExpr()
|
|
{
|
|
TypeExpr *base = nullptr;
|
|
|
|
// 目前只支持命名类型 (以后可以加函数类型 (Int)->Int)
|
|
if (currentToken().isIdentifier())
|
|
{
|
|
auto res = parseNamedTypeExpr();
|
|
if (!res) return std::unexpected(res.error());
|
|
base = *res;
|
|
}
|
|
else return std::unexpected(makeUnexpectTokenError("TypeExpr", "name", currentToken()));
|
|
|
|
// 空安全处理: Int?? 也可以,但 Analyzer 会规范化它
|
|
while (match(TokenType::Question))
|
|
{
|
|
base = arena.Allocate<NullableTypeExpr>(base, makeSourceLocation(prevToken()));
|
|
}
|
|
|
|
return base;
|
|
}
|
|
}
|