1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#![allow(unused_variables)]
use crate::entity::Entity;
use crate::function::Function;
use crate::process::Process;
use std;
use std::collections::HashMap;
use crate::value::{Context, EntityRef, FunctionRef, ProcessRef, Value, ValueRef};
pub struct Module {
funcs: HashMap<FunctionRef, Function>,
procs: HashMap<ProcessRef, Process>,
entities: HashMap<EntityRef, Entity>,
values: Vec<ValueRef>,
}
impl Module {
pub fn new() -> Module {
Module {
funcs: HashMap::new(),
procs: HashMap::new(),
entities: HashMap::new(),
values: Vec::new(),
}
}
pub fn add_function(&mut self, func: Function) -> FunctionRef {
let r = func.as_ref();
self.funcs.insert(r, func);
self.values.push(r.into());
r
}
pub fn add_process(&mut self, prok: Process) -> ProcessRef {
let r = prok.as_ref();
self.procs.insert(r, prok);
self.values.push(r.into());
r
}
pub fn add_entity(&mut self, entity: Entity) -> EntityRef {
let r = entity.as_ref();
self.entities.insert(r, entity);
self.values.push(r.into());
r
}
pub fn function(&self, func: FunctionRef) -> &Function {
self.funcs.get(&func).unwrap()
}
pub fn function_mut(&mut self, func: FunctionRef) -> &mut Function {
self.funcs.get_mut(&func).unwrap()
}
pub fn process(&self, prok: ProcessRef) -> &Process {
self.procs.get(&prok).unwrap()
}
pub fn process_mut(&mut self, prok: ProcessRef) -> &mut Process {
self.procs.get_mut(&prok).unwrap()
}
pub fn entity(&self, entity: EntityRef) -> &Entity {
self.entities.get(&entity).unwrap()
}
pub fn entity_mut(&mut self, entity: EntityRef) -> &mut Entity {
self.entities.get_mut(&entity).unwrap()
}
pub fn values(&self) -> std::slice::Iter<ValueRef> {
self.values.iter()
}
}
pub struct ModuleContext<'tctx> {
module: &'tctx Module,
}
impl<'tctx> ModuleContext<'tctx> {
pub fn new(module: &Module) -> ModuleContext {
ModuleContext { module: module }
}
pub fn function(&self, func: FunctionRef) -> &Function {
self.module.function(func)
}
pub fn process(&self, prok: ProcessRef) -> &Process {
self.module.process(prok)
}
pub fn entity(&self, entity: EntityRef) -> &Entity {
self.module.entity(entity)
}
}
impl<'tctx> Context for ModuleContext<'tctx> {
fn try_value(&self, value: &ValueRef) -> Option<&Value> {
match *value {
ValueRef::Function(id) => Some(self.function(id)),
ValueRef::Process(id) => Some(self.process(id)),
ValueRef::Entity(id) => Some(self.entity(id)),
_ => None,
}
}
}