forked from syndicate-lang/preserves
74 lines
1.8 KiB
Rust
74 lines
1.8 KiB
Rust
use preserves::value::{Map, IOValue};
|
|
|
|
use crate::syntax::block::Item;
|
|
|
|
use super::types;
|
|
|
|
pub struct ModuleContext {
|
|
pub literals: Map<IOValue, String>,
|
|
pub typedefs: Vec<Item>,
|
|
pub functiondefs: Vec<Item>,
|
|
}
|
|
|
|
pub struct FunctionContext<'a> {
|
|
pub m: &'a mut ModuleContext,
|
|
pub temp_counter: usize,
|
|
pub temps: Map<types::TDefinition, Vec<String>>,
|
|
pub captures: Vec<Capture>,
|
|
}
|
|
|
|
pub struct Capture {
|
|
pub field_name: String,
|
|
pub ty: types::TField,
|
|
pub source_expr: String,
|
|
}
|
|
|
|
impl ModuleContext {
|
|
pub fn new() -> Self {
|
|
ModuleContext {
|
|
literals: Map::new(),
|
|
typedefs: Vec::new(),
|
|
functiondefs: Vec::new(),
|
|
}
|
|
}
|
|
|
|
pub fn define_literal(&mut self, v: &IOValue) -> String {
|
|
let next_id = format!("LIT{}", self.literals.len());
|
|
"&*".to_owned() + self.literals.entry(v.clone()).or_insert(next_id)
|
|
}
|
|
|
|
pub fn define_type(&mut self, i: Item) {
|
|
self.typedefs.push(i)
|
|
}
|
|
|
|
pub fn define_function<F: FnOnce(FunctionContext) -> Item>(&mut self, f: F) {
|
|
let i = f(FunctionContext::new(self));
|
|
self.functiondefs.push(i)
|
|
}
|
|
}
|
|
|
|
impl<'a> FunctionContext<'a> {
|
|
pub fn new(m: &'a mut ModuleContext) -> Self {
|
|
FunctionContext {
|
|
m: m,
|
|
temp_counter: 0,
|
|
temps: Map::new(),
|
|
captures: Vec::new(),
|
|
}
|
|
}
|
|
|
|
pub fn capture(&mut self, field_name: String, ty: types::TField, source_expr: String) {
|
|
self.captures.push(Capture {
|
|
field_name: field_name,
|
|
ty: ty,
|
|
source_expr: source_expr,
|
|
})
|
|
}
|
|
|
|
pub fn gentempname(&mut self) -> String {
|
|
let i = self.temp_counter;
|
|
self.temp_counter += 1;
|
|
format!("_tmp{}", i)
|
|
}
|
|
}
|