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 {
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]

View File

@ -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

View File

@ -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<Parameter>,
pub returns: Vec<Type>,
pub statements: Vec<Statement>,
@ -164,7 +168,7 @@ pub struct Function {
pub struct Program {
pub id: String,
pub structs: HashMap<Variable, Struct>,
pub functions: HashMap<Variable, Function>,
pub functions: HashMap<FunctionName, Function>,
// pub statements: Vec<Statement>,
pub arguments: Vec<Variable>,
pub returns: Vec<Variable>,

View File

@ -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!(

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 {
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<ast::Function<'ast>> for types::Function {
.collect();
types::Function {
variable,
function_name,
parameters,
returns,
statements,
@ -710,7 +716,7 @@ impl<'ast> From<ast::File<'ast>> 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),
);
});

View File

@ -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<Parameter<'ast>>,
pub returns: Vec<Type<'ast>>,
pub statements: Vec<Statement<'ast>>,

View File

@ -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