Rename Function's name from 'variable' to 'function_name', and adds FunctionName type in pest and types

This commit is contained in:
howardwu 2020-04-15 23:58:47 -07:00
parent 59d2fa861a
commit 0a8cda3d7f
7 changed files with 43 additions and 13 deletions

View File

@ -1,6 +1,9 @@
struct Foo { struct Foo {
bool x bool x
} }
def main() -> (bool): def main() -> (field):
Foo f = Foo {x: true} field[3] a = [1, 2, 3] // initialize the array
return f.x field[4] b = [42; 4]
c = a[1..3] // helloo???
bool[3] d = [true, false, true]
return a[0]

View File

@ -742,10 +742,10 @@ impl ResolvedProgram {
program program
.functions .functions
.into_iter() .into_iter()
.for_each(|(variable, function)| { .for_each(|(function_name, function)| {
resolved_program resolved_program
.resolved_variables .resolved_variables
.insert(variable, ResolvedValue::Function(function)); .insert(Variable(function_name.0), ResolvedValue::Function(function));
}); });
let main = resolved_program let main = resolved_program

View File

@ -151,9 +151,13 @@ pub struct Parameter {
pub variable: Variable, 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)] #[derive(Clone)]
pub struct Function { pub struct Function {
pub variable: Variable, pub function_name: FunctionName,
pub parameters: Vec<Parameter>, pub parameters: Vec<Parameter>,
pub returns: Vec<Type>, pub returns: Vec<Type>,
pub statements: Vec<Statement>, pub statements: Vec<Statement>,
@ -164,7 +168,7 @@ pub struct Function {
pub struct Program { pub struct Program {
pub id: String, pub id: String,
pub structs: HashMap<Variable, Struct>, pub structs: HashMap<Variable, Struct>,
pub functions: HashMap<Variable, Function>, pub functions: HashMap<FunctionName, Function>,
// pub statements: Vec<Statement>, // pub statements: Vec<Statement>,
pub arguments: Vec<Variable>, pub arguments: Vec<Variable>,
pub returns: Vec<Variable>, pub returns: Vec<Variable>,

View File

@ -6,7 +6,7 @@
use crate::aleo_program::{ use crate::aleo_program::{
BooleanExpression, BooleanSpread, BooleanSpreadOrExpression, Expression, FieldExpression, BooleanExpression, BooleanSpread, BooleanSpreadOrExpression, Expression, FieldExpression,
FieldRangeOrExpression, FieldSpread, FieldSpreadOrExpression, Function, Parameter, Statement, FieldRangeOrExpression, FieldSpread, FieldSpreadOrExpression, Function, FunctionName, Parameter, Statement,
Struct, StructField, Type, Variable, 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 { impl fmt::Display for Function {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!( write!(

View File

@ -668,9 +668,15 @@ impl<'ast> From<ast::Parameter<'ast>> for types::Parameter {
} }
} }
impl<'ast> From<ast::FunctionName<'ast>> for types::FunctionName {
fn from(name: ast::FunctionName<'ast>) -> Self {
types::FunctionName(name.value)
}
}
impl<'ast> From<ast::Function<'ast>> for types::Function { impl<'ast> From<ast::Function<'ast>> for types::Function {
fn from(function_definition: ast::Function<'ast>) -> Self { 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 let parameters = function_definition
.parameters .parameters
.into_iter() .into_iter()
@ -688,7 +694,7 @@ impl<'ast> From<ast::Function<'ast>> for types::Function {
.collect(); .collect();
types::Function { types::Function {
variable, function_name,
parameters, parameters,
returns, returns,
statements, statements,
@ -710,7 +716,7 @@ impl<'ast> From<ast::File<'ast>> for types::Program {
}); });
file.functions.into_iter().for_each(|function_def| { file.functions.into_iter().for_each(|function_def| {
functions.insert( functions.insert(
types::Variable::from(function_def.variable.clone()), types::FunctionName::from(function_def.function_name.clone()),
types::Function::from(function_def), types::Function::from(function_def),
); );
}); });

View File

@ -786,10 +786,19 @@ pub struct Parameter<'ast> {
pub span: Span<'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)] #[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::function_definition))] #[pest_ast(rule(Rule::function_definition))]
pub struct Function<'ast> { pub struct Function<'ast> {
pub variable: Variable<'ast>, pub function_name: FunctionName<'ast>,
pub parameters: Vec<Parameter<'ast>>, pub parameters: Vec<Parameter<'ast>>,
pub returns: Vec<Type<'ast>>, pub returns: Vec<Type<'ast>>,
pub statements: Vec<Statement<'ast>>, pub statements: Vec<Statement<'ast>>,

View File

@ -150,7 +150,9 @@ statement = {
parameter = {visibility? ~ ty ~ variable} parameter = {visibility? ~ ty ~ variable}
parameter_list = _{(parameter ~ ("," ~ parameter)*)?} 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 /// Utilities