Fixed array expr
This commit is contained in:
+16
-18
@@ -48,24 +48,6 @@ impl GenRust for Literal {
|
|||||||
|
|
||||||
cg.add(")");
|
cg.add(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::Array(values) => {
|
|
||||||
cg.add("[");
|
|
||||||
|
|
||||||
for val in values {
|
|
||||||
val.gen_rust(ctx, cg);
|
|
||||||
}
|
|
||||||
|
|
||||||
cg.add("]");
|
|
||||||
}
|
|
||||||
|
|
||||||
Self::ArrayRepeat(value, repeat) => {
|
|
||||||
cg.add("[");
|
|
||||||
value.gen_rust(ctx, cg);
|
|
||||||
cg.add("; ");
|
|
||||||
repeat.gen_rust(ctx, cg);
|
|
||||||
cg.add("]");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,6 +66,22 @@ impl GenRust for Expression {
|
|||||||
Expression::Path(path) => cg.add(&path.get_rust()),
|
Expression::Path(path) => cg.add(&path.get_rust()),
|
||||||
Expression::Literal(literal) => literal.gen_rust(ctx, cg),
|
Expression::Literal(literal) => literal.gen_rust(ctx, cg),
|
||||||
Expression::Statement(stmt) => stmt.gen_rust(ctx, cg),
|
Expression::Statement(stmt) => stmt.gen_rust(ctx, cg),
|
||||||
|
Expression::Array(values) => {
|
||||||
|
cg.add("[");
|
||||||
|
|
||||||
|
for val in values {
|
||||||
|
val.gen_rust(ctx, cg);
|
||||||
|
}
|
||||||
|
|
||||||
|
cg.add("]");
|
||||||
|
}
|
||||||
|
Expression::ArrayRepeat(value, repeat) => {
|
||||||
|
cg.add("[");
|
||||||
|
value.gen_rust(ctx, cg);
|
||||||
|
cg.add("; ");
|
||||||
|
repeat.gen_rust(ctx, cg);
|
||||||
|
cg.add("]");
|
||||||
|
}
|
||||||
Expression::Fix {
|
Expression::Fix {
|
||||||
initial,
|
initial,
|
||||||
prefixes,
|
prefixes,
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ pub enum Expression {
|
|||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
Path(ExprPath),
|
Path(ExprPath),
|
||||||
Statement(Box<Statement>),
|
Statement(Box<Statement>),
|
||||||
|
Array(Vec<Expression>),
|
||||||
|
ArrayRepeat(Box<Expression>, Box<Expression>),
|
||||||
Closure {
|
Closure {
|
||||||
return_type: Option<TypeExpr>,
|
return_type: Option<TypeExpr>,
|
||||||
params: Vec<VarDecl>,
|
params: Vec<VarDecl>,
|
||||||
@@ -31,8 +33,6 @@ pub enum Literal {
|
|||||||
Float(f64),
|
Float(f64),
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
Tuple(Vec<Expression>),
|
Tuple(Vec<Expression>),
|
||||||
Array(Vec<Expression>),
|
|
||||||
ArrayRepeat(Box<Expression>, Box<Expression>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
|||||||
@@ -459,10 +459,12 @@ tuple = {
|
|||||||
"(" ~ (expr ~ ("," ~ expr)*)? ~ ","? ~ ")"
|
"(" ~ (expr ~ ("," ~ expr)*)? ~ ","? ~ ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
array = {
|
array_repeat = {
|
||||||
"[" ~ expr ~ ";" ~ expr ~ "]" // Repeat notation: [0; 10]
|
"[" ~ expr ~ ";" ~ expr ~ "]"
|
||||||
|
}
|
||||||
|
|
||||||
| "[" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ "]" // Standard elements initialization
|
array = {
|
||||||
|
"[" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
@@ -496,6 +498,7 @@ primary = {
|
|||||||
| literal
|
| literal
|
||||||
| expr_path
|
| expr_path
|
||||||
| tuple
|
| tuple
|
||||||
|
| array_repeat
|
||||||
| array
|
| array
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -624,3 +627,4 @@ bin_op = {
|
|||||||
expr = { term ~ (bin_op ~ term)* }
|
expr = { term ~ (bin_op ~ term)* }
|
||||||
|
|
||||||
term = { prefix* ~ primary ~ postfix* }
|
term = { prefix* ~ primary ~ postfix* }
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Rule::closure => {
|
Rule::closure => {
|
||||||
ast_expr!(Self::Closure {
|
ast_expr!(Expression::Closure {
|
||||||
return_type: consume_rule(&mut inner, Rule::type_expr)
|
return_type: consume_rule(&mut inner, Rule::type_expr)
|
||||||
.map(TypeExpr::try_from)
|
.map(TypeExpr::try_from)
|
||||||
.transpose(),
|
.transpose(),
|
||||||
@@ -85,6 +85,17 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Rule::array => {
|
||||||
|
ast_expr!(Expression::Array(collect_recovered(inner)))
|
||||||
|
}
|
||||||
|
|
||||||
|
Rule::array_repeat => {
|
||||||
|
ast_expr!(Expression::ArrayRepeat(
|
||||||
|
Expression::try_from(inner.next().unwrap()).map(Box::new),
|
||||||
|
Expression::try_from(inner.next().unwrap()).map(Box::new)
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
Rule::primary => pair.into_inner().next().unwrap().try_into(),
|
Rule::primary => pair.into_inner().next().unwrap().try_into(),
|
||||||
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
|
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
|
||||||
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
|
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
|
||||||
|
|||||||
Reference in New Issue
Block a user