Method-style
This commit is contained in:
parent
c3be644133
commit
16a463b1a0
@ -39,40 +39,51 @@ pub struct Func {
|
|||||||
body: Vec<Stmt>,
|
body: Vec<Stmt>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn perform_ast_reduction(ast: &AST) -> Result<ReducedAST, String> {
|
impl AST {
|
||||||
use parsing::Statement::*;
|
pub fn reduce(&self) -> Result<ReducedAST, String> {
|
||||||
let mut output = vec![];
|
use parsing::Statement::*;
|
||||||
for statement in ast.0.iter() {
|
let mut output = vec![];
|
||||||
match statement {
|
for statement in self.0.iter() {
|
||||||
&ExpressionStatement(ref expr) => output.push(reduce_expr(expr)?),
|
match statement {
|
||||||
&Declaration(ref decl) => output.push(reduce_decl(decl)?),
|
&ExpressionStatement(ref expr) => output.push(expr.reduce()?),
|
||||||
|
&Declaration(ref decl) => output.push(decl.reduce()?),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Ok(ReducedAST(output))
|
||||||
}
|
}
|
||||||
Ok(ReducedAST(output))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_expr(expr: &Expression) -> Result<Stmt, String> {
|
impl Expression {
|
||||||
use parsing::ExpressionType::*;
|
fn reduce(&self) -> Result<Stmt, String> {
|
||||||
let ref input = expr.0;
|
use parsing::ExpressionType::*;
|
||||||
let output_expr = match input {
|
let ref input = self.0;
|
||||||
&IntLiteral(ref n) => Expr::Lit(Lit::Int(*n)),
|
let output_expr = match input {
|
||||||
&FloatLiteral(ref f) => Expr::Lit(Lit::Float(*f)),
|
&IntLiteral(ref n) => Expr::Lit(Lit::Int(*n)),
|
||||||
&StringLiteral(ref s) => Expr::Lit(Lit::StringLit(s.clone())),
|
&FloatLiteral(ref f) => Expr::Lit(Lit::Float(*f)),
|
||||||
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
|
&StringLiteral(ref s) => Expr::Lit(Lit::StringLit(s.clone())),
|
||||||
&BinExp(ref binop, ref lhs, ref rhs) => reduce_binop(binop, lhs, rhs)?,
|
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
|
||||||
&PrefixExp(ref op, ref arg) => reduce_prefix(op, arg)?,
|
&BinExp(ref binop, ref lhs, ref rhs) => binop.reduce(lhs, rhs)?,
|
||||||
e => return Err(format!("{:?} not implemented in reduction", e))
|
&PrefixExp(ref op, ref arg) => op.reduce(arg)?,
|
||||||
};
|
e => return Err(format!("{:?} not implemented in reduction", e))
|
||||||
Ok(Stmt::Expr(output_expr))
|
};
|
||||||
}
|
Ok(Stmt::Expr(output_expr))
|
||||||
fn reduce_decl(expr: &Declaration) -> Result<Stmt, String> {
|
}
|
||||||
Ok(Stmt::Expr(Expr::Lit(Lit::Int(0))))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_binop(binop: &BinOp, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Result<Expr, String> {
|
impl Declaration {
|
||||||
|
fn reduce(&self) -> Result<Stmt, String> {
|
||||||
|
Ok(Stmt::Expr(Expr::Lit(Lit::Int(0))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BinOp {
|
||||||
|
fn reduce(&self, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Result<Expr, String> {
|
||||||
Err(format!("NOTDONE"))
|
Err(format!("NOTDONE"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_prefix(op: &PrefixOp, arg: &Box<Expression>) -> Result<Expr, String> {
|
impl PrefixOp {
|
||||||
|
fn reduce(&self, arg: &Box<Expression>) -> Result<Expr, String> {
|
||||||
Err(format!("NOTDONE"))
|
Err(format!("NOTDONE"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,7 @@ fn typechecking(handle: &mut Schala, input: parsing::AST, comp: Option<&mut Unfi
|
|||||||
|
|
||||||
type TempASTReduction = (ast_reducing::ReducedAST, parsing::AST);
|
type TempASTReduction = (ast_reducing::ReducedAST, parsing::AST);
|
||||||
fn ast_reducing(handle: &mut Schala, input: parsing::AST, comp: Option<&mut UnfinishedComputation>) -> Result<TempASTReduction, String> {
|
fn ast_reducing(handle: &mut Schala, input: parsing::AST, comp: Option<&mut UnfinishedComputation>) -> Result<TempASTReduction, String> {
|
||||||
let output = ast_reducing::perform_ast_reduction(&input)?;
|
let output = input.reduce()?;
|
||||||
println!("REDUCED: {:?}", output);
|
println!("REDUCED: {:?}", output);
|
||||||
Ok((output, input))
|
Ok((output, input))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user