λ Support functions! #5
@@ -27,7 +27,7 @@ pub struct Program {
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum TopLevel {
|
||||
Statement(Statement),
|
||||
Function(Name, Vec<Name>, Expression),
|
||||
Function(Option<Name>, Vec<Name>, Expression),
|
||||
}
|
||||
|
||||
/// A Name.
|
||||
|
||||
@@ -76,7 +76,14 @@ pub TopLevel: TopLevel = {
|
||||
}
|
||||
|
||||
Function: TopLevel = {
|
||||
"function" "(" Arguments OptionalComma ")" Expression => unimplemented!(),
|
||||
"function" <opt_name:OptionalName> "(" <args:Arguments> OptionalComma ")" <exp:Expression> =>
|
||||
TopLevel::Function(opt_name, args, exp),
|
||||
}
|
||||
|
||||
OptionalName: Option<Name> = {
|
||||
<name_start: @L> <v:"<var>"> <name_end: @L> =>
|
||||
Some(Name::new(v, Location::new(file_idx, name_start..name_end))),
|
||||
=> None,
|
||||
}
|
||||
|
||||
Arguments: Vec<Name> = {
|
||||
|
||||
@@ -33,7 +33,11 @@ where
|
||||
TopLevel::Function(name, arg_names, body) => allocator
|
||||
.text("function")
|
||||
.append(allocator.space())
|
||||
.append(allocator.text(name.to_string()))
|
||||
.append(
|
||||
name.as_ref()
|
||||
.map(|x| allocator.text(x.to_string()))
|
||||
.unwrap_or_else(|| allocator.nil()),
|
||||
)
|
||||
.append(
|
||||
allocator
|
||||
.intersperse(
|
||||
|
||||
@@ -117,7 +117,9 @@ impl TopLevel {
|
||||
match self {
|
||||
TopLevel::Function(name, arguments, body) => {
|
||||
bound_variables.new_scope();
|
||||
bound_variables.insert(name.name.clone(), name.location.clone());
|
||||
if let Some(name) = name {
|
||||
bound_variables.insert(name.name.clone(), name.location.clone());
|
||||
}
|
||||
for arg in arguments.iter() {
|
||||
bound_variables.insert(arg.name.clone(), arg.location.clone());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user