Skip to content

Commit

Permalink
Fix returning enum structs not working.
Browse files Browse the repository at this point in the history
Bug: issue #1012
Test: new test case
  • Loading branch information
dvander committed Nov 30, 2024
1 parent 72b94a9 commit b657378
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/code-generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ CodeGenerator::EmitReturnStmt(ReturnStmt* stmt)
EmitExpr(stmt->expr());

const auto& v = stmt->expr()->val();
if (v.type()->isArray())
if (v.type()->isArray() || v.type()->isEnumStruct())
EmitReturnArrayStmt(stmt);
} else {
/* this return statement contains no expression */
Expand Down
20 changes: 20 additions & 0 deletions tests/enum-structs/return-enum-struct.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
First Struct
1
1.000000
Hello
1
1.000000
Hello
1
1.000000
Hello
New Struct Below
1
1.000000
Hello
1
1.000000
Hello
1
1.000000
Hello
45 changes: 45 additions & 0 deletions tests/enum-structs/return-enum-struct.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <shell>

enum struct MyStruct
{
int v;
float f;
char sz[8];
}

MyStruct CreateStruct()
{
MyStruct st;
st.v = 1;
st.f = 1.0;
st.sz = "Hello";
return st;
}

void PrintStruct(MyStruct st)
{
printnums(st.v);
printfloat(st.f);
print(st.sz);
print("\n");
}

public void main()
{
print("First Struct\n");
MyStruct st;
st = CreateStruct();
PrintStruct(st);
st = CreateStruct();
PrintStruct(st);
st = CreateStruct();
PrintStruct(st);
print("New Struct Below\n");
MyStruct st2;
st2 = CreateStruct();
PrintStruct(st2);
st2 = CreateStruct();
PrintStruct(st2);
st2 = CreateStruct();
PrintStruct(st2);
}

0 comments on commit b657378

Please sign in to comment.