include in ayin-game
This commit is contained in:
parent
b512d4ac97
commit
f32b5eb4d2
5 changed files with 59 additions and 60 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
include "./stdlib.ayin"
|
||||
include "../stdlib/stdlib.ayin"
|
||||
|
||||
let main = fn() {
|
||||
map(fn (x) { x + 1 }, [1,2,3])
|
||||
|
|
|
|||
38
src/game.rs
38
src/game.rs
|
|
@ -27,8 +27,6 @@ async fn main() {
|
|||
let (_hot, reader) = hotwatch();
|
||||
|
||||
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;
|
||||
|
|
@ -54,19 +52,26 @@ async fn main() {
|
|||
next_frame().await;
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn read_file() -> std::io::Result<String> {
|
||||
fn read_file() -> Result<ayin::ast::Program, String> {
|
||||
let args: Vec<String> = 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<String> {
|
||||
Ok(include_str!("../games/there-she-is.ayin").to_string())
|
||||
fn read_file() -> Result<ast::Program, String> {
|
||||
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<RwLock<Option<ayin::ast::Program>>>) {
|
|||
let reader = writer.clone();
|
||||
let args: Vec<String> = 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!");
|
||||
|
||||
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 std::fs::read_to_string(file.clone()) {
|
||||
Err(err) => println!("Error: {err:#?}"),
|
||||
Ok(txt) => match ayin::parser::parse_file(txt) {
|
||||
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!");
|
||||
}
|
||||
(hotwatch, reader)
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
|
|
|
|||
|
|
@ -16,13 +16,12 @@ pub fn parse_program(file: &String) -> Result<crate::ast::Program, Error> {
|
|||
let mut programs: HashMap<crate::ast::File, crate::ast::Program> = HashMap::new();
|
||||
parse_files(&file, &mut programs)?;
|
||||
let mut defs: Vec<crate::ast::Definition> = vec![];
|
||||
for (_, program) in programs.iter_mut() {
|
||||
let mut includes: Vec<crate::ast::File> = 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(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue