Evaluation stuff
This commit is contained in:
parent
c5feea4597
commit
f6536e7ebd
@ -3,6 +3,17 @@ use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionTy
|
|||||||
pub struct ReplState {
|
pub struct ReplState {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EvalResult<T> = Result<T, String>;
|
||||||
|
|
||||||
|
enum FullyEvaluatedExpr {
|
||||||
|
UnsignedInt(u64),
|
||||||
|
SignedInt(i64),
|
||||||
|
Float(f64),
|
||||||
|
Str(String),
|
||||||
|
Bool(bool),
|
||||||
|
Other
|
||||||
|
}
|
||||||
|
|
||||||
impl ReplState {
|
impl ReplState {
|
||||||
pub fn new() -> ReplState {
|
pub fn new() -> ReplState {
|
||||||
ReplState { }
|
ReplState { }
|
||||||
@ -11,9 +22,17 @@ impl ReplState {
|
|||||||
pub fn evaluate(&mut self, ast: AST) -> String {
|
pub fn evaluate(&mut self, ast: AST) -> String {
|
||||||
let mut acc = String::new();
|
let mut acc = String::new();
|
||||||
for statement in ast.0 {
|
for statement in ast.0 {
|
||||||
if let Some(output) = self.eval_statement(statement) {
|
match self.eval_statement(statement) {
|
||||||
acc.push_str(&output);
|
Ok(output) => {
|
||||||
acc.push_str("\n");
|
if let Some(s) = output {
|
||||||
|
acc.push_str(&s);
|
||||||
|
acc.push_str("\n");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(error) => {
|
||||||
|
acc.push_str(&format!("Error: {}", error));
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
format!("{}", acc)
|
format!("{}", acc)
|
||||||
@ -21,28 +40,44 @@ impl ReplState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ReplState {
|
impl ReplState {
|
||||||
fn eval_statement(&mut self, statement: Statement) -> Option<String> {
|
fn eval_statement(&mut self, statement: Statement) -> EvalResult<Option<String>> {
|
||||||
|
use self::FullyEvaluatedExpr::*;
|
||||||
match statement {
|
match statement {
|
||||||
Statement::ExpressionStatement(expr) => self.eval_expr(expr),
|
Statement::ExpressionStatement(expr) => {
|
||||||
Statement::Declaration(decl) => self.eval_decl(decl),
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eval_decl(&mut self, decl: Declaration) -> Option<String> {
|
fn eval_decl(&mut self, decl: Declaration) -> EvalResult<()> {
|
||||||
Some("UNIMPLEMENTED".to_string())
|
Err("Not implmemented".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eval_expr(&mut self, expr: Expression) -> Option<String> {
|
fn eval_expr(&mut self, expr: Expression) -> EvalResult<FullyEvaluatedExpr> {
|
||||||
use self::ExpressionType::*;
|
use self::ExpressionType::*;
|
||||||
|
use self::FullyEvaluatedExpr::*;
|
||||||
|
|
||||||
let expr_type = expr.0;
|
let expr_type = expr.0;
|
||||||
Some(match expr_type {
|
match expr_type {
|
||||||
IntLiteral(n) => format!("{}", n),
|
IntLiteral(n) => Ok(UnsignedInt(n)),
|
||||||
FloatLiteral(f) => format!("{}", f),
|
FloatLiteral(f) => Ok(Float(f)),
|
||||||
StringLiteral(s) => format!("{}", s),
|
StringLiteral(s) => Ok(Str(s.to_string())),
|
||||||
BoolLiteral(b) => format!("{}", b),
|
BoolLiteral(b) => Ok(Bool(b)),
|
||||||
_ => format!("UNIMPLEMENTED"),
|
_ => Err(format!("Unimplemented")),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user