2017-10-01 17:50:26 -07:00
|
|
|
use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType, TypeAnno};
|
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),
|
|
|
|
Other
|
|
|
|
}
|
|
|
|
|
2017-09-30 23:30:02 -07:00
|
|
|
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 {
|
2017-10-01 19:09:55 -07:00
|
|
|
match self.eval_statement(statement) {
|
|
|
|
Ok(output) => {
|
|
|
|
if let Some(s) = output {
|
|
|
|
acc.push_str(&s);
|
|
|
|
acc.push_str("\n");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(error) => {
|
|
|
|
acc.push_str(&format!("Error: {}", error));
|
|
|
|
return acc;
|
|
|
|
},
|
2017-10-01 12:55:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
format!("{}", acc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
Statement::Declaration(decl) => {
|
|
|
|
self.eval_decl(decl);
|
|
|
|
Ok(None)
|
|
|
|
}
|
2017-10-01 12:55:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 19:09:55 -07:00
|
|
|
fn eval_decl(&mut self, decl: Declaration) -> EvalResult<()> {
|
|
|
|
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)),
|
|
|
|
_ => Err(format!("Unimplemented")),
|
|
|
|
}
|
2017-10-01 12:55:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum TypeCheck {
|
|
|
|
OK,
|
|
|
|
Error(String)
|
|
|
|
}
|
2017-10-01 17:50:26 -07:00
|
|
|
impl TypeCheck {
|
|
|
|
fn new(msg: &str) -> TypeCheck {
|
|
|
|
TypeCheck::Error(msg.to_string())
|
|
|
|
}
|
|
|
|
}
|
2017-10-01 12:55:28 -07:00
|
|
|
|
|
|
|
impl ReplState {
|
2017-10-01 17:50:26 -07:00
|
|
|
pub fn type_check(&mut self, ast: &AST) -> TypeCheck {
|
|
|
|
use self::ExpressionType::*;
|
|
|
|
for statement in ast.0.iter() {
|
|
|
|
match statement {
|
|
|
|
&Statement::Declaration(ref decl) => {
|
|
|
|
return TypeCheck::new("Declarations not supported");
|
|
|
|
},
|
|
|
|
&Statement::ExpressionStatement(ref expr) => {
|
|
|
|
match (&expr.0, &expr.1) {
|
|
|
|
(&IntLiteral(_), &Some(ref t)) => {
|
|
|
|
match t {
|
|
|
|
&TypeAnno::Singleton { ref name, ref params } if **name == "Int" && params.len() == 0 => (),
|
|
|
|
t => return TypeCheck::new(&format!("Bad type {:?} for int literal", t)),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-01 00:50:13 -07:00
|
|
|
TypeCheck::OK
|
2017-10-01 00:48:08 -07:00
|
|
|
}
|
2017-09-30 23:30:02 -07:00
|
|
|
}
|
|
|
|
|