Add support for syntax blocks.

This commit is contained in:
2024-03-06 09:50:42 -08:00
parent 8dab797c90
commit ac564e6e41
11 changed files with 39 additions and 5 deletions

View File

@@ -95,6 +95,7 @@ impl fmt::Display for Name {
pub enum Statement {
Binding(Location, Name, Expression),
Print(Location, Name),
Expression(Expression),
}
impl PartialEq for Statement {
@@ -108,6 +109,10 @@ impl PartialEq for Statement {
Statement::Print(_, name2) => name1 == name2,
_ => false,
},
Statement::Expression(e1) => match other {
Statement::Expression(e2) => e1 == e2,
_ => false,
},
}
}
}

View File

@@ -69,6 +69,8 @@ impl Statement {
stdout.push_str(&line);
Ok(Value::Void)
}
Statement::Expression(e) => e.eval(stdout, env),
}
}
}

View File

@@ -77,7 +77,7 @@ pub TopLevel: TopLevel = {
}
Function: TopLevel = {
"function" <opt_name:OptionalName> "(" <args:Arguments> OptionalComma ")" <exp:Expression> =>
"function" <opt_name:OptionalName> "(" <args:Arguments> OptionalComma ")" <exp:Expression> ";" =>
TopLevel::Function(opt_name, args, exp),
}
@@ -154,6 +154,10 @@ Statement: Statement = {
Location::new(file_idx, ls..le),
Name::new(v, Location::new(file_idx, name_start..name_end)),
),
// A statement can just be an expression.
<e: Expression> ";" =>
Statement::Expression(e),
}
// Expressions! Expressions are a little fiddly, because we're going to

View File

@@ -56,6 +56,7 @@ impl Statement {
.text("print")
.append(allocator.space())
.append(allocator.text(var.to_string())),
Statement::Expression(e) => e.pretty(allocator),
}
}
}

View File

@@ -177,6 +177,13 @@ impl Statement {
Statement::Print(loc, var) => {
errors.push(Error::UnboundVariable(loc.clone(), var.to_string()))
}
Statement::Expression(e) => {
let (mut exp_errors, mut exp_warnings) = e.validate(bound_variables);
errors.append(&mut exp_errors);
warnings.append(&mut exp_warnings);
}
}
(errors, warnings)