From a7c60c32bd2708f6f0867e5f502e91b72fa3b671 Mon Sep 17 00:00:00 2001 From: Benjamin Bolte Date: Wed, 30 Oct 2024 23:27:36 -0700 Subject: [PATCH] v0.0.1 --- klang/src/parser/mod.rs | 23 +++++++++++++++++++++-- klang/src/parser/passes/mod.rs | 21 ++++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/klang/src/parser/mod.rs b/klang/src/parser/mod.rs index 9ea9e63..000b8ee 100644 --- a/klang/src/parser/mod.rs +++ b/klang/src/parser/mod.rs @@ -60,8 +60,27 @@ pub fn write_program_to_file( )) })?; } else { - let program_str = format!("{:#?}", program.ast_program); - fs::write(file_path, &program_str).map_err(|e| { + let mut output = String::new(); + + fn render_command(cmd: &ast::Command, indent: usize) -> String { + let mut result = format!("{:indent$}{}", "", cmd.text, indent = indent); + if !cmd.children.is_empty() { + result.push_str(" {\n"); + for child in &cmd.children { + result.push_str(&render_command(child, indent + 2)); + } + result.push_str(&format!("{:indent$}}}\n", "", indent = indent)); + } else { + result.push('\n'); + } + result + } + + for command in &program.ast_program.commands { + output.push_str(&render_command(command, 0)); + } + + fs::write(file_path, &output).map_err(|e| { ParseError::new(format!( "Error writing program to file '{}': {}", file_path.display(), diff --git a/klang/src/parser/passes/mod.rs b/klang/src/parser/passes/mod.rs index 2619062..8e1d06f 100644 --- a/klang/src/parser/passes/mod.rs +++ b/klang/src/parser/passes/mod.rs @@ -106,7 +106,12 @@ fn process_line_with_args( ))); } call_stack.push(func_sig.clone()); - let mut commands = Vec::new(); + + // Create parent command with function name + let function_text = substitute_text_with_args(name, arg_map)?; + + // Process child commands + let mut children = Vec::new(); for inner_line in &func_def.lines { let mut cmds = process_line_with_args( inner_line, @@ -114,10 +119,16 @@ fn process_line_with_args( call_stack, &new_arg_map, )?; - commands.append(&mut cmds); + children.append(&mut cmds); } + call_stack.pop(); - return Ok(commands); + + // Return single command with children + return Ok(vec![AstCommand { + text: function_text, + children, + }]); } } } @@ -152,7 +163,7 @@ fn process_command_with_args( let text = substitute_text_with_args(text, arg_map)?; Ok(AstCommand { text, - children: Vec::new(), // Removed children handling since field doesn't exist + children: Vec::new(), }) } else { Err(ParseError::new("Command without text".to_string())) @@ -179,7 +190,7 @@ fn substitute_text_with_args( if let Some(value) = arg_map.get(&arg.text) { result.push_str(value); } else { - return Err(ParseError::new(format!("Argument not found: {}", arg.text))); + result.push_str(&arg.text); } } None => {}