schala/src/schala_lang/eval.rs

110 lines
3.3 KiB
Rust
Raw Normal View History

2017-10-04 02:07:30 -07:00
use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType, Operation};
2017-09-30 23:30:02 -07:00
pub struct ReplState {
}
2017-10-01 19:09:55 -07:00
type EvalResult<T> = Result<T, String>;
enum FullyEvaluatedExpr {
UnsignedInt(u64),
SignedInt(i64),
Float(f64),
Str(String),
Bool(bool),
}
2017-09-30 23:30:02 -07:00
impl ReplState {
pub fn new() -> ReplState {
ReplState { }
}
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) => {
if let Some(s) = output {
2017-10-01 19:29:05 -07:00
acc.push(s);
2017-10-01 19:09:55 -07:00
}
},
Err(error) => {
2017-10-01 19:29:05 -07:00
acc.push(format!("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
}
}
impl ReplState {
2017-10-01 19:09:55 -07:00
fn eval_statement(&mut self, statement: Statement) -> EvalResult<Option<String>> {
use self::FullyEvaluatedExpr::*;
2017-10-01 12:55:28 -07:00
match statement {
2017-10-01 19:09:55 -07:00
Statement::ExpressionStatement(expr) => {
self.eval_expr(expr).map( |eval| {
match eval {
UnsignedInt(n) => Some(format!("{}", n)),
SignedInt(n) => Some(format!("{}", n)),
Float(f) => Some(format!("{}", f)),
Str(s) => Some(format!("\"{}\"", s)),
Bool(b) => Some(format!("{}", b)),
}
})
},
Statement::Declaration(decl) => {
2017-10-01 19:31:43 -07:00
self.eval_decl(decl).map(|_| None)
2017-10-01 19:09:55 -07:00
}
2017-10-01 12:55:28 -07:00
}
}
2017-10-02 01:52:46 -07:00
fn eval_decl(&mut self, _decl: Declaration) -> EvalResult<()> {
2017-10-01 19:09:55 -07:00
Err("Not implmemented".to_string())
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),
2017-10-01 19:09:55 -07:00
_ => Err(format!("Unimplemented")),
}
2017-10-01 12:55:28 -07:00
}
2017-10-02 20:11:27 -07:00
fn eval_binexp(&mut self, op: Operation, lhs: Box<Expression>, rhs: Box<Expression>) -> EvalResult<FullyEvaluatedExpr> {
use self::FullyEvaluatedExpr::*;
let evaled_lhs = self.eval_expr(*lhs)?;
let evaled_rhs = self.eval_expr(*rhs)?;
let opstr: &str = &op.0;
Ok(match (opstr, evaled_lhs, evaled_rhs) {
("+", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l + r),
("-", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l - r),
("*", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l * r),
("/", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l / r),
("%", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l % r),
_ => return Err(format!("Runtime error: not yet implemented")),
})
}
fn eval_prefix_exp(&mut self, op: Operation, expr: Box<Expression>) -> EvalResult<FullyEvaluatedExpr> {
use self::FullyEvaluatedExpr::*;
let evaled_expr = self.eval_expr(*expr)?;
let opstr: &str = &op.0;
Ok(match (opstr, evaled_expr) {
("!", Bool(true)) => Bool(false),
("!", Bool(false)) => Bool(true),
("-", UnsignedInt(n)) => SignedInt(-1*(n as i64)),
("-", SignedInt(n)) => SignedInt(-1*(n as i64)),
_ => return Err(format!("Runtime error: not yet implemented")),
})
}
2017-10-01 12:55:28 -07:00
}