Add chrono dependency and example files

This commit introduces the `chrono` dependency to the project, enabling
the use of time-related functionalities. Additionally, it adds several
example files (`closure.myc`, `fib.myc`, `hof.myc`) to showcase the
language's features like closures, recursion, and higher-order
functions.

The `Cargo.lock` and `Cargo.toml` files have been updated to reflect the
new dependency. The `integration_test.rs` file now includes a test to
run all `.myc` files found in the `examples` directory, ensuring their
correctness. The `main.rs` file has also been updated to load and
display these examples in the UI.
This commit is contained in:
Michael Schimmel
2026-02-17 14:21:31 +01:00
parent 97542b5ca4
commit b1ca16149d
7 changed files with 154 additions and 43 deletions
+49 -42
View File
@@ -13,15 +13,37 @@ fn main() -> eframe::Result {
)
}
struct Example {
name: String,
content: String,
is_benchmark: bool,
}
struct CompilerApp {
source_code: String,
output_log: String,
env: Environment,
is_first_frame: bool,
examples: Vec<Example>,
}
impl Default for CompilerApp {
fn default() -> Self {
let examples = std::fs::read_dir("examples")
.map(|entries| {
entries
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().map_or(false, |ext| ext == "myc"))
.filter_map(|e| {
let name = e.file_name().into_string().ok()?;
let content = std::fs::read_to_string(e.path()).ok()?;
let is_benchmark = content.contains(";; Benchmark");
Some(Example { name, content, is_benchmark })
})
.collect()
})
.unwrap_or_default();
Self {
source_code: String::from(r#"
(do
@@ -30,33 +52,29 @@ impl Default for CompilerApp {
n
(+ (fib (- n 1)) (fib (- n 2))))))
(def result (fib 10))
(def data {
:name "Fibonacci"
:input 10
:output result
:sequence [0 1 1 2 3 5 8 13 21 34 55]
})
data
(fib 10)
)
"#),
output_log: String::from("Ready to compile..."),
env: Environment::new(),
is_first_frame: true,
examples,
}
}
}
impl CompilerApp {
fn compile(&mut self) {
let start = std::time::Instant::now();
match self.env.run_script(&self.source_code) {
Ok(result) => {
let duration = start.elapsed();
let now = chrono::Local::now().format("%H:%M:%S").to_string();
self.output_log = format!(
"Execution Successful.\nResult: {}\n\nFinished at {:?}",
"Execution Successful.\nResult: {}\n\nDuration: {:?}\nFinished at: {}",
result,
std::time::SystemTime::now(),
duration,
now,
);
}
Err(e) => {
@@ -68,6 +86,25 @@ impl CompilerApp {
impl eframe::App for CompilerApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// Left Side Panel: Examples
egui::SidePanel::left("examples_panel")
.resizable(true)
.default_width(150.0)
.show(ctx, |ui| {
ui.heading("Examples");
ui.add_space(5.0);
for example in &self.examples {
let label = if example.is_benchmark {
format!("{}", example.name)
} else {
example.name.clone()
};
if ui.button(label).clicked() {
self.source_code = example.content.clone();
}
}
});
// Bottom Panel: Output Log & Controls (Resizable)
egui::TopBottomPanel::bottom("bottom_panel")
.resizable(true)
@@ -137,33 +174,3 @@ impl eframe::App for CompilerApp {
});
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::types::Value;
#[test]
fn test_basic_eval() {
let env = Environment::new();
let result = env.run_script("(+ 1 2)").unwrap();
if let Value::Int(i) = result {
assert_eq!(i, 3);
} else if let Value::Float(f) = result {
assert_eq!(f, 3.0);
} else {
panic!("Expected number result");
}
}
#[test]
fn test_type_error() {
let env = Environment::new();
// This should fail because 'if' condition must be bool (and currently '+' returns Any/Float/Int)
// Wait, '+' returns Any currently in my registration.
// Let's try something that definitely fails type check.
let result = env.run_script("(if 1 2 3)");
assert!(result.is_err());
assert!(result.unwrap_err().contains("Condition must be boolean"));
}
}