1
0

Implement short-circuiting for logical operators; Organize compiler

This commit is contained in:
Jeff 2024-12-18 14:57:29 -05:00
parent 71da39f8b5
commit e2a462c3f6
6 changed files with 461 additions and 397 deletions

View File

@ -25,6 +25,7 @@ smartstring = { version = "1.0.1", features = [
[dev-dependencies] [dev-dependencies]
criterion = { version = "0.3.4", features = ["html_reports"] } criterion = { version = "0.3.4", features = ["html_reports"] }
smallvec = { version = "1.13.2", features = ["const_generics"] }
[[bench]] [[bench]]
name = "addictive_addition" name = "addictive_addition"

View File

@ -74,7 +74,7 @@ impl Chunk {
} }
} }
#[cfg(test)] #[cfg(any(test, debug_assertions))]
pub fn with_data( pub fn with_data(
name: Option<DustString>, name: Option<DustString>,
r#type: FunctionType, r#type: FunctionType,

View File

@ -20,13 +20,12 @@
//! runtime error, it is the compiler's fault and the error should be fixed here. //! runtime error, it is the compiler's fault and the error should be fixed here.
mod error; mod error;
mod optimize; mod optimize;
mod parse_rule;
pub use error::CompileError; pub use error::CompileError;
use parse_rule::{ParseRule, Precedence};
use std::{ use std::mem::{replace, swap};
fmt::{self, Display, Formatter},
mem::replace,
};
use colored::Colorize; use colored::Colorize;
use optimize::control_flow_register_consolidation; use optimize::control_flow_register_consolidation;
@ -119,14 +118,14 @@ pub struct Compiler<'src> {
/// The current scope of the compiler. This is used to test if a variable is in scope. /// The current scope of the compiler. This is used to test if a variable is in scope.
current_scope: Scope, current_scope: Scope,
/// Index of the record (i.e. runtime data) that the VM will use when calling the function. This /// Index of the [`Record`] that the VM will use when calling the function. This is a
/// is a depth-first index. /// depth-first index.
record_index: u8, record_index: u8,
/// Record index for the next nested chunk that is compiled. When a function is compiled, its /// Record index for the next nested chunk that is compiled. When a function is compiled, its
/// `record_index` is assigned from this value. Then `next_record_index` is incremented to /// `record_index` is assigned from this value. Then `next_record_index` is incremented to
/// maintain the depth-first index. After the function is compiled, the its `next_record_index` /// maintain the depth-first index. After the function is compiled, its `next_record_index`
/// is assigned to this chunk. /// is assigned back to this chunk.
next_record_index: u8, next_record_index: u8,
} }
@ -360,6 +359,12 @@ impl<'src> Compiler<'src> {
} }
} }
fn get_last_operation(&self) -> Option<Operation> {
self.instructions
.last()
.map(|(instruction, _, _)| instruction.operation())
}
fn get_last_operations<const COUNT: usize>(&self) -> Option<[Operation; COUNT]> { fn get_last_operations<const COUNT: usize>(&self) -> Option<[Operation; COUNT]> {
let mut n_operations = [Operation::RETURN; COUNT]; let mut n_operations = [Operation::RETURN; COUNT];
@ -946,19 +951,30 @@ impl<'src> Compiler<'src> {
} }
fn parse_logical_binary(&mut self) -> Result<(), CompileError> { fn parse_logical_binary(&mut self) -> Result<(), CompileError> {
let is_logic_chain = matches!( let (last_instruction, last_type, last_position) =
self.get_last_operations(), self.instructions
Some([_, Operation::TEST, Operation::JUMP, _]) .pop()
); .ok_or_else(|| CompileError::ExpectedExpression {
let jump_over_index = self.instructions.len() - 2; found: self.previous_token.to_owned(),
let jump_over = if is_logic_chain { position: self.previous_position,
let (instruction, _, _) = self.instructions.remove(jump_over_index); })?;
let operand_register = if last_instruction.operation() == Operation::GET_LOCAL {
let (local, _) = self.get_local(last_instruction.b_field())?;
Some(instruction) local.register_index
} else if last_instruction.yields_value() {
let register = last_instruction.a_field();
self.instructions
.push((last_instruction, last_type, last_position));
register
} else { } else {
None self.instructions
.push((last_instruction, last_type, last_position));
self.next_register() - 1
}; };
let operand_register = self.next_register() - 1;
let operator = self.current_token; let operator = self.current_token;
let operator_position = self.current_position; let operator_position = self.current_position;
let rule = ParseRule::from(&operator); let rule = ParseRule::from(&operator);
@ -974,44 +990,29 @@ impl<'src> Compiler<'src> {
} }
}; };
let test = Instruction::test(operand_register, test_boolean); let test = Instruction::test(operand_register, test_boolean);
let jump = Instruction::jump(1, true);
self.advance()?;
self.emit_instruction(test, Type::None, operator_position); self.emit_instruction(test, Type::None, operator_position);
self.emit_instruction(jump, Type::None, operator_position);
let jump_index = self.instructions.len(); self.advance()?;
self.parse_sub_expression(&rule.precedence)?; self.parse_sub_expression(&rule.precedence)?;
let jump_distance = (self.instructions.len() - jump_index) as u8; let instructions_length = self.instructions.len() - 1;
let jump = Instruction::jump(jump_distance, true);
self.instructions for (group_index, instructions) in self.instructions.rchunks_mut(3).enumerate().rev() {
.insert(jump_index, (jump, Type::None, operator_position)); if instructions[0].0.operation() != Operation::TEST
|| instructions[1].0.operation() != Operation::JUMP
{
continue;
}
if let Some(jump_over) = jump_over { let jump = &mut instructions[1].0;
let is_end_of_context = matches!(self.current_token, Token::RightBrace | Token::Eof); let jump_index = instructions_length - group_index * 3 - 1;
let jump_over_distance = let short_circuit_distance = (instructions_length - jump_index) as u8;
jump_over.b_field() + (self.instructions.len() - jump_over_index) as u8 - {
if is_end_of_context {
1
} else {
0
}
};
let jump_over = Instruction::jump(jump_over_distance, true);
self.instructions *jump = Instruction::jump(short_circuit_distance, true);
.insert(jump_over_index, (jump_over, Type::None, operator_position));
} }
// short_circuit_jump_distance += (self.instructions.len() - short_circuit_jump_index) as u8;
// let jump = Instruction::jump(short_circuit_jump_distance, true);
// self.instructions.insert(
// short_circuit_jump_index,
// (jump, Type::None, operator_position),
// );
Ok(()) Ok(())
} }
@ -1241,8 +1242,8 @@ impl<'src> Compiler<'src> {
match else_block_distance { match else_block_distance {
0 => {} 0 => {}
1 => { 1 => {
if let Some([Operation::LOAD_BOOLEAN | Operation::LOAD_CONSTANT]) = if let Some(Operation::LOAD_BOOLEAN | Operation::LOAD_CONSTANT) =
self.get_last_operations() self.get_last_operation()
{ {
let (mut loader, _, _) = self.instructions.last_mut().unwrap(); let (mut loader, _, _) = self.instructions.last_mut().unwrap();
@ -1921,318 +1922,3 @@ impl<'src> Compiler<'src> {
} }
} }
} }
/// Operator precedence levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Precedence {
Primary = 9,
Call = 8,
Unary = 7,
Factor = 6,
Term = 5,
Comparison = 4,
LogicalAnd = 3,
LogicalOr = 2,
Assignment = 1,
None = 0,
}
impl Precedence {
fn increment(&self) -> Self {
match self {
Precedence::None => Precedence::Assignment,
Precedence::Assignment => Precedence::LogicalOr,
Precedence::LogicalOr => Precedence::LogicalAnd,
Precedence::LogicalAnd => Precedence::Comparison,
Precedence::Comparison => Precedence::Term,
Precedence::Term => Precedence::Factor,
Precedence::Factor => Precedence::Unary,
Precedence::Unary => Precedence::Call,
Precedence::Call => Precedence::Primary,
Precedence::Primary => Precedence::Primary,
}
}
}
impl Display for Precedence {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
type Parser<'a> = fn(&mut Compiler<'a>) -> Result<(), CompileError>;
/// Rule that defines how to parse a token.
#[derive(Debug, Clone, Copy)]
struct ParseRule<'a> {
pub prefix: Option<Parser<'a>>,
pub infix: Option<Parser<'a>>,
pub precedence: Precedence,
}
impl From<&Token<'_>> for ParseRule<'_> {
fn from(token: &Token) -> Self {
match token {
Token::ArrowThin => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Async => todo!(),
Token::Bang => ParseRule {
prefix: Some(Compiler::parse_unary),
infix: None,
precedence: Precedence::Unary,
},
Token::BangEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::Bool => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Boolean(_) => ParseRule {
prefix: Some(Compiler::parse_boolean),
infix: None,
precedence: Precedence::None,
},
Token::Break => todo!(),
Token::Byte(_) => ParseRule {
prefix: Some(Compiler::parse_byte),
infix: None,
precedence: Precedence::None,
},
Token::Character(_) => ParseRule {
prefix: Some(Compiler::parse_character),
infix: None,
precedence: Precedence::None,
},
Token::Colon => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Comma => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Dot => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::DoubleAmpersand => ParseRule {
prefix: None,
infix: Some(Compiler::parse_logical_binary),
precedence: Precedence::LogicalAnd,
},
Token::DoubleEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::DoublePipe => ParseRule {
prefix: None,
infix: Some(Compiler::parse_logical_binary),
precedence: Precedence::LogicalOr,
},
Token::DoubleDot => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Eof => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Equal => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::Assignment,
},
Token::Else => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Float(_) => ParseRule {
prefix: Some(Compiler::parse_float),
infix: None,
precedence: Precedence::None,
},
Token::FloatKeyword => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Fn => ParseRule {
prefix: Some(Compiler::parse_function),
infix: None,
precedence: Precedence::None,
},
Token::Greater => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::GreaterEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::Identifier(_) => ParseRule {
prefix: Some(Compiler::parse_variable),
infix: None,
precedence: Precedence::None,
},
Token::If => ParseRule {
prefix: Some(Compiler::parse_if),
infix: None,
precedence: Precedence::None,
},
Token::Int => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Integer(_) => ParseRule {
prefix: Some(Compiler::parse_integer),
infix: None,
precedence: Precedence::None,
},
Token::LeftBrace => ParseRule {
prefix: Some(Compiler::parse_block),
infix: None,
precedence: Precedence::None,
},
Token::LeftParenthesis => ParseRule {
prefix: Some(Compiler::parse_grouped),
infix: Some(Compiler::parse_call),
precedence: Precedence::Call,
},
Token::LeftBracket => ParseRule {
prefix: Some(Compiler::parse_list),
infix: None,
precedence: Precedence::None,
},
Token::Less => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::LessEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::Let => ParseRule {
prefix: Some(Compiler::parse_let_statement),
infix: None,
precedence: Precedence::Assignment,
},
Token::Loop => todo!(),
Token::Map => todo!(),
Token::Minus => ParseRule {
prefix: Some(Compiler::parse_unary),
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Term,
},
Token::MinusEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Mut => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Percent => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Factor,
},
Token::PercentEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Plus => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Term,
},
Token::PlusEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Return => ParseRule {
prefix: Some(Compiler::parse_return_statement),
infix: None,
precedence: Precedence::None,
},
Token::RightBrace => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::RightParenthesis => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::RightBracket => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Semicolon => ParseRule {
prefix: Some(Compiler::parse_semicolon),
infix: None,
precedence: Precedence::None,
},
Token::Slash => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Factor,
},
Token::SlashEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Star => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Factor,
},
Token::StarEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Str => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::String(_) => ParseRule {
prefix: Some(Compiler::parse_string),
infix: None,
precedence: Precedence::None,
},
Token::Struct => todo!(),
Token::While => ParseRule {
prefix: Some(Compiler::parse_while),
infix: None,
precedence: Precedence::None,
},
}
}
}

View File

@ -0,0 +1,320 @@
use std::fmt::{self, Display, Formatter};
use crate::Token;
use super::{CompileError, Compiler};
pub type Parser<'a> = fn(&mut Compiler<'a>) -> Result<(), CompileError>;
/// Rule that defines how to parse a token.
#[derive(Debug, Clone, Copy)]
pub struct ParseRule<'a> {
pub prefix: Option<Parser<'a>>,
pub infix: Option<Parser<'a>>,
pub precedence: Precedence,
}
impl From<&Token<'_>> for ParseRule<'_> {
fn from(token: &Token) -> Self {
match token {
Token::ArrowThin => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Async => todo!(),
Token::Bang => ParseRule {
prefix: Some(Compiler::parse_unary),
infix: None,
precedence: Precedence::Unary,
},
Token::BangEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::Bool => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Boolean(_) => ParseRule {
prefix: Some(Compiler::parse_boolean),
infix: None,
precedence: Precedence::None,
},
Token::Break => todo!(),
Token::Byte(_) => ParseRule {
prefix: Some(Compiler::parse_byte),
infix: None,
precedence: Precedence::None,
},
Token::Character(_) => ParseRule {
prefix: Some(Compiler::parse_character),
infix: None,
precedence: Precedence::None,
},
Token::Colon => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Comma => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Dot => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::DoubleAmpersand => ParseRule {
prefix: None,
infix: Some(Compiler::parse_logical_binary),
precedence: Precedence::LogicalAnd,
},
Token::DoubleEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::DoublePipe => ParseRule {
prefix: None,
infix: Some(Compiler::parse_logical_binary),
precedence: Precedence::LogicalOr,
},
Token::DoubleDot => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Eof => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Equal => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::Assignment,
},
Token::Else => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Float(_) => ParseRule {
prefix: Some(Compiler::parse_float),
infix: None,
precedence: Precedence::None,
},
Token::FloatKeyword => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Fn => ParseRule {
prefix: Some(Compiler::parse_function),
infix: None,
precedence: Precedence::None,
},
Token::Greater => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::GreaterEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::Identifier(_) => ParseRule {
prefix: Some(Compiler::parse_variable),
infix: None,
precedence: Precedence::None,
},
Token::If => ParseRule {
prefix: Some(Compiler::parse_if),
infix: None,
precedence: Precedence::None,
},
Token::Int => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Integer(_) => ParseRule {
prefix: Some(Compiler::parse_integer),
infix: None,
precedence: Precedence::None,
},
Token::LeftBrace => ParseRule {
prefix: Some(Compiler::parse_block),
infix: None,
precedence: Precedence::None,
},
Token::LeftParenthesis => ParseRule {
prefix: Some(Compiler::parse_grouped),
infix: Some(Compiler::parse_call),
precedence: Precedence::Call,
},
Token::LeftBracket => ParseRule {
prefix: Some(Compiler::parse_list),
infix: None,
precedence: Precedence::None,
},
Token::Less => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::LessEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_comparison_binary),
precedence: Precedence::Comparison,
},
Token::Let => ParseRule {
prefix: Some(Compiler::parse_let_statement),
infix: None,
precedence: Precedence::Assignment,
},
Token::Loop => todo!(),
Token::Map => todo!(),
Token::Minus => ParseRule {
prefix: Some(Compiler::parse_unary),
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Term,
},
Token::MinusEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Mut => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Percent => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Factor,
},
Token::PercentEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Plus => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Term,
},
Token::PlusEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Return => ParseRule {
prefix: Some(Compiler::parse_return_statement),
infix: None,
precedence: Precedence::None,
},
Token::RightBrace => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::RightParenthesis => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::RightBracket => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Semicolon => ParseRule {
prefix: Some(Compiler::parse_semicolon),
infix: None,
precedence: Precedence::None,
},
Token::Slash => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Factor,
},
Token::SlashEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Star => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Factor,
},
Token::StarEqual => ParseRule {
prefix: None,
infix: Some(Compiler::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Str => ParseRule {
prefix: Some(Compiler::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::String(_) => ParseRule {
prefix: Some(Compiler::parse_string),
infix: None,
precedence: Precedence::None,
},
Token::Struct => todo!(),
Token::While => ParseRule {
prefix: Some(Compiler::parse_while),
infix: None,
precedence: Precedence::None,
},
}
}
}
/// Operator precedence levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Precedence {
Primary = 9,
Call = 8,
Unary = 7,
Factor = 6,
Term = 5,
Comparison = 4,
LogicalAnd = 3,
LogicalOr = 2,
Assignment = 1,
None = 0,
}
impl Precedence {
pub fn increment(&self) -> Self {
match self {
Precedence::None => Precedence::Assignment,
Precedence::Assignment => Precedence::LogicalOr,
Precedence::LogicalOr => Precedence::LogicalAnd,
Precedence::LogicalAnd => Precedence::Comparison,
Precedence::Comparison => Precedence::Term,
Precedence::Term => Precedence::Factor,
Precedence::Factor => Precedence::Unary,
Precedence::Unary => Precedence::Call,
Precedence::Call => Precedence::Primary,
Precedence::Primary => Precedence::Primary,
}
}
}
impl Display for Precedence {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

View File

@ -26,6 +26,30 @@ pub enum Value {
} }
impl Value { impl Value {
pub fn boolean(boolean: bool) -> Self {
Value::Concrete(ConcreteValue::Boolean(boolean))
}
pub fn byte(byte: u8) -> Self {
Value::Concrete(ConcreteValue::Byte(byte))
}
pub fn character(character: char) -> Self {
Value::Concrete(ConcreteValue::Character(character))
}
pub fn float(float: f64) -> Self {
Value::Concrete(ConcreteValue::Float(float))
}
pub fn integer(integer: i64) -> Self {
Value::Concrete(ConcreteValue::Integer(integer))
}
pub fn string(string: DustString) -> Self {
Value::Concrete(ConcreteValue::String(string))
}
pub fn as_boolean(&self) -> Option<&bool> { pub fn as_boolean(&self) -> Option<&bool> {
if let Value::Concrete(ConcreteValue::Boolean(value)) = self { if let Value::Concrete(ConcreteValue::Boolean(value)) = self {
Some(value) Some(value)

View File

@ -1,4 +1,5 @@
use dust_lang::*; use dust_lang::*;
use smallvec::smallvec;
#[test] #[test]
fn true_and_true() { fn true_and_true() {
@ -13,19 +14,27 @@ fn true_and_true() {
value_parameters: None, value_parameters: None,
return_type: Type::Boolean, return_type: Type::Boolean,
}, },
vec![ smallvec![
(Instruction::load_boolean(0, true, false), Span(0, 4)), Instruction::load_boolean(0, true, false),
(Instruction::test(Argument::Register(0), true), Span(5, 7)), Instruction::test(0, true),
(Instruction::jump(1, true), Span(5, 7)), Instruction::jump(1, true),
(Instruction::load_boolean(1, true, false), Span(8, 12)), Instruction::load_boolean(1, true, false),
(Instruction::r#return(true), Span(12, 12)), Instruction::r#return(true),
], ],
vec![], smallvec![
Span(0, 4),
Span(5, 7),
Span(5, 7),
Span(8, 12),
Span(12, 12),
],
smallvec![],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true)))); assert_eq!(run(source), Ok(Some(Value::boolean(true))));
} }
#[test] #[test]
@ -41,19 +50,27 @@ fn false_and_false() {
value_parameters: None, value_parameters: None,
return_type: Type::Boolean, return_type: Type::Boolean,
}, },
vec![ smallvec![
(Instruction::load_boolean(0, false, false), Span(0, 5)), Instruction::load_boolean(0, false, false),
(Instruction::test(Argument::Register(0), true), Span(6, 8)), Instruction::test(0, true),
(Instruction::jump(1, true), Span(6, 8)), Instruction::jump(1, true),
(Instruction::load_boolean(1, false, false), Span(9, 14)), Instruction::load_boolean(1, false, false),
(Instruction::r#return(true), Span(14, 14)), Instruction::r#return(true),
], ],
vec![], smallvec![
Span(0, 5),
Span(6, 8),
Span(6, 8),
Span(9, 14),
Span(14, 14),
],
smallvec![],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false)))); assert_eq!(run(source), Ok(Some(Value::boolean(false))));
} }
#[test] #[test]
@ -69,19 +86,27 @@ fn false_and_true() {
value_parameters: None, value_parameters: None,
return_type: Type::Boolean, return_type: Type::Boolean,
}, },
vec![ smallvec![
(Instruction::load_boolean(0, false, false), Span(0, 5)), Instruction::load_boolean(0, false, false),
(Instruction::test(Argument::Register(0), true), Span(6, 8)), Instruction::test(0, true),
(Instruction::jump(1, true), Span(6, 8)), Instruction::jump(1, true),
(Instruction::load_boolean(1, true, false), Span(9, 13)), Instruction::load_boolean(1, true, false),
(Instruction::r#return(true), Span(13, 13)), Instruction::r#return(true),
], ],
vec![], smallvec![
Span(0, 5),
Span(6, 8),
Span(6, 8),
Span(9, 13),
Span(13, 13)
],
smallvec![],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false)))); assert_eq!(run(source), Ok(Some(Value::boolean(false))));
} }
#[test] #[test]
@ -97,17 +122,25 @@ fn true_and_false() {
value_parameters: None, value_parameters: None,
return_type: Type::Boolean, return_type: Type::Boolean,
}, },
vec![ smallvec![
(Instruction::load_boolean(0, true, false), Span(0, 4)), Instruction::load_boolean(0, true, false),
(Instruction::test(Argument::Register(0), true), Span(5, 7)), Instruction::test(0, true),
(Instruction::jump(1, true), Span(5, 7)), Instruction::jump(1, true),
(Instruction::load_boolean(1, false, false), Span(8, 13)), Instruction::load_boolean(1, false, false),
(Instruction::r#return(true), Span(13, 13)), Instruction::r#return(true),
], ],
vec![], smallvec![
Span(0, 4),
Span(5, 7),
Span(5, 7),
Span(8, 13),
Span(13, 13)
],
smallvec![],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false)))); assert_eq!(run(source), Ok(Some(Value::boolean(false))));
} }