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
use std::io::Write;
use cpclib_common::itertools::Itertools;
use cpclib_tokens::symbols::{Symbol, SymbolsTableTrait, Value};
use cpclib_tokens::ExprResult;
#[derive(Clone)]
pub struct SymbolOutputGenerator {
forbidden: Vec<Symbol>,
allowed: Vec<Symbol>,
all_forbidden: bool,
all_allowed: bool
}
impl Default for SymbolOutputGenerator {
fn default() -> Self {
Self {
forbidden: Vec::new(),
allowed: Vec::new(),
all_forbidden: false,
all_allowed: true
}
}
}
impl SymbolOutputGenerator {
pub fn generate<W: Write>(
&self,
w: &mut W,
symbs: &impl SymbolsTableTrait
) -> std::io::Result<()> {
for (k, v) in symbs
.expression_symbol()
.iter()
.filter(|(s, _v)| self.keep_symbol(s))
.sorted_by_key(|(s, _v)| s.to_string().to_ascii_lowercase())
{
match v {
Value::Address(a) => {
writeln!(w, "{} equ #{:04X}", k.value(), a.address())
}
Value::Expr(ExprResult::Value(i)) => {
writeln!(w, "{} equ #{:04X}", k.value(), i)
}
Value::Expr(ExprResult::Bool(b)) => {
writeln!(w, "{} equ {}", k.value(), *b)
}
Value::Expr(e @ ExprResult::Float(_f)) => {
writeln!(w, "{} equ #{:04X}", k.value(), e.int().unwrap())
}
Value::Expr(ExprResult::String(s)) => {
writeln!(w, "{} equ {}", k.value(), s)
}
Value::Expr(l @ ExprResult::List(_)) => {
writeln!(w, "{} equ {}", k.value(), l)
}
Value::Expr(m @ ExprResult::Matrix { .. }) => {
writeln!(w, "{} equ {}", k.value(), m)
}
_ => unimplemented!("{:?}", v)
}?;
}
Ok(())
}
pub fn keep_symbol(&self, sym: &Symbol) -> bool {
assert!(self.all_allowed ^ self.all_forbidden);
if sym.value() == "$" {
return false;
}
if sym.value() == "$$" {
return false;
}
else if self.all_allowed {
!Self::is_included(&self.forbidden, sym)
}
else
{
Self::is_included(&self.allowed, sym)
}
}
fn is_included(list: &[Symbol], sym: &Symbol) -> bool {
list.iter()
.find(|s2| {
if **s2 == *sym {
return true;
}
sym.value().starts_with(&format!("{}.", s2.value()))
})
.is_some()
}
pub fn forbid_all_symbols(&mut self) {
self.forbidden.clear();
self.all_forbidden = true;
self.all_allowed = false;
}
pub fn allow_all_symbols(&mut self) {
self.allowed.clear();
self.all_allowed = true;
self.all_forbidden = false;
}
pub fn forbid_symbol<S: Into<Symbol>>(&mut self, s: S) {
self.forbidden.push(s.into());
}
pub fn allow_symbol<S: Into<Symbol>>(&mut self, s: S) {
self.allowed.push(s.into());
}
}