schala/src/schala_lang/eval.rs

61 lines
1.4 KiB
Rust
Raw Normal View History

2017-10-01 12:55:28 -07:00
use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType};
2017-09-30 23:30:02 -07:00
pub struct ReplState {
}
impl ReplState {
pub fn new() -> ReplState {
ReplState { }
}
pub fn evaluate(&mut self, ast: AST) -> String {
2017-10-01 12:55:28 -07:00
let mut acc = String::new();
for statement in ast.0 {
if let Some(output) = self.eval_statement(statement) {
acc.push_str(&output);
acc.push_str("\n");
}
}
format!("{}", acc)
}
}
impl ReplState {
fn eval_statement(&mut self, statement: Statement) -> Option<String> {
match statement {
Statement::ExpressionStatement(expr) => self.eval_expr(expr),
Statement::Declaration(decl) => self.eval_decl(decl),
}
}
fn eval_decl(&mut self, decl: Declaration) -> Option<String> {
Some("UNIMPLEMENTED".to_string())
2017-09-30 23:30:02 -07:00
}
2017-10-01 00:48:08 -07:00
2017-10-01 12:55:28 -07:00
fn eval_expr(&mut self, expr: Expression) -> Option<String> {
use self::ExpressionType::*;
let expr_type = expr.0;
Some(match expr_type {
IntLiteral(n) => format!("{}", n),
FloatLiteral(f) => format!("{}", f),
StringLiteral(s) => format!("{}", s),
BoolLiteral(b) => format!("{}", b),
_ => format!("UNIMPLEMENTED"),
})
}
}
pub enum TypeCheck {
OK,
Error(String)
}
impl ReplState {
2017-10-01 00:50:13 -07:00
pub fn type_check(&mut self, _ast: &AST) -> TypeCheck {
//TypeCheck::Error("type lol".to_string())
TypeCheck::OK
2017-10-01 00:48:08 -07:00
}
2017-09-30 23:30:02 -07:00
}