Skip to content

Commit 8fa22be

Browse files
committed
Tweaks
1 parent d0e7f82 commit 8fa22be

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,12 @@ expr-solver -e "2+3*4" -o expr.bin
137137
# Execute compiled binary
138138
expr-solver -i expr.bin
139139

140-
# View assembly
140+
# View assembly from expression or file
141141
expr-solver -e "2+3" -a
142+
expr-solver -i expr.bin -a
143+
144+
# Recompile bytecode (e.g., version migration)
145+
expr-solver -i old.bin -o new.bin
142146

143147
# List available functions and constants
144148
expr-solver -t

bin/src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct Args {
3636
assembly: bool,
3737
}
3838

39+
/// Parses a key=value pair for custom constant definitions.
3940
fn parse_key_val(s: &str) -> Result<(String, f64), Box<dyn std::error::Error + Send + Sync>> {
4041
let pos = s
4142
.find('=')
@@ -49,6 +50,7 @@ fn main() {
4950
}
5051
}
5152

53+
/// Main execution logic: parses arguments, loads program, and executes requested action.
5254
fn run() -> Result<(), String> {
5355
let args = Args::parse();
5456

@@ -63,19 +65,16 @@ fn run() -> Result<(), String> {
6365

6466
// Load input from either expression or file
6567
let program = if let Some(expr) = args.expression.as_ref().or(args.expr.as_ref()) {
66-
Program::new_from_source(expr)
67-
.map_err(|err| err.to_string())?
68-
.link(table)
69-
.map_err(|err| err.to_string())?
68+
Program::new_from_source(expr).map_err(|err| err.to_string())?
7069
} else if let Some(file) = &args.input {
71-
Program::new_from_file(file.to_string_lossy().as_ref())
72-
.map_err(|err| err.to_string())?
73-
.link(table)
74-
.map_err(|err| err.to_string())?
70+
Program::new_from_file(file.to_string_lossy().as_ref()).map_err(|err| err.to_string())?
7571
} else {
7672
return Err("no input".to_string());
7773
};
7874

75+
// Link the program with the symbol table
76+
let program = program.link(table).map_err(|err| err.to_string())?;
77+
7978
// Act on the loaded program
8079
if args.assembly {
8180
print!("{}", program.get_assembly());
@@ -91,6 +90,7 @@ fn run() -> Result<(), String> {
9190
Ok(())
9291
}
9392

93+
/// Creates a symbol table with standard library and user-defined constants.
9494
fn create_symbol_table(defines: &[(String, f64)]) -> Result<SymTable, String> {
9595
let mut table = SymTable::stdlib();
9696

@@ -114,6 +114,7 @@ fn create_symbol_table(defines: &[(String, f64)]) -> Result<SymTable, String> {
114114
Ok(table)
115115
}
116116

117+
/// Prints all available constants and functions in the symbol table.
117118
fn list_symbol_table(table: &SymTable) {
118119
println!("Available constants:");
119120
for symbol in table.symbols() {

0 commit comments

Comments
 (0)