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:
+37
-1
@@ -6,8 +6,9 @@ mod tests {
|
||||
use crate::ast::compiler::binder::Binder;
|
||||
use crate::ast::vm::VM;
|
||||
use crate::ast::types::Value;
|
||||
|
||||
use crate::ast::nodes::UntypedKind;
|
||||
use crate::ast::environment::Environment;
|
||||
use std::fs;
|
||||
|
||||
#[test]
|
||||
fn test_parse_integer_constant() {
|
||||
@@ -89,4 +90,39 @@ mod tests {
|
||||
Err(e) => panic!("VM Error: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
let entries = fs::read_dir("examples").expect("Could not read examples directory");
|
||||
for entry in entries {
|
||||
let entry = entry.expect("Invalid entry");
|
||||
let path = entry.path();
|
||||
if path.extension().map_or(false, |ext| ext == "myc") {
|
||||
let content = fs::read_to_string(&path).expect("Could not read file");
|
||||
|
||||
// Skip benchmarks during normal functional tests
|
||||
if content.contains(";; Benchmark") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find expected output tag: ;; Output: <value>
|
||||
let expected_output = content.lines()
|
||||
.find(|line| line.starts_with(";; Output:"))
|
||||
.map(|line| line.replace(";; Output:", "").trim().to_string());
|
||||
|
||||
if let Some(expected) = expected_output {
|
||||
let env = Environment::new();
|
||||
let result = env.run_script(&content);
|
||||
|
||||
match result {
|
||||
Ok(val) => {
|
||||
let val_str = format!("{}", val);
|
||||
assert_eq!(val_str, expected, "Example {:?} failed: expected {}, got {}", path, expected, val_str);
|
||||
}
|
||||
Err(e) => panic!("Example {:?} failed with error: {}", path, e),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+49
-42
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user