λ Support functions! #5
@@ -27,7 +27,7 @@ pub struct Program {
|
|||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum TopLevel {
|
pub enum TopLevel {
|
||||||
Statement(Statement),
|
Statement(Statement),
|
||||||
Function(Name, Vec<Name>, Expression),
|
Function(Option<Name>, Vec<Name>, Expression),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A Name.
|
/// A Name.
|
||||||
|
|||||||
@@ -76,7 +76,14 @@ pub TopLevel: TopLevel = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Function: 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> = {
|
Arguments: Vec<Name> = {
|
||||||
|
|||||||
@@ -33,7 +33,11 @@ where
|
|||||||
TopLevel::Function(name, arg_names, body) => allocator
|
TopLevel::Function(name, arg_names, body) => allocator
|
||||||
.text("function")
|
.text("function")
|
||||||
.append(allocator.space())
|
.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(
|
.append(
|
||||||
allocator
|
allocator
|
||||||
.intersperse(
|
.intersperse(
|
||||||
|
|||||||
@@ -117,7 +117,9 @@ impl TopLevel {
|
|||||||
match self {
|
match self {
|
||||||
TopLevel::Function(name, arguments, body) => {
|
TopLevel::Function(name, arguments, body) => {
|
||||||
bound_variables.new_scope();
|
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() {
|
for arg in arguments.iter() {
|
||||||
bound_variables.insert(arg.name.clone(), arg.location.clone());
|
bound_variables.insert(arg.name.clone(), arg.location.clone());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user