diff --git a/simple.program b/simple.program index dd5f5ba1e8..39835d769f 100644 --- a/simple.program +++ b/simple.program @@ -1,6 +1,9 @@ struct Foo { bool x } -def main() -> (bool): - Foo f = Foo {x: true} - return f.x +def main() -> (field): + field[3] a = [1, 2, 3] // initialize the array + field[4] b = [42; 4] + c = a[1..3] // helloo??? + bool[3] d = [true, false, true] + return a[0] diff --git a/src/aleo_program/constraints.rs b/src/aleo_program/constraints.rs index 0c753648a6..f2304c8488 100644 --- a/src/aleo_program/constraints.rs +++ b/src/aleo_program/constraints.rs @@ -742,10 +742,10 @@ impl ResolvedProgram { program .functions .into_iter() - .for_each(|(variable, function)| { + .for_each(|(function_name, function)| { resolved_program .resolved_variables - .insert(variable, ResolvedValue::Function(function)); + .insert(Variable(function_name.0), ResolvedValue::Function(function)); }); let main = resolved_program diff --git a/src/aleo_program/types.rs b/src/aleo_program/types.rs index 3f61518019..6b099c102e 100644 --- a/src/aleo_program/types.rs +++ b/src/aleo_program/types.rs @@ -151,9 +151,13 @@ pub struct Parameter { pub variable: Variable, } +/// The given name for a defined function in the program. +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct FunctionName(pub String); + #[derive(Clone)] pub struct Function { - pub variable: Variable, + pub function_name: FunctionName, pub parameters: Vec, pub returns: Vec, pub statements: Vec, @@ -164,7 +168,7 @@ pub struct Function { pub struct Program { pub id: String, pub structs: HashMap, - pub functions: HashMap, + pub functions: HashMap, // pub statements: Vec, pub arguments: Vec, pub returns: Vec, diff --git a/src/aleo_program/types_display.rs b/src/aleo_program/types_display.rs index 617557978a..f27dcc86ce 100644 --- a/src/aleo_program/types_display.rs +++ b/src/aleo_program/types_display.rs @@ -6,7 +6,7 @@ use crate::aleo_program::{ BooleanExpression, BooleanSpread, BooleanSpreadOrExpression, Expression, FieldExpression, - FieldRangeOrExpression, FieldSpread, FieldSpreadOrExpression, Function, Parameter, Statement, + FieldRangeOrExpression, FieldSpread, FieldSpreadOrExpression, Function, FunctionName, Parameter, Statement, Struct, StructField, Type, Variable, }; @@ -241,6 +241,12 @@ impl fmt::Debug for Parameter { } } +impl fmt::Debug for FunctionName { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.0) + } +} + impl fmt::Display for Function { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( diff --git a/src/aleo_program/types_from.rs b/src/aleo_program/types_from.rs index 8ba951c389..0db8ffe941 100644 --- a/src/aleo_program/types_from.rs +++ b/src/aleo_program/types_from.rs @@ -668,9 +668,15 @@ impl<'ast> From> for types::Parameter { } } +impl<'ast> From> for types::FunctionName { + fn from(name: ast::FunctionName<'ast>) -> Self { + types::FunctionName(name.value) + } +} + impl<'ast> From> for types::Function { fn from(function_definition: ast::Function<'ast>) -> Self { - let variable = types::Variable::from(function_definition.variable); + let function_name = types::FunctionName::from(function_definition.function_name); let parameters = function_definition .parameters .into_iter() @@ -688,7 +694,7 @@ impl<'ast> From> for types::Function { .collect(); types::Function { - variable, + function_name, parameters, returns, statements, @@ -710,7 +716,7 @@ impl<'ast> From> for types::Program { }); file.functions.into_iter().for_each(|function_def| { functions.insert( - types::Variable::from(function_def.variable.clone()), + types::FunctionName::from(function_def.function_name.clone()), types::Function::from(function_def), ); }); diff --git a/src/ast.rs b/src/ast.rs index 6fc9dbf50f..4539e63b9d 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -786,10 +786,19 @@ pub struct Parameter<'ast> { pub span: Span<'ast>, } +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::function_name))] +pub struct FunctionName<'ast> { + #[pest_ast(outer(with(span_into_string)))] + pub value: String, + #[pest_ast(outer())] + pub span: Span<'ast>, +} + #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::function_definition))] pub struct Function<'ast> { - pub variable: Variable<'ast>, + pub function_name: FunctionName<'ast>, pub parameters: Vec>, pub returns: Vec>, pub statements: Vec>, diff --git a/src/language.pest b/src/language.pest index e06adab820..dd2691ac6f 100644 --- a/src/language.pest +++ b/src/language.pest @@ -150,7 +150,9 @@ statement = { parameter = {visibility? ~ ty ~ variable} parameter_list = _{(parameter ~ ("," ~ parameter)*)?} -function_definition = {"def" ~ variable ~ "(" ~ parameter_list ~ ")" ~ "->" ~ "(" ~ type_list ~ ")" ~ ":" ~ NEWLINE* ~ statement* } + +function_name = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* } +function_definition = {"def" ~ function_name ~ "(" ~ parameter_list ~ ")" ~ "->" ~ "(" ~ type_list ~ ")" ~ ":" ~ NEWLINE* ~ statement* } /// Utilities