Add true and false literals

This commit is contained in:
Olivier 'reivilibre' 2025-06-11 22:17:49 +01:00
parent 2281a9bb50
commit 1cb196571f
4 changed files with 7 additions and 1 deletions

View File

@ -199,6 +199,7 @@ pub enum Expression {
IntLiteral {
val: i64,
},
BoolLiteral(bool),
NoneLiteral,
StringExpr(StringExpr),

View File

@ -220,11 +220,13 @@ MethodCall = { "." ~ ws* ~ Identifier ~ "(" ~ commaSeparatedExprs ~ ")" }
FieldLookup = { "." ~ ws* ~ Identifier }
Indexing = { "[" ~ ws* ~ Expr ~ ws* ~ "]" }
Term = _{ (IntLiteral | bracketedTerm | FunctionCall | ListLiteral | MapLiteral | String | Variable | NoneLiteral) }
Term = _{ (IntLiteral | bracketedTerm | FunctionCall | ListLiteral | MapLiteral | String | Variable | NoneLiteral | TrueLiteral | FalseLiteral) }
bracketedTerm = _{ "(" ~ Expr ~ ")" }
NoneLiteral = { "None" }
TrueLiteral = { "true" }
FalseLiteral = { "false" }
IntLiteral = @{ (ASCII_NONZERO_DIGIT ~ ASCII_DIGIT+ | ASCII_DIGIT) }
// `-` is important in identifiers for `kebab-case` HTML element attributes
// We could consider splitting this out into its own kind of identifier but let's not bother now.

View File

@ -404,6 +404,8 @@ impl HornbeamParser {
Ok(match node.as_rule() {
Rule::IntLiteral => Expression::IntLiteral { val: node.as_str().parse().map_err(|e| error(&format!("can't parse int: {e:?}"), node.as_span()))? },
Rule::NoneLiteral => Expression::NoneLiteral,
Rule::TrueLiteral => Expression::BoolLiteral(true),
Rule::FalseLiteral => Expression::BoolLiteral(false),
Rule::String => Expression::StringExpr(HornbeamParser::String(node)?),
Rule::Variable => HornbeamParser::Variable(node)?,
Rule::FunctionCall => HornbeamParser::FunctionCall(node)?,

View File

@ -749,6 +749,7 @@ impl<'a, O: OutputSystem + Send, LS: LocalisationSystem + Sync + Send> Interpret
Ok(Value::List(result))
}
Expression::IntLiteral { val } => Ok(Value::Int(*val)),
Expression::BoolLiteral(val) => Ok(Value::Bool(*val)),
Expression::NoneLiteral => Ok(Value::Reflective(Box::new(None::<()>))),
Expression::StringExpr(sexpr) => {
let mut output = String::new();