From d16a0c9380a3972b4b168130ef92b74bdc9c4b4a Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 1 Oct 2017 12:55:28 -0700 Subject: [PATCH] Evaluation of literals --- src/schala_lang/eval.rs | 49 ++++++++++++++++++++++++++++++++------ src/schala_lang/parsing.rs | 6 ++--- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/schala_lang/eval.rs b/src/schala_lang/eval.rs index 1c8c096..19b20ee 100644 --- a/src/schala_lang/eval.rs +++ b/src/schala_lang/eval.rs @@ -1,22 +1,57 @@ -use schala_lang::parsing::AST; +use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType}; pub struct ReplState { } -pub enum TypeCheck { - OK, - Error(String) -} - impl ReplState { pub fn new() -> ReplState { ReplState { } } 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 { + match statement { + Statement::ExpressionStatement(expr) => self.eval_expr(expr), + Statement::Declaration(decl) => self.eval_decl(decl), + } } + fn eval_decl(&mut self, decl: Declaration) -> Option { + Some("UNIMPLEMENTED".to_string()) + } + + fn eval_expr(&mut self, expr: Expression) -> Option { + 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 { //TypeCheck::Error("type lol".to_string()) TypeCheck::OK diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 3932873..092d6b2 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -383,7 +383,7 @@ macro_rules! expect { } #[derive(Debug, PartialEq)] -pub struct AST(Vec); +pub struct AST(pub Vec); #[derive(Debug, PartialEq)] pub enum Statement { @@ -411,7 +411,7 @@ pub enum Declaration { } #[derive(Debug, PartialEq)] -pub struct TypeBody(Vec); +pub struct TypeBody(pub Vec); #[derive(Debug, PartialEq)] pub enum Variant { @@ -421,7 +421,7 @@ pub enum Variant { } #[derive(Debug, PartialEq)] -pub struct Expression(ExpressionType, Option); +pub struct Expression(pub ExpressionType, pub Option); #[derive(Debug, PartialEq)] pub enum TypeAnno {