Skip to content

Commit

Permalink
fix(zscript): compiler crashing on for-each loops
Browse files Browse the repository at this point in the history
This regressed because of the recent error locations refactor.
  • Loading branch information
connorjclark committed Apr 24, 2024
1 parent 7feb9af commit b00f59a
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/parser/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,9 @@ void ASTStmtFor::execute(ASTVisitor& visitor, void* param)
uint ASTStmtForEach::next_comment_id = 0;

ASTStmtForEach::ASTStmtForEach(
std::string const& identifier, ASTExpr* expr, ASTStmt* body,
ASTString* identifier, ASTExpr* expr, ASTStmt* body,
ASTStmt* elseBlock, LocationData const& location)
: ASTStmt(location), iden(identifier), indxdecl(nullptr), arrdecl(nullptr),
: ASTStmt(location), identifier(identifier), indxdecl(nullptr), arrdecl(nullptr),
decl(nullptr), arrExpr(expr), body(body),
elseBlock(elseBlock), scope(nullptr),
ends_loop(true), ends_else(true)
Expand Down
4 changes: 2 additions & 2 deletions src/parser/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,14 +605,14 @@ namespace ZScript
{
DEF_COMMENT_UID();
public:
ASTStmtForEach(std::string const& identifier, ASTExpr* expr,
ASTStmtForEach(ASTString* identifier, ASTExpr* expr,
ASTStmt* body, ASTStmt* elseBlock,
LocationData const& location = LOC_NONE);
ASTStmtForEach* clone() const {return new ASTStmtForEach(*this);}

void execute(ASTVisitor& visitor, void* param = NULL);

std::string iden;
owning_ptr<ASTString> identifier;
owning_ptr<ASTDataDecl> indxdecl;
owning_ptr<ASTDataDecl> arrdecl;
owning_ptr<ASTDataDecl> decl;
Expand Down
2 changes: 1 addition & 1 deletion src/parser/BuildVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ void BuildOpcodes::caseDataDecl(ASTDataDecl& host, void* param)

void BuildOpcodes::buildVariable(ASTDataDecl& host, OpcodeContext& context)
{
if (host.list->internal)
if (host.list && host.list->internal)
return;

Datum& manager = *host.manager;
Expand Down
2 changes: 2 additions & 0 deletions src/parser/MetadataVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ static std::string make_uri(std::string path)

static auto LocationData_json(const LocationData& loc)
{
assert(loc.first_line > 0 && loc.first_column > 0);
assert(loc.last_line > 0 && loc.last_column > 0);
return json{
{"start", {
{"line", loc.first_line - 1}, {"character", loc.first_column - 1},
Expand Down
6 changes: 3 additions & 3 deletions src/parser/SemanticAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,18 @@ void SemanticAnalyzer::caseStmtForEach(ASTStmtForEach& host, void* param)

//The array iter declaration
ASTDataDecl* indxdecl = new ASTDataDecl(host.location);
indxdecl->identifier = new ASTString("__LOOP_ITER");
indxdecl->identifier = new ASTString("__LOOP_ITER", host.location);
indxdecl->baseType = new ASTDataType(DataType::FLOAT, host.location);
host.indxdecl = indxdecl;
//The array holder declaration
ASTDataDecl* arrdecl = new ASTDataDecl(host.location);
indxdecl->identifier = new ASTString("__LOOP_ARR");
arrdecl->identifier = new ASTString("__LOOP_ARR", host.location);
arrdecl->setInitializer(host.arrExpr.clone());
arrdecl->baseType = new ASTDataType(ty, host.location);
host.arrdecl = arrdecl;
//The data declaration
ASTDataDecl* decl = new ASTDataDecl(host.location);
decl->identifier = new ASTString(host.iden);
decl->identifier = host.identifier;
decl->baseType = new ASTDataType(ty, host.location);
host.decl = decl;

Expand Down
6 changes: 2 additions & 4 deletions src/parser/ffscript.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -1610,8 +1610,7 @@ Statement_For_Each :
ASTExpr* expr = (ASTExpr*)$5;
ASTStmt* body = (ASTExpr*)$7;

$$ = new ASTStmtForEach(iden->getValue(), expr, body, nullptr, @$);
delete iden;
$$ = new ASTStmtForEach(iden, expr, body, nullptr, @$);
}
| FOR LPAREN Identifier Token_In Expression RPAREN
Block_Statement ELSE Block_Statement
Expand All @@ -1621,8 +1620,7 @@ Statement_For_Each :
ASTStmt* body = (ASTExpr*)$7;
ASTStmt* elseblock = (ASTExpr*)$9;

$$ = new ASTStmtForEach(iden->getValue(), expr, body, elseblock, @$);
delete iden;
$$ = new ASTStmtForEach(iden, expr, body, elseblock, @$);
}
;

Expand Down
6 changes: 6 additions & 0 deletions tests/scripts/metadata.zs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ ffc script Metadata {
// equal to [c->speed]
int e = c->speed;
WrapDeg(e);
// an array
int arr[] = {1, 2, 3};
for (v : arr) {
// TODO: make symbol lookup find `v`
Trace(v);
}
}
}

Expand Down

0 comments on commit b00f59a

Please sign in to comment.