array types can now use an expression to define the array size

this was surprisingly non-trivial
This commit is contained in:
Janis 2024-09-16 17:18:23 +02:00
parent 4f70fa2246
commit cbbbb7989a
4 changed files with 31 additions and 18 deletions

View file

@ -3350,23 +3350,24 @@ pub mod ast_gen {
fn parse_array_type(&mut self, tokens: &mut TokenIterator) -> ParseResult<Index> {
let loc = tokens.current_source_location();
let length_expr = self.parse_bracketed(tokens, |this, tokens| {
let next = tokens.peek_token().ok_or(ErrorInfo {
error: ParseError::UnexpectedEndOfTokens,
loc: tokens.current_source_location(),
})?;
match next.token() {
Token::IntegerBinConstant
| Token::IntegerHexConstant
| Token::IntegerOctConstant
| Token::IntegerConstant => {
_ = tokens.next();
Ok(this.parse_integral_constant(&next, next.source_location()))
}
_ => Err(ErrorInfo {
error: ParseError::ExpectedConstantLiteral,
loc: tokens.current_source_location(),
}),
}
this.parse_expr(tokens)
// let next = tokens.peek_token().ok_or(ErrorInfo {
// error: ParseError::UnexpectedEndOfTokens,
// loc: tokens.current_source_location(),
// })?;
// match next.token() {
// Token::IntegerBinConstant
// | Token::IntegerHexConstant
// | Token::IntegerOctConstant
// | Token::IntegerConstant => {
// _ = tokens.next();
// Ok(this.parse_integral_constant(&next, next.source_location()))
// }
// _ => Err(ErrorInfo {
// error: ParseError::ExpectedConstantLiteral,
// loc: tokens.current_source_location(),
// }),
// }
})?;
let &[cnst, vol, noalias] =

View file

@ -1,4 +1,4 @@
fn main() {
var arr: [4] u8;
arr[0] = 1;
}
}

View file

@ -0,0 +1,6 @@
const ARRAY_SIZE: usize = 16;
fn main() {
var arr: [ARRAY_SIZE << 1] u8;
arr[0] = 1;
}

6
tests/legal/comptime.sea Normal file
View file

@ -0,0 +1,6 @@
const RANDOM: u32 = (3 + 54*5) >> 3;
fn main() -> u32 {
let a: u32 = (3 + 54*5) >> 3;
RANDOM
}