Run rustfmt on some files
This commit is contained in:
parent
d4d61ce5ad
commit
af45004afa
@ -12,7 +12,7 @@ pub fn compilation_sequence(ast: AST, sourcefile: &str) {
|
|||||||
|
|
||||||
let ll_filename = "out.ll";
|
let ll_filename = "out.ll";
|
||||||
let obj_filename = "out.o";
|
let obj_filename = "out.o";
|
||||||
let q: Vec<&str> = sourcefile.split('.').collect();
|
let q: Vec<&str> = sourcefile.split('.').collect();
|
||||||
let bin_filename = match &q[..] {
|
let bin_filename = match &q[..] {
|
||||||
&[name, "schala"] => name,
|
&[name, "schala"] => name,
|
||||||
_ => panic!("Bad filename {}", sourcefile),
|
_ => panic!("Bad filename {}", sourcefile),
|
||||||
@ -85,7 +85,7 @@ fn compile_ast(ast: AST, filename: &str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
trait CodeGen {
|
trait CodeGen {
|
||||||
fn codegen(&self, &mut CompilationData) -> LLVMValueRef;
|
fn codegen(&self, &mut CompilationData) -> LLVMValueRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodeGen for AST {
|
impl CodeGen for AST {
|
||||||
@ -124,9 +124,7 @@ impl CodeGen for Expression {
|
|||||||
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
&Variable(ref name) => {
|
&Variable(ref name) => *data.variables.get(name).unwrap(),
|
||||||
*data.variables.get(name).unwrap()
|
|
||||||
},
|
|
||||||
&BinExp(ref op, ref left, ref right) if op == "=" => {
|
&BinExp(ref op, ref left, ref right) if op == "=" => {
|
||||||
if let Variable(ref name) = **left {
|
if let Variable(ref name) = **left {
|
||||||
let new_value = right.codegen(data);
|
let new_value = right.codegen(data);
|
||||||
@ -135,7 +133,7 @@ impl CodeGen for Expression {
|
|||||||
} else {
|
} else {
|
||||||
panic!("Bad variable assignment")
|
panic!("Bad variable assignment")
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
&BinExp(ref op, ref left, ref right) => {
|
&BinExp(ref op, ref left, ref right) => {
|
||||||
let lhs = left.codegen(data);
|
let lhs = left.codegen(data);
|
||||||
let rhs = right.codegen(data);
|
let rhs = right.codegen(data);
|
||||||
@ -149,12 +147,12 @@ impl CodeGen for Expression {
|
|||||||
};
|
};
|
||||||
|
|
||||||
generator(data.builder, lhs, rhs, "temp")
|
generator(data.builder, lhs, rhs, "temp")
|
||||||
},
|
}
|
||||||
&Number(ref n) => {
|
&Number(ref n) => {
|
||||||
let native_val = *n as u64;
|
let native_val = *n as u64;
|
||||||
let int_value: LLVMValueRef = LLVMWrap::ConstInt(int_type, native_val, false);
|
let int_value: LLVMValueRef = LLVMWrap::ConstInt(int_type, native_val, false);
|
||||||
int_value
|
int_value
|
||||||
},
|
}
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
131
src/eval.rs
131
src/eval.rs
@ -10,12 +10,12 @@ enum SideEffect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Varmap {
|
struct Varmap {
|
||||||
map: HashMap<String, Expression>
|
map: HashMap<String, Expression>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Varmap {
|
impl Varmap {
|
||||||
fn new() -> Varmap {
|
fn new() -> Varmap {
|
||||||
Varmap { map: HashMap::new()}
|
Varmap { map: HashMap::new() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,18 +37,18 @@ pub struct Evaluator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Evaluator {
|
impl Evaluator {
|
||||||
|
|
||||||
pub fn new() -> Evaluator {
|
pub fn new() -> Evaluator {
|
||||||
Evaluator { varmap: Varmap::new(),
|
Evaluator {
|
||||||
funcmap: Funcmap::new(),
|
varmap: Varmap::new(),
|
||||||
frames: Vec::new(),
|
funcmap: Funcmap::new(),
|
||||||
|
frames: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self, ast: AST) -> Vec<String> {
|
pub fn run(&mut self, ast: AST) -> Vec<String> {
|
||||||
ast.into_iter().map(|astnode| {
|
ast.into_iter()
|
||||||
self.reduce(astnode)
|
.map(|astnode| self.reduce(astnode))
|
||||||
}).collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_binding(&mut self, var: String, value: Expression) {
|
fn add_binding(&mut self, var: String, value: Expression) {
|
||||||
@ -87,7 +87,7 @@ impl Evaluable for ASTNode {
|
|||||||
use parser::ASTNode::*;
|
use parser::ASTNode::*;
|
||||||
match self {
|
match self {
|
||||||
&ExprNode(ref expr) => expr.is_reducible(),
|
&ExprNode(ref expr) => expr.is_reducible(),
|
||||||
&FuncNode(_) => true,
|
&FuncNode(_) => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +109,7 @@ impl Evaluator {
|
|||||||
loop {
|
loop {
|
||||||
node = self.step(node);
|
node = self.step(node);
|
||||||
if !node.is_reducible() {
|
if !node.is_reducible() {
|
||||||
break
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ impl Evaluator {
|
|||||||
for side_effect in l {
|
for side_effect in l {
|
||||||
self.perform_side_effect(side_effect);
|
self.perform_side_effect(side_effect);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,12 +146,12 @@ impl Evaluator {
|
|||||||
} else {
|
} else {
|
||||||
(ExprNode(expr), None)
|
(ExprNode(expr), None)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
FuncNode(func) => {
|
FuncNode(func) => {
|
||||||
let fn_name = func.prototype.name.clone();
|
let fn_name = func.prototype.name.clone();
|
||||||
self.add_function(fn_name, func);
|
self.add_function(fn_name, func);
|
||||||
(ExprNode(Expression::Null), None)
|
(ExprNode(Expression::Null), None)
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,28 +159,28 @@ impl Evaluator {
|
|||||||
use parser::Expression::*;
|
use parser::Expression::*;
|
||||||
match expression {
|
match expression {
|
||||||
Null => (Null, None),
|
Null => (Null, None),
|
||||||
e@StringLiteral(_) => (e, None),
|
e @ StringLiteral(_) => (e, None),
|
||||||
e@Number(_) => (e, None),
|
e @ Number(_) => (e, None),
|
||||||
Variable(var) => {
|
Variable(var) => {
|
||||||
match self.lookup_binding(var) {
|
match self.lookup_binding(var) {
|
||||||
None => (Null, None),
|
None => (Null, None),
|
||||||
Some(expr) => (expr, None),
|
Some(expr) => (expr, None),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
BinExp(op, box left, box right) => {
|
BinExp(op, box left, box right) => {
|
||||||
if right.is_reducible() {
|
if right.is_reducible() {
|
||||||
let new = self.reduce_expr(right);
|
let new = self.reduce_expr(right);
|
||||||
return (BinExp(op, Box::new(left), Box::new(new.0)), new.1);
|
return (BinExp(op, Box::new(left), Box::new(new.0)), new.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//special case for variable assignment
|
// special case for variable assignment
|
||||||
if op == "=" {
|
if op == "=" {
|
||||||
match left {
|
match left {
|
||||||
Variable(var) => {
|
Variable(var) => {
|
||||||
self.add_binding(var, right);
|
self.add_binding(var, right);
|
||||||
return (Null, None); //TODO variable binding should be an effect
|
return (Null, None); //TODO variable binding should be an effect
|
||||||
},
|
}
|
||||||
_ => ()
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,14 +190,12 @@ impl Evaluator {
|
|||||||
} else {
|
} else {
|
||||||
(self.reduce_binop(op, left, right), None) //can assume both arguments are maximally reduced
|
(self.reduce_binop(op, left, right), None) //can assume both arguments are maximally reduced
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Call(name, mut args) => {
|
Call(name, mut args) => {
|
||||||
let mut f = true;
|
let mut f = true;
|
||||||
for arg in args.iter_mut() {
|
for arg in args.iter_mut() {
|
||||||
if arg.is_reducible() {
|
if arg.is_reducible() {
|
||||||
take_mut::take(arg, |arg| {
|
take_mut::take(arg, |arg| self.reduce_expr(arg).0);
|
||||||
self.reduce_expr(arg).0
|
|
||||||
});
|
|
||||||
f = false;
|
f = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -207,50 +205,67 @@ impl Evaluator {
|
|||||||
} else {
|
} else {
|
||||||
(Call(name, args), None)
|
(Call(name, args), None)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Conditional(_,_,_) => unimplemented!(),
|
Conditional(_, _, _) => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_binop(&mut self, op: String, left: Expression, right: Expression) -> Expression {
|
fn reduce_binop(&mut self, op: String, left: Expression, right: Expression) -> Expression {
|
||||||
use parser::Expression::*;
|
use parser::Expression::*;
|
||||||
match &op[..] {
|
match &op[..] {
|
||||||
"+" => match (left, right) {
|
"+" => {
|
||||||
(Number(l), Number(r)) => Number(l + r),
|
match (left, right) {
|
||||||
(StringLiteral(s1), StringLiteral(s2)) => StringLiteral(format!("{}{}", s1, s2)),
|
(Number(l), Number(r)) => Number(l + r),
|
||||||
_ => Null,
|
(StringLiteral(s1), StringLiteral(s2)) => {
|
||||||
},
|
StringLiteral(format!("{}{}", s1, s2))
|
||||||
"-" => match (left, right) {
|
}
|
||||||
(Number(l), Number(r)) => Number(l - r),
|
_ => Null,
|
||||||
_ => Null,
|
}
|
||||||
},
|
}
|
||||||
"*" => match (left, right) {
|
"-" => {
|
||||||
(Number(l), Number(r)) => Number(l * r),
|
match (left, right) {
|
||||||
_ => Null,
|
(Number(l), Number(r)) => Number(l - r),
|
||||||
},
|
_ => Null,
|
||||||
"/" => match (left, right) {
|
}
|
||||||
(Number(l), Number(r)) if r != 0.0 => Number(l / r),
|
}
|
||||||
_ => Null,
|
"*" => {
|
||||||
},
|
match (left, right) {
|
||||||
"%" => match (left, right) {
|
(Number(l), Number(r)) => Number(l * r),
|
||||||
(Number(l), Number(r)) => Number(l % r),
|
_ => Null,
|
||||||
_ => Null,
|
}
|
||||||
},
|
}
|
||||||
"=" => match (left, right) {
|
"/" => {
|
||||||
(Variable(var), right) => {
|
match (left, right) {
|
||||||
self.add_binding(var, right);
|
(Number(l), Number(r)) if r != 0.0 => Number(l / r),
|
||||||
Null
|
_ => Null,
|
||||||
},
|
}
|
||||||
_ => Null,
|
}
|
||||||
},
|
"%" => {
|
||||||
|
match (left, right) {
|
||||||
|
(Number(l), Number(r)) => Number(l % r),
|
||||||
|
_ => Null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"=" => {
|
||||||
|
match (left, right) {
|
||||||
|
(Variable(var), right) => {
|
||||||
|
self.add_binding(var, right);
|
||||||
|
Null
|
||||||
|
}
|
||||||
|
_ => Null,
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => Null,
|
_ => Null,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_call(&mut self, name: String, arguments: Vec<Expression>) -> (Expression, Option<SideEffect>) {
|
fn reduce_call(&mut self,
|
||||||
|
name: String,
|
||||||
|
arguments: Vec<Expression>)
|
||||||
|
-> (Expression, Option<SideEffect>) {
|
||||||
use parser::Expression::*;
|
use parser::Expression::*;
|
||||||
|
|
||||||
//ugly hack for now
|
// ugly hack for now
|
||||||
if name == "print" {
|
if name == "print" {
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
for arg in arguments {
|
for arg in arguments {
|
||||||
@ -262,11 +277,11 @@ impl Evaluator {
|
|||||||
|
|
||||||
let function = match self.lookup_function(name) {
|
let function = match self.lookup_function(name) {
|
||||||
Some(func) => func,
|
Some(func) => func,
|
||||||
None => return (Null, None)
|
None => return (Null, None),
|
||||||
};
|
};
|
||||||
|
|
||||||
if function.prototype.parameters.len() != arguments.len() {
|
if function.prototype.parameters.len() != arguments.len() {
|
||||||
return (Null, None)
|
return (Null, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut frame: Varmap = Varmap::new();
|
let mut frame: Varmap = Varmap::new();
|
||||||
|
Loading…
Reference in New Issue
Block a user