diff --git a/src/main.rs b/src/main.rs index cc63a0c..205eb3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -209,9 +209,10 @@ fn compile(file_content: &String, file_path: &str, intrinsics: &HashMap<&str, (V { let mut tokens: Vec = tokenize(&file_content)?; println!("---Done tokenizing, got {} tokens---", tokens.len()); - let mut functions: Vec = extract_functions(&mut tokens, &intrinsics, debug)?; + let mut functions: Vec = Vec::new(); + extract_functions(&mut tokens, &mut functions, &intrinsics, debug)?; println!("---Done extracting functions, got {} functions and reduced the token count to {}---", functions.len(), tokens.len()); - resolve_imports(&mut tokens, &mut functions, file_path, &mut Vec::from([PathBuf::from(file_path)]), intrinsics, debug)?; + resolve_imports(&mut tokens, &mut functions, file_path, &mut Vec::from([fs::canonicalize(file_path).unwrap()]), intrinsics, debug)?; println!("---Done importing files---"); let mut arrays: Vec = extract_arrays(&mut tokens, &intrinsics, &functions, debug)?; println!("---Done extracting arrays, got {} arrays and reduced the token count to {}---", arrays.len(), tokens.len()); @@ -303,7 +304,7 @@ fn resolve_imports(tokens: &mut Vec, functions: &mut Vec, file_ { if let Some(Token::StringLit(import_path, _, _)) = tokens_iter.next() { - match fs::canonicalize(format!("{}/{}", PathBuf::from(file_path).parent().unwrap_or(&PathBuf::from(".")).display(), import_path)) + match fs::canonicalize(format!("{}/{}", fs::canonicalize(file_path).map_err(|e| e.to_string())?.parent().unwrap_or(&PathBuf::from(".")).display(), import_path)) { Ok(full_import_path) => { @@ -321,17 +322,15 @@ fn resolve_imports(tokens: &mut Vec, functions: &mut Vec, file_ { let mut import_tokens: Vec = tokenize(&file_content)?; println!("--Done tokenizing the imported file at {}:{}, got {} tokens--", line, col, tokens.len()); - let import_functions = extract_functions(&mut import_tokens, &intrinsics, debug)?; + extract_functions(&mut import_tokens, functions, &intrinsics, debug)?; resolve_imports(&mut import_tokens, functions, file_path, visited_paths, intrinsics, debug)?; - println!("--Done extracting {} functions--", import_functions.len()); - functions.extend(import_functions); println!("--Now totalling {} functions--", functions.len()); } - Err(e) => return Err(format!("{}: {}", line!(), e.to_string())) + Err(e) => return Err(e.to_string()), } } } - Err(e) => return Err(format!("{}: {} {}/{}", line!(), e.to_string(), file_path, import_path)) + Err(e) => return Err(e.to_string()), } } else @@ -1483,10 +1482,9 @@ fn str_to_datatype(s: &str, line: i32, col: i32) -> Result } } -fn extract_functions(tokens: &mut Vec, intrinsics: &HashMap<&str, (Vec, Vec)>, debug: bool) -> Result, String> +fn extract_functions(tokens: &mut Vec, functions: &mut Vec, intrinsics: &HashMap<&str, (Vec, Vec)>, debug: bool) -> Result<(), String> { let mut tokens_iter = tokens.iter().peekable(); - let mut functions: Vec = Vec::new(); let mut new_tokens: Vec = Vec::new(); while let Some(token) = tokens_iter.next() { @@ -1560,7 +1558,7 @@ fn extract_functions(tokens: &mut Vec, intrinsics: &HashMap<&str, (Vec outs.push(Datatype::String), "int" => outs.push(Datatype::Int), "bool" => outs.push(Datatype::Bool), - "{" | "}" | "deq" | "req" | "dup" | "swp" | "true" | "false" | "depth" | "???" => return Err(format!("Expected function name but got {} at {}:{}", word, line, col)), + "{" | "}" | "deq" | "req" | "dup" | "swp" | "true" | "false" | "depth" | "???" | "import" => return Err(format!("Expected function name but got {} at {}:{}", word, line, col)), _ => { if functions.iter().any(|x| &x.name == word) @@ -1599,7 +1597,7 @@ fn extract_functions(tokens: &mut Vec, intrinsics: &HashMap<&str, (Vec>, intrinsics: &HashMap<&str, (Vec, Vec)>, debug: bool) -> Result, String> diff --git a/tests/comparisons.qbl b/tests/comparisons.qbl index 8d4474e..434f2ce 100644 --- a/tests/comparisons.qbl +++ b/tests/comparisons.qbl @@ -11,17 +11,7 @@ //true //:END: -function bool => str boolToStr -{ - if - { - "true" - } - else - { - "false" - } -} +import "../std.qbl" 1 0 > boolToStr println 1 0 < boolToStr println @@ -33,4 +23,4 @@ function bool => str boolToStr 1 1 < boolToStr println 1 1 >= boolToStr println 1 1 <= boolToStr println -1 1 == boolToStr println \ No newline at end of file +1 1 == boolToStr println diff --git a/tests/transitive_import.qbl b/tests/transitive_import.qbl new file mode 100644 index 0000000..ff0e242 --- /dev/null +++ b/tests/transitive_import.qbl @@ -0,0 +1,5 @@ +//valid,false +//:END: +import "basic_import.qbl" + +false boolToStr println diff --git a/tests/while.qbl b/tests/while.qbl index 69fb4de..8f898b8 100644 --- a/tests/while.qbl +++ b/tests/while.qbl @@ -1,6 +1,8 @@ //valid,10987654321falsefalse //:END: +import "../std.qbl" + true while { false @@ -16,20 +18,8 @@ while } deq -function bool => str boolToStr -{ - if - { - "true" - } - else - { - "false" - } -} - true true true while { false } -boolToStr boolToStr print println \ No newline at end of file +boolToStr boolToStr print println