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
172
173
174
175
176
177
178
|
mod compiler;
mod environment;
mod parsers;
mod tokens;
pub use compiler::*;
pub use environment::*;
pub use parsers::*;
pub use tokens::*;
pub use assembler::*;
use log::{info, fatal};
use switchboard::{Switchboard, SwitchQuery};
use std::io::{Read, Write};
fn print_version() -> ! {
let version = env!("CARGO_PKG_VERSION");
eprintln!("torque assembler, version {version}");
eprintln!("written by ben bridle");
std::process::exit(0);
}
fn main() {
let mut args = Switchboard::from_env();
if args.named("version").as_bool() {
print_version();
}
if args.named("verbose").short('v').as_bool() {
log::set_log_level(log::LogLevel::Info);
}
let source_path = args.positional("source").as_path_opt().map(
|p| p.canonicalize().unwrap_or_else(|e| fatal!("{p:?}: {e:?}")));
let destination_path = args.positional("destination").as_path_opt();
let extension = args.named("ext").default("tq").as_string();
let no_libs = args.named("no-libs").as_bool();
let no_project_libs = args.named("no-project-libs").as_bool();
let no_environment_libs = args.named("no-env-libs").as_bool();
let print_tree = args.named("tree").as_bool();
let dry_run = args.named("dry-run").short('n').as_bool();
let only_resolve = args.named("resolve").as_bool();
let _export_symbols = args.named("symbols").as_bool();
// -----------------------------------------------------------------------
let mut compiler = if let Some(path) = &source_path {
info!("Reading program source from {path:?}");
Compiler::from_path(path).unwrap_or_else(|err| match err {
FileError::InvalidExtension => fatal!(
"File {path:?} has invalid extension, must be '.{extension}'"),
FileError::NotFound => fatal!(
"File {path:?} was not found"),
FileError::InvalidUtf8 => fatal!(
"File {path:?} does not contain valid UTF-8 text"),
FileError::NotReadable => fatal!(
"File {path:?} is not readable"),
FileError::IsADirectory => fatal!(
"File {path:?} is a directory"),
FileError::Unknown => fatal!(
"Unknown error while attempting to read from {path:?}")
})
} else {
let mut source_code = String::new();
info!("Reading program source from standard input");
if let Err(err) = std::io::stdin().read_to_string(&mut source_code) {
fatal!("Could not read from standard input\n{err:?}");
}
Compiler::from_string(source_code, "<standard input>")
};
if compiler.error().is_some() && !no_libs && !no_project_libs {
compiler.include_libs_from_parent(&extension);
}
if compiler.error().is_some() && !no_libs && !no_environment_libs {
compiler.include_libs_from_path_variable("TORQUE_LIBS", &extension);
}
if print_tree {
compiler.resolver.hierarchy().report()
}
if let Some(error) = compiler.error() {
error.report();
std::process::exit(1);
}
let merged_source = compiler.get_compiled_source().unwrap_or_else(
|error| { error.report(); std::process::exit(1); }
);
if only_resolve && !dry_run {
write_bytes_and_exit(merged_source.as_bytes(), destination_path.as_ref());
}
// -----------------------------------------------------------------------
// // TODO: Remove this block
// let code = &compiler.resolver.source_units[0].source_unit.main.source_code;
// let parser = SyntacticParser::from_source_code(code, Some("<main>"));
// println!();
// for t in parser {
// println!("{t:?}");
// }
// Parse syntactic tokens from merged source code.
let path = Some("<merged source>");
let parser = SyntacticParser::from_source_code(&merged_source, path);
let syntactic_tokens: Vec<_> = parser.collect();
report_syntactic_errors(&syntactic_tokens, &merged_source);
// let mut semantic_parser = SemanticParser::new(syntactic_tokens);
// semantic_parser.parse();
}
fn write_bytes_and_exit<P: AsRef<Path>>(bytes: &[u8], path: Option<&P>) -> ! {
match path {
Some(path) => match std::fs::write(path, bytes) {
Ok(_) => info!("Wrote output to path {:?}", path.as_ref()),
Err(err) => fatal!("Could not write to path {:?}\n{err:?}", path.as_ref()),
}
None => match std::io::stdout().write_all(bytes) {
Ok(_) => info!("Wrote output to standard output"),
Err(err) => fatal!("Could not write to standard output\n{err:?}"),
}
}
std::process::exit(0);
}
fn report_syntactic_errors(syntactic_tokens: &[SyntacticToken], source_code: &str) {
for token in syntactic_tokens {
let context = Context { source_code: &source_code, source: &token.source };
match &token.variant {
SyntacticTokenVariant::ConstantExpression(expr) => for t in &expr.tokens {
let context = Context { source_code: &source_code, source: &t.source };
if let ConstantExpressionTokenVariant::Error(err) = &t.variant {
let ConstantExpressionParseError::InvalidHexadecimalLiteral(hex) = err;
let message = format!("Invalid hexadecimal literal {hex:?} in constant expression");
report_source_issue(LogLevel::Error, &context, &message);
}
}
SyntacticTokenVariant::PackedBinaryLiteral(pbl) => for e in &pbl.errors {
let context = Context { source_code: &source_code, source: &e.source };
match e.variant {
PackedBinaryLiteralParseErrorVariant::DuplicateFieldName(name) => {
let message = format!("Duplicate field name {name:?} in packed binary literal");
report_source_issue(LogLevel::Error, &context, &message);
}
PackedBinaryLiteralParseErrorVariant::InvalidCharacter(c) => {
let message = format!("Invalid character {c:?} in packed binary literal");
report_source_issue(LogLevel::Error, &context, &message);
}
}
}
SyntacticTokenVariant::Error(err) => match err {
SyntacticParseError::InvalidHexadecimalLiteral(hex) => {
let message = format!("Invalid hexadecimal literal {hex:?}");
report_source_issue(LogLevel::Error, &context, &message);
}
SyntacticParseError::InvalidSymbolIdentifier(name) => {
let message = format!("Invalid identifier {name:?}");
report_source_issue(LogLevel::Error, &context, &message);
}
SyntacticParseError::UnterminatedComment => {
let message = format!("Unterminated comment");
report_source_issue(LogLevel::Error, &context, &message);
}
SyntacticParseError::UnterminatedConstantExpression => {
let message = format!("Unterminated constant expression");
report_source_issue(LogLevel::Error, &context, &message);
}
}
_ => (),
}
}
}
|