Nats, some binop reduction
This commit is contained in:
parent
87c3b8e234
commit
633b4fe7a4
@ -27,17 +27,21 @@ pub enum Expr {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Lit {
|
pub enum Lit {
|
||||||
Int(u64),
|
Nat(u64),
|
||||||
|
Int(i64),
|
||||||
Float(f64),
|
Float(f64),
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
StringLit(Rc<String>),
|
StringLit(Rc<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Func {
|
pub enum Func {
|
||||||
|
BuiltIn(Rc<String>),
|
||||||
|
UserDefined {
|
||||||
params: Vec<Rc<String>>,
|
params: Vec<Rc<String>>,
|
||||||
body: Vec<Stmt>,
|
body: Vec<Stmt>,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AST {
|
impl AST {
|
||||||
pub fn reduce(&self) -> Result<ReducedAST, String> {
|
pub fn reduce(&self) -> Result<ReducedAST, String> {
|
||||||
@ -45,7 +49,7 @@ impl AST {
|
|||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
for statement in self.0.iter() {
|
for statement in self.0.iter() {
|
||||||
match statement {
|
match statement {
|
||||||
&ExpressionStatement(ref expr) => output.push(expr.reduce()?),
|
&ExpressionStatement(ref expr) => output.push(Stmt::Expr(expr.reduce()?)),
|
||||||
&Declaration(ref decl) => output.push(decl.reduce()?),
|
&Declaration(ref decl) => output.push(decl.reduce()?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,11 +58,11 @@ impl AST {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Expression {
|
impl Expression {
|
||||||
fn reduce(&self) -> Result<Stmt, String> {
|
fn reduce(&self) -> Result<Expr, String> {
|
||||||
use parsing::ExpressionType::*;
|
use parsing::ExpressionType::*;
|
||||||
let ref input = self.0;
|
let ref input = self.0;
|
||||||
let output_expr = match input {
|
let output_expr = match input {
|
||||||
&IntLiteral(ref n) => Expr::Lit(Lit::Int(*n)),
|
&IntLiteral(ref n) => Expr::Lit(Lit::Nat(*n)), //TODO I should rename IntLiteral if I want the Nat/Int distinction, which I do
|
||||||
&FloatLiteral(ref f) => Expr::Lit(Lit::Float(*f)),
|
&FloatLiteral(ref f) => Expr::Lit(Lit::Float(*f)),
|
||||||
&StringLiteral(ref s) => Expr::Lit(Lit::StringLit(s.clone())),
|
&StringLiteral(ref s) => Expr::Lit(Lit::StringLit(s.clone())),
|
||||||
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
|
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
|
||||||
@ -66,7 +70,7 @@ impl Expression {
|
|||||||
&PrefixExp(ref op, ref arg) => op.reduce(arg)?,
|
&PrefixExp(ref op, ref arg) => op.reduce(arg)?,
|
||||||
e => return Err(format!("{:?} not implemented in reduction", e))
|
e => return Err(format!("{:?} not implemented in reduction", e))
|
||||||
};
|
};
|
||||||
Ok(Stmt::Expr(output_expr))
|
Ok(output_expr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +82,8 @@ impl Declaration {
|
|||||||
|
|
||||||
impl BinOp {
|
impl BinOp {
|
||||||
fn reduce(&self, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Result<Expr, String> {
|
fn reduce(&self, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Result<Expr, String> {
|
||||||
Err(format!("NOTDONE"))
|
let f = Func::BuiltIn(self.sigil().clone());
|
||||||
|
Ok(Expr::Call { f, args: vec![lhs.reduce()?, rhs.reduce()?]})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user