Improve importing

This commit is contained in:
0x4261756D 2023-02-06 15:00:37 +01:00
parent 131386632f
commit 5aa365c681
4 changed files with 20 additions and 37 deletions

View File

@ -209,9 +209,10 @@ fn compile(file_content: &String, file_path: &str, intrinsics: &HashMap<&str, (V
{
let mut tokens: Vec<Token> = tokenize(&file_content)?;
println!("---Done tokenizing, got {} tokens---", tokens.len());
let mut functions: Vec<Function> = extract_functions(&mut tokens, &intrinsics, debug)?;
let mut functions: Vec<Function> = 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<Arr> = 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<Token>, functions: &mut Vec<Function>, 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<Token>, functions: &mut Vec<Function>, file_
{
let mut import_tokens: Vec<Token> = 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<Datatype, String>
}
}
fn extract_functions(tokens: &mut Vec<Token>, intrinsics: &HashMap<&str, (Vec<Datatype>, Vec<Datatype>)>, debug: bool) -> Result<Vec<Function>, String>
fn extract_functions(tokens: &mut Vec<Token>, functions: &mut Vec<Function>, intrinsics: &HashMap<&str, (Vec<Datatype>, Vec<Datatype>)>, debug: bool) -> Result<(), String>
{
let mut tokens_iter = tokens.iter().peekable();
let mut functions: Vec<Function> = Vec::new();
let mut new_tokens: Vec<Token> = Vec::new();
while let Some(token) = tokens_iter.next()
{
@ -1560,7 +1558,7 @@ fn extract_functions(tokens: &mut Vec<Token>, intrinsics: &HashMap<&str, (Vec<Da
"str" => 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<Token>, intrinsics: &HashMap<&str, (Vec<Da
}
tokens.clear();
tokens.extend_from_slice(&new_tokens);
return Ok(functions);
return Ok(());
}
fn parse_block(tokens_iter: &mut Peekable<std::slice::Iter<Token>>, intrinsics: &HashMap<&str, (Vec<Datatype>, Vec<Datatype>)>, debug: bool) -> Result<Vec<Operation>, String>

View File

@ -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
1 1 == boolToStr println

View File

@ -0,0 +1,5 @@
//valid,false
//:END:
import "basic_import.qbl"
false boolToStr println

View File

@ -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
boolToStr boolToStr print println