Skip to content

Commit 3ebfb63

Browse files
committed
feat: Accept file path as argument
1 parent bfa32f3 commit 3ebfb63

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

packages/cli/src/main.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
use std::{path::PathBuf, str::FromStr};
2-
31
use lang_engine::Engine;
42

53
#[macro_use] extern crate log;
64

7-
fn main() -> Result<(), Box<dyn std::error::Error>> {
5+
fn main() {
86
pretty_env_logger::formatted_builder()
97
.target(pretty_env_logger::env_logger::Target::Stdout)
108
.format_module_path(true)
119
.filter_level(log::LevelFilter::Trace)
1210
.init();
1311

14-
info!("creating engine");
12+
debug!("initialized logger");
1513

1614
let mut engine = Engine::default();
1715

18-
match engine.exec_file(&PathBuf::from_str("./script.tsh")?) {
16+
17+
let args: Vec<String> = std::env::args().collect();
18+
if args.len() < 2 {
19+
error!("no input file provided");
20+
return;
21+
}
22+
23+
let file_path = &args[1];
24+
let absolute_path = match std::fs::canonicalize(file_path) {
25+
Ok(path) => path,
26+
Err(err) => {
27+
error!("Could not get absolute path: {}", err);
28+
return;
29+
}
30+
};
31+
32+
match engine.exec_file(&absolute_path) {
1933
Ok(status) => info!("finished with status {status}"),
2034
Err(err) => error!("{err}")
2135
};
22-
23-
Ok(())
2436
}

packages/engine/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ pub struct Engine {
2424

2525
impl Engine {
2626
pub fn create() -> Self {
27-
Self {
28-
29-
}
27+
debug!("created engine");
28+
Self {}
3029
}
3130

3231
pub fn exec_file(&mut self, file: &PathBuf) -> EngineResult<i32> {
32+
debug!("executing file {file:?}");
33+
3334
let code = if file.is_file() && (file.is_absolute() || file.is_relative()) {
3435
std::fs::read_to_string(file).map_err(|_| error::EngineError::UnknownError)?
3536
} else {
@@ -45,8 +46,9 @@ impl Engine {
4546
}
4647

4748
pub fn exec(&mut self, code: &str) -> EngineResult<i32> {
49+
debug!("executing script");
50+
4851
let mut lexer = Lexer::create(code, None);
49-
5052
println!("{:#?}", lexer.tokenize());
5153

5254
lexer.print_errors();

script.tsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var test = "
1+
var test = ""

0 commit comments

Comments
 (0)