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) {
|
let migrate = fn(state) {
|
||||||
state
|
state
|
||||||
}
|
}
|
||||||
|
|
@ -23,19 +25,3 @@ let update = fn(state, input) {
|
||||||
let draw = fn(state) {
|
let draw = fn(state) {
|
||||||
frame_clear(state.color.r, state.color.g, state.color.b)
|
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() {
|
let main = fn() {
|
||||||
map(fn (x) { x + 1 }, [1,2,3])
|
map(fn (x) { x + 1 }, [1,2,3])
|
||||||
|
|
|
||||||
90
src/game.rs
90
src/game.rs
|
|
@ -28,45 +28,50 @@ async fn main() {
|
||||||
|
|
||||||
match read_file() {
|
match read_file() {
|
||||||
Err(err) => println!("Error: {err:#?}"),
|
Err(err) => println!("Error: {err:#?}"),
|
||||||
Ok(txt) => match ayin::parser::parse_file(txt) {
|
Ok(program) => {
|
||||||
Err(err) => println!("Error: {err:#?}"),
|
let mut state = setup(program).await;
|
||||||
Ok(program) => {
|
|
||||||
let mut state = setup(program).await;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let new_program = {
|
let new_program = {
|
||||||
let prog = { (*(reader.read().unwrap())).clone() };
|
let prog = { (*(reader.read().unwrap())).clone() };
|
||||||
if let Some(new_program) = prog {
|
if let Some(new_program) = prog {
|
||||||
let mut w = reader.write().unwrap();
|
let mut w = reader.write().unwrap();
|
||||||
*w = None;
|
*w = None;
|
||||||
Some(new_program)
|
Some(new_program)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
|
||||||
};
|
|
||||||
if let Some(new_program) = new_program {
|
|
||||||
state = migrate(state, new_program);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
let events = fetch_events(&mut state);
|
if let Some(new_program) = new_program {
|
||||||
update(&mut state, events);
|
state = migrate(state, new_program);
|
||||||
draw(&mut state);
|
|
||||||
next_frame().await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let events = fetch_events(&mut state);
|
||||||
|
update(&mut state, events);
|
||||||
|
draw(&mut state);
|
||||||
|
next_frame().await;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[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 args: Vec<String> = std::env::args().collect();
|
||||||
let file = args[1].clone();
|
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")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
fn read_file() -> std::io::Result<String> {
|
fn read_file() -> Result<ast::Program, String> {
|
||||||
Ok(include_str!("../games/there-she-is.ayin").to_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"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
|
@ -75,24 +80,33 @@ fn hotwatch() -> (hotwatch::Hotwatch, Arc<RwLock<Option<ayin::ast::Program>>>) {
|
||||||
let reader = writer.clone();
|
let reader = writer.clone();
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
let file = args[1].clone();
|
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))
|
let mut hotwatch = hotwatch::Hotwatch::new_with_custom_delay(Duration::from_millis(100))
|
||||||
.expect("hotwatch failed to initialize!");
|
.expect("hotwatch failed to initialize!");
|
||||||
hotwatch
|
|
||||||
.watch(file.clone(), move |event: hotwatch::Event| {
|
for file in files {
|
||||||
if let hotwatch::EventKind::Modify(_) = event.kind {
|
let my_writer = writer.clone();
|
||||||
match std::fs::read_to_string(file.clone()) {
|
hotwatch
|
||||||
Err(err) => println!("Error: {err:#?}"),
|
.watch(file.clone(), move |event: hotwatch::Event| {
|
||||||
Ok(txt) => match ayin::parser::parse_file(txt) {
|
if let hotwatch::EventKind::Modify(_) = event.kind {
|
||||||
|
match read_file() {
|
||||||
Err(err) => println!("Error: {err:#?}"),
|
Err(err) => println!("Error: {err:#?}"),
|
||||||
Ok(program) => {
|
Ok(program) => {
|
||||||
let mut w = writer.write().unwrap();
|
let mut w = my_writer.write().unwrap();
|
||||||
*w = Some(program);
|
*w = Some(program);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
})
|
.expect("failed to watch file!");
|
||||||
.expect("failed to watch file!");
|
}
|
||||||
(hotwatch, reader)
|
(hotwatch, reader)
|
||||||
}
|
}
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[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();
|
let mut programs: HashMap<crate::ast::File, crate::ast::Program> = HashMap::new();
|
||||||
parse_files(&file, &mut programs)?;
|
parse_files(&file, &mut programs)?;
|
||||||
let mut defs: Vec<crate::ast::Definition> = vec![];
|
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);
|
defs.append(&mut program.defs);
|
||||||
|
includes.push(file.clone());
|
||||||
}
|
}
|
||||||
Ok(crate::ast::Program {
|
Ok(crate::ast::Program { defs, includes })
|
||||||
defs,
|
|
||||||
includes: vec![],
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_files(
|
fn parse_files(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue