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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
use crate::*;
/// The entire semantic program, ready to generate bytecode.
pub struct Program {
pub definitions: Vec<Definition>,
pub invocations: Vec<Invocation>,
pub errors: Vec<ParseError>,
}
/// A symbol definition.
pub struct Definition {
pub name: String,
pub source: SourceSpan,
pub arguments: Vec<ArgumentDefinition>,
pub variant: DefinitionVariant,
}
pub struct ArgumentDefinition {
pub name: String,
pub source: SourceSpan,
pub variant: ArgumentDefinitionVariant,
}
pub enum ArgumentDefinitionVariant {
Integer,
Block,
}
pub enum DefinitionVariant {
Integer(IntegerDefinition),
Block(BlockDefinition),
Reference(ReferenceDefinition),
}
pub struct IntegerDefinition {
pub source: SourceSpan,
pub variant: IntegerDefinitionVariant,
}
pub enum IntegerDefinitionVariant {
Literal(usize),
Constant(ConstantExpression),
}
pub struct BlockDefinition {
pub tokens: Vec<BlockToken>,
pub errors: Vec<ParseError>,
}
pub struct BlockToken {
pub source: SourceSpan,
pub variant: BlockTokenVariant,
}
pub enum BlockTokenVariant {
Invocation(Invocation),
Comment(String),
Word(PackedBinaryLiteral),
}
/// References aren't necessarily an integer or a block
pub struct ReferenceDefinition {
pub source: SourceSpan,
pub name: String,
}
pub struct Invocation {
pub name: String,
pub arguments: Vec<DefinitionVariant>,
pub errors: Vec<ParseError>,
}
pub struct ParseError {
pub source: SourceSpan,
pub variant: ParseErrorVariant,
}
pub enum ParseErrorVariant {
UnterminatedMacroDefinition(String),
UnterminatedBlockDefinition,
/// Name of the macro.
InvalidArgumentDefinition(String),
InvalidToken,
}
// ------------------------------------------------------------------------ //
macro_rules! indent {
($indent:expr => $($tokens:tt)*) => {{
for _ in 0..$indent { print!(" "); }
println!($($tokens)*);
}};
}
impl Program {
pub fn print_definitions(&self) {
for definition in &self.definitions {
let variant = match &definition.variant {
DefinitionVariant::Integer(_) => "INTEGER",
DefinitionVariant::Block(_) => "BLOCK",
DefinitionVariant::Reference(_) => "REFERENCE",
};
println!("DEFINE {variant} '{}'", definition.name);
for argument in &definition.arguments {
self.print_argument_definition(argument);
}
match &definition.variant {
DefinitionVariant::Integer(integer) =>
self.print_integer_definition(1, integer),
DefinitionVariant::Block(block) =>
self.print_block_definition(1, block),
DefinitionVariant::Reference(reference) =>
indent!(1 => "REFERENCE '{}'", reference.name),
};
println!();
}
for invocation in &self.invocations {
self.print_invocation(0, invocation);
}
}
fn print_argument_definition(&self, argument: &ArgumentDefinition) {
let variant = match argument.variant {
ArgumentDefinitionVariant::Integer => "INTEGER",
ArgumentDefinitionVariant::Block => "BLOCK",
};
println!(" ARGUMENT {variant} '{}'", argument.name);
}
fn print_integer_definition(&self, indent: usize, definition: &IntegerDefinition) {
match &definition.variant {
IntegerDefinitionVariant::Literal(value) =>
indent!(indent => "LITERAL {value}"),
IntegerDefinitionVariant::Constant(expr) =>
indent!(indent => "CONSTANT [{expr:?}]"),
}
}
fn print_block_definition(&self, indent: usize, definition: &BlockDefinition) {
indent!(indent => "BLOCK");
let indent = indent + 1;
for token in &definition.tokens {
match &token.variant {
BlockTokenVariant::Invocation(invocation) =>
self.print_invocation(indent, invocation),
BlockTokenVariant::Comment(_) =>
indent!(indent => "COMMENT"),
BlockTokenVariant::Word(word) =>
indent!(indent => "WORD #{word}"),
}
}
}
fn print_invocation(&self, indent: usize, invocation: &Invocation) {
indent!(indent => "INVOCATION '{}'", invocation.name);
let indent = indent + 1;
for argument in &invocation.arguments {
match &argument {
DefinitionVariant::Integer(integer) =>
self.print_integer_definition(indent, integer),
DefinitionVariant::Block(block) =>
self.print_block_definition(indent, block),
DefinitionVariant::Reference(reference) =>
indent!(indent => "REFERENCE '{}'", reference.name),
};
}
}
}
|