-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: logging, aborting, Rhai main loop
- Loading branch information
1 parent
f9fd0ed
commit 2fb0fe8
Showing
14 changed files
with
325 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
Cargo.lock | ||
*.html | ||
.#* | ||
log.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
fn block() { | ||
loop { | ||
let keyprog = synless_internals::block_on_key(); | ||
if keyprog.close_menu { | ||
s::close_menu(); | ||
return call(keyprog.prog); | ||
} | ||
call(keyprog.prog); | ||
} | ||
} | ||
|
||
fn abort() { | ||
synless_internals::prepare_to_abort(); | ||
exit(); | ||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
loop { | ||
try { | ||
s::block(); // ignoring return value | ||
} catch (err) { | ||
if type_of(err) == "SynlessError" { | ||
let category = err.category; | ||
let msg = err.message; | ||
s::log_error(`${category}: ${msg}`); | ||
if err.category == "Abort" { | ||
s::abort(); | ||
} | ||
} else { | ||
if type_of(err) == "map" && "message" in err { | ||
let msg = err.message; | ||
s::log_error(`Rhai: ${msg}`); | ||
} else { | ||
s::log_error(`Thrown: ${err}`); | ||
}; | ||
} | ||
s::close_menu(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,83 @@ | ||
use synless::{Engine, Settings}; | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
use synless::{log, ColorTheme, Log, Runtime, Settings, SynlessBug, Terminal}; | ||
|
||
fn main() { | ||
Engine::new(Settings::default()); | ||
// TODO: Make this work if you start in a different cwd | ||
const BASE_MODULE_PATH: &str = "scripts/base_module.rhai"; | ||
const INTERNALS_MODULE_PATH: &str = "scripts/internals_module.rhai"; | ||
const INIT_PATH: &str = "scripts/init.rhai"; | ||
const MAIN_PATH: &str = "scripts/main.rhai"; | ||
|
||
fn make_engine() -> rhai::Engine { | ||
let mut engine = rhai::Engine::new(); | ||
engine.set_fail_on_invalid_map_property(true); | ||
engine.on_print(|msg| log!(Info, "{msg}")); | ||
engine.on_debug(|msg, src, pos| { | ||
let src = src.unwrap_or("unknown"); | ||
log!(Debug, "{src} @ {pos:?} > {msg}"); | ||
}); | ||
|
||
engine.build_type::<synless::KeyProg>(); | ||
engine.build_type::<synless::SynlessError>(); | ||
|
||
println!("Signatures:"); | ||
engine | ||
.gen_fn_signatures(false) | ||
.into_iter() | ||
.for_each(|func| println!(" {func}")); | ||
println!(); | ||
|
||
engine | ||
} | ||
|
||
fn make_runtime() -> Rc<RefCell<Runtime<Terminal>>> { | ||
let settings = Settings::default(); | ||
let terminal = | ||
Terminal::new(ColorTheme::default_dark()).bug_msg("Failed to construct terminal frontend"); | ||
let runtime = Runtime::new(settings, terminal); | ||
Rc::new(RefCell::new(runtime)) | ||
} | ||
|
||
fn run() -> Result<(), Box<rhai::EvalAltResult>> { | ||
let mut engine = make_engine(); | ||
|
||
panic!("not yet implemented"); | ||
// Load internals_module.rhai | ||
let mut internals_mod = { | ||
let internals_ast = engine.compile_file(INTERNALS_MODULE_PATH.into())?; | ||
rhai::Module::eval_ast_as_new(rhai::Scope::new(), &internals_ast, &engine)? | ||
}; | ||
|
||
// Load base_module.rhai | ||
let mut base_mod = { | ||
let base_ast = engine.compile_file(BASE_MODULE_PATH.into())?; | ||
rhai::Module::eval_ast_as_new(rhai::Scope::new(), &base_ast, &engine)? | ||
}; | ||
|
||
// Register runtime methods into internals_module and base_module | ||
let runtime = make_runtime(); | ||
Runtime::register_internal_methods(runtime.clone(), &mut internals_mod); | ||
engine.register_static_module("synless_internals", internals_mod.into()); | ||
Runtime::register_external_methods(runtime, &mut base_mod); | ||
engine.register_static_module("s", base_mod.into()); | ||
|
||
// Can't set this before modules are registered, as they reference each other | ||
engine.set_strict_variables(true); | ||
|
||
// Load init.rhai | ||
let init_ast = engine.compile_file(INIT_PATH.into())?; | ||
engine.run_ast(&init_ast)?; | ||
|
||
// Load main.rhai | ||
let main_ast = engine.compile_file(MAIN_PATH.into())?; | ||
engine.run_ast(&main_ast)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
log!(Info, "Synless is starting"); | ||
if let Err(err) = run() { | ||
log!(Error, "{}", err); | ||
} | ||
println!("{}", Log::to_string()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.