Compare commits

...

3 Commits

Author SHA1 Message Date
211cca50b8 Update readme and tests 2025-07-24 22:06:58 +03:00
0e63219205 Implement shorthand for array definition 2025-07-24 22:04:48 +03:00
1ec9bdb166 Update readme 2025-07-24 21:55:22 +03:00
6 changed files with 53 additions and 9 deletions

View File

@ -60,8 +60,8 @@ Big features that I want later but are not necessary:
Smaller features:
- ~~Hex-numbers~~
- Bitwise operations
- Easier way to initialize arrays with a single value
- Void-returns (`return;` for void-returning functions)
- ~~Easier way to initialize arrays with a single value~~
- ~~Void-returns (`return;` for void-returning functions)~~
- ~~Only include standard library at all if it is imported~~
- Lexical scopes for Debug Information

View File

@ -0,0 +1,7 @@
// Arithmetic, function calls and imports!
fn main() -> u16 {
let mut a = [5; 20];
return a[15];
}

View File

@ -57,6 +57,7 @@ pub enum ExpressionKind {
Deref(String),
Literal(Literal),
Array(Vec<Expression>),
ArrayShort(Box<Expression>, u64),
/// Array-indexed, e.g. <expr>[<expr>]
Indexed(Box<Expression>, Box<Expression>),
/// Struct-accessed, e.g. <expr>.<expr>

View File

@ -250,16 +250,37 @@ impl Parse for PrimaryExpression {
}
Token::BracketOpen => {
stream.next(); // Consume
let mut expressions = Vec::new();
if let Ok(exp) = stream.parse() {
expressions.push(exp);
while let Some(Token::Comma) = stream.peek() {
stream.next(); // Consume comma
expressions.push(stream.parse()?);
if let Some(Token::Semi) = stream.peek() {
stream.next(); // Consume colon
let Some(Token::DecimalValue(val)) = stream.next() else {
return Err(stream
.expecting_err("decimal value describing array length")?);
};
stream.expect(Token::BracketClose)?;
Expression(
Kind::ArrayShort(
Box::new(exp),
u64::from_str_radix(&val, 10).expect(
"Unable to parse array length to 64-bit decimal value",
),
),
stream.get_range().unwrap(),
)
} else {
let mut expressions = Vec::new();
expressions.push(exp);
while let Some(Token::Comma) = stream.peek() {
stream.next(); // Consume comma
expressions.push(stream.parse()?);
}
stream.expect(Token::BracketClose)?;
Expression(Kind::Array(expressions), stream.get_range().unwrap())
}
} else {
stream.expect(Token::BraceClose)?;
Expression(Kind::Array(Vec::new()), stream.get_range().unwrap())
}
stream.expect(Token::BracketClose)?;
Expression(Kind::Array(expressions), stream.get_range().unwrap())
}
_ => Err(stream.expecting_err("expression")?)?,
}

View File

@ -378,6 +378,12 @@ impl ast::Expression {
Box::new(expression.process(module_id)),
ty.0.clone().into_mir(module_id),
),
ast::ExpressionKind::ArrayShort(expression, len) => mir::ExprKind::Array(
vec![*expression.clone(); *len as usize]
.iter()
.map(|e| e.process(module_id))
.collect(),
),
};
mir::Expression(kind, self.1.as_meta(module_id))

View File

@ -182,3 +182,12 @@ fn custom_binop_compiles_well() {
Some(21),
);
}
#[test]
fn array_short_compiles_well() {
test(
include_str!("../../examples/array_short.reid"),
"test",
Some(5),
);
}