None literal 🕳

This commit is contained in:
Laurenz 2021-01-04 17:19:49 +01:00
parent 32af3095f8
commit 77c06ebc24
6 changed files with 15 additions and 1 deletions

View File

@ -176,6 +176,7 @@ impl Eval for Spanned<&Lit> {
Value::Error
}
},
Lit::None => Value::None,
Lit::Bool(v) => Value::Bool(v),
Lit::Int(v) => Value::Int(v),
Lit::Float(v) => Value::Float(v),

View File

@ -326,6 +326,7 @@ fn value(p: &mut Parser) -> Option<Expr> {
}
// Basic values.
Some(Token::None) => Expr::Lit(Lit::None),
Some(Token::Bool(b)) => Expr::Lit(Lit::Bool(b)),
Some(Token::Int(i)) => Expr::Lit(Lit::Int(i)),
Some(Token::Float(f)) => Expr::Lit(Lit::Float(f)),

View File

@ -614,6 +614,7 @@ fn test_parse_values() {
t!("{name}" Block(Id("name")));
t!("{ke-bab}" Block(Id("ke-bab")));
t!("{α}" Block(Id("α")));
t!("{none}" Block(Expr::Lit(Lit::None)));
t!("{true}" Block(Bool(true)));
t!("{false}" Block(Bool(false)));
t!("{1.0e-4}" Block(Float(1e-4)));

View File

@ -294,6 +294,7 @@ impl<'s> Tokens<'s> {
self.s.eat_while(is_id_continue);
let string = self.s.eaten_from(start);
match string {
"none" => Token::None,
"true" => Token::Bool(true),
"false" => Token::Bool(false),
_ => Token::Ident(string),
@ -377,6 +378,7 @@ mod tests {
use super::*;
use crate::parse::tests::check;
use Option::None;
use Token::{
BlockComment as BC, Ident as Id, LeftBrace as LB, LeftBracket as L,
LeftParen as LP, LineComment as LC, RightBrace as RB, RightBracket as R,
@ -679,7 +681,11 @@ mod tests {
}
#[test]
fn test_tokenize_bools() {
fn test_tokenize_keywords() {
// Test none.
t!(Header[" /"]: "none" => Token::None);
t!(Header[" /"]: "None" => Id("None"));
// Test valid bools.
t!(Header[" /"]: "false" => Bool(false));
t!(Header[" /"]: "true" => Bool(true));

View File

@ -108,6 +108,8 @@ pub type ExprContent = Tree;
pub enum Lit {
/// A identifier literal: `left`.
Ident(Ident),
/// The none literal: `none`.
None,
/// A boolean literal: `true`, `false`.
Bool(bool),
/// An integer literal: `120`.

View File

@ -63,6 +63,8 @@ pub enum Token<'s> {
/// An identifier: `center`.
Ident(&'s str),
/// A none: `none`.
None,
/// A boolean: `true`, `false`.
Bool(bool),
/// An integer: `120`.
@ -152,6 +154,7 @@ impl<'s> Token<'s> {
Self::Slash => "slash",
Self::Ident(_) => "identifier",
Self::None => "none",
Self::Bool(_) => "bool",
Self::Int(_) => "integer",
Self::Float(_) => "float",