mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-26 11:06:00 +03:00
Add leo_typed_ast program, has one serialization error
This commit is contained in:
parent
0d5a15cd41
commit
1ea5f7de4a
5
Cargo.lock
generated
5
Cargo.lock
generated
@ -603,7 +603,7 @@ dependencies = [
|
||||
"leo-ast",
|
||||
"leo-gadgets",
|
||||
"leo-inputs",
|
||||
"leo-types",
|
||||
"leo-typed",
|
||||
"log",
|
||||
"num-bigint",
|
||||
"pest",
|
||||
@ -649,13 +649,14 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "leo-types"
|
||||
name = "leo-typed"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"leo-ast",
|
||||
"leo-inputs",
|
||||
"pest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"snarkos-errors",
|
||||
"snarkos-models",
|
||||
]
|
||||
|
@ -8,7 +8,7 @@ edition = "2018"
|
||||
leo-ast = { path = "../ast", version = "0.1.0" }
|
||||
leo-gadgets = { path = "../gadgets", version = "0.1.0" }
|
||||
leo-inputs = { path = "../leo-inputs", version = "0.1.0" }
|
||||
leo-types = { path = "../typed", version = "0.1.0" }
|
||||
leo-typed = { path = "../typed", version = "0.1.0" }
|
||||
|
||||
snarkos-curves = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-curves", default-features = false }
|
||||
snarkos-dpc = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-dpc", default-features = false }
|
||||
|
@ -1,9 +1,13 @@
|
||||
[package]
|
||||
name = "leo-types"
|
||||
name = "leo-typed"
|
||||
version = "0.1.0"
|
||||
authors = ["The Aleo Team <hello@aleo.org>"]
|
||||
edition = "2018"
|
||||
|
||||
[[bin]]
|
||||
name = "leo_typed_ast"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
leo-ast = { path = "../ast", version = "0.1.0" }
|
||||
leo-inputs = { path = "../leo-inputs", version = "0.1.0" }
|
||||
@ -13,3 +17,4 @@ snarkos-models = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "
|
||||
|
||||
pest = { version = "2.0" }
|
||||
serde = { version = "1.0" }
|
||||
serde_json = { version = "1.0" }
|
||||
|
@ -36,6 +36,8 @@ pub use self::types::*;
|
||||
|
||||
use leo_ast::LeoAst;
|
||||
|
||||
use serde_json;
|
||||
|
||||
pub struct LeoTypedAst {
|
||||
typed_ast: Program,
|
||||
}
|
||||
@ -48,7 +50,13 @@ impl LeoTypedAst {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner typed syntax tree representation.
|
||||
pub fn into_repr(self) -> Program {
|
||||
self.typed_ast
|
||||
}
|
||||
|
||||
/// Serializes the abstract syntax tree into a JSON string.
|
||||
pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
|
||||
Ok(serde_json::to_string_pretty(&self.typed_ast)?)
|
||||
}
|
||||
}
|
||||
|
56
typed/src/main.rs
Normal file
56
typed/src/main.rs
Normal file
@ -0,0 +1,56 @@
|
||||
use leo_ast::{LeoAst, ParserError};
|
||||
use leo_typed::LeoTypedAst;
|
||||
use std::{env, fs, path::Path};
|
||||
|
||||
fn to_leo_typed_tree(filepath: &Path) -> Result<String, ParserError> {
|
||||
// Loads the Leo code as a string from the given file path.
|
||||
let program_filepath = filepath.to_path_buf();
|
||||
let program_string = LeoAst::load_file(&program_filepath)?;
|
||||
|
||||
// Parses the Leo file and constructs an abstract syntax tree.
|
||||
let ast = LeoAst::new(&program_filepath, &program_string)?;
|
||||
|
||||
// Parse the abstract syntax tree and constructs a typed syntax tree.
|
||||
let typed_ast = LeoTypedAst::new("leo_typed_tree", &ast);
|
||||
|
||||
// Serializes the typed syntax tree into JSON format.
|
||||
let serialized_typed_tree = LeoTypedAst::to_json_string(&typed_ast)?;
|
||||
|
||||
Ok(serialized_typed_tree)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), ParserError> {
|
||||
// Parse the command-line arguments as strings.
|
||||
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 {
|
||||
eprintln!("Warning - an invalid number of command-line arguments were provided.");
|
||||
println!(
|
||||
"\nCommand-line usage:\n\n\tleo_typed_ast {{PATH/TO/INPUT_FILENAME}}.leo {{PATH/TO/OUTPUT_DIRECTORY (optional)}}\n"
|
||||
);
|
||||
return Ok(()); // Exit innocently
|
||||
}
|
||||
|
||||
// Construct the input filepath.
|
||||
let input_filepath = Path::new(&cli_arguments[1]);
|
||||
|
||||
// Construct the serialized typed syntax tree.
|
||||
let serialized_typed_tree = to_leo_typed_tree(&input_filepath)?;
|
||||
println!("{}", serialized_typed_tree);
|
||||
|
||||
// Determine the output directory.
|
||||
let output_directory = match cli_arguments.len() == 3 {
|
||||
true => format!(
|
||||
"{}/{}.json",
|
||||
cli_arguments[2],
|
||||
input_filepath.file_stem().unwrap().to_str().unwrap()
|
||||
),
|
||||
false => format!("./{}.json", input_filepath.file_stem().unwrap().to_str().unwrap()),
|
||||
};
|
||||
|
||||
// Write the serialized abstract syntax tree to the output directory.
|
||||
fs::write(Path::new(&output_directory), serialized_typed_tree)?;
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue
Block a user