2018-02-23 19:06:37 -08:00
|
|
|
use schala_lang::parsing::{AST, Statement, Declaration, Expression, Variant, ExpressionType};
|
|
|
|
use schala_lang::builtin::{BinOp, PrefixOp};
|
2017-10-13 03:05:18 -07:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::rc::Rc;
|
2017-09-30 23:30:02 -07:00
|
|
|
|
2018-02-24 13:56:04 -08:00
|
|
|
pub struct State<'a> {
|
|
|
|
parent_frame: Option<&'a State<'a>>,
|
2017-10-13 03:05:18 -07:00
|
|
|
values: HashMap<Rc<String>, ValueEntry>,
|
|
|
|
}
|
|
|
|
|
2018-02-26 19:55:27 -08:00
|
|
|
#[derive(Debug)]
|
2017-10-13 03:05:18 -07:00
|
|
|
enum ValueEntry {
|
|
|
|
Binding {
|
|
|
|
val: FullyEvaluatedExpr,
|
|
|
|
},
|
|
|
|
Function {
|
2018-02-26 19:55:27 -08:00
|
|
|
param_names: Vec<Rc<String>>,
|
2017-10-13 03:05:18 -07:00
|
|
|
body: Vec<Statement>,
|
|
|
|
}
|
2017-09-30 23:30:02 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 19:09:55 -07:00
|
|
|
type EvalResult<T> = Result<T, String>;
|
|
|
|
|
2017-10-13 03:05:18 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2017-10-01 19:09:55 -07:00
|
|
|
enum FullyEvaluatedExpr {
|
|
|
|
UnsignedInt(u64),
|
|
|
|
SignedInt(i64),
|
|
|
|
Float(f64),
|
|
|
|
Str(String),
|
|
|
|
Bool(bool),
|
2017-10-23 01:54:02 -07:00
|
|
|
FuncLit(Rc<String>),
|
2017-10-13 03:05:18 -07:00
|
|
|
Custom {
|
|
|
|
string_rep: Rc<String>,
|
2017-10-23 00:22:25 -07:00
|
|
|
},
|
|
|
|
Tuple(Vec<FullyEvaluatedExpr>),
|
2017-10-01 19:09:55 -07:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:56:04 -08:00
|
|
|
impl FullyEvaluatedExpr {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
use self::FullyEvaluatedExpr::*;
|
|
|
|
match self {
|
|
|
|
&UnsignedInt(ref n) => format!("{}", n),
|
|
|
|
&SignedInt(ref n) => format!("{}", n),
|
|
|
|
&Float(ref f) => format!("{}", f),
|
|
|
|
&Str(ref s) => format!("\"{}\"", s),
|
|
|
|
&Bool(ref b) => format!("{}", b),
|
|
|
|
&Custom { ref string_rep } => format!("{}", string_rep),
|
|
|
|
&Tuple(ref _items) => format!("(tuple to be defined later)"),
|
|
|
|
&FuncLit(ref name) => format!("<function {}>", name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> State<'a> {
|
|
|
|
pub fn new() -> State<'a> {
|
|
|
|
State { parent_frame: None, values: HashMap::new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_parent(parent: &'a State<'a>) -> State<'a> {
|
|
|
|
State { parent_frame: Some(parent), values: HashMap::new() }
|
2017-09-30 23:30:02 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 19:29:05 -07:00
|
|
|
pub fn evaluate(&mut self, ast: AST) -> Vec<String> {
|
|
|
|
let mut acc = vec![];
|
2017-10-01 12:55:28 -07:00
|
|
|
for statement in ast.0 {
|
2017-10-01 19:09:55 -07:00
|
|
|
match self.eval_statement(statement) {
|
|
|
|
Ok(output) => {
|
2018-02-24 13:56:04 -08:00
|
|
|
if let Some(fully_evaluated) = output {
|
|
|
|
acc.push(fully_evaluated.to_string());
|
2017-10-01 19:09:55 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(error) => {
|
2017-10-13 03:05:18 -07:00
|
|
|
acc.push(format!("Eval error: {}", error));
|
2017-10-01 19:09:55 -07:00
|
|
|
return acc;
|
|
|
|
},
|
2017-10-01 12:55:28 -07:00
|
|
|
}
|
|
|
|
}
|
2017-10-01 19:29:05 -07:00
|
|
|
acc
|
2017-10-01 12:55:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 13:56:04 -08:00
|
|
|
impl<'a> State<'a> {
|
|
|
|
fn eval_statement(&mut self, statement: Statement) -> EvalResult<Option<FullyEvaluatedExpr>> {
|
|
|
|
Ok(match statement {
|
|
|
|
Statement::ExpressionStatement(expr) => Some(self.eval_expr(expr)?),
|
|
|
|
Statement::Declaration(decl) => { self.eval_decl(decl)?; None }
|
|
|
|
})
|
2017-10-01 12:55:28 -07:00
|
|
|
}
|
|
|
|
|
2017-10-13 03:05:18 -07:00
|
|
|
fn eval_decl(&mut self, decl: Declaration) -> EvalResult<()> {
|
|
|
|
use self::Declaration::*;
|
|
|
|
use self::Variant::*;
|
|
|
|
|
|
|
|
match decl {
|
|
|
|
FuncDecl(signature, statements) => {
|
|
|
|
let name = signature.name;
|
2018-02-26 19:55:27 -08:00
|
|
|
let param_names: Vec<Rc<String>> = signature.params.iter().map(|fp| fp.0.clone()).collect();
|
|
|
|
self.values.insert(name, ValueEntry::Function { body: statements.clone(), param_names });
|
2017-10-13 03:05:18 -07:00
|
|
|
},
|
|
|
|
TypeDecl(_name, body) => {
|
|
|
|
for variant in body.0.iter() {
|
|
|
|
match variant {
|
|
|
|
&UnitStruct(ref name) => self.values.insert(name.clone(),
|
|
|
|
ValueEntry::Binding { val: FullyEvaluatedExpr::Custom { string_rep: name.clone() } }),
|
2017-10-23 01:54:02 -07:00
|
|
|
&TupleStruct(ref _name, ref _args) => unimplemented!(),
|
|
|
|
&Record(ref _name, ref _fields) => unimplemented!(),
|
2017-10-13 03:05:18 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2018-02-26 18:18:42 -08:00
|
|
|
Binding { name, constant, expr } => {
|
|
|
|
let val = self.eval_expr(expr)?;
|
|
|
|
self.values.insert(name.clone(), ValueEntry::Binding { val });
|
|
|
|
},
|
2017-10-13 03:05:18 -07:00
|
|
|
_ => return Err(format!("Declaration evaluation not yet implemented"))
|
|
|
|
}
|
|
|
|
Ok(())
|
2017-09-30 23:30:02 -07:00
|
|
|
}
|
2017-10-01 00:48:08 -07:00
|
|
|
|
2017-10-01 19:09:55 -07:00
|
|
|
fn eval_expr(&mut self, expr: Expression) -> EvalResult<FullyEvaluatedExpr> {
|
2017-10-01 12:55:28 -07:00
|
|
|
use self::ExpressionType::*;
|
2017-10-01 19:09:55 -07:00
|
|
|
use self::FullyEvaluatedExpr::*;
|
2017-10-01 12:55:28 -07:00
|
|
|
|
|
|
|
let expr_type = expr.0;
|
2017-10-01 19:09:55 -07:00
|
|
|
match expr_type {
|
|
|
|
IntLiteral(n) => Ok(UnsignedInt(n)),
|
|
|
|
FloatLiteral(f) => Ok(Float(f)),
|
|
|
|
StringLiteral(s) => Ok(Str(s.to_string())),
|
|
|
|
BoolLiteral(b) => Ok(Bool(b)),
|
2017-10-02 20:11:27 -07:00
|
|
|
PrefixExp(op, expr) => self.eval_prefix_exp(op, expr),
|
|
|
|
BinExp(op, lhs, rhs) => self.eval_binexp(op, lhs, rhs),
|
2018-02-26 18:12:37 -08:00
|
|
|
Value(name) => self.eval_value(name),
|
2017-10-23 00:22:25 -07:00
|
|
|
TupleLiteral(expressions) => {
|
|
|
|
let mut evals = Vec::new();
|
|
|
|
for expr in expressions {
|
|
|
|
match self.eval_expr(expr) {
|
|
|
|
Ok(fully_evaluated) => evals.push(fully_evaluated),
|
|
|
|
error => return error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Tuple(evals))
|
|
|
|
}
|
2018-02-26 19:55:27 -08:00
|
|
|
Call { f, arguments } => {
|
|
|
|
let mut evaled_arguments = Vec::new();
|
|
|
|
for arg in arguments.into_iter() {
|
|
|
|
evaled_arguments.push(self.eval_expr(arg)?);
|
|
|
|
}
|
|
|
|
self.eval_application(*f, evaled_arguments)
|
|
|
|
},
|
2017-10-23 00:22:25 -07:00
|
|
|
x => Err(format!("Unimplemented thing {:?}", x)),
|
2017-10-01 19:09:55 -07:00
|
|
|
}
|
2017-10-01 12:55:28 -07:00
|
|
|
}
|
2017-10-02 20:11:27 -07:00
|
|
|
|
2018-02-26 19:55:27 -08:00
|
|
|
fn eval_application(&mut self, f: Expression, arguments: Vec<FullyEvaluatedExpr>) -> EvalResult<FullyEvaluatedExpr> {
|
2017-10-23 01:54:02 -07:00
|
|
|
use self::ExpressionType::*;
|
|
|
|
match f {
|
2018-02-26 18:12:37 -08:00
|
|
|
Expression(Value(identifier), _) => {
|
2017-10-23 01:54:02 -07:00
|
|
|
match self.values.get(&identifier) {
|
2018-02-26 19:55:27 -08:00
|
|
|
Some(&ValueEntry::Function { ref body, ref param_names }) => {
|
2018-02-24 14:31:04 -08:00
|
|
|
let mut new_state = State::new_with_parent(self);
|
|
|
|
let sub_ast = body.clone();
|
|
|
|
|
2018-02-26 19:55:27 -08:00
|
|
|
if arguments.len() != param_names.len() {
|
|
|
|
return Err(format!("Wrong number of arguments for the function"));
|
|
|
|
}
|
|
|
|
for (param, val) in param_names.iter().zip(arguments.into_iter()) {
|
|
|
|
new_state.values.insert(param.clone(), ValueEntry::Binding { val });
|
|
|
|
}
|
|
|
|
|
2018-02-24 14:31:04 -08:00
|
|
|
let mut ret: Option<FullyEvaluatedExpr> = None;
|
|
|
|
for statement in sub_ast.into_iter() {
|
|
|
|
ret = new_state.eval_statement(statement)?;
|
|
|
|
}
|
|
|
|
Ok(ret.unwrap_or(FullyEvaluatedExpr::Custom { string_rep: Rc::new("()".to_string()) }))
|
2017-10-23 01:54:02 -07:00
|
|
|
},
|
|
|
|
_ => Err(format!("Function {} not found", identifier)),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
x => Err(format!("Trying to apply {:?} which is not a function", x)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 03:05:18 -07:00
|
|
|
fn eval_value(&mut self, name: Rc<String>) -> EvalResult<FullyEvaluatedExpr> {
|
|
|
|
use self::ValueEntry::*;
|
|
|
|
match self.values.get(&name) {
|
|
|
|
None => return Err(format!("Value {} not found", *name)),
|
2018-02-26 19:16:49 -08:00
|
|
|
Some(lookup) => match lookup {
|
|
|
|
&Binding { ref val } => Ok(val.clone()),
|
|
|
|
&Function { .. } => Ok(FullyEvaluatedExpr::FuncLit(name.clone()))
|
2017-10-13 03:05:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:06:37 -08:00
|
|
|
fn eval_binexp(&mut self, op: BinOp, lhs: Box<Expression>, rhs: Box<Expression>) -> EvalResult<FullyEvaluatedExpr> {
|
2017-10-02 20:11:27 -07:00
|
|
|
use self::FullyEvaluatedExpr::*;
|
|
|
|
let evaled_lhs = self.eval_expr(*lhs)?;
|
|
|
|
let evaled_rhs = self.eval_expr(*rhs)?;
|
2018-02-24 14:39:45 -08:00
|
|
|
let sigil = op.sigil();
|
|
|
|
//let sigil: &str = op.sigil().as_ref().as_str();
|
|
|
|
Ok(match (sigil.as_str(), evaled_lhs, evaled_rhs) {
|
2017-10-02 20:11:27 -07:00
|
|
|
("+", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l + r),
|
2017-10-12 23:59:52 -07:00
|
|
|
("++", Str(s1), Str(s2)) => Str(format!("{}{}", s1, s2)),
|
2017-10-02 20:11:27 -07:00
|
|
|
("-", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l - r),
|
|
|
|
("*", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l * r),
|
2018-02-26 02:11:56 -08:00
|
|
|
("/", UnsignedInt(l), UnsignedInt(r)) => Float((l as f64)/ (r as f64)),
|
|
|
|
("//", UnsignedInt(l), UnsignedInt(r)) => if r == 0 {
|
|
|
|
return Err(format!("Runtime error: divide by zero"));
|
|
|
|
} else {
|
|
|
|
UnsignedInt(l / r)
|
|
|
|
},
|
2017-10-02 20:11:27 -07:00
|
|
|
("%", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l % r),
|
2018-02-26 02:21:21 -08:00
|
|
|
("^", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l ^ r),
|
|
|
|
("&", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l & r),
|
|
|
|
("|", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l | r),
|
2017-10-02 20:11:27 -07:00
|
|
|
_ => return Err(format!("Runtime error: not yet implemented")),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:06:37 -08:00
|
|
|
fn eval_prefix_exp(&mut self, op: PrefixOp, expr: Box<Expression>) -> EvalResult<FullyEvaluatedExpr> {
|
2017-10-02 20:11:27 -07:00
|
|
|
use self::FullyEvaluatedExpr::*;
|
|
|
|
let evaled_expr = self.eval_expr(*expr)?;
|
2018-02-24 14:39:45 -08:00
|
|
|
let sigil = op.sigil();
|
2017-10-02 20:11:27 -07:00
|
|
|
|
2018-02-24 14:39:45 -08:00
|
|
|
Ok(match (sigil.as_str(), evaled_expr) {
|
2017-10-02 20:11:27 -07:00
|
|
|
("!", Bool(true)) => Bool(false),
|
|
|
|
("!", Bool(false)) => Bool(true),
|
|
|
|
("-", UnsignedInt(n)) => SignedInt(-1*(n as i64)),
|
|
|
|
("-", SignedInt(n)) => SignedInt(-1*(n as i64)),
|
2018-02-26 02:21:21 -08:00
|
|
|
("+", SignedInt(n)) => SignedInt(n),
|
|
|
|
("+", UnsignedInt(n)) => UnsignedInt(n),
|
2017-10-02 20:11:27 -07:00
|
|
|
_ => return Err(format!("Runtime error: not yet implemented")),
|
|
|
|
})
|
|
|
|
}
|
2017-10-01 12:55:28 -07:00
|
|
|
}
|