-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmain.rs
More file actions
180 lines (163 loc) · 5.72 KB
/
main.rs
File metadata and controls
180 lines (163 loc) · 5.72 KB
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
179
180
// Simplicity "Human-Readable" Language
// Written in 2023 by
// Andrew Poelstra <simplicity@wpsoftware.net>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//
use simplicity::human_encoding::Forest;
use simplicity::jet::JetEnvironment;
use simplicity::node::CommitNode;
use simplicity::{self, BitIter};
use simplicity::base64::engine::general_purpose::STANDARD;
use std::str::FromStr;
use std::{env, fs};
/// What set of jets to use in the program.
// FIXME this should probably be configurable.
type DefaultJetEnv = simplicity::jet::ElementsTxEnv;
type DefaultJet = <DefaultJetEnv as JetEnvironment>::Jet;
fn usage(process_name: &str) {
eprintln!("Usage:");
eprintln!(" {} assemble <filename>", process_name);
eprintln!(" {} disassemble <base64>", process_name);
eprintln!(" {} graph <base64>", process_name);
eprintln!(" {} relabel <base64>", process_name);
eprintln!();
eprintln!("For commands which take an optional expression, the default value is \"main\".");
eprintln!();
eprintln!("Run `{} help` to display this message.", process_name);
}
fn invalid_usage(process_name: &str) -> Result<(), String> {
usage(process_name);
Err("invalid usage".into())
}
enum Command {
Assemble,
Disassemble,
Graph,
Relabel,
Help,
}
impl FromStr for Command {
type Err = String;
fn from_str(s: &str) -> Result<Self, String> {
match s {
"assemble" => Ok(Command::Assemble),
"disassemble" => Ok(Command::Disassemble),
"graphviz" | "dot" | "graph" => Ok(Command::Graph),
"relabel" => Ok(Command::Relabel),
"help" => Ok(Command::Help),
x => Err(format!("unknown command {}", x)),
}
}
}
impl Command {
fn takes_optional_exprname(&self) -> bool {
match *self {
Command::Assemble => false,
Command::Disassemble => false,
Command::Graph => false,
Command::Relabel => false,
Command::Help => false,
}
}
}
fn parse_file(name: &str) -> Result<Forest<DefaultJet>, String> {
let s = fs::read_to_string(name).map_err(|e| format!("failed to read file {}: {}", name, e))?;
match Forest::parse::<DefaultJetEnv>(&s) {
Ok(prog) => Ok(prog),
Err(mut errs) => {
errs.add_context(std::sync::Arc::from(s));
eprintln!("Errors:");
eprintln!("{}", errs);
eprintln!();
Err(format!("failed to parse file {}", name))
}
}
}
fn main() -> Result<(), String> {
let mut args = env::args();
let process_name = args.next().unwrap();
let process_name = match process_name.rfind('/') {
Some(idx) => &process_name[idx + 1..],
None => &process_name[..],
};
// Parse command-line args into (command, first_arg, expression)
let command = match args.next() {
Some(cmd) => match Command::from_str(&cmd) {
Ok(cmd) => cmd,
Err(e) => {
eprintln!("Error: {}.", e);
eprintln!();
return invalid_usage(process_name);
}
},
None => return invalid_usage(process_name),
};
if let Command::Help = command {
usage(process_name);
return Ok(());
}
let first_arg = match args.next() {
Some(s) => s,
None => return invalid_usage(process_name),
};
let _expression = if command.takes_optional_exprname() {
args.next().unwrap_or("main".to_owned())
} else {
String::new()
};
if args.next().is_some() {
invalid_usage(process_name)?;
}
// Execute command
match command {
Command::Assemble => {
let prog = parse_file(&first_arg)?;
let roots = prog.roots();
let mut error = false;
for name in roots.keys() {
if name.as_ref() != "main" {
eprintln!("Expression `{}` not rooted at `main`.", name);
error = true;
}
}
if let Some(prog) = roots.get("main") {
if !error {
println!("{}", prog);
}
} else {
eprintln!("Expression `main` not found.");
}
}
Command::Disassemble => {
let v = simplicity::base64::Engine::decode(&STANDARD, first_arg.as_bytes())
.map_err(|e| format!("failed to parse base64: {}", e))?;
let iter = BitIter::from(v.into_iter());
let commit = CommitNode::decode::<_, DefaultJetEnv>(iter)
.map_err(|e| format!("failed to decode program: {}", e))?;
let prog = Forest::<DefaultJet>::from_program(commit);
println!("{}", prog.string_serialize());
}
Command::Graph => {
let v = simplicity::base64::Engine::decode(&STANDARD, first_arg.as_bytes())
.map_err(|e| format!("failed to parse base64: {}", e))?;
let iter = BitIter::from(v.into_iter());
let commit = CommitNode::decode::<_, DefaultJetEnv>(iter)
.map_err(|e| format!("failed to decode program: {}", e))?;
println!("{}", commit.display_as_dot());
}
Command::Relabel => {
let prog = parse_file(&first_arg)?;
println!("{}", prog.string_serialize());
}
Command::Help => unreachable!(),
}
Ok(())
}