added additional argument to examples

This commit is contained in:
damirka 2022-02-21 21:52:12 +03:00
parent 7a29c8ba6d
commit e439a2fdff
2 changed files with 16 additions and 16 deletions

View File

@ -19,24 +19,23 @@ use leo_span::symbol::create_session_if_not_set_then;
use std::{env, fs, path::Path};
fn to_leo_tree(filepath: &Path) -> Result<String, String> {
// Loads the Leo code as a string from the given file path.
let program_filepath = filepath.to_path_buf();
let program_string = fs::read_to_string(&program_filepath).expect("failed to open input file");
fn to_leo_tree(input_filepath: &Path, state_filepath: &Path) -> Result<String, String> {
// Loads the inputs a string from the given file path.
let input_string = fs::read_to_string(&input_filepath.to_path_buf()).expect("failed to open an input file");
let state_string = fs::read_to_string(&state_filepath.to_path_buf()).expect("failed to open a state file");
// Parses the Leo file constructing an ast which is then serialized.
create_session_if_not_set_then(|_| {
Handler::with(|handler| {
let input = leo_parser::parse_program_input(
let input = leo_parser::parse_program_inputs(
&handler,
program_string.clone(),
filepath.to_str().unwrap(),
program_string,
filepath.to_str().unwrap(),
input_string.clone(),
input_filepath.to_str().unwrap(),
state_string,
state_filepath.to_str().unwrap(),
)?;
let json = input.to_json_string()?;
println!("{}", json);
Ok(json)
}).map_err(|e| e.to_string())
})
@ -47,23 +46,24 @@ fn main() -> Result<(), String> {
let cli_arguments = env::args().collect::<Vec<String>>();
// Check that the correct number of command-line arguments were passed in.
if cli_arguments.len() < 2 || cli_arguments.len() > 3 {
if cli_arguments.len() < 3 || cli_arguments.len() > 4 {
eprintln!("Warning - an invalid number of command-line arguments were provided.");
println!(
"\nCommand-line usage:\n\n\tleo_ast {{PATH/TO/INPUT_FILENAME}}.in {{PATH/TO/OUTPUT_DIRECTORY (optional)}}\n"
"\nCommand-line usage:\n\n\tleo_ast {{PATH/TO/INPUT_FILENAME}}.in {{PATH/TO/STATE_FILENAME}}.in {{PATH/TO/OUTPUT_DIRECTORY (optional)}}\n"
);
return Ok(()); // Exit innocently
}
// Construct the input filepath.
let input_filepath = Path::new(&cli_arguments[1]);
let state_filepath = Path::new(&cli_arguments[2]);
// Construct the serialized syntax tree.
let serialized_leo_tree = to_leo_tree(input_filepath)?;
let serialized_leo_tree = to_leo_tree(input_filepath, state_filepath)?;
println!("{}", serialized_leo_tree);
// Determine the output directory.
let output_directory = match cli_arguments.len() == 3 {
let output_directory = match cli_arguments.len() == 4 {
true => format!(
"{}/{}.json",
cli_arguments[2],

View File

@ -42,8 +42,8 @@ pub fn parse_ast<T: AsRef<str>, Y: AsRef<str>>(handler: &Handler, path: T, sourc
Ok(Ast::new(parser::parse(handler, path.as_ref(), source.as_ref())?))
}
/// Parses program input from from the input file path and state file path
pub fn parse_program_input<T: AsRef<str>, Y: AsRef<str>, T2: AsRef<str>, Y2: AsRef<str>>(
/// Parses program inputs from from the input file path and state file path
pub fn parse_program_inputs<T: AsRef<str>, Y: AsRef<str>, T2: AsRef<str>, Y2: AsRef<str>>(
handler: &Handler,
input_string: T,
input_path: Y,