mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-29 11:43:28 +03:00
add function and {}
This commit is contained in:
parent
ac9707da92
commit
2255bac5a1
10
simple.leo
10
simple.leo
@ -1,6 +1,4 @@
|
||||
def main() -> (fe):
|
||||
a = 1fe
|
||||
for i in 0..4 do
|
||||
a = a + 1fe
|
||||
endfor
|
||||
return a
|
||||
function main() -> (u32) {
|
||||
a = 1 + 1
|
||||
return a
|
||||
}
|
116
src/ast.rs
116
src/ast.rs
@ -56,19 +56,19 @@ pub struct Not<'ast> {
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::operation_post_increment))]
|
||||
pub struct Increment<'ast> {
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::operation_post_decrement))]
|
||||
pub struct Decrement<'ast> {
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
// #[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
// #[pest_ast(rule(Rule::operation_post_increment))]
|
||||
// pub struct Increment<'ast> {
|
||||
// #[pest_ast(outer())]
|
||||
// pub span: Span<'ast>,
|
||||
// }
|
||||
//
|
||||
// #[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
// #[pest_ast(rule(Rule::operation_post_decrement))]
|
||||
// pub struct Decrement<'ast> {
|
||||
// #[pest_ast(outer())]
|
||||
// pub span: Span<'ast>,
|
||||
// }
|
||||
|
||||
// Binary Operations
|
||||
|
||||
@ -493,23 +493,23 @@ pub struct NotExpression<'ast> {
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_increment))]
|
||||
pub struct IncrementExpression<'ast> {
|
||||
pub expression: Box<Expression<'ast>>,
|
||||
pub operation: Increment<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_decrement))]
|
||||
pub struct DecrementExpression<'ast> {
|
||||
pub expression: Box<Expression<'ast>>,
|
||||
pub operation: Decrement<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
// #[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
// #[pest_ast(rule(Rule::expression_increment))]
|
||||
// pub struct IncrementExpression<'ast> {
|
||||
// pub expression: Box<Expression<'ast>>,
|
||||
// pub operation: Increment<'ast>,
|
||||
// #[pest_ast(outer())]
|
||||
// pub span: Span<'ast>,
|
||||
// }
|
||||
//
|
||||
// #[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
// #[pest_ast(rule(Rule::expression_decrement))]
|
||||
// pub struct DecrementExpression<'ast> {
|
||||
// pub expression: Box<Expression<'ast>>,
|
||||
// pub operation: Decrement<'ast>,
|
||||
// #[pest_ast(outer())]
|
||||
// pub span: Span<'ast>,
|
||||
// }
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct BinaryExpression<'ast> {
|
||||
@ -534,8 +534,8 @@ pub enum Expression<'ast> {
|
||||
Value(Value<'ast>),
|
||||
Variable(Variable<'ast>),
|
||||
Not(NotExpression<'ast>),
|
||||
Increment(IncrementExpression<'ast>),
|
||||
Decrement(DecrementExpression<'ast>),
|
||||
// Increment(IncrementExpression<'ast>),
|
||||
// Decrement(DecrementExpression<'ast>),
|
||||
Binary(BinaryExpression<'ast>),
|
||||
Ternary(TernaryExpression<'ast>),
|
||||
ArrayInline(ArrayInlineExpression<'ast>),
|
||||
@ -578,8 +578,8 @@ impl<'ast> Expression<'ast> {
|
||||
Expression::Value(expression) => &expression.span(),
|
||||
Expression::Variable(expression) => &expression.span,
|
||||
Expression::Not(expression) => &expression.span,
|
||||
Expression::Increment(expression) => &expression.span,
|
||||
Expression::Decrement(expression) => &expression.span,
|
||||
// Expression::Increment(expression) => &expression.span,
|
||||
// Expression::Decrement(expression) => &expression.span,
|
||||
Expression::Binary(expression) => &expression.span,
|
||||
Expression::Ternary(expression) => &expression.span,
|
||||
Expression::ArrayInline(expression) => &expression.span,
|
||||
@ -596,8 +596,8 @@ impl<'ast> fmt::Display for Expression<'ast> {
|
||||
Expression::Value(ref expression) => write!(f, "{}", expression),
|
||||
Expression::Variable(ref expression) => write!(f, "{}", expression),
|
||||
Expression::Not(ref expression) => write!(f, "!{}", expression.expression),
|
||||
Expression::Increment(ref expression) => write!(f, "{}++", expression.expression),
|
||||
Expression::Decrement(ref expression) => write!(f, "{}--", expression.expression),
|
||||
// Expression::Increment(ref expression) => write!(f, "{}++", expression.expression),
|
||||
// Expression::Decrement(ref expression) => write!(f, "{}--", expression.expression),
|
||||
Expression::Binary(ref expression) => {
|
||||
write!(f, "{} == {}", expression.left, expression.right)
|
||||
}
|
||||
@ -683,28 +683,28 @@ fn parse_term(pair: Pair<Rule>) -> Box<Expression> {
|
||||
let expression = parse_term(inner.next().unwrap());
|
||||
Expression::Not(NotExpression { operation, expression, span })
|
||||
},
|
||||
Rule::expression_increment => {
|
||||
println!("expression increment");
|
||||
let span = next.as_span();
|
||||
let mut inner = next.into_inner();
|
||||
let expression = parse_term(inner.next().unwrap());
|
||||
let operation = match inner.next().unwrap().as_rule() {
|
||||
Rule::operation_post_increment => Increment::from_pest(&mut pair.into_inner().next().unwrap().into_inner()).unwrap(),
|
||||
rule => unreachable!("`expression_increment` should yield `operation_post_increment`, found {:#?}", rule)
|
||||
};
|
||||
Expression::Increment(IncrementExpression { operation, expression, span })
|
||||
},
|
||||
Rule::expression_decrement => {
|
||||
println!("expression decrement");
|
||||
let span = next.as_span();
|
||||
let mut inner = next.into_inner();
|
||||
let expression = parse_term(inner.next().unwrap());
|
||||
let operation = match inner.next().unwrap().as_rule() {
|
||||
Rule::operation_post_decrement => Decrement::from_pest(&mut pair.into_inner().next().unwrap().into_inner()).unwrap(),
|
||||
rule => unreachable!("`expression_decrement` should yield `operation_post_decrement`, found {:#?}", rule)
|
||||
};
|
||||
Expression::Decrement(DecrementExpression { operation, expression, span })
|
||||
},
|
||||
// Rule::expression_increment => {
|
||||
// println!("expression increment");
|
||||
// // let span = next.as_span();
|
||||
// // let mut inner = next.into_inner();
|
||||
// // let expression = parse_term(inner.next().unwrap());
|
||||
// // let operation = match inner.next().unwrap().as_rule() {
|
||||
// // Rule::operation_post_increment => Increment::from_pest(&mut pair.into_inner().next().unwrap().into_inner()).unwrap(),
|
||||
// // rule => unreachable!("`expression_increment` should yield `operation_post_increment`, found {:#?}", rule)
|
||||
// // };
|
||||
// // Expression::Increment(IncrementExpression { operation, expression, span })
|
||||
// },
|
||||
// Rule::expression_decrement => {
|
||||
// println!("expression decrement");
|
||||
// // let span = next.as_span();
|
||||
// // let mut inner = next.into_inner();
|
||||
// // let expression = parse_term(inner.next().unwrap());
|
||||
// // let operation = match inner.next().unwrap().as_rule() {
|
||||
// // Rule::operation_post_decrement => Decrement::from_pest(&mut pair.into_inner().next().unwrap().into_inner()).unwrap(),
|
||||
// // rule => unreachable!("`expression_decrement` should yield `operation_post_decrement`, found {:#?}", rule)
|
||||
// // };
|
||||
// // Expression::Decrement(DecrementExpression { operation, expression, span })
|
||||
// },
|
||||
Rule::expression_postfix => {
|
||||
Expression::Postfix(
|
||||
PostfixExpression::from_pest(&mut pair.into_inner()).unwrap(),
|
||||
|
16
src/leo.pest
16
src/leo.pest
@ -9,11 +9,11 @@ visibility = { visibility_public | visibility_private }
|
||||
operation_pre_not = { "!" }
|
||||
expression_not = { operation_pre_not ~ expression_term }
|
||||
|
||||
operation_post_increment = { "++" }
|
||||
expression_increment = { operation_post_increment ~ expression_term }
|
||||
|
||||
operation_post_decrement = { "--" }
|
||||
expression_decrement = { operation_post_decrement ~ expression_term }
|
||||
// operation_post_increment = { "++" }
|
||||
// expression_increment = { operation_post_increment ~ expression_term }
|
||||
//
|
||||
// operation_post_decrement = { "--" }
|
||||
// expression_decrement = { operation_post_decrement ~ expression_term }
|
||||
|
||||
/// Binary Operations
|
||||
|
||||
@ -128,8 +128,8 @@ expression_term = {
|
||||
| expression_postfix
|
||||
| expression_primitive
|
||||
| expression_not
|
||||
| expression_increment
|
||||
| expression_decrement
|
||||
// | expression_increment
|
||||
// | expression_decrement
|
||||
| expression_array_inline
|
||||
| expression_array_initializer
|
||||
}
|
||||
@ -159,7 +159,7 @@ parameter = {variable ~ ":" ~ visibility? ~ ty}
|
||||
parameter_list = _{(parameter ~ ("," ~ parameter)*)?}
|
||||
|
||||
function_name = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||
function_definition = {"def" ~ function_name ~ "(" ~ parameter_list ~ ")" ~ "->" ~ "(" ~ type_list ~ ")" ~ ":" ~ NEWLINE* ~ statement* }
|
||||
function_definition = {"function" ~ function_name ~ "(" ~ parameter_list ~ ")" ~ "->" ~ "(" ~ type_list ~ ")" ~ "{" ~ NEWLINE* ~ statement* ~ "}"}
|
||||
|
||||
/// Utilities
|
||||
|
||||
|
@ -398,8 +398,7 @@ impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ResolvedProgram<F, CS> {
|
||||
// Functions
|
||||
Expression::FunctionCall(function, arguments) => {
|
||||
self.enforce_function_access_expression(cs, scope, function, arguments)
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
} // _ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ impl<F: Field + PrimeField> ResolvedValue<F> {
|
||||
(ResolvedValue::U32(ref _a), Type::U32) => true,
|
||||
(ResolvedValue::FieldElement(ref _a), Type::FieldElement) => true,
|
||||
(ResolvedValue::Boolean(ref _a), Type::Boolean) => true,
|
||||
(ResolvedValue::Array(ref _arr), Type::Array(ref _ty, ref _len)) => true, // todo: add array types
|
||||
(ResolvedValue::Array(ref arr), Type::Array(ref _ty, ref len)) => arr.len() == *len, // todo: add array types
|
||||
(
|
||||
ResolvedValue::StructExpression(ref actual_name, ref _members),
|
||||
Type::Struct(ref expected_name),
|
||||
|
@ -269,7 +269,7 @@ impl<'ast, F: Field + PrimeField> From<ast::Expression<'ast>> for types::Express
|
||||
unimplemented!("unknown type for inline struct expression")
|
||||
}
|
||||
ast::Expression::Postfix(expression) => types::Expression::from(expression),
|
||||
_ => unimplemented!(),
|
||||
// _ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user