From 376a1217a63291fb6714430b1a4dfd64909906c9 Mon Sep 17 00:00:00 2001
From: David Li
Date: Sun, 26 Nov 2017 19:33:08 -0500
Subject: Translate binop (only num + num)
---
src/semantic/analysis.rs | 4 ++--
src/semantic/translate.rs | 9 +++++++++
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/src/semantic/analysis.rs b/src/semantic/analysis.rs
index cf19351..09832c4 100644
--- a/src/semantic/analysis.rs
+++ b/src/semantic/analysis.rs
@@ -218,8 +218,8 @@ fn trans_exp<'a, F: frame::Frame>(
&Add => {
match (left_ty, right_ty) {
(Ty::Int, Ty::Int) => {
- err!(exp, TypeError::Unimplemented)
- // Ok(Ty::Int)
+ Ok((tr.make_binop(ir::Binop::Plus, left_exp, right_exp),
+ Ty::Int))
}
(Ty::String, Ty::String) => {
err!(exp, TypeError::Unimplemented)
diff --git a/src/semantic/translate.rs b/src/semantic/translate.rs
index 179eb62..bcbad3a 100644
--- a/src/semantic/translate.rs
+++ b/src/semantic/translate.rs
@@ -48,4 +48,13 @@ impl Translate {
pub fn make_num(&mut self, num: u64) -> Expression {
Expression::Exp(ir::Expression::Const(num))
}
+
+ pub fn make_binop(&mut self, op: ir::Binop,
+ left: Expression, right: Expression) -> Expression {
+ match (left, right) {
+ (Expression::Exp(left), Expression::Exp(right)) =>
+ Expression::Exp(ir::Expression::Binop(op, Box::new(left), Box::new(right))),
+ _ => panic!("Can't make binop from Void"),
+ }
+ }
}
--
cgit v1.2.3