Evaluation of literals

This commit is contained in:
greg 2017-10-01 12:55:28 -07:00
parent daf9878020
commit d16a0c9380
2 changed files with 45 additions and 10 deletions

View File

@ -1,22 +1,57 @@
use schala_lang::parsing::AST; use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType};
pub struct ReplState { pub struct ReplState {
} }
pub enum TypeCheck {
OK,
Error(String)
}
impl ReplState { impl ReplState {
pub fn new() -> ReplState { pub fn new() -> ReplState {
ReplState { } ReplState { }
} }
pub fn evaluate(&mut self, ast: AST) -> String { pub fn evaluate(&mut self, ast: AST) -> String {
format!("Evaluated AST: {:?}", ast) 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())
}
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 {
pub fn type_check(&mut self, _ast: &AST) -> TypeCheck { pub fn type_check(&mut self, _ast: &AST) -> TypeCheck {
//TypeCheck::Error("type lol".to_string()) //TypeCheck::Error("type lol".to_string())
TypeCheck::OK TypeCheck::OK

View File

@ -383,7 +383,7 @@ macro_rules! expect {
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct AST(Vec<Statement>); pub struct AST(pub Vec<Statement>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Statement { pub enum Statement {
@ -411,7 +411,7 @@ pub enum Declaration {
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct TypeBody(Vec<Variant>); pub struct TypeBody(pub Vec<Variant>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Variant { pub enum Variant {
@ -421,7 +421,7 @@ pub enum Variant {
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Expression(ExpressionType, Option<TypeAnno>); pub struct Expression(pub ExpressionType, pub Option<TypeAnno>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum TypeAnno { pub enum TypeAnno {