修复了parser解析initexpr模式判断错误的问题。修复标准库改名忘改的问题

This commit is contained in:
2026-02-04 20:34:35 +08:00
parent b4b6d409b5
commit 50ca68b1a4
4 changed files with 128 additions and 20 deletions

View File

@@ -5,7 +5,7 @@
Copyright © 2025 PuqiAR. All rights reserved.
*/
import std.value; // `type` function and string_from
// import std.value; // `type` function and string_from
struct FormatError
@@ -37,7 +37,7 @@ public func format(objects ...) -> Any
}
var fmt := objects[0];
var fmtType := value.type(fmt);
var fmtType := type(fmt);
if fmtType != "String"
{
throw new FormatError{"arg 0 (fmt) must be String type, got " + fmtType};
@@ -88,7 +88,7 @@ public func format(objects ...) -> Any
throw new FormatError{"require enough format expression"};
}
result += value.string_from(objects[argIndex]);
result += objects[argIndex] as String;
argIndex += 1;
i = endIndex + 1;
@@ -116,7 +116,7 @@ public func format(objects ...) -> Any
public func formatByListArgs(objects) -> Any
{
if value.type(objects) != "List"
if not (objects is List)
{
return null;
}
@@ -126,7 +126,7 @@ if objects.length() < 1
}
var fmt := objects[0];
var fmtType := value.type(fmt);
var fmtType := type(fmt);
if fmtType != "String"
{
throw new FormatError{"arg 0 (fmt) must be String type, got " + fmtType};
@@ -177,7 +177,7 @@ if objects.length() < 1
throw new FormatError{"require enough format expression"};
}
result += value.string_from(objects[argIndex]);
result += objects[argIndex] as String;
argIndex += 1;
i = endIndex + 1;

View File

@@ -1,3 +1,4 @@
#include "Ast/Expressions/InitExpr.hpp"
#include <Ast/Expressions/VarExpr.hpp>
#include <Ast/Statements/ErrorFlow.hpp>
#include <Ast/Statements/ImplementSt.hpp>
@@ -6,6 +7,7 @@
#include <Error/error.hpp>
#include <Token/token.hpp>
#include <Parser/parser.hpp>
#include <memory>
namespace Fig
{
@@ -881,10 +883,6 @@ namespace Fig
if (mode == 0)
{
if (isThis(TokenType::Identifier) && isNext(TokenType::Colon)) { mode = 2; }
else if (isThis(TokenType::Identifier) && (isNext(TokenType::Comma) || isNext(TokenType::RightBrace)))
{
mode = 3;
}
else
{
mode = 1;
@@ -908,16 +906,6 @@ namespace Fig
Ast::Expression expr = parseExpression(0, TokenType::Comma, TokenType::RightBrace);
args.push_back({fieldName, std::move(expr)});
}
else if (mode == 3)
{
// 3 Person {name, age, sex};
expect(TokenType::Identifier);
FString fieldName = currentToken().getValue();
Ast::Expression expr = makeAst<Ast::VarExprAst>(fieldName);
args.push_back({fieldName, std::move(expr)});
next(); // consume identifier
}
if (isThis(TokenType::Comma))
{
next(); // consume comma
@@ -929,6 +917,33 @@ namespace Fig
currentToken().toString().toBasicString())));
}
}
bool shorthand = true;
if (mode == 1)
{
for (auto &[name, exp] : args)
{
if (!name.empty())
{
shorthand = false;
}
if (exp->getType() != Ast::AstType::VarExpr)
{
shorthand = false;
}
}
if (shorthand)
{
mode = 3; // all are identifiers, so it's shorthand mode, not positional
std::vector<std::pair<FString, Ast::Expression>> nargs;
for (auto &[name, exp] : args)
{
const Ast::VarExpr var = std::static_pointer_cast<Ast::VarExprAst>(exp);
nargs.push_back({var->name, exp});
}
args = nargs;
}
}
expect(TokenType::RightBrace);
next(); // consume `}`
return makeAst<Ast::InitExprAst>(structe, args, static_cast<Ast::InitExprAst::InitMode>(mode));