From c6ed9cb2860f1b394e6b59490f1c66f20aea943e Mon Sep 17 00:00:00 2001 From: "Troels F. Roennow" Date: Thu, 19 Oct 2023 12:27:12 +0100 Subject: [PATCH] Reverting CLI main changes --- products/bluebell/cli/src/main.rs | 36 +++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/products/bluebell/cli/src/main.rs b/products/bluebell/cli/src/main.rs index 9c7fabc3d..51178dce8 100644 --- a/products/bluebell/cli/src/main.rs +++ b/products/bluebell/cli/src/main.rs @@ -1,3 +1,4 @@ +// Importing necessary modules and types use bluebell::support::modules::BluebellModule; use bluebell::support::modules::ScillaDebugBuiltins; use std::ffi::CStr; @@ -30,48 +31,58 @@ use bluebell::parser::{parser, ParserError}; use evm_assembly::types::EvmTypeValue; use log::{Log, Metadata, Record}; +// Logger struct to capture logs struct CaptureLogger {} +// Implementation of logger impl CaptureLogger { + // Constructor for CaptureLogger fn new() -> Self { Self {} } } +// Implementing Log trait for CaptureLogger impl Log for CaptureLogger { + // Method to check if logging is enabled fn enabled(&self, _metadata: &Metadata) -> bool { // self.delegate.enabled(metadata) true } + // Method to log a record fn log(&self, record: &Record) { if self.enabled(record.metadata()) { print!("{}", record.args().to_string()); } } + // Method to flush the logger fn flush(&self) {} } -// Later, you'd set the logger as: +// Function to setup the logger fn setup_logger() { let logger = Box::new(CaptureLogger::new()); log::set_boxed_logger(logger).unwrap(); log::set_max_level(log::LevelFilter::Info); } +// Enum to define the output format of Bluebell #[derive(Clone, Debug, Subcommand)] enum BluebellOutputFormat { LlvmIr, FormattedScilla, } +// Enum to define the backend of Bluebell #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] enum BluebellBackend { Llvm, Evm, } +// Enum to define the command of Bluebell #[derive(Clone, Debug, Subcommand)] enum BluebellCommand { Emit { @@ -98,7 +109,7 @@ enum BluebellCommand { }, } -/// Scilla compiler and executor +// Struct to hold the arguments for Scilla compiler and executor #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] struct Args { @@ -118,7 +129,9 @@ struct Args { mode: BluebellCommand, } +// Implementation of Args struct impl Args { + // Method to get the features fn features(&self) -> Vec { match &self.features_raw { Some(v) => v.split(",").map(|s| s.to_string()).collect(), @@ -127,6 +140,7 @@ impl Args { } } +// Function to run Bluebell with EVM backend fn bluebell_evm_run( ast: &NodeProgram, entry_point: String, @@ -170,6 +184,7 @@ fn bluebell_evm_run( executable.execute(&entry_point, arguments); } +// Function to run Bluebell with LLVM backend fn bluebell_llvm_run(ast: &NodeProgram, entry_point: String, debug: bool) { // DEPRECATED panic!("LLVM support is DEPRECATED for now."); @@ -290,31 +305,43 @@ fn bluebell_llvm_run(ast: &NodeProgram, entry_point: String, debug: bool) { */ } +// Main function fn main() { + // Setting up the logger setup_logger(); + // Parsing the arguments let args = Args::parse(); + // Getting the features let features = args.features(); // Accessing the values let mut errors: Vec = [].to_vec(); + // Opening the file let mut file = File::open(args.filename).expect("Unable to open file"); let mut script = String::new(); + // Reading the file file.read_to_string(&mut script) .expect("Unable to read file"); + // Creating a new lexer let lexer = Lexer::new(&script); + // Creating a new parser let parser = parser::ProgramParser::new(); + // Parsing the script match parser.parse(&mut errors, lexer) { Ok(ast) => { + // Running the appropriate command based on the mode match args.mode { BluebellCommand::Run { entry_point, args: arguments, backend, } => match backend { + // Running with LLVM backend BluebellBackend::Llvm => bluebell_llvm_run(&ast, entry_point, args.debug), + // Running with EVM backend BluebellBackend::Evm => { bluebell_evm_run(&ast, entry_point, arguments, features, args.debug) } @@ -334,6 +361,7 @@ fn main() { */ } Err(error) => { + // Handling syntax errors let message = format!("Syntax error {:?}", error); let mut pos: Vec = [].to_vec(); error.map_location(|l| { @@ -375,6 +403,7 @@ fn main() { line_end = script.len(); } + // Printing the line with the error let line = &script[line_start..line_end]; println!("Line {},{}:{}", line_counter, char_counter, line); print!( @@ -385,13 +414,16 @@ fn main() { println!("{}", "^".repeat(pos[1].position - pos[0].position)); } + // Creating a new ParserError let my_error = ParserError { message, line: 0, //error.location_line(), column: 0, // err.location_column(), }; + // Printing the error println!("{}", my_error); + // Exiting the process with an error code process::exit(-1); } }