@@ -36,6 +36,7 @@ struct Args {
3636 assembly : bool ,
3737}
3838
39+ /// Parses a key=value pair for custom constant definitions.
3940fn 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.
5254fn 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.
9494fn 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.
117118fn list_symbol_table ( table : & SymTable ) {
118119 println ! ( "Available constants:" ) ;
119120 for symbol in table. symbols ( ) {
0 commit comments