[feat]类初始化

This commit is contained in:
2025-12-22 17:38:52 +08:00
parent cff6bb77a3
commit 34a56beb90
13 changed files with 294 additions and 107 deletions

View File

@@ -11,13 +11,27 @@ namespace Fig::Ast
std::vector<std::pair<FString, Expression>> args; std::vector<std::pair<FString, Expression>> args;
enum class InitMode
{
Positional = 1,
Named,
Shorthand
} initMode;
/*
3 ways of calling constructor
.1 Person {"Fig", 1, "IDK"};
.2 Person {name: "Fig", age: 1, sex: "IDK"}; // can be unordered
.3 Person {name, age, sex};
*/
InitExprAst() InitExprAst()
{ {
type = AstType::InitExpr; type = AstType::InitExpr;
} }
InitExprAst(FString _structName, std::vector<std::pair<FString, Expression>> _args) : InitExprAst(FString _structName, std::vector<std::pair<FString, Expression>> _args, InitMode _initMode) :
structName(std::move(_structName)), args(std::move(_args)) structName(std::move(_structName)), args(std::move(_args)), initMode(_initMode)
{ {
type = AstType::InitExpr; type = AstType::InitExpr;
} }

View File

@@ -1,26 +0,0 @@
#pragma once
#include <Ast/astBase.hpp>
namespace Fig::Ast
{
class VarAssignSt final : public StatementAst
{
public:
const FString varName;
const Expression valueExpr;
VarAssignSt()
{
type = AstType::VarAssignSt;
}
VarAssignSt(FString _varName, Expression _valueExpr) :
varName(std::move(_varName)), valueExpr(std::move(_valueExpr))
{
type = AstType::VarAssignSt;
}
};
using VarAssign = std::shared_ptr<VarAssignSt>;
}; // namespace Fig

View File

@@ -25,7 +25,6 @@ namespace Fig::Ast
UnaryExpr, UnaryExpr,
BinaryExpr, BinaryExpr,
TernaryExpr, TernaryExpr,
ListExpr, // [] ListExpr, // []
TupleExpr, // () TupleExpr, // ()
MapExpr, // {} MapExpr, // {}
@@ -215,7 +214,8 @@ namespace Fig::Ast
ShiftRight, // >> ShiftRight, // >>
// 赋值表达式 // 赋值表达式
Walrus, // := Assign, // =
// Walrus, // :=
// 点运算符 . // 点运算符 .
Dot, Dot,
@@ -250,7 +250,7 @@ namespace Fig::Ast
Operator::ShiftLeft, Operator::ShiftLeft,
Operator::ShiftRight, Operator::ShiftRight,
Operator::Walrus, // Operator::Walrus,
Operator::Dot}; Operator::Dot};
static const std::unordered_set<Operator> ternaryOps{Operator::TernaryCond}; static const std::unordered_set<Operator> ternaryOps{Operator::TernaryCond};
@@ -290,7 +290,7 @@ namespace Fig::Ast
{TokenType::ShiftRight, Operator::ShiftRight}, {TokenType::ShiftRight, Operator::ShiftRight},
// 赋值表达式 // 赋值表达式
{TokenType::Walrus, Operator::Walrus}, // {TokenType::Walrus, Operator::Walrus},
// 点运算符 // 点运算符
{TokenType::Dot, Operator::Dot}, {TokenType::Dot, Operator::Dot},
}; // := }; // :=

View File

@@ -4,8 +4,6 @@
#include <variant> #include <variant>
#include <map> #include <map>
#include <vector>
#include <list>
namespace Fig namespace Fig
{ {

View File

@@ -8,14 +8,14 @@ namespace Fig
{ {
struct StructInstanceT final struct StructInstanceT final
{ {
FString structName; // 类的名字 (StructType), 非变量名。用于获取所属类 size_t parentId;
ContextPtr localContext; ContextPtr localContext;
StructInstanceT(FString _structName, ContextPtr _localContext) : StructInstanceT(size_t _parentId, ContextPtr _localContext) :
structName(std::move(_structName)), localContext(std::move(_localContext)) {} parentId(std::move(_parentId)), localContext(std::move(_localContext)) {}
StructInstanceT(const StructInstanceT &other) : StructInstanceT(const StructInstanceT &other) :
structName(other.structName), localContext(other.localContext) {} parentId(other.parentId), localContext(other.localContext) {}
StructInstanceT &operator=(const StructInstanceT &) = default; StructInstanceT &operator=(const StructInstanceT &) = default;
StructInstanceT(StructInstanceT &&) = default; StructInstanceT(StructInstanceT &&) = default;
StructInstanceT &operator=(StructInstanceT &&) = default; StructInstanceT &operator=(StructInstanceT &&) = default;
@@ -31,10 +31,10 @@ namespace Fig
{ {
data = std::make_unique<StructInstanceT>(x); data = std::make_unique<StructInstanceT>(x);
} }
StructInstance(FString _structName, ContextPtr _localContext) : StructInstance(size_t _parentId, ContextPtr _localContext) :
__ValueWrapper(ValueType::StructInstance) __ValueWrapper(ValueType::StructInstance)
{ {
data = std::make_unique<StructInstanceT>(std::move(_structName), std::move(_localContext)); data = std::make_unique<StructInstanceT>(std::move(_parentId), std::move(_localContext));
} }
bool operator==(const StructInstance &other) const noexcept bool operator==(const StructInstance &other) const noexcept

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <Ast/astBase.hpp> #include <Ast/astBase.hpp>
#include <Ast/AccessModifier.hpp> #include <Ast/AccessModifier.hpp>
#include <Ast/BinaryExpr.hpp> #include <Ast/BinaryExpr.hpp>
#include <Ast/ContainerInitExprs.hpp> #include <Ast/ContainerInitExprs.hpp>
@@ -18,7 +19,6 @@
#include <Ast/TernaryExpr.hpp> #include <Ast/TernaryExpr.hpp>
#include <Ast/UnaryExpr.hpp> #include <Ast/UnaryExpr.hpp>
#include <Ast/ValueExpr.hpp> #include <Ast/ValueExpr.hpp>
#include <Ast/VarAssignSt.hpp>
#include <Ast/VarDef.hpp> #include <Ast/VarDef.hpp>
#include <Ast/VarExpr.hpp> #include <Ast/VarExpr.hpp>
#include <Ast/WhileSt.hpp> #include <Ast/WhileSt.hpp>

View File

@@ -20,6 +20,8 @@ namespace Fig
std::unordered_map<std::size_t, FunctionStruct> functions; std::unordered_map<std::size_t, FunctionStruct> functions;
std::unordered_map<std::size_t, FString> functionNames; std::unordered_map<std::size_t, FString> functionNames;
std::unordered_map<std::size_t, FString> structTypeNames;
public: public:
ContextPtr parent; ContextPtr parent;
@@ -129,6 +131,11 @@ namespace Fig
functions[fn.id] = fn; functions[fn.id] = fn;
functionNames[fn.id] = name; functionNames[fn.id] = name;
} }
if (ti == ValueType::StructType)
{
auto &st = value.as<StructType>().getValue();
structTypeNames[st.id] = name;
}
} }
std::optional<FunctionStruct> getFunction(std::size_t id) std::optional<FunctionStruct> getFunction(std::size_t id)
{ {
@@ -162,6 +169,22 @@ namespace Fig
return std::nullopt; return std::nullopt;
} }
} }
std::optional<FString> getStructName(std::size_t id)
{
auto it = structTypeNames.find(id);
if (it != structTypeNames.end())
{
return it->second;
}
else if (parent)
{
return parent->getFunctionName(id);
}
else
{
return std::nullopt;
}
}
bool contains(const FString &name) bool contains(const FString &name)
{ {
if (variables.contains(name)) if (variables.contains(name))

View File

@@ -302,7 +302,6 @@ namespace Fig
Ast::ValueExpr __parseValueExpr(); Ast::ValueExpr __parseValueExpr();
Ast::FunctionParameters __parseFunctionParameters(); // entry: current is Token::LeftParen Ast::FunctionParameters __parseFunctionParameters(); // entry: current is Token::LeftParen
Ast::BlockStatement __parseBlockStatement(); // entry: current is Token::LeftBrace Ast::BlockStatement __parseBlockStatement(); // entry: current is Token::LeftBrace
Ast::VarAssign __parseVarAssign(FString); // entry: current is Token::Assign, para1 is var name
Ast::If __parseIf(); // entry: current is Token::If Ast::If __parseIf(); // entry: current is Token::If
Ast::While __parseWhile(); // entry: current is Token::While Ast::While __parseWhile(); // entry: current is Token::While
Ast::Statement __parseIncrementStatement(); // only allowed in __parseFor function Ast::Statement __parseIncrementStatement(); // only allowed in __parseFor function

View File

@@ -126,12 +126,16 @@ namespace Fig
line = _line; line = _line;
column = _column; column = _column;
} }
Token setPos(size_t _line, size_t _column) const Token& setPos(size_t _line, size_t _column)
{ {
line = _line; line = _line;
column = _column; column = _column;
return *this; return *this;
} }
size_t getLength()
{
return value.length();
}
const FString& getValue() const const FString& getValue() const
{ {
return value; return value;

View File

@@ -182,7 +182,7 @@ namespace Fig
if (is<StructInstance>()) if (is<StructInstance>())
{ {
return FString(std::format("<Struct Instance('{}') at {:p}", return FString(std::format("<Struct Instance('{}') at {:p}",
as<StructInstance>().getValue().structName.toBasicString(), as<StructInstance>().getValue().parentId,
static_cast<const void *>(as<StructInstance>().data.get()))); static_cast<const void *>(as<StructInstance>().data.get())));
} }
return FString(u8"<error>"); return FString(u8"<error>");

View File

@@ -34,10 +34,10 @@ namespace Fig
case Operator::ShiftLeft: return shift_left(lhs, rhs); case Operator::ShiftLeft: return shift_left(lhs, rhs);
case Operator::ShiftRight: return shift_right(lhs, rhs); case Operator::ShiftRight: return shift_right(lhs, rhs);
case Operator::Walrus: { // case Operator::Walrus: {
static constexpr char WalrusErrorName[] = "WalrusError"; // static constexpr char WalrusErrorName[] = "WalrusError";
throw EvaluatorError<WalrusErrorName>(FStringView(u8"Walrus operator is not supported"), currentAddressInfo); // using parent address info for now // throw EvaluatorError<WalrusErrorName>(FStringView(u8"Walrus operator is not supported"), currentAddressInfo); // using parent address info for now
} // }
default: default:
throw RuntimeError(FStringView(u8"Unsupported operator")); throw RuntimeError(FStringView(u8"Unsupported operator"));
} }
@@ -45,6 +45,55 @@ namespace Fig
Value Evaluator::evalBinary(const Ast::BinaryExpr &binExp) Value Evaluator::evalBinary(const Ast::BinaryExpr &binExp)
{ {
if (binExp->op == Ast::Operator::Dot)
{
const Value &lhs = eval(binExp->lexp);
if (!lhs.is<StructInstance>())
{
static constexpr char AccessOpObjectNotStructError[] = "AccessOpObjectNotStructError";
throw EvaluatorError<AccessOpObjectNotStructError>(FStringView(
std::format("Object not a struct")),
binExp->lexp->getAAI());
}
const StructInstanceT &st = lhs.as<StructInstance>().getValue();
Ast::VarExpr varExp;
if (!(varExp = std::dynamic_pointer_cast<Ast::VarExprAst>(binExp->rexp)))
{
static constexpr char AccessOpNotAFieldNameError[] = "AccessOpNotAFieldNameError";
throw EvaluatorError<AccessOpNotAFieldNameError>(FStringView(
std::format("{} is not a field name", binExp->rexp->toString().toBasicString())),
binExp->rexp->getAAI());
}
FString member = varExp->name;
auto structTypeNameOpt = currentContext->getStructName(st.parentId);
if (!structTypeNameOpt) throw RuntimeError(FStringView("Can't get struct type name"));
FString structTypeName = *structTypeNameOpt;
if (!st.localContext->containsInThisScope(member))
{
static constexpr char NoAttributeError[] = "NoAttributeError";
throw EvaluatorError<NoAttributeError>(FStringView(
std::format("Struct `{}` has no attribute '{}'", structTypeName.toBasicString(), member.toBasicString())),
binExp->rexp->getAAI());
}
return *st.localContext->get(member); // safe
}
if (binExp->op == Ast::Operator::Assign)
{
Ast::VarExpr varExp;
if (!(varExp = std::dynamic_pointer_cast<Ast::VarExprAst>(binExp->rexp)))
{
static constexpr char AssignToRightValueError[] = "AssignToRightValueError";
throw EvaluatorError<AssignToRightValueError>(FStringView(
std::format("Can't assign to right value {}", binExp->lexp->toString().toBasicString())),
binExp->lexp->getAAI());
}
const FString& varName = varExp->name;
if (!currentContext->contains(varName))
{
static constexpr char VariableNotFoundErrorName[] = "VariableNotFoundError";
throw EvaluatorError<VariableNotFoundErrorName>(FStringView(std::format("Variable '{}' not defined", varName.toBasicString())), currentAddressInfo);
}
}
return __evalOp(binExp->op, eval(binExp->lexp), eval(binExp->rexp)); return __evalOp(binExp->op, eval(binExp->lexp), eval(binExp->rexp));
} }
Value Evaluator::evalUnary(const Ast::UnaryExpr &unExp) Value Evaluator::evalUnary(const Ast::UnaryExpr &unExp)
@@ -253,8 +302,133 @@ namespace Fig
currentContext); currentContext);
} }
} }
case AstType::ListExpr: { case AstType::InitExpr: {
auto listexpr = std::dynamic_pointer_cast<Ast::ListExprAst>(exp); auto initExpr = std::dynamic_pointer_cast<Ast::InitExprAst>(exp);
if (!currentContext->contains(initExpr->structName))
{
static constexpr char StructNotFoundErrorName[] = "StructNotFoundError";
throw EvaluatorError<StructNotFoundErrorName>(FStringView(std::format("Structure type '{}' not found", initExpr->structName.toBasicString())), initExpr->getAAI());
}
Value structTypeVal = currentContext->get(initExpr->structName).value();
if (!structTypeVal.is<StructType>())
{
static constexpr char NotAStructTypeErrorName[] = "NotAStructTypeError";
throw EvaluatorError<NotAStructTypeErrorName>(FStringView(std::format("'{}' is not a structure type", initExpr->structName.toBasicString())), initExpr->getAAI());
}
const StructT &structT = structTypeVal.as<StructType>().getValue();
ContextPtr defContext = structT.defContext; // definition context
// check init args
size_t minArgs = 0;
size_t maxArgs = structT.fields.size();
for (auto &f : structT.fields)
{
if (f.defaultValue == nullptr) minArgs++;
}
size_t got = initExpr->args.size();
if (got > maxArgs || got < minArgs)
{
static constexpr char StructInitArgumentMismatchErrorName[] = "StructInitArgumentMismatchError";
throw EvaluatorError<StructInitArgumentMismatchErrorName>(FStringView(std::format("Structure '{}' expects {} to {} fields, but {} were provided", initExpr->structName.toBasicString(), minArgs, maxArgs, initExpr->args.size())), initExpr->getAAI());
}
std::vector<std::pair<FString, Value>> evaluatedArgs;
for (const auto &[argName, argExpr] : initExpr->args)
{
evaluatedArgs.push_back({argName, eval(argExpr)});
}
ContextPtr instanceCtx = std::make_shared<Context>(
FString(std::format("<StructInstance {}>", initExpr->structName.toBasicString())),
currentContext);
/*
3 ways of calling constructor
.1 Person {"Fig", 1, "IDK"};
.2 Person {name: "Fig", age: 1, sex: "IDK"}; // can be unordered
.3 Person {name, age, sex};
*/
{
using enum Ast::InitExprAst::InitMode;
if (initExpr->initMode == Positional)
{
for (size_t i = 0; i < maxArgs; ++i)
{
const Field &field = structT.fields[i];
const FString &fieldName = field.name;
const TypeInfo &expectedType = field.type;
if (i >= evaluatedArgs.size())
{
// we've checked argument count before, so here must be a default value
ContextPtr previousContext = currentContext;
currentContext = defContext; // evaluate default value in definition context
Value defaultVal = eval(field.defaultValue); // it can't be null here
currentContext = previousContext;
// type check
if (expectedType != defaultVal.getTypeInfo() && expectedType != ValueType::Any)
{
static constexpr char StructFieldTypeMismatchErrorName[] = "StructFieldTypeMismatchError";
throw EvaluatorError<StructFieldTypeMismatchErrorName>(FStringView(std::format("In structure '{}', field '{}' expects type '{}', but got type '{}'", initExpr->structName.toBasicString(), fieldName.toBasicString(), expectedType.toString().toBasicString(), defaultVal.getTypeInfo().toString().toBasicString())), initExpr->getAAI());
}
instanceCtx->def(fieldName, expectedType, field.am, defaultVal);
continue;
}
const Value &argVal = evaluatedArgs[i].second;
if (expectedType != argVal.getTypeInfo() && expectedType != ValueType::Any)
{
static constexpr char StructFieldTypeMismatchErrorName[] = "StructFieldTypeMismatchError";
throw EvaluatorError<StructFieldTypeMismatchErrorName>(FStringView(std::format("In structure '{}', field '{}' expects type '{}', but got type '{}'", initExpr->structName.toBasicString(), fieldName.toBasicString(), expectedType.toString().toBasicString(), argVal.getTypeInfo().toString().toBasicString())), initExpr->getAAI());
}
instanceCtx->def(fieldName, expectedType, field.am, argVal);
}
}
else
{
// named / shorthand init
for (size_t i = 0; i < maxArgs; ++i)
{
const Field &field = structT.fields[i];
const FString &fieldName = (field.name.empty() ? evaluatedArgs[i].first : field.name);
if (instanceCtx->containsInThisScope(fieldName))
{
static constexpr char StructFieldRedeclarationErrorName[] = "StructFieldRedeclarationError";
throw EvaluatorError<StructFieldRedeclarationErrorName>(FStringView(std::format("Field '{}' already initialized in structure '{}'", fieldName.toBasicString(), initExpr->structName.toBasicString())), initExpr->getAAI());
}
if (i + 1 > got)
{
// use default value
ContextPtr previousContext = currentContext;
currentContext = defContext; // evaluate default value in definition context
Value defaultVal = eval(field.defaultValue); // it can't be null here
currentContext = previousContext;
// type check
const TypeInfo &expectedType = field.type;
if (expectedType != defaultVal.getTypeInfo() && expectedType != ValueType::Any)
{
static constexpr char StructFieldTypeMismatchErrorName[] = "StructFieldTypeMismatchError";
throw EvaluatorError<StructFieldTypeMismatchErrorName>(FStringView(std::format("In structure '{}', field '{}' expects type '{}', but got type '{}'", initExpr->structName.toBasicString(), fieldName.toBasicString(), expectedType.toString().toBasicString(), defaultVal.getTypeInfo().toString().toBasicString())), initExpr->getAAI());
}
instanceCtx->def(fieldName, field.type, field.am, defaultVal);
continue;
}
const Value &argVal = evaluatedArgs[i].second;
if (field.type != argVal.getTypeInfo() && field.type != ValueType::Any)
{
static constexpr char StructFieldTypeMismatchErrorName[] = "StructFieldTypeMismatchError";
throw EvaluatorError<StructFieldTypeMismatchErrorName>(FStringView(std::format("In structure '{}', field '{}' expects type '{}', but got type '{}'", initExpr->structName.toBasicString(), fieldName.toBasicString(), field.type.toString().toBasicString(), argVal.getTypeInfo().toString().toBasicString())), initExpr->getAAI());
}
instanceCtx->def(fieldName, field.type, field.am, argVal);
}
}
}
return StructInstance(structT.id, instanceCtx);
} }
default: default:
throw RuntimeError(FStringView("Unknown expression type:" + std::to_string(static_cast<int>(exp->getType())))); throw RuntimeError(FStringView("Unknown expression type:" + std::to_string(static_cast<int>(exp->getType()))));
@@ -359,7 +533,7 @@ namespace Fig
}; };
case AstType::StructSt: { case AstType::StructSt: {
auto stDef = std::dynamic_pointer_cast<Ast::StructDefSt>(stmt); auto stDef = std::dynamic_pointer_cast<Ast::StructDefSt>(stmt);
if (currentContext->contains(stDef->name)) if (currentContext->containsInThisScope(stDef->name))
{ {
static constexpr char RedeclarationErrorName[] = "RedeclarationError"; static constexpr char RedeclarationErrorName[] = "RedeclarationError";
throw EvaluatorError<RedeclarationErrorName>(FStringView(std::format("Structure '{}' already defined in this scope", stDef->name.toBasicString())), currentAddressInfo); throw EvaluatorError<RedeclarationErrorName>(FStringView(std::format("Structure '{}' already defined in this scope", stDef->name.toBasicString())), currentAddressInfo);
@@ -377,6 +551,7 @@ namespace Fig
} }
ContextPtr defContext(currentContext); ContextPtr defContext(currentContext);
AccessModifier am = (stDef->isPublic ? AccessModifier::PublicConst : AccessModifier::Const); AccessModifier am = (stDef->isPublic ? AccessModifier::PublicConst : AccessModifier::Const);
TypeInfo _(stDef->name, true); // register type name
currentContext->def( currentContext->def(
stDef->name, stDef->name,
ValueType::StructType, ValueType::StructType,
@@ -386,32 +561,32 @@ namespace Fig
fields))); fields)));
return StatementResult::normal(); return StatementResult::normal();
} }
case AstType::VarAssignSt: { // case AstType::VarAssignSt: {
auto varAssign = std::dynamic_pointer_cast<Ast::VarAssignSt>(stmt); // auto varAssign = std::dynamic_pointer_cast<Ast::VarAssignSt>(stmt);
if (!currentContext->contains(varAssign->varName)) // if (!currentContext->contains(varAssign->varName))
{ // {
static constexpr char VariableNotFoundErrorName[] = "VariableNotFoundError"; // static constexpr char VariableNotFoundErrorName[] = "VariableNotFoundError";
throw EvaluatorError<VariableNotFoundErrorName>(FStringView(std::format("Variable '{}' not defined", varAssign->varName.toBasicString())), currentAddressInfo); // throw EvaluatorError<VariableNotFoundErrorName>(FStringView(std::format("Variable '{}' not defined", varAssign->varName.toBasicString())), currentAddressInfo);
} // }
if (!currentContext->isVariableMutable(varAssign->varName)) // if (!currentContext->isVariableMutable(varAssign->varName))
{ // {
static constexpr char ConstAssignmentErrorName[] = "ConstAssignmentError"; // static constexpr char ConstAssignmentErrorName[] = "ConstAssignmentError";
throw EvaluatorError<ConstAssignmentErrorName>(FStringView(std::format("Cannot assign to constant variable '{}'", varAssign->varName.toBasicString())), currentAddressInfo); // throw EvaluatorError<ConstAssignmentErrorName>(FStringView(std::format("Cannot assign to constant variable '{}'", varAssign->varName.toBasicString())), currentAddressInfo);
} // }
Value val = eval(varAssign->valueExpr); // Value val = eval(varAssign->valueExpr);
if (currentContext->getTypeInfo(varAssign->varName) != ValueType::Any) // if (currentContext->getTypeInfo(varAssign->varName) != ValueType::Any)
{ // {
TypeInfo expectedType = currentContext->getTypeInfo(varAssign->varName); // TypeInfo expectedType = currentContext->getTypeInfo(varAssign->varName);
TypeInfo actualType = val.getTypeInfo(); // TypeInfo actualType = val.getTypeInfo();
if (expectedType != actualType) // if (expectedType != actualType)
{ // {
static constexpr char VariableTypeMismatchErrorName[] = "VariableTypeMismatchError"; // static constexpr char VariableTypeMismatchErrorName[] = "VariableTypeMismatchError";
throw EvaluatorError<VariableTypeMismatchErrorName>(FStringView(std::format("assigning: Variable '{}' expects type '{}', but got type '{}'", varAssign->varName.toBasicString(), expectedType.toString().toBasicString(), actualType.toString().toBasicString())), currentAddressInfo); // throw EvaluatorError<VariableTypeMismatchErrorName>(FStringView(std::format("assigning: Variable '{}' expects type '{}', but got type '{}'", varAssign->varName.toBasicString(), expectedType.toString().toBasicString(), actualType.toString().toBasicString())), currentAddressInfo);
} // }
} // }
currentContext->set(varAssign->varName, val); // currentContext->set(varAssign->varName, val);
return StatementResult::normal(); // return StatementResult::normal();
}; // };
case AstType::IfSt: { case AstType::IfSt: {
auto ifSt = std::dynamic_pointer_cast<Ast::IfSt>(stmt); auto ifSt = std::dynamic_pointer_cast<Ast::IfSt>(stmt);
Value condVal = eval(ifSt->condition); Value condVal = eval(ifSt->condition);

View File

@@ -34,8 +34,10 @@ namespace Fig
{Ast::Operator::ShiftLeft, {15, 16}}, {Ast::Operator::ShiftLeft, {15, 16}},
{Ast::Operator::ShiftRight, {15, 16}}, {Ast::Operator::ShiftRight, {15, 16}},
{Ast::Operator::Assign, {2, 1}}, // 右结合
// 海象运算符 // 海象运算符
{Ast::Operator::Walrus, {2, 1}}, // 右结合 // {Ast::Operator::Walrus, {2, 1}}, // 右结合
// 点运算符 // 点运算符
{Ast::Operator::Dot, {40, 41}}, {Ast::Operator::Dot, {40, 41}},
@@ -391,12 +393,6 @@ namespace Fig
next(); next();
stmt = __parseStructDef(false); stmt = __parseStructDef(false);
} }
else if (isThis(TokenType::Identifier) and isNext(TokenType::Assign))
{
FString varName = currentToken().getValue();
next(); // consume identifier
stmt = __parseVarAssign(varName);
}
else if (isThis(TokenType::If)) else if (isThis(TokenType::If))
{ {
stmt = __parseIf(); stmt = __parseIf();
@@ -454,15 +450,6 @@ namespace Fig
stmts.push_back(__parseStatement()); stmts.push_back(__parseStatement());
} }
} }
Ast::VarAssign Parser::__parseVarAssign(FString varName)
{
// entry: current is `=`
next(); // consume `=`
Ast::Expression exp = parseExpression(0);
expectSemicolon();
return makeAst<Ast::VarAssignSt>(varName, exp);
}
Ast::If Parser::__parseIf() Ast::If Parser::__parseIf()
{ {
// entry: current is `if` // entry: current is `if`
@@ -678,7 +665,7 @@ namespace Fig
.2 Person {name: "Fig", age: 1, sex: "IDK"}; // can be unordered .2 Person {name: "Fig", age: 1, sex: "IDK"}; // can be unordered
.3 Person {name, age, sex}; .3 Person {name, age, sex};
*/ */
uint8_t mode = 0; // 0=undetermined, 1=positional, 2=named, 3=shorthand uint8_t mode; // 0=undetermined, 1=positional, 2=named, 3=shorthand
while (!isThis(TokenType::RightBrace)) while (!isThis(TokenType::RightBrace))
{ {
@@ -701,7 +688,7 @@ namespace Fig
if (mode == 1) if (mode == 1)
{ {
// 1 Person {"Fig", 1, "IDK"}; // 1 Person {"Fig", 1, "IDK"};
Ast::Expression expr = parseExpression(0); Ast::Expression expr = parseExpression(0, TokenType::Comma, TokenType::RightBrace);
args.push_back({FString(), std::move(expr)}); args.push_back({FString(), std::move(expr)});
} }
else if (mode == 2) else if (mode == 2)
@@ -712,7 +699,7 @@ namespace Fig
next(); // consume identifier next(); // consume identifier
expect(TokenType::Colon); expect(TokenType::Colon);
next(); // consume colon next(); // consume colon
Ast::Expression expr = parseExpression(0); Ast::Expression expr = parseExpression(0, TokenType::Comma, TokenType::RightBrace);
args.push_back({fieldName, std::move(expr)}); args.push_back({fieldName, std::move(expr)});
} }
else if (mode == 3) else if (mode == 3)
@@ -731,12 +718,17 @@ namespace Fig
} }
else if (!isThis(TokenType::RightBrace)) else if (!isThis(TokenType::RightBrace))
{ {
throwAddressableError<SyntaxError>(u8"Expected comma or right brace"); throwAddressableError<SyntaxError>(FStringView(
std::format("Expect `,` or `}}` in struct initialization expression, got {}",
currentToken().toString().toBasicString())
));
} }
} }
expect(TokenType::RightBrace); expect(TokenType::RightBrace);
next(); // consume `}` next(); // consume `}`
return makeAst<Ast::InitExprAst>(structName, args); return makeAst<Ast::InitExprAst>(structName, args,
(mode == 1 ? Ast::InitExprAst::InitMode::Positional :
(mode == 2 ? Ast::InitExprAst::InitMode::Named : Ast::InitExprAst::InitMode::Shorthand)));
} }
Ast::Expression Parser::__parseTupleOrParenExpr() Ast::Expression Parser::__parseTupleOrParenExpr()
{ {

View File

@@ -1,9 +1,17 @@
var call := 0; struct Person
func t()
{ {
call = call + 1; name: String;
__fstdout_println(call); age: Int = 10;
t();
public func getName()
{
return name;
}
} }
t(); var person = Person{"123"};
const print := __fstdout_print;
print(person.name);
person.name = "sb";
print(person.name);