merge dev
This commit is contained in:
commit
e7142cfb90
5 changed files with 49 additions and 13 deletions
|
@ -1,5 +1,6 @@
|
|||
def main() -> (field):
|
||||
field[3] a = [1, 2, 3]
|
||||
field[4] b = [...a, 4]
|
||||
a[2] = 4
|
||||
return a[0] + a[2]
|
||||
field[3] a = [1, 2, 3] // initialize an array with values
|
||||
a[2] = 4 // set a member to a value
|
||||
field[4] b = [42; 4] // initialize an array of 4 values all equal to 42
|
||||
field[4] c = [...a, 4] // initialize an array copying values from `a`, followed by 4
|
||||
return a[0] + b[1] + c[2]
|
|
@ -257,6 +257,7 @@ impl<'ast, T: Field> From<pest::Expression<'ast>> for absy::ExpressionNode<'ast,
|
|||
pest::Expression::Identifier(e) => absy::ExpressionNode::from(e),
|
||||
pest::Expression::Postfix(e) => absy::ExpressionNode::from(e),
|
||||
pest::Expression::InlineArray(e) => absy::ExpressionNode::from(e),
|
||||
pest::Expression::ArrayInitializer(e) => absy::ExpressionNode::from(e),
|
||||
pest::Expression::Unary(e) => absy::ExpressionNode::from(e),
|
||||
}
|
||||
}
|
||||
|
@ -371,6 +372,23 @@ impl<'ast, T: Field> From<pest::InlineArrayExpression<'ast>> for absy::Expressio
|
|||
}
|
||||
}
|
||||
|
||||
impl<'ast, T: Field> From<pest::ArrayInitializerExpression<'ast>>
|
||||
for absy::ExpressionNode<'ast, T>
|
||||
{
|
||||
fn from(initializer: pest::ArrayInitializerExpression<'ast>) -> absy::ExpressionNode<'ast, T> {
|
||||
use absy::NodeValue;
|
||||
|
||||
let value = absy::ExpressionNode::from(*initializer.value);
|
||||
let count: absy::ExpressionNode<T> = absy::ExpressionNode::from(initializer.count);
|
||||
let count = match count.value {
|
||||
absy::Expression::Number(v) => v.to_dec_string().parse::<usize>().unwrap(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
absy::Expression::InlineArray(vec![absy::SpreadOrExpression::Expression(value); count])
|
||||
.span(initializer.span)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, T: Field> From<pest::UnaryExpression<'ast>> for absy::ExpressionNode<'ast, T> {
|
||||
fn from(unary: pest::UnaryExpression<'ast>) -> absy::ExpressionNode<'ast, T> {
|
||||
use absy::NodeValue;
|
||||
|
|
|
@ -48,7 +48,7 @@ optionally_typed_identifier = { ty? ~ identifier }
|
|||
expression_list = _{(expression ~ ("," ~ expression)*)?}
|
||||
|
||||
expression = { term ~ (op_binary ~ term)* }
|
||||
term = { ("(" ~ expression ~ ")") | conditional_expression | postfix_expression | primary_expression | inline_array_expression | unary_expression }
|
||||
term = { ("(" ~ expression ~ ")") | conditional_expression | postfix_expression | primary_expression | inline_array_expression | array_initializer_expression | unary_expression }
|
||||
spread = { "..." ~ expression }
|
||||
|
||||
conditional_expression = { "if" ~ expression ~ "then" ~ expression ~ "else" ~ expression ~ "fi"}
|
||||
|
@ -65,6 +65,8 @@ primary_expression = { identifier
|
|||
inline_array_expression = { "[" ~ inline_array_inner ~ "]" }
|
||||
inline_array_inner = _{(spread_or_expression ~ ("," ~ spread_or_expression)*)?}
|
||||
spread_or_expression = { spread | expression }
|
||||
array_initializer_expression = { "[" ~ expression ~ ";" ~ constant ~ "]" }
|
||||
|
||||
unary_expression = { op_unary ~ term }
|
||||
|
||||
// End Expressions
|
||||
|
|
|
@ -8,12 +8,13 @@ use zokrates_parser::Rule;
|
|||
extern crate lazy_static;
|
||||
|
||||
pub use ast::{
|
||||
Access, ArrayAccess, ArrayType, AssertionStatement, Assignee, AssignmentStatement, BasicType,
|
||||
BinaryExpression, BinaryOperator, CallAccess, ConstantExpression, DefinitionStatement,
|
||||
Expression, File, Function, IdentifierExpression, ImportDirective, ImportSource,
|
||||
InlineArrayExpression, IterationStatement, MultiAssignmentStatement, Parameter,
|
||||
PostfixExpression, ReturnStatement, Span, Spread, SpreadOrExpression, Statement,
|
||||
TernaryExpression, Type, UnaryExpression, UnaryOperator, Visibility,
|
||||
Access, ArrayAccess, ArrayInitializerExpression, ArrayType, AssertionStatement, Assignee,
|
||||
AssignmentStatement, BasicType, BinaryExpression, BinaryOperator, CallAccess,
|
||||
ConstantExpression, DefinitionStatement, Expression, File, Function, IdentifierExpression,
|
||||
ImportDirective, ImportSource, InlineArrayExpression, IterationStatement,
|
||||
MultiAssignmentStatement, Parameter, PostfixExpression, ReturnStatement, Span, Spread,
|
||||
SpreadOrExpression, Statement, TernaryExpression, Type, UnaryExpression, UnaryOperator,
|
||||
Visibility,
|
||||
};
|
||||
|
||||
mod ast {
|
||||
|
@ -123,6 +124,9 @@ mod ast {
|
|||
Rule::inline_array_expression => Expression::InlineArray(
|
||||
InlineArrayExpression::from_pest(&mut pair.into_inner()).unwrap(),
|
||||
),
|
||||
Rule::array_initializer_expression => Expression::ArrayInitializer(
|
||||
ArrayInitializerExpression::from_pest(&mut pair.into_inner()).unwrap()
|
||||
),
|
||||
Rule::unary_expression => {
|
||||
let span = next.as_span();
|
||||
let mut inner = next.into_inner();
|
||||
|
@ -137,7 +141,7 @@ mod ast {
|
|||
span
|
||||
})
|
||||
},
|
||||
r => unreachable!("`term` should contain one of [`expression`, `conditional_expression`, `primary_expression`, `postfix_expression`, `inline_array_expression`, `unary_expression`], found {:#?}", r)
|
||||
r => unreachable!("`term` should contain one of [`expression`, `conditional_expression`, `primary_expression`, `postfix_expression`, `inline_array_expression`, `unary_expression`, `array_initializer_expression`], found {:#?}", r)
|
||||
}
|
||||
}
|
||||
r => unreachable!(
|
||||
|
@ -352,6 +356,7 @@ mod ast {
|
|||
Identifier(IdentifierExpression<'ast>),
|
||||
Constant(ConstantExpression<'ast>),
|
||||
InlineArray(InlineArrayExpression<'ast>),
|
||||
ArrayInitializer(ArrayInitializerExpression<'ast>),
|
||||
Unary(UnaryExpression<'ast>),
|
||||
}
|
||||
|
||||
|
@ -396,6 +401,15 @@ mod ast {
|
|||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Debug, FromPest, PartialEq, Clone)]
|
||||
#[pest_ast(rule(Rule::array_initializer_expression))]
|
||||
pub struct ArrayInitializerExpression<'ast> {
|
||||
pub value: Box<Expression<'ast>>,
|
||||
pub count: ConstantExpression<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Debug, FromPest, PartialEq, Clone)]
|
||||
#[pest_ast(rule(Rule::optionally_typed_identifier))]
|
||||
pub struct OptionallyTypedIdentifier<'ast> {
|
||||
|
@ -483,6 +497,7 @@ mod ast {
|
|||
Expression::Ternary(t) => &t.span,
|
||||
Expression::Postfix(p) => &p.span,
|
||||
Expression::InlineArray(a) => &a.span,
|
||||
Expression::ArrayInitializer(a) => &a.span,
|
||||
Expression::Unary(u) => &u.span,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
def main(field selector, field[256] lhs, field[256] rhs) -> (field[256]):
|
||||
|
||||
field[256] out = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
field[256] out = [0; 256]
|
||||
|
||||
for field i in 0..256 do
|
||||
out[i] = if selector == 0 then lhs[i] else rhs[i] fi
|
||||
|
|
Loading…
Reference in a new issue