sunflower/src/main.rs

142 lines
4.3 KiB
Rust

use std::fs::read_to_string;
use env::Env;
use expression::{BinaryOperation, Expression};
use function::{Function, FunctionMap, NativeFunction};
use pest::Parser;
use statement::Statement;
use value::Value;
mod env;
mod expression;
mod function;
mod macros;
mod statement;
mod value;
// mod lexer;
// mod parser;
mod pest_parser;
fn run(functions: FunctionMap, statements: &[Statement]) {
let env = Env::new();
for statement in statements {
statement.eval(&env, &functions);
}
}
fn main() {
// let statements = vec![
// Statement::Assignment("a".to_string(), Expression::from(1)),
// Statement::Assignment("b".to_string(), Expression::from(2)),
// Statement::Assignment("c".to_string(), Expression::from(3)),
// Statement::Print(Expression::Binary(
// Box::new(Expression::Variable("a".to_string())),
// BinaryOperation::Add,
// Box::new(Expression::Variable("b".to_string())),
// )),
// Statement::Print(Expression::Binary(
// Box::new(Expression::Variable("a".to_string())),
// BinaryOperation::Add,
// Box::new(Expression::Variable("c".to_string())),
// )),
// Statement::Print(Expression::Binary(
// Box::new(Expression::Variable("b".to_string())),
// BinaryOperation::Add,
// Box::new(Expression::Variable("c".to_string())),
// )),
// ];
// run(FunctionMap::new(), &statements);
// let while_test = vec![
// Statement::Print("while test".to_string().into()),
// Statement::Assignment("a".to_string(), Expression::from(1)),
// Statement::While(
// Expression::Binary(
// Box::new(Expression::Variable("a".to_string())),
// BinaryOperation::LessThan,
// Box::new(10.into()),
// ),
// vec![
// Statement::Assignment(
// "a".to_string(),
// Expression::Binary(
// Box::new(Expression::Variable("a".to_string())),
// BinaryOperation::Add,
// Box::new(1.into()),
// ),
// ),
// Statement::Print(Expression::Variable("a".to_string())),
// ],
// ),
// ];
// run(FunctionMap::new(), &while_test);
// {
// let env = Env::new();
// let res = (|arg: &i32| arg + 1).native_execute(&env, &[Value::Int(1)]);
// println!("{:?}", res);
// }
// {
// let z = || 100;
// let env = Env::new();
// let res = z.native_execute(&env, &[]);
// println!("{:?}", res);
// }
// {
// let z = |a: &Value| a.clone();
// let env = Env::new();
// let res = z.into_function().execute(&env, &FunctionMap::new(), &[]);
// println!("{:?}", res);
// }
// {
// let z = |a: &String| a.clone();
// let env = Env::new();
// let res = z.native_execute(&env, &[100.into()]);
// println!("{:?}", res);
// }
// {
// let v: Vec<Box<dyn Function>> = vec![
// Box::new((|| 100).into_function()),
// Box::new((|a: &i32| a + 1).into_function()),
// ];
// for f in v {
// let r = f.execute(&Env::new(), &FunctionMap::new(), &[100.into()]);
// println!("{:?}", r);
// }
// }
// let mut m: FunctionMap = Default::default();
// m.insert_native("print", |a: &Value| {
// println!("{}", a);
// });
// m.insert_native("waluigi", || "Waluigi");
// m.insert_interpreter("call_print", vec!["what".to_string()], vec![
// Statement::Expression(Expression::Call("print".to_string(), vec![Expression::Variable("what".to_string())])),
// ]);
// run(
// m,
// &vec![Statement::Expression(Expression::Call(
// "call_print".to_string(),
// vec![Expression::Call("waluigi".to_string(), vec![])],
// ))],
// );
let f = read_to_string("./test.foo").unwrap();
let mut m: FunctionMap = Default::default();
m.insert_native("print", |a: &Value| {
println!("{}", a);
});
m.insert_native("waluigi", || "Waluigi");
let res = pest_parser::parse(f);
let env = &Env::new();
res.eval(env, &m);
}