diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index f1d3f24f..cb83ce57 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -65,7 +65,7 @@ impl<'a, S: SyscallHandler> Interpreter<'a, S> { let function = self.ir_module.functions.get(&function_ref).unwrap(); if function.is_cstyle_variadic { - todo!("c-style variadic functions are not supported in interpreter yet"); + todo!("c-style variadic functions are not supported in interpreter yet - (for function {:?})", function.mangled_name); } assert_eq!(function.parameters.len(), args.len()); diff --git a/src/main.rs b/src/main.rs index 1e6cf818..9d00bfe0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,7 +120,7 @@ fn build_project(build_command: BuildCommand) { }; if metadata.is_dir() { - compile_workspace(&mut compiler, filepath); + compile_workspace(&mut compiler, filepath, None); } else { if filepath.extension().unwrap_or_default() == "h" { let source_files = compiler.source_files; @@ -151,7 +151,7 @@ fn build_project(build_command: BuildCommand) { } let project_folder = filepath.parent().unwrap(); - compile_single_file_only(&mut compiler, project_folder, &filename, filepath); + compile_single_file_only(&mut compiler, project_folder, filepath); } } diff --git a/src/single_file_only/mod.rs b/src/single_file_only/mod.rs index 9c2b8db1..e4dd5ead 100644 --- a/src/single_file_only/mod.rs +++ b/src/single_file_only/mod.rs @@ -4,107 +4,9 @@ --------------------------------------------------------------------------- */ -use crate::{ - ast::AstWorkspace, - compiler::Compiler, - exit_unless, - inflow::IntoInflow, - interpreter_env::{run_build_system_interpreter, setup_build_system_interpreter_symbols}, - lexer::Lexer, - llvm_backend::llvm_backend, - lower::lower, - parser::parse, - resolve::resolve, - text::{IntoText, IntoTextStream}, - workspace::fs::Fs, -}; -use indexmap::IndexMap; -use std::{ffi::OsString, fs::create_dir_all, path::Path, process::exit}; +use crate::{compiler::Compiler, workspace::compile_workspace}; +use std::path::Path; -pub fn compile_single_file_only( - compiler: &mut Compiler, - project_folder: &Path, - filename: &str, - filepath: &Path, -) { - let source_files = compiler.source_files; - - let project_name = filepath.file_stem().map(OsString::from).unwrap_or_else(|| { - std::env::current_dir() - .ok() - .map(|dir| { - dir.file_name() - .map(OsString::from) - .unwrap_or_else(|| OsString::from("main")) - }) - .unwrap_or_else(|| OsString::from("main")) - }); - - let bin_folder = project_folder.join("bin"); - let obj_folder = project_folder.join("obj"); - - create_dir_all(&bin_folder).expect("failed to create bin folder"); - create_dir_all(&obj_folder).expect("failed to create obj folder"); - - let exe_filepath = bin_folder.join(compiler.target.default_executable_name(&project_name)); - let obj_filepath = obj_folder.join(compiler.target.default_object_file_name(&project_name)); - - let content = std::fs::read_to_string(filename) - .map_err(|err| { - eprintln!("{}", err); - exit(1); - }) - .unwrap(); - - let key = source_files.add(filename.into(), content); - let content = source_files.get(key).content(); - let text = content.chars().into_text_stream(key).into_text(); - - let fs = Fs::new(); - let fs_node_id = fs.insert(filepath, None).expect("inserted"); - - let mut ast_file = exit_unless( - parse(Lexer::new(text).into_inflow(), source_files, key), - source_files, - ); - - if compiler.options.interpret { - setup_build_system_interpreter_symbols(&mut ast_file); - } - - let files = IndexMap::from_iter(std::iter::once((fs_node_id, ast_file))); - let mut workspace = AstWorkspace::new(fs, files, compiler.source_files, None); - - let resolved_ast = exit_unless(resolve(&mut workspace, &compiler.options), source_files); - - let ir_module = exit_unless( - lower(&compiler.options, &resolved_ast, &compiler.target), - source_files, - ); - - if compiler.options.interpret { - match run_build_system_interpreter(&resolved_ast, &ir_module) { - Ok(_) => return, - Err(err) => { - eprintln!("{}", err); - exit(1); - } - } - } - - exit_unless( - unsafe { - llvm_backend( - compiler, - &ir_module, - &resolved_ast, - &obj_filepath, - &exe_filepath, - &compiler.diagnostics, - ) - }, - source_files, - ); - - compiler.maybe_execute_result(&exe_filepath); +pub fn compile_single_file_only(compiler: &mut Compiler, project_folder: &Path, filepath: &Path) { + compile_workspace(compiler, project_folder, Some(filepath.to_path_buf())) } diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index 37208199..dede55ec 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -4,7 +4,7 @@ --------------------------------------------------------------------------- */ -mod compile; +pub mod compile; mod explore; pub mod fs; mod module_file; @@ -22,6 +22,7 @@ use crate::{ diagnostics::{ErrorDiagnostic, WarningDiagnostic}, exit_unless, inflow::{Inflow, IntoInflow}, + interpreter_env::{run_build_system_interpreter, setup_build_system_interpreter_symbols}, lexer::Lexer, line_column::Location, llvm_backend::llvm_backend, @@ -47,7 +48,7 @@ use std::{ collections::HashMap, ffi::OsString, fs::create_dir_all, - path::Path, + path::{Path, PathBuf}, process::exit, sync::{ atomic::{AtomicU64, Ordering}, @@ -74,7 +75,11 @@ impl<'a, I: Inflow> CodeFile<'a, I> { } } -pub fn compile_workspace(compiler: &mut Compiler, project_folder: &Path) { +pub fn compile_workspace( + compiler: &mut Compiler, + project_folder: &Path, + single_file: Option, +) { let compiler = compiler; let start_time = Instant::now(); @@ -84,11 +89,28 @@ pub fn compile_workspace(compiler: &mut Compiler, project_folder: &Path) { let num_module_files_failed = AtomicU64::new(0); let fs = Fs::new(); + let mut guaranteed_entry = None; let Some(ExploreResult { module_files, normal_files, - }) = explore(&fs, project_folder) + }) = (if let Some(single_file) = single_file { + let fs_node_id = fs.insert(&single_file, None).expect("inserted"); + + let file = ModuleFile { + path: single_file, + fs_node_id, + }; + + guaranteed_entry = Some(fs_node_id); + + Some(ExploreResult { + normal_files: vec![], + module_files: vec![file], + }) + } else { + explore(&fs, project_folder) + }) else { eprintln!( "error: Could not locate workspace folder '{}'", @@ -274,7 +296,20 @@ pub fn compile_workspace(compiler: &mut Compiler, project_folder: &Path) { }; let module_folders = HashMap::::from_iter(module_folders.into_iter()); - let files = IndexMap::from_iter(ast_files.into_iter()); + let mut files = IndexMap::from_iter(ast_files.into_iter()); + + if compiler.options.interpret { + if let Some(guaranteed_entry) = guaranteed_entry { + let ast_file = files.get_mut(&guaranteed_entry).unwrap(); + setup_build_system_interpreter_symbols(ast_file); + } else { + eprintln!( + "error: experimental manual interpreter does not properly handle multiple files yet" + ); + exit(1); + } + } + let workspace = AstWorkspace::new(fs, files, compiler.source_files, Some(module_folders)); let resolved_ast = exit_unless( @@ -301,6 +336,16 @@ pub fn compile_workspace(compiler: &mut Compiler, project_folder: &Path) { .unwrap_or_else(|| OsString::from("main")) }); + if compiler.options.interpret { + match run_build_system_interpreter(&resolved_ast, &ir_module) { + Ok(_) => return, + Err(err) => { + eprintln!("{}", err); + exit(1); + } + } + } + let bin_folder = project_folder.join("bin"); let obj_folder = project_folder.join("obj"); diff --git a/tests/_should_fail/mismatching_yielded_types/main.adept b/tests/_should_fail/mismatching_yielded_types/main.adept index a1d3cc0b..a521add5 100644 --- a/tests/_should_fail/mismatching_yielded_types/main.adept +++ b/tests/_should_fail/mismatching_yielded_types/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/_should_fail/recursive_type_alias/main.adept b/tests/_should_fail/recursive_type_alias/main.adept index f1afa344..0092e3f7 100644 --- a/tests/_should_fail/recursive_type_alias/main.adept +++ b/tests/_should_fail/recursive_type_alias/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + typealias Type1 = Type2 typealias Type2 = Type3 typealias Type3 = Type1 diff --git a/tests/and_or/main.adept b/tests/and_or/main.adept index cdbbef1a..50e77271 100644 --- a/tests/and_or/main.adept +++ b/tests/and_or/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/annotation_groups/main.adept b/tests/annotation_groups/main.adept index 372db128..4848a5bf 100644 --- a/tests/annotation_groups/main.adept +++ b/tests/annotation_groups/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] { // NOTE: This is only macOS (also type is incorrect but it's a pointer so doesn't matter) diff --git a/tests/array_access/main.adept b/tests/array_access/main.adept index ed6b9950..22cb4aa4 100644 --- a/tests/array_access/main.adept +++ b/tests/array_access/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] { func printf(format ptr, ...) int diff --git a/tests/bitwise_operators/main.adept b/tests/bitwise_operators/main.adept index feb68f35..b3c1db4e 100644 --- a/tests/bitwise_operators/main.adept +++ b/tests/bitwise_operators/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/c_printf/main.adept b/tests/c_printf/main.adept index ba77c9af..a5eb712e 100644 --- a/tests/c_printf/main.adept +++ b/tests/c_printf/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/character_literals/main.adept b/tests/character_literals/main.adept index ea336854..02ff93aa 100644 --- a/tests/character_literals/main.adept +++ b/tests/character_literals/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/comparison_operators/main.adept b/tests/comparison_operators/main.adept index f843e3b9..5f759438 100644 --- a/tests/comparison_operators/main.adept +++ b/tests/comparison_operators/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/defines/main.adept b/tests/defines/main.adept index e588688a..b92fc3cf 100644 --- a/tests/defines/main.adept +++ b/tests/defines/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/enums/main.adept b/tests/enums/main.adept index 0c51366d..701e9939 100644 --- a/tests/enums/main.adept +++ b/tests/enums/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + enum Color (Red, Green, Blue) #[foreign] diff --git a/tests/float_literal/main.adept b/tests/float_literal/main.adept index e1070526..ed541022 100644 --- a/tests/float_literal/main.adept +++ b/tests/float_literal/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/function_parameters/main.adept b/tests/function_parameters/main.adept index 2e3cacdc..8b552e53 100644 --- a/tests/function_parameters/main.adept +++ b/tests/function_parameters/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/function_simple/main.adept b/tests/function_simple/main.adept index 8bf11e3a..3ae74e28 100644 --- a/tests/function_simple/main.adept +++ b/tests/function_simple/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/global_variables/main.adept b/tests/global_variables/main.adept index 90a9eb23..94ec155a 100644 --- a/tests/global_variables/main.adept +++ b/tests/global_variables/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/hello_world/main.adept b/tests/hello_world/main.adept index ba77c9af..a5eb712e 100644 --- a/tests/hello_world/main.adept +++ b/tests/hello_world/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/if/main.adept b/tests/if/main.adept index f1be288a..13e69c48 100644 --- a/tests/if/main.adept +++ b/tests/if/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] { func printf(format ptr, ...) int diff --git a/tests/if_elif_else/main.adept b/tests/if_elif_else/main.adept index c345b211..d77412ee 100644 --- a/tests/if_elif_else/main.adept +++ b/tests/if_elif_else/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] { // NOTE: This is only macOS (also type is incorrect but it's a pointer so doesn't matter) diff --git a/tests/if_eval/main.adept b/tests/if_eval/main.adept index 2a10de66..d94c73e2 100644 --- a/tests/if_eval/main.adept +++ b/tests/if_eval/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/integer_and_float_literals_combining/main.adept b/tests/integer_and_float_literals_combining/main.adept index 80fbe200..076cfe20 100644 --- a/tests/integer_and_float_literals_combining/main.adept +++ b/tests/integer_and_float_literals_combining/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/integer_hex_literals/main.adept b/tests/integer_hex_literals/main.adept index 6a59c667..49f6871e 100644 --- a/tests/integer_hex_literals/main.adept +++ b/tests/integer_hex_literals/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/integer_literal_conforming/main.adept b/tests/integer_literal_conforming/main.adept index da01f936..bec36abe 100644 --- a/tests/integer_literal_conforming/main.adept +++ b/tests/integer_literal_conforming/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/integer_signed_overflow/main.adept b/tests/integer_signed_overflow/main.adept index 2455dde1..90d38a91 100644 --- a/tests/integer_signed_overflow/main.adept +++ b/tests/integer_signed_overflow/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + func main { x i64 = 9223372036854775807 x = x + 1 diff --git a/tests/integer_unsigned_overflow/main.adept b/tests/integer_unsigned_overflow/main.adept index 0863247c..4a239686 100644 --- a/tests/integer_unsigned_overflow/main.adept +++ b/tests/integer_unsigned_overflow/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + func main { x u64 = 18446744073709551615 x = x + 1 diff --git a/tests/integer_value_conforming/main.adept b/tests/integer_value_conforming/main.adept index f3a81ce0..0ce77d9c 100644 --- a/tests/integer_value_conforming/main.adept +++ b/tests/integer_value_conforming/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/math_floats/main.adept b/tests/math_floats/main.adept index 4056c071..4a58b942 100644 --- a/tests/math_floats/main.adept +++ b/tests/math_floats/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/math_simple/main.adept b/tests/math_simple/main.adept index 650ed713..f5d721da 100644 --- a/tests/math_simple/main.adept +++ b/tests/math_simple/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/member/main.adept b/tests/member/main.adept index c39a3622..9428daf2 100644 --- a/tests/member/main.adept +++ b/tests/member/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/multiline_comments/main.adept b/tests/multiline_comments/main.adept new file mode 100644 index 00000000..8e77eb9d --- /dev/null +++ b/tests/multiline_comments/main.adept @@ -0,0 +1,24 @@ + +pragma => adept("3.0") + +#[foreign] +func printf(format ptr, ...) int + +func main { + /* + This + is + a multi-line + comment + */ + + printf(c"Multi-Line ") + + /* + They are also be /* nested + */ + */ + + printf(c"Comments!\n") +} + diff --git a/tests/nested_expressions/main.adept b/tests/nested_expressions/main.adept index b1d04ae9..8ce66dcf 100644 --- a/tests/nested_expressions/main.adept +++ b/tests/nested_expressions/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/object_mutation/main.adept b/tests/object_mutation/main.adept index bff23af4..3406651b 100644 --- a/tests/object_mutation/main.adept +++ b/tests/object_mutation/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/op_then_assign/main.adept b/tests/op_then_assign/main.adept index 13890c5c..a6b821d2 100644 --- a/tests/op_then_assign/main.adept +++ b/tests/op_then_assign/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/pointers/main.adept b/tests/pointers/main.adept index d0531816..cb7bcd5d 100644 --- a/tests/pointers/main.adept +++ b/tests/pointers/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/preprocessor_toggle/main.adept b/tests/preprocessor_toggle/main.adept index eb6e0dd8..57f32f4b 100644 --- a/tests/preprocessor_toggle/main.adept +++ b/tests/preprocessor_toggle/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + func main { printf(c"Hello world, from protected printf!\n") } diff --git a/tests/return/main.adept b/tests/return/main.adept index 7c231ef7..e73eab5c 100644 --- a/tests/return/main.adept +++ b/tests/return/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + func main int { return 123 } diff --git a/tests/return_message/main.adept b/tests/return_message/main.adept index 6093fc56..6cbfa2a2 100644 --- a/tests/return_message/main.adept +++ b/tests/return_message/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/signed_unsigned_promotion/main.adept b/tests/signed_unsigned_promotion/main.adept index ac71eb4b..2a5dd333 100644 --- a/tests/signed_unsigned_promotion/main.adept +++ b/tests/signed_unsigned_promotion/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/structure_definitions/main.adept b/tests/structure_definitions/main.adept index 6fc9093f..e45ddf04 100644 --- a/tests/structure_definitions/main.adept +++ b/tests/structure_definitions/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/structure_literals/main.adept b/tests/structure_literals/main.adept index bb382104..4893c3b5 100644 --- a/tests/structure_literals/main.adept +++ b/tests/structure_literals/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/structure_literals_abbr/main.adept b/tests/structure_literals_abbr/main.adept index 4c2c5610..a7837a55 100644 --- a/tests/structure_literals_abbr/main.adept +++ b/tests/structure_literals_abbr/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/type_aliases/main.adept b/tests/type_aliases/main.adept index 6990002e..51ca7fde 100644 --- a/tests/type_aliases/main.adept +++ b/tests/type_aliases/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/ufcs/main.adept b/tests/ufcs/main.adept index 3cff0a0f..9cbc488f 100644 --- a/tests/ufcs/main.adept +++ b/tests/ufcs/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/unary_operators/main.adept b/tests/unary_operators/main.adept index d1c419f7..d33a1c92 100644 --- a/tests/unary_operators/main.adept +++ b/tests/unary_operators/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/variables/main.adept b/tests/variables/main.adept index 0d024973..2995b9bb 100644 --- a/tests/variables/main.adept +++ b/tests/variables/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/variables_override/main.adept b/tests/variables_override/main.adept index ff606f38..3fd9a3f3 100644 --- a/tests/variables_override/main.adept +++ b/tests/variables_override/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/variables_typed/main.adept b/tests/variables_typed/main.adept index 14a416bd..cf7340c1 100644 --- a/tests/variables_typed/main.adept +++ b/tests/variables_typed/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int diff --git a/tests/while/main.adept b/tests/while/main.adept index 806c06b1..bf01c223 100644 --- a/tests/while/main.adept +++ b/tests/while/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] { // NOTE: This is only macOS (also type is incorrect but it's a pointer so doesn't matter) diff --git a/tests/zeroed/main.adept b/tests/zeroed/main.adept index 3fbca663..e8c9f740 100644 --- a/tests/zeroed/main.adept +++ b/tests/zeroed/main.adept @@ -1,4 +1,6 @@ +pragma => adept("3.0") + #[foreign] func printf(format ptr, ...) int