diff --git a/games/basic.ayin b/games/basic.ayin index f8de173..1a9e57f 100644 --- a/games/basic.ayin +++ b/games/basic.ayin @@ -1,3 +1,5 @@ +include "../stdlib/stdlib.ayin" + let migrate = fn(state) { state } @@ -23,19 +25,3 @@ let update = fn(state, input) { let draw = fn(state) { frame_clear(state.color.r, state.color.g, state.color.b) } - -let min = fn(a,b) { - if a < b { - a - } else { - b - } -} - -let max = fn(a,b) { - if a < b { - b - } else { - a - } -} diff --git a/programs/map.ayin b/programs/map.ayin index 9132308..1739746 100644 --- a/programs/map.ayin +++ b/programs/map.ayin @@ -1,4 +1,4 @@ -include "./stdlib.ayin" +include "../stdlib/stdlib.ayin" let main = fn() { map(fn (x) { x + 1 }, [1,2,3]) diff --git a/src/game.rs b/src/game.rs index 087515a..b660476 100644 --- a/src/game.rs +++ b/src/game.rs @@ -28,45 +28,50 @@ async fn main() { match read_file() { Err(err) => println!("Error: {err:#?}"), - Ok(txt) => match ayin::parser::parse_file(txt) { - Err(err) => println!("Error: {err:#?}"), - Ok(program) => { - let mut state = setup(program).await; + Ok(program) => { + let mut state = setup(program).await; - loop { - let new_program = { - let prog = { (*(reader.read().unwrap())).clone() }; - if let Some(new_program) = prog { - let mut w = reader.write().unwrap(); - *w = None; - Some(new_program) - } else { - None - } - }; - if let Some(new_program) = new_program { - state = migrate(state, new_program); + loop { + let new_program = { + let prog = { (*(reader.read().unwrap())).clone() }; + if let Some(new_program) = prog { + let mut w = reader.write().unwrap(); + *w = None; + Some(new_program) + } else { + None } - - let events = fetch_events(&mut state); - update(&mut state, events); - draw(&mut state); - next_frame().await; + }; + if let Some(new_program) = new_program { + state = migrate(state, new_program); } + + let events = fetch_events(&mut state); + update(&mut state, events); + draw(&mut state); + next_frame().await; } - }, + } } } #[cfg(not(target_arch = "wasm32"))] -fn read_file() -> std::io::Result { +fn read_file() -> Result { let args: Vec = std::env::args().collect(); let file = args[1].clone(); - std::fs::read_to_string(file) + + match ayin::parser::parse_program(&file) { + Err(err) => Err(format!("Error: {err:#?}")), + Ok(program) => Ok(program), + } } #[cfg(target_arch = "wasm32")] -fn read_file() -> std::io::Result { - Ok(include_str!("../games/there-she-is.ayin").to_string()) +fn read_file() -> Result { + let code = Ok(include_str!("../games/there-she-is.ayin").to_string()); + match ayin::parser::parse_file(code) { + Err(err) => Err(format!("Error: {err:#?}")), + Ok(program) => Ok(program), + } } #[cfg(not(target_arch = "wasm32"))] @@ -75,24 +80,33 @@ fn hotwatch() -> (hotwatch::Hotwatch, Arc>>) { let reader = writer.clone(); let args: Vec = std::env::args().collect(); let file = args[1].clone(); + + let files = match read_file() { + Ok(program) => program.includes.into_iter().map(|f| f.0).collect(), + Err(err) => { + println!("Error: {err:#?}"); + vec![file.clone()] + } + }; let mut hotwatch = hotwatch::Hotwatch::new_with_custom_delay(Duration::from_millis(100)) .expect("hotwatch failed to initialize!"); - hotwatch - .watch(file.clone(), move |event: hotwatch::Event| { - if let hotwatch::EventKind::Modify(_) = event.kind { - match std::fs::read_to_string(file.clone()) { - Err(err) => println!("Error: {err:#?}"), - Ok(txt) => match ayin::parser::parse_file(txt) { + + for file in files { + let my_writer = writer.clone(); + hotwatch + .watch(file.clone(), move |event: hotwatch::Event| { + if let hotwatch::EventKind::Modify(_) = event.kind { + match read_file() { Err(err) => println!("Error: {err:#?}"), Ok(program) => { - let mut w = writer.write().unwrap(); + let mut w = my_writer.write().unwrap(); *w = Some(program); } - }, + } } - } - }) - .expect("failed to watch file!"); + }) + .expect("failed to watch file!"); + } (hotwatch, reader) } #[cfg(target_arch = "wasm32")] diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 17cb85e..f115bcc 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -16,13 +16,12 @@ pub fn parse_program(file: &String) -> Result { let mut programs: HashMap = HashMap::new(); parse_files(&file, &mut programs)?; let mut defs: Vec = vec![]; - for (_, program) in programs.iter_mut() { + let mut includes: Vec = vec![]; + for (file, program) in programs.iter_mut() { defs.append(&mut program.defs); + includes.push(file.clone()); } - Ok(crate::ast::Program { - defs, - includes: vec![], - }) + Ok(crate::ast::Program { defs, includes }) } fn parse_files( diff --git a/programs/stdlib.ayin b/stdlib/stdlib.ayin similarity index 100% rename from programs/stdlib.ayin rename to stdlib/stdlib.ayin