Add keywords nil, true, false

This commit is contained in:
Sofia 2026-03-17 21:05:51 +02:00
parent 1e2feb9c3c
commit f6548019e3
5 changed files with 42 additions and 2 deletions

View File

@ -27,3 +27,4 @@ print(b)
print(min(11, 9)) print(min(11, 9))
print(10 - 15) print(10 - 15)
print("hello there!") print("hello there!")
print(nil or 0)

View File

@ -471,6 +471,13 @@ impl Parse for PrimaryExpression {
} else if let Some(Token::StringLit(value)) = peeked { } else if let Some(Token::StringLit(value)) = peeked {
stream.next(); // Consume string-literal stream.next(); // Consume string-literal
Expression::Literal(Literal::String(value)) Expression::Literal(Literal::String(value))
} else if let Some(Token::Keyword(Keyword::True) | Token::Keyword(Keyword::False)) = peeked
{
let value = Token::Keyword(Keyword::True) == stream.next().unwrap();
Expression::Literal(Literal::Bool(value))
} else if let Some(Token::Keyword(Keyword::Nil)) = peeked {
stream.next(); // Consume nil
Expression::Literal(Literal::Nil)
} else if let Some(Token::Symbol('{')) = peeked { } else if let Some(Token::Symbol('{')) = peeked {
stream.next(); stream.next();
stream.expect_symbol('}')?; stream.expect_symbol('}')?;
@ -556,4 +563,6 @@ pub enum Literal {
Float(LuaFloat), Float(LuaFloat),
Integer(LuaInteger), Integer(LuaInteger),
String(String), String(String),
Bool(bool),
Nil,
} }

View File

@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use crate::{ use crate::{
ast::{AccessModifier, BinaryOperator, Block, Expression, Literal, Statement, UnaryOperator}, ast::{AccessModifier, BinaryOperator, Block, Expression, Literal, Statement, UnaryOperator},
vm::{Constant, Instruction, VMFloat}, vm::{Constant, Instruction, LuaBool, VMFloat},
}; };
pub struct State { pub struct State {
@ -386,6 +386,16 @@ impl Expression {
constants.insert(Constant::String(value.clone())); constants.insert(Constant::String(value.clone()));
constants constants
} }
Literal::Bool(value) => {
let mut constants = HashSet::new();
constants.insert(Constant::Bool(LuaBool(*value)));
constants
}
Literal::Nil => {
let mut constants = HashSet::new();
constants.insert(Constant::Nil);
constants
}
}, },
Expression::TableConstructor => { Expression::TableConstructor => {
let constants = HashSet::new(); let constants = HashSet::new();
@ -604,6 +614,8 @@ impl Expression {
Literal::Float(value) => Constant::Float(value.vm_number()), Literal::Float(value) => Constant::Float(value.vm_number()),
Literal::String(value) => Constant::String(value.clone()), Literal::String(value) => Constant::String(value.clone()),
Literal::Integer(lua_integer) => Constant::Integer(*lua_integer), Literal::Integer(lua_integer) => Constant::Integer(*lua_integer),
Literal::Bool(value) => Constant::Bool(LuaBool(*value)),
Literal::Nil => Constant::Nil,
}), }),
)); ));
(instructions, vec![reg]) (instructions, vec![reg])

View File

@ -16,6 +16,10 @@ pub enum Keyword {
Return, Return,
If, If,
Then, Then,
True,
False,
Nil,
Not,
} }
impl Keyword { impl Keyword {
@ -28,6 +32,10 @@ impl Keyword {
"return" => Keyword::Return, "return" => Keyword::Return,
"if" => Keyword::If, "if" => Keyword::If,
"then" => Keyword::Then, "then" => Keyword::Then,
"true" => Keyword::True,
"false" => Keyword::False,
"nil" => Keyword::Nil,
"not" => Keyword::Not,
_ => None?, _ => None?,
}) })
} }
@ -43,6 +51,10 @@ impl ToString for Keyword {
Keyword::Return => "return", Keyword::Return => "return",
Keyword::If => "if", Keyword::If => "if",
Keyword::Then => "then", Keyword::Then => "then",
Keyword::True => "true",
Keyword::False => "false",
Keyword::Nil => "nil",
Keyword::Not => "not",
} }
.to_string() .to_string()
} }

View File

@ -90,6 +90,8 @@ pub enum Constant {
String(String), String(String),
Float(VMFloat), Float(VMFloat),
Integer(LuaInteger), Integer(LuaInteger),
Bool(LuaBool),
Nil,
} }
impl Debug for Constant { impl Debug for Constant {
@ -98,6 +100,8 @@ impl Debug for Constant {
Self::String(arg0) => f.debug_tuple("String").field(arg0).finish(), Self::String(arg0) => f.debug_tuple("String").field(arg0).finish(),
Self::Float(arg0) => f.debug_tuple("Number").field(&arg0.lua_number()).finish(), Self::Float(arg0) => f.debug_tuple("Number").field(&arg0.lua_number()).finish(),
Self::Integer(arg0) => f.debug_tuple("Integer").field(arg0).finish(), Self::Integer(arg0) => f.debug_tuple("Integer").field(arg0).finish(),
Self::Bool(arg0) => f.debug_tuple("Boolean").field(arg0).finish(),
Self::Nil => f.debug_tuple("Nil").finish(),
} }
} }
} }
@ -657,6 +661,8 @@ impl ClosureRunner {
Constant::String(value) => Value::String(value.clone()), Constant::String(value) => Value::String(value.clone()),
Constant::Float(value) => Value::Float(*value), Constant::Float(value) => Value::Float(*value),
Constant::Integer(value) => Value::Integer(*value), Constant::Integer(value) => Value::Integer(*value),
Constant::Bool(lua_bool) => Value::Boolean(*lua_bool),
Constant::Nil => Value::Nil,
}), }),
); );
} }