Compare commits
3 Commits
be3c415a57
...
211cca50b8
Author | SHA1 | Date | |
---|---|---|---|
211cca50b8 | |||
0e63219205 | |||
1ec9bdb166 |
@ -60,8 +60,8 @@ Big features that I want later but are not necessary:
|
|||||||
Smaller features:
|
Smaller features:
|
||||||
- ~~Hex-numbers~~
|
- ~~Hex-numbers~~
|
||||||
- Bitwise operations
|
- Bitwise operations
|
||||||
- Easier way to initialize arrays with a single value
|
- ~~Easier way to initialize arrays with a single value~~
|
||||||
- Void-returns (`return;` for void-returning functions)
|
- ~~Void-returns (`return;` for void-returning functions)~~
|
||||||
- ~~Only include standard library at all if it is imported~~
|
- ~~Only include standard library at all if it is imported~~
|
||||||
- Lexical scopes for Debug Information
|
- Lexical scopes for Debug Information
|
||||||
|
|
||||||
|
7
examples/array_short.reid
Normal file
7
examples/array_short.reid
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
// Arithmetic, function calls and imports!
|
||||||
|
|
||||||
|
fn main() -> u16 {
|
||||||
|
let mut a = [5; 20];
|
||||||
|
|
||||||
|
return a[15];
|
||||||
|
}
|
@ -57,6 +57,7 @@ pub enum ExpressionKind {
|
|||||||
Deref(String),
|
Deref(String),
|
||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
Array(Vec<Expression>),
|
Array(Vec<Expression>),
|
||||||
|
ArrayShort(Box<Expression>, u64),
|
||||||
/// Array-indexed, e.g. <expr>[<expr>]
|
/// Array-indexed, e.g. <expr>[<expr>]
|
||||||
Indexed(Box<Expression>, Box<Expression>),
|
Indexed(Box<Expression>, Box<Expression>),
|
||||||
/// Struct-accessed, e.g. <expr>.<expr>
|
/// Struct-accessed, e.g. <expr>.<expr>
|
||||||
|
@ -250,16 +250,37 @@ impl Parse for PrimaryExpression {
|
|||||||
}
|
}
|
||||||
Token::BracketOpen => {
|
Token::BracketOpen => {
|
||||||
stream.next(); // Consume
|
stream.next(); // Consume
|
||||||
let mut expressions = Vec::new();
|
|
||||||
if let Ok(exp) = stream.parse() {
|
if let Ok(exp) = stream.parse() {
|
||||||
expressions.push(exp);
|
if let Some(Token::Semi) = stream.peek() {
|
||||||
while let Some(Token::Comma) = stream.peek() {
|
stream.next(); // Consume colon
|
||||||
stream.next(); // Consume comma
|
let Some(Token::DecimalValue(val)) = stream.next() else {
|
||||||
expressions.push(stream.parse()?);
|
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")?)?,
|
_ => Err(stream.expecting_err("expression")?)?,
|
||||||
}
|
}
|
||||||
|
@ -378,6 +378,12 @@ impl ast::Expression {
|
|||||||
Box::new(expression.process(module_id)),
|
Box::new(expression.process(module_id)),
|
||||||
ty.0.clone().into_mir(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))
|
mir::Expression(kind, self.1.as_meta(module_id))
|
||||||
|
@ -182,3 +182,12 @@ fn custom_binop_compiles_well() {
|
|||||||
Some(21),
|
Some(21),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn array_short_compiles_well() {
|
||||||
|
test(
|
||||||
|
include_str!("../../examples/array_short.reid"),
|
||||||
|
"test",
|
||||||
|
Some(5),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user