Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions FxScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ FxAstNode* FxConfigScript::TryParseKeyword(FxAstBlock* parent_block)
// global [type] [name] <?assignment> ;
constexpr FxHash kw_global = FxHashStr("global");

// struct [name] { <members...> }
constexpr FxHash kw_struct = FxHashStr("struct");

// return ;
constexpr FxHash kw_return = FxHashStr("return");

Expand All @@ -183,6 +186,27 @@ FxAstNode* FxConfigScript::TryParseKeyword(FxAstBlock* parent_block)
EatToken(TT::Identifier);
return ParseVarDeclare(&mScopes[0]);
}
if (hash == kw_struct) {
EatToken(TT::Identifier);
Token& name = EatToken(TT::Identifier);
FxAstStructDecl* struct_decl = FX_SCRIPT_ALLOC_NODE(FxAstStructDecl);
struct_decl->Name = &name;

EatToken(TT::LBrace);

while (GetToken().Type != TT::RBrace && mTokenIndex < mTokens.Size()) {
FxAstVarDecl* member = ParseVarDeclare();

// Expect a semicolon after each member declaration
EatToken(TT::Semicolon);

struct_decl->Members.push_back(member);
}

EatToken(TT::RBrace);

return struct_decl;
}
if (hash == kw_return) {
EatToken(TT::Identifier);

Expand Down Expand Up @@ -1552,6 +1576,22 @@ void FxScriptInterpreter::Visit(FxAstNode* node)

Visit(vardecl->Assignment);
}
else if (node->NodeType == FX_AST_STRUCTDECL) {
FxAstStructDecl* structdecl = reinterpret_cast<FxAstStructDecl*>(node);

// Visit all member variable declarations to ensure they are valid
for (FxAstNode* member_node : structdecl->Members) {
if (member_node->NodeType != FX_AST_VARDECL) {
continue;
}

FxAstVarDecl* member_decl = reinterpret_cast<FxAstVarDecl*>(member_node);
Visit(member_decl);
}

FxScriptScope* scope = mCurrentScope;
scope->Structs.Insert(*structdecl);
}
else if (node->NodeType == FX_AST_ASSIGN) {
FxAstAssign* assign = reinterpret_cast<FxAstAssign*>(node);
VisitAssignment(assign);
Expand Down
14 changes: 14 additions & 0 deletions FxScript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ enum FxAstType
FX_AST_VARDECL,
FX_AST_ASSIGN,

// Type defs
FX_AST_STRUCTDECL,

// Actions
FX_AST_ACTIONDECL,
FX_AST_ACTIONCALL,
Expand Down Expand Up @@ -205,6 +208,16 @@ struct FxAstVarDecl : public FxAstNode
bool DefineAsGlobal = false;
};

struct FxAstStructDecl : public FxAstNode
{
FxAstStructDecl()
{
this->NodeType = FX_AST_STRUCTDECL;
}
FxTokenizer::Token* Name = nullptr;
std::vector<FxAstVarDecl*> Members;
};

struct FxAstDocComment : public FxAstNode
{
FxAstDocComment()
Expand Down Expand Up @@ -374,6 +387,7 @@ struct FxScriptScope
{
FxMPPagedArray<FxScriptVar> Vars;
FxMPPagedArray<FxScriptAction> Actions;
FxMPPagedArray<FxAstStructDecl> Structs;

FxScriptScope* Parent = nullptr;

Expand Down
6 changes: 6 additions & 0 deletions Main.fxS
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ action test_action(int x, int y)
#log("Value: ", value);
}

struct TestStruct
{
int a;
int b;
};

test_action(2, 3);