sunflower/src/main.rs

85 lines
1.7 KiB
Rust
Raw Permalink Normal View History

use std::{
any::Any,
fs::read_to_string,
};
2022-07-03 15:17:37 +02:00
2022-07-02 23:42:15 +02:00
use env::Env;
2022-07-03 16:01:45 +02:00
use function::FunctionMap;
2022-07-02 23:42:15 +02:00
use statement::Statement;
use value::{custom::CustomValue, Value};
2022-07-02 23:42:15 +02:00
mod env;
mod expression;
mod function;
mod macros;
mod statement;
mod value;
2022-07-03 15:17:37 +02:00
// mod lexer;
// mod parser;
mod pest_parser;
2022-07-02 23:42:15 +02:00
2022-07-04 00:11:55 +02:00
fn run(functions: &FunctionMap, statement: &Statement) {
2022-07-02 23:42:15 +02:00
let env = Env::new();
2022-07-04 00:11:55 +02:00
statement.eval(&env, functions);
2022-07-02 23:42:15 +02:00
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct Foo {
x: i32,
}
impl Foo {
fn edit(&self) -> Self {
let mut f = self.clone();
f.x += 1;
f
}
}
impl CustomValue for Foo {
fn as_any(&self) -> &dyn Any {
self
}
fn format(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Foo({})", self.x)
}
fn eq(&self, other: &Value) -> bool {
if let Some(v) = other.as_t() {
self == v
} else {
false
}
}
2022-07-04 21:49:08 +02:00
fn functions(&self) -> Option<FunctionMap> {
let mut map = FunctionMap::new();
map.insert_native("edit_foo", Foo::edit);
Some(map)
}
fn add(&self, other: &Value) -> Value {
if let Some(v) = other.as_t::<Foo>() {
Foo { x: self.x + v.x }.into()
} else {
Value::default()
}
}
}
2022-07-02 23:42:15 +02:00
fn main() {
2022-07-03 15:17:37 +02:00
let f = read_to_string("./test.foo").unwrap();
2022-07-04 00:11:55 +02:00
let (res, funcs) = pest_parser::parse(&f);
2022-07-03 16:01:45 +02:00
let mut m = FunctionMap::new();
m.insert_holder(funcs);
2022-07-02 23:42:15 +02:00
m.insert_native("print", |a: &Value| {
println!("{}", a);
});
m.insert_native("waluigi", || "Waluigi");
m.insert_native("native_test", || Foo { x: 41 });
m.insert_native("other_foo", || Foo { x: 1 });
2022-07-04 00:11:55 +02:00
run(&m, &res);
2022-07-02 23:42:15 +02:00
}