diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-01-10 14:06:38 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-01-10 14:06:38 +0100 |
| commit | 78749296776b2d9b7cb1d03b93c97ceb32c8c0b3 (patch) | |
| tree | 45b29dada0784b3b7c6264dae61169fe4ecaa84d /src | |
| parent | de8108932c3ac9ef1ea70ea5b0c74f369c36c442 (diff) | |
| download | sylt-78749296776b2d9b7cb1d03b93c97ceb32c8c0b3.tar.gz | |
take Paths where files are needed
Diffstat (limited to 'src')
| -rw-r--r-- | src/compiler.rs | 10 | ||||
| -rw-r--r-- | src/main.rs | 22 | ||||
| -rw-r--r-- | src/tokenizer.rs | 5 | ||||
| -rw-r--r-- | src/vm.rs | 13 |
4 files changed, 30 insertions, 20 deletions
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<PathBuf> { + 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<PlacedToken>; -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(); @@ -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<Op>, last_line_offset: Option<usize>, line_offsets: HashMap<usize, usize>, } 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<String>, } @@ -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, } |
