Skip to content

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Oct 31, 2024
1 parent 6730c05 commit a7c60c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
23 changes: 21 additions & 2 deletions klang/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
21 changes: 16 additions & 5 deletions klang/src/parser/passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,29 @@ 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,
functions,
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,
}]);
}
}
}
Expand Down Expand Up @@ -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()))
Expand All @@ -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 => {}
Expand Down

0 comments on commit a7c60c3

Please sign in to comment.