Compile statements with variables

This commit is contained in:
greg 2016-12-27 17:33:24 -08:00
parent db52f9b716
commit c389b44bf8
2 changed files with 21 additions and 3 deletions

View File

@ -1,5 +1,7 @@
fn main() fn main()
1 + 2 + 3 + 4 * 2 a = 4
b = 20
a + b
end end

View File

@ -109,8 +109,12 @@ impl CodeGen for ASTNode {
impl CodeGen for Function { impl CodeGen for Function {
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef { fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
let ref body = self.body; let ref body = self.body;
let first = body.get(0).unwrap(); let int_type = LLVMWrap::Int64TypeInContext(data.context);
first.codegen(data) let mut ret = LLVMWrap::ConstInt(int_type, 0, false);
for expr in body {
ret = expr.codegen(data);
}
ret
} }
} }
@ -121,6 +125,18 @@ 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) => {
*data.variables.get(name).unwrap()
},
&BinExp(ref op, ref left, ref right) if op == "=" => {
if let Variable(ref name) = **left {
let new_value = right.codegen(data);
data.variables.insert(name.clone(), new_value);
new_value
} else {
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);