1
0
This commit is contained in:
Jeff 2024-10-18 21:34:47 -04:00
parent 95e5b3062d
commit 19f2d19134
3 changed files with 42 additions and 78 deletions

View File

@ -1,4 +1,7 @@
use std::fmt::{self, Debug, Display}; use std::{
fmt::{self, Debug, Display},
path::PathBuf,
};
use colored::Colorize; use colored::Colorize;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -280,27 +283,16 @@ impl Display for Chunk {
impl Debug for Chunk { impl Debug for Chunk {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let timestamp = std::time::SystemTime::now() let executable = std::env::current_exe().unwrap_or_else(|_| PathBuf::new());
.duration_since(std::time::UNIX_EPOCH) let disassembly = self
.unwrap() .disassembler(&executable.to_string_lossy())
.as_secs(); .styled(false)
.disassemble();
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
write!( write!(f, "\n{}", disassembly)
f,
"\n{}\n",
self.disassembler(&format!("Dust Program 0x{timestamp:x}"))
.styled(false)
.disassemble()
)
} else { } else {
write!( write!(f, "{}", disassembly)
f,
"{}",
self.disassembler(&format!("Dust Program 0x{timestamp:x}"))
.styled(false)
.disassemble()
)
} }
} }
} }

View File

@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use crate::{ use crate::{
AnnotatedError, Chunk, ChunkError, DustError, FunctionType, Identifier, Instruction, LexError, AnnotatedError, Chunk, ChunkError, DustError, FunctionType, Identifier, Instruction, LexError,
Lexer, Operation, Span, Token, TokenKind, TokenOwned, Type, Value, Lexer, Local, Operation, Span, Token, TokenKind, TokenOwned, Type, Value,
}; };
pub fn parse(source: &str) -> Result<Chunk, DustError> { pub fn parse(source: &str) -> Result<Chunk, DustError> {
@ -654,10 +654,10 @@ impl<'src> Parser<'src> {
self.advance()?; self.advance()?;
let local_index = self.parse_identifier_from(token, start_position)?; let local_index = self.parse_identifier_from(token, start_position)?;
let to_register = self let is_mutable = self
.chunk .chunk
.get_local(local_index, start_position)? .get_local(local_index, start_position)?
.register_index; .is_mutable;
if self.allow(Token::Equal)? { if self.allow(Token::Equal)? {
if !allowed.assignment { if !allowed.assignment {
@ -667,11 +667,6 @@ impl<'src> Parser<'src> {
}); });
} }
let is_mutable = self
.chunk
.get_local(local_index, start_position)?
.is_mutable;
if !is_mutable { if !is_mutable {
return Err(ParseError::CannotMutateImmutableVariable { return Err(ParseError::CannotMutateImmutableVariable {
identifier: self.chunk.get_identifier(local_index).cloned().unwrap(), identifier: self.chunk.get_identifier(local_index).cloned().unwrap(),
@ -700,15 +695,14 @@ impl<'src> Parser<'src> {
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, local_index), Instruction::set_local(previous_instruction.a(), local_index),
start_position, start_position,
); );
self.increment_register()?;
self.parsed_expression = false; self.parsed_expression = false;
} else { } else {
self.emit_instruction( self.emit_instruction(
Instruction::get_local(to_register, local_index), Instruction::get_local(self.current_register, local_index),
self.previous_position, self.previous_position,
); );

View File

@ -2,7 +2,7 @@ use std::{fs::read_to_string, io::Write};
use clap::Parser; use clap::Parser;
use colored::Colorize; use colored::Colorize;
use dust_lang::{format, parse, run, Chunk, DustError, Vm}; use dust_lang::{format, parse, run};
use log::Level; use log::Level;
#[derive(Parser)] #[derive(Parser)]
@ -23,10 +23,6 @@ struct Cli {
#[arg(short = 'o', long)] #[arg(short = 'o', long)]
format_colored: bool, format_colored: bool,
/// Whether to run the source code
#[arg(short, long)]
no_run: bool,
/// Whether to output the disassembled chunk /// Whether to output the disassembled chunk
#[arg(short, long)] #[arg(short, long)]
parse: bool, parse: bool,
@ -72,40 +68,6 @@ fn main() {
return; return;
}; };
if !args.no_run {
if args.format {
format_source(source, args.format_line_numbers, args.format_colored);
}
let run_result = if args.parse {
let chunk = parse(source).unwrap();
let disassembly = chunk
.disassembler("Dust CLI Input")
.source(source)
.styled(args.style_disassembly)
.disassemble();
println!("{}", disassembly);
let mut vm = Vm::new(chunk);
vm.run()
.map_err(|error| DustError::Runtime { error, source })
} else {
run(source)
};
match run_result {
Ok(Some(value)) => println!("{}", value),
Ok(_) => {}
Err(error) => {
eprintln!("{}", error.report());
}
}
return;
}
if args.format { if args.format {
format_source(source, args.format_line_numbers, args.format_colored); format_source(source, args.format_line_numbers, args.format_colored);
} }
@ -113,9 +75,15 @@ fn main() {
if args.parse { if args.parse {
parse_source(source, args.style_disassembly); parse_source(source, args.style_disassembly);
} }
if args.format || args.parse {
return;
} }
pub fn format_source(source: &str, line_numbers: bool, colored: bool) { run_source(source);
}
fn format_source(source: &str, line_numbers: bool, colored: bool) {
log::info!("Formatting source"); log::info!("Formatting source");
match format(source, line_numbers, colored) { match format(source, line_numbers, colored) {
@ -126,19 +94,29 @@ pub fn format_source(source: &str, line_numbers: bool, colored: bool) {
} }
} }
fn parse_source(source: &str, styled: bool) -> Option<Chunk> { fn parse_source(source: &str, styled: bool) {
parse(source) match parse(source) {
.inspect(|chunk| { Ok(chunk) => {
let disassembly = chunk let disassembly = chunk
.disassembler("Dust CLI Input") .disassembler("Dust CLI Input")
.source(source) .source(source)
.styled(styled) .styled(styled)
.disassemble(); .disassemble();
println!("{disassembly}",); println!("{}", disassembly);
}) }
.inspect_err(|error| { Err(error) => {
eprintln!("{}", error.report()); eprintln!("{}", error.report());
}) }
.ok() }
}
fn run_source(source: &str) {
match run(source) {
Ok(Some(value)) => println!("{}", value),
Ok(None) => {}
Err(error) => {
eprintln!("{}", error.report());
}
}
} }