1
0
This commit is contained in:
Jeff 2024-09-18 23:02:28 -04:00
parent bf4f319302
commit 03113fdf5e
2 changed files with 28 additions and 21 deletions

View File

@ -482,20 +482,14 @@ impl<'src> Parser<'src> {
previous_instruction.set_destination(register_index); previous_instruction.set_destination(register_index);
self.emit_instruction(previous_instruction, self.current_position); self.emit_instruction(previous_instruction, self.current_position);
} else {
self.emit_instruction(previous_instruction, previous_position);
self.emit_instruction(
Instruction::set_local(self.current_register - 1, local_index),
self.current_position,
);
} }
} else { }
self.emit_instruction(previous_instruction, previous_position); self.emit_instruction(previous_instruction, previous_position);
self.emit_instruction( self.emit_instruction(
Instruction::set_local(self.current_register - 1, local_index), Instruction::set_local(self.current_register - 1, local_index),
start_position, start_position,
); );
}
} else { } else {
self.emit_instruction( self.emit_instruction(
Instruction::get_local(self.current_register, local_index), Instruction::get_local(self.current_register, local_index),
@ -595,6 +589,14 @@ impl<'src> Parser<'src> {
Ok(()) Ok(())
} }
fn parse_while(&mut self, allow_assignment: bool) -> Result<(), ParseError> {
self.advance()?;
self.parse_expression()?;
self.parse_block(allow_assignment)?;
Ok(())
}
fn parse_expression(&mut self) -> Result<(), ParseError> { fn parse_expression(&mut self) -> Result<(), ParseError> {
self.parse(Precedence::None) self.parse(Precedence::None)
} }
@ -853,7 +855,11 @@ impl From<&TokenKind> for ParseRule<'_> {
TokenKind::Loop => todo!(), TokenKind::Loop => todo!(),
TokenKind::Map => todo!(), TokenKind::Map => todo!(),
TokenKind::Str => todo!(), TokenKind::Str => todo!(),
TokenKind::While => todo!(), TokenKind::While => ParseRule {
prefix: Some(Parser::parse_while),
infix: None,
precedence: Precedence::None,
},
TokenKind::BangEqual => todo!(), TokenKind::BangEqual => todo!(),
TokenKind::Bang => ParseRule { TokenKind::Bang => ParseRule {
prefix: Some(Parser::parse_unary), prefix: Some(Parser::parse_unary),

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
dust_error::AnnotatedError, parse, Chunk, ChunkError, DustError, Identifier, Instruction, parse, AnnotatedError, Chunk, ChunkError, DustError, Identifier, Instruction, Local, Operation,
Local, Operation, Span, Value, ValueError, Span, Value, ValueError,
}; };
pub fn run(source: &str) -> Result<Option<Value>, DustError> { pub fn run(source: &str) -> Result<Option<Value>, DustError> {
@ -35,11 +35,12 @@ impl Vm {
} }
pub fn run(&mut self) -> Result<Option<Value>, VmError> { pub fn run(&mut self) -> Result<Option<Value>, VmError> {
// DRY helper closure to take a constant or clone a register // DRY helper to take constants or clone registers for binary operations
let take_constants_or_clone = |vm: &mut Vm, fn take_constants_or_clone(
vm: &mut Vm,
instruction: Instruction, instruction: Instruction,
position: Span| position: Span,
-> Result<(Value, Value), VmError> { ) -> Result<(Value, Value), VmError> {
let left = if instruction.first_argument_is_constant() { let left = if instruction.first_argument_is_constant() {
vm.chunk vm.chunk
.take_constant(instruction.first_argument(), position)? .take_constant(instruction.first_argument(), position)?
@ -54,7 +55,7 @@ impl Vm {
}; };
Ok((left, right)) Ok((left, right))
}; }
while let Ok((instruction, position)) = self.read(Span(0, 0)).copied() { while let Ok((instruction, position)) = self.read(Span(0, 0)).copied() {
log::trace!("Running instruction {instruction} at {position}"); log::trace!("Running instruction {instruction} at {position}");