Fix the parser, make the function name optional.

This commit is contained in:
2023-10-07 11:25:49 +02:00
parent 736d27953f
commit ca26d0ea40
4 changed files with 17 additions and 4 deletions

View File

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

View File

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

View File

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

View File

@@ -117,7 +117,9 @@ impl TopLevel {
match self {
TopLevel::Function(name, arguments, body) => {
bound_variables.new_scope();
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());
}