From 00945be8de227e2a6f56819e5864b58559e37ee6 Mon Sep 17 00:00:00 2001
From: David Li
Date: Tue, 7 Nov 2017 19:09:42 -0500
Subject: Parameterize translation on frame type
---
src/semantic/analysis.rs | 15 +++++++++------
src/semantic/translate.rs | 36 ++++++++++++++++++++++++++++++------
2 files changed, 39 insertions(+), 12 deletions(-)
(limited to 'src/semantic')
diff --git a/src/semantic/analysis.rs b/src/semantic/analysis.rs
index edc229f..cf19351 100644
--- a/src/semantic/analysis.rs
+++ b/src/semantic/analysis.rs
@@ -5,6 +5,7 @@
use ::ast::{self, WithLocation};
use super::environment;
+use super::frame;
use super::translate::{self, Translate};
use super::types::{self, Ty};
@@ -25,8 +26,10 @@ pub enum TypeError {
type TypeEnvironment<'a> = environment::Environment<'a, String, Ty>;
pub type Result = ::std::result::Result>;
-pub fn translate(translate: &mut Translate, program: &ast::Program)
- -> Result<(translate::Expression, Ty)> {
+pub fn translate(
+ translate: &mut Translate,
+ program: &ast::Program)
+ -> Result<(translate::Expression, Ty)> {
let mut venv = TypeEnvironment::new(None);
let mut tenv = TypeEnvironment::new(None);
tenv.add_binding("int".into(), Ty::Int);
@@ -65,8 +68,8 @@ fn trans_ty<'a>(
}
}
-fn trans_decl<'a>(
- tr: &mut Translate,
+fn trans_decl<'a, F: frame::Frame>(
+ tr: &mut Translate,
venv: &mut TypeEnvironment<'a>,
tenv: &mut TypeEnvironment<'a>,
decl: &WithLocation) -> Result {
@@ -119,8 +122,8 @@ fn trans_decl<'a>(
}
}
-fn trans_exp<'a>(
- tr: &mut Translate,
+fn trans_exp<'a, F: frame::Frame>(
+ tr: &mut Translate,
venv: &mut TypeEnvironment<'a>,
tenv: &mut TypeEnvironment<'a>,
exp: &WithLocation)
diff --git a/src/semantic/translate.rs b/src/semantic/translate.rs
index cc5aec0..179eb62 100644
--- a/src/semantic/translate.rs
+++ b/src/semantic/translate.rs
@@ -1,4 +1,6 @@
+use super::frame;
use super::ir;
+use super::temp;
#[derive(Debug)]
pub enum Expression {
@@ -6,18 +8,40 @@ pub enum Expression {
Void(ir::Statement),
}
-pub struct Translate {
-
+pub struct Translate {
+ level_counter: usize,
+ _frametype: ::std::marker::PhantomData,
}
-pub enum Level {
+pub enum Level<'a, F>
+ where F: 'a + frame::Frame {
Top,
- Level,
+ Level {
+ parent: &'a Level<'a, F>,
+ frame: F,
+ unique_id: usize,
+ },
}
-impl Translate {
- pub fn new() -> Translate {
+impl Translate {
+ pub fn new() -> Translate {
Translate {
+ level_counter: 0,
+ _frametype: ::std::marker::PhantomData,
+ }
+ }
+
+ pub fn make_level<'a>(
+ &mut self,
+ parent: &'a Level,
+ name: temp::TempLabel,
+ formals: Vec) -> Level<'a, F> {
+ let id = self.level_counter;
+ self.level_counter += 1;
+ Level::Level {
+ parent: parent,
+ frame: F::new(name, formals),
+ unique_id: id,
}
}
--
cgit v1.2.3