Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions src/ast/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct List {
pub enum ListDefault {
Values(Vec<ConstExpr>),
File { path: SmolStr, span: Span },
FixedLength(ConstExpr, ConstExpr),
}

impl List {
Expand Down Expand Up @@ -63,4 +64,20 @@ impl List {
is_used: false,
}
}

pub fn new_fixed_length(
name: SmolStr,
span: Span,
type_: Type,
default: ConstExpr,
length: ConstExpr,
) -> Self {
Self {
name,
span,
type_,
default: Some(ListDefault::FixedLength(default, length)),
is_used: false,
}
}
}
10 changes: 10 additions & 0 deletions src/codegen/sb3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,16 @@ where T: Write + Seek
vec![]
}
},
Some(ListDefault::FixedLength(value, length)) => {
let value = s.evaluate_const_expr(d, value);
let len = s.evaluate_const_expr(d, length).to_number();
Comment thread
aspizu marked this conversation as resolved.
if len > 200_000_f64 {
d.report(DiagnosticKind::ListTooBig, &length.span());
vec![]
} else {
vec![value; len as usize]
Comment thread
aspizu marked this conversation as resolved.
Outdated
}
}
None => vec![],
};
match &list.type_ {
Expand Down
3 changes: 3 additions & 0 deletions src/diagnostic/diagnostic_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub enum DiagnosticKind {
UnusedFunc(SmolStr),
UnusedArg(SmolStr),
UnusedStructField(SmolStr),
ListTooBig,
}

impl DiagnosticKind {
Expand Down Expand Up @@ -298,6 +299,7 @@ impl DiagnosticKind {
format!("duplicate variant {variant_name} in enum {enum_name}")
}
DiagnosticKind::EmptyStruct(name) => format!("struct {name} is empty"),
DiagnosticKind::ListTooBig => format!("list too big, length should be atmost 200,000"),
}
}

Expand Down Expand Up @@ -485,6 +487,7 @@ impl From<&DiagnosticKind> for Level {
| DiagnosticKind::InvalidCostumeFormat { .. }
| DiagnosticKind::InvalidSoundFormat { .. }
| DiagnosticKind::LocalNotSupported
| DiagnosticKind::ListTooBig
| DiagnosticKind::UnknownDirective(_) => Level::Error,

| DiagnosticKind::FollowedByUnreachableCode
Expand Down
3 changes: 3 additions & 0 deletions src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ Declr: () = {
LIST <type_:Type> <l:@L> <name:NAME> <r:@R> <pl:@L> <path:STR> <pr:@R> ";" => {
sprite.add_list(List::new_file(name, l..r, type_, path, pl..pr), diagnostics);
},
LIST <type_:Type> <l:@L> <name:NAME> <r:@R> "=" "[" <default:ConstExpr> ";" <length:ConstExpr> "]" ";" => {
sprite.add_list(List::new_fixed_length(name, l..r, type_, default, length), diagnostics);
}
}

EnumVariant: EnumVariant = {
Expand Down
Loading