From 78749296776b2d9b7cb1d03b93c97ceb32c8c0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 10 Jan 2021 14:06:38 +0100 Subject: take Paths where files are needed --- src/compiler.rs | 10 ++++++---- src/main.rs | 22 ++++++++++++++-------- src/tokenizer.rs | 5 +++-- src/vm.rs | 13 +++++++------ 4 files changed, 30 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/compiler.rs b/src/compiler.rs index 7732992..218711c 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -1,3 +1,5 @@ +use std::path::Path; + use crate::tokenizer::{Token, TokenStream}; use crate::vm::{Value, Block, Op}; @@ -196,8 +198,8 @@ impl Compiler { } } - pub fn compile(&mut self, name: &str, filename: &str) -> Block { - let mut block = Block::new(name, filename); + pub fn compile(&mut self, name: &str, file: &Path) -> Block { + let mut block = Block::new(name, file); loop { if self.peek() == Token::EOF { @@ -217,6 +219,6 @@ impl Compiler { } } -pub fn compile(name: &str, filename: &str, tokens: TokenStream) -> Block { - Compiler::new(tokens).compile(name, filename) +pub fn compile(name: &str, file: &Path, tokens: TokenStream) -> Block { + Compiler::new(tokens).compile(name, file) } diff --git a/src/main.rs b/src/main.rs index 987bcd2..e6265f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,24 @@ +use std::path::{Path, PathBuf}; + mod tokenizer; mod vm; mod compiler; fn main() { - let file = "tests/simple.tdy"; - let tokens = tokenizer::file_to_tokens(file); - - for token in tokens.iter() { - println!("{:?}", token); + let file = file_from_args().unwrap_or_else(|| Path::new("tests/simple.tdy").to_owned()); + if let Err(err) = run_file(&file) { + println!("{}", err); } +} - let block = compiler::compile("main", file, tokens); +fn file_from_args() -> Option { + std::env::args().skip(1).map(|s| Path::new(&s).to_owned()).find(|p| p.is_file()) +} - if let Err(err) = vm::run_block(block) { - println!("{}", err); +fn run_file(path: &Path) -> Result<(), vm::VMError> { + let tokens = tokenizer::file_to_tokens(path); + let block = compiler::compile("main", path, tokens); // path -> str might fail + vm::run_block(block) +} } } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 9b33a06..e360085 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -1,4 +1,5 @@ use std::fs; +use std::path::Path; use logos::Logos; #[derive(Logos, Debug, PartialEq, Clone)] @@ -117,8 +118,8 @@ pub enum Token { pub type PlacedToken = (Token, usize); pub type TokenStream = Vec; -pub fn file_to_tokens(filename: &str) -> TokenStream { - let content = fs::read_to_string(filename).unwrap(); +pub fn file_to_tokens(file: &Path) -> TokenStream { + let content = fs::read_to_string(file).unwrap(); let lexer = Token::lexer(&content); let mut placed_tokens = lexer.spanned().peekable(); diff --git a/src/vm.rs b/src/vm.rs index 530eeec..5d8b5f8 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::fmt; +use std::path::{Path, PathBuf}; #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub enum Value { @@ -36,17 +37,17 @@ pub enum Op { #[derive(Debug)] pub struct Block { name: String, - filename: String, + file: PathBuf, ops: Vec, last_line_offset: Option, line_offsets: HashMap, } impl Block { - pub fn new(name: &str, filename: &str) -> Self { + pub fn new(name: &str, file: &Path) -> Self { Self { name: String::from(name), - filename: String::from(filename), + file: file.to_owned(), ops: Vec::new(), last_line_offset: None, line_offsets: HashMap::new(), @@ -90,7 +91,7 @@ pub enum VMErrorKind { #[derive(Debug)] pub struct VMError { kind: VMErrorKind, - filename: String, + file: PathBuf, line: usize, message: Option, } @@ -117,7 +118,7 @@ impl fmt::Display for VMError { Some(s) => format!("\n{}", s), None => String::from(""), }; - write!(f, "{}:{} [Runtime Error] {}{}", self.filename, self.line, self.kind, message) + write!(f, "{:?}:{} [Runtime Error] {}{}", self.file, self.line, self.kind, message) } } @@ -164,7 +165,7 @@ impl VM { VMError { kind, - filename: self.block.filename.clone(), + file: self.block.file.clone(), line: find_line(), message, } -- cgit v1.2.1