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
62 changes: 61 additions & 1 deletion src/codegen/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::{
};

use logos::Span;
use serde_json::json;

use super::{
input::{
Expand Down Expand Up @@ -246,6 +247,38 @@ where T: Write + Seek
return self.list_contains(s, d, this_id, parent_id, &qualified_name, lhs);
}
}
// Handle struct field access: "value" in struct_list.field
if let Expr::Dot {
lhs,
rhs,
rhs_span: _,
} = rhs
{
if let Expr::Name(name) = lhs.as_ref() {
if let Some(QualifiedName::List(list_name, _)) = s.qualify_name(Some(d), name) {
let list = s.get_list(&list_name).unwrap();
if let Some((type_name, _type_span)) = list.type_.struct_() {
let struct_ = s.get_struct(type_name).unwrap();
// Verify the field exists in the struct
if struct_
.fields
.iter()
.any(|field| field.name == rhs.as_str())
{
let qualified_name = qualify_struct_var_name(rhs, name.basename());
return self.list_contains(
s,
d,
this_id,
parent_id,
&qualified_name,
lhs,
);
}
}
}
}
}
}
let lhs_id = self.id.new_id();
let rhs_id = self.id.new_id();
Expand Down Expand Up @@ -433,7 +466,7 @@ where T: Write + Seek
pub fn expr_dot(
&mut self,
s: S,
_d: D,
d: D,
_this_id: NodeID,
_parent_id: NodeID,
lhs: &Expr,
Expand All @@ -444,6 +477,33 @@ where T: Write + Seek
if let Some(_enum_) = s.get_enum(name.basename()) {
return Ok(());
}

// Check if this is a struct list field access
// First check if this is a list directly
if let Some(list) = s.get_list(name.basename()) {
if let Some((type_name, _type_span)) = list.type_.struct_() {
// This is a struct list, check if field exists in struct
let struct_ = s.get_struct(type_name).unwrap();
// Verify the field exists in the struct
if struct_
.fields
.iter()
.any(|field| field.name == rhs.as_str())
{
let qualified_name = qualify_struct_var_name(rhs, name.basename());
let qualified_list_name = QualifiedName::List(qualified_name, Type::Value);
match qualified_list_name {
QualifiedName::Var(qname, _) => {
write!(self, "[3,[12,{},{}],", json!(*qname), json!(*qname))?;
}
QualifiedName::List(qname, _) => {
write!(self, "[3,[13,{},{}],", json!(*qname), json!(*qname))?;
}
}
return write!(self, "[10, \"\"]]");
}
}
}
}
eprintln!("attempted to codegen Expr::Dot lhs = {lhs:#?}, rhs = {rhs:#?}");
Ok(())
Expand Down
30 changes: 28 additions & 2 deletions tests/lists/main.gs
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
costumes "blank.svg";

onflag {
say "Hello, World!";
struct MyStruct {
name,
date,
}

list MyStruct my_struct_list;

proc main {
# Test basic struct field access
add MyStruct { name: "Alice", date: "01/01/2026" } to my_struct_list;
add MyStruct { name: "Bob", date: "02/02/2026" } to my_struct_list;
add MyStruct { name: "Charlie", date: "03/03/2026" } to my_struct_list;

# Test struct field index query
index = "02/02/2026" in my_struct_list.date;
say "Index: " & index;

# Test another field
has_alice = "Alice" in my_struct_list.name;
say "Has Alice: " & has_alice;

# Test non-existent value
has_nonexistent = "04/04/2026" in my_struct_list.date;
say "Has non-existent date: " & has_nonexistent;
}

onflag {
main;
}