Clean up
This commit is contained in:
parent
95e5b3062d
commit
19f2d19134
@ -1,4 +1,7 @@
|
||||
use std::fmt::{self, Debug, Display};
|
||||
use std::{
|
||||
fmt::{self, Debug, Display},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use colored::Colorize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -280,27 +283,16 @@ impl Display for Chunk {
|
||||
|
||||
impl Debug for Chunk {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let timestamp = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
let executable = std::env::current_exe().unwrap_or_else(|_| PathBuf::new());
|
||||
let disassembly = self
|
||||
.disassembler(&executable.to_string_lossy())
|
||||
.styled(false)
|
||||
.disassemble();
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
write!(
|
||||
f,
|
||||
"\n{}\n",
|
||||
self.disassembler(&format!("Dust Program 0x{timestamp:x}"))
|
||||
.styled(false)
|
||||
.disassemble()
|
||||
)
|
||||
write!(f, "\n{}", disassembly)
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
self.disassembler(&format!("Dust Program 0x{timestamp:x}"))
|
||||
.styled(false)
|
||||
.disassemble()
|
||||
)
|
||||
write!(f, "{}", disassembly)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
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> {
|
||||
@ -654,10 +654,10 @@ impl<'src> Parser<'src> {
|
||||
self.advance()?;
|
||||
|
||||
let local_index = self.parse_identifier_from(token, start_position)?;
|
||||
let to_register = self
|
||||
let is_mutable = self
|
||||
.chunk
|
||||
.get_local(local_index, start_position)?
|
||||
.register_index;
|
||||
.is_mutable;
|
||||
|
||||
if self.allow(Token::Equal)? {
|
||||
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 {
|
||||
return Err(ParseError::CannotMutateImmutableVariable {
|
||||
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(
|
||||
Instruction::set_local(self.current_register, local_index),
|
||||
Instruction::set_local(previous_instruction.a(), local_index),
|
||||
start_position,
|
||||
);
|
||||
self.increment_register()?;
|
||||
|
||||
self.parsed_expression = false;
|
||||
} else {
|
||||
self.emit_instruction(
|
||||
Instruction::get_local(to_register, local_index),
|
||||
Instruction::get_local(self.current_register, local_index),
|
||||
self.previous_position,
|
||||
);
|
||||
|
||||
|
@ -2,7 +2,7 @@ use std::{fs::read_to_string, io::Write};
|
||||
|
||||
use clap::Parser;
|
||||
use colored::Colorize;
|
||||
use dust_lang::{format, parse, run, Chunk, DustError, Vm};
|
||||
use dust_lang::{format, parse, run};
|
||||
use log::Level;
|
||||
|
||||
#[derive(Parser)]
|
||||
@ -23,10 +23,6 @@ struct Cli {
|
||||
#[arg(short = 'o', long)]
|
||||
format_colored: bool,
|
||||
|
||||
/// Whether to run the source code
|
||||
#[arg(short, long)]
|
||||
no_run: bool,
|
||||
|
||||
/// Whether to output the disassembled chunk
|
||||
#[arg(short, long)]
|
||||
parse: bool,
|
||||
@ -72,40 +68,6 @@ fn main() {
|
||||
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 {
|
||||
format_source(source, args.format_line_numbers, args.format_colored);
|
||||
}
|
||||
@ -113,9 +75,15 @@ fn main() {
|
||||
if args.parse {
|
||||
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");
|
||||
|
||||
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> {
|
||||
parse(source)
|
||||
.inspect(|chunk| {
|
||||
fn parse_source(source: &str, styled: bool) {
|
||||
match parse(source) {
|
||||
Ok(chunk) => {
|
||||
let disassembly = chunk
|
||||
.disassembler("Dust CLI Input")
|
||||
.source(source)
|
||||
.styled(styled)
|
||||
.disassemble();
|
||||
|
||||
println!("{disassembly}",);
|
||||
})
|
||||
.inspect_err(|error| {
|
||||
eprintln!("{}", error.report());
|
||||
})
|
||||
.ok()
|
||||
println!("{}", disassembly);
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("{}", error.report());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_source(source: &str) {
|
||||
match run(source) {
|
||||
Ok(Some(value)) => println!("{}", value),
|
||||
Ok(None) => {}
|
||||
Err(error) => {
|
||||
eprintln!("{}", error.report());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user