From 437b8e3eb224881ee727fde25f0b3cd9fa4ba25c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20MARTIN?= <maelle.martin@proton.me> Date: Thu, 5 Sep 2024 10:21:56 +0200 Subject: [PATCH] SCRIPT: Fix the way of getting the name of a module --- src/Rust/vvs_cli/src/args.rs | 2 +- src/Rust/vvs_cli/src/main.rs | 13 ++++---- src/Rust/vvs_lang/src/frontend_pipeline.rs | 6 ++-- src/Rust/vvs_lang/src/lib.rs | 36 ++++++++++++++++++---- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/Rust/vvs_cli/src/args.rs b/src/Rust/vvs_cli/src/args.rs index 08e8ea60..5f74ba2f 100644 --- a/src/Rust/vvs_cli/src/args.rs +++ b/src/Rust/vvs_cli/src/args.rs @@ -21,7 +21,7 @@ pub struct Args { /// touch or call fields from vivy that begins by underscores. #[arg( action = clap::ArgAction::Set , id = "script.vvs" - , value_parser = FileTypeValueParser::new("vvs") + , value_parser = FileTypeValueParser::new(vvs_lang::VIVY_SCRIPT_EXTENSION) )] pub script: Option<PathBuf>, diff --git a/src/Rust/vvs_cli/src/main.rs b/src/Rust/vvs_cli/src/main.rs index 6898acb3..c8030eca 100644 --- a/src/Rust/vvs_cli/src/main.rs +++ b/src/Rust/vvs_cli/src/main.rs @@ -2,7 +2,7 @@ //! The VivyScript cli -use anyhow::{Context as _, Result}; +use anyhow::{Context, Result}; use clap::{CommandFactory as _, Parser as _}; use std::{fs, io, path::Path}; use vvs_cli::{ @@ -52,15 +52,16 @@ fn main() -> Result<()> { } // Parse, build, execute the script. - let Some(script) = script else { - return Ok(Args::command().print_help()?); + let script = match script { + Some(script) => SourceCode::from_path(&script)?, + None => return Ok(Args::command().print_help()?), }; - - let output = FrontendPipeline::new(&SourceCode::from_path(&script)?) + println!("main module ........ {}", script.module().context("expected a valid module name")?); + let output = FrontendPipeline::new(&script) .passes(&[]) .search( &SearchPath::from_iter(includes) - .with_script_folder(script.parent().context("expected the script to have a parent folder")?), + .with_script_folder(script.folder().context("expected the script to have a parent folder")?), ) .options(options.as_ref())? .run()?; diff --git a/src/Rust/vvs_lang/src/frontend_pipeline.rs b/src/Rust/vvs_lang/src/frontend_pipeline.rs index 420739ad..88746a34 100644 --- a/src/Rust/vvs_lang/src/frontend_pipeline.rs +++ b/src/Rust/vvs_lang/src/frontend_pipeline.rs @@ -198,15 +198,15 @@ impl FromIterator<ImportAllResult> for ImportAllResult { impl<'a> FrontendPipeline<'a> { /// Create a new pipeline to parse, to check and do black magic. - pub fn new(SourceCode { name, code }: &'a SourceCode) -> Self { - log::debug!(target: "vvs_parser", "run script `{name}`"); + pub fn new(source @ SourceCode { code, .. }: &'a SourceCode) -> Self { + log::debug!(target: "vvs_parser", "run script `{}`", source.module().unwrap()); Self { program: Cow::Borrowed(code.as_ref()), options: None, imports: Default::default(), is_main: true, library: Rc::new(RefCell::new(Library::default())), - process_path: vec![ShortString::new(name.as_ref())], + process_path: vec![ShortString::new(source.module().unwrap())], search: &DEFAULT_SEARCH_PATH, passes: &[], } diff --git a/src/Rust/vvs_lang/src/lib.rs b/src/Rust/vvs_lang/src/lib.rs index 9cbc7b63..30af3b33 100644 --- a/src/Rust/vvs_lang/src/lib.rs +++ b/src/Rust/vvs_lang/src/lib.rs @@ -8,14 +8,26 @@ mod passes; mod search_path; mod symbol_table; -use std::{borrow::Cow, fs, io, path::Path}; +use derive_more::Display; +use std::{ + borrow::Cow, + fs, io, + path::{Path, PathBuf}, +}; pub use crate::{ - error_report::ErrorReport, frontend_pipeline::*, main_program::*, passes::UserFrontendPass, search_path::SearchPath, + error_report::ErrorReport, + frontend_pipeline::*, + main_program::*, + passes::UserFrontendPass, + search_path::{SearchPath, VIVY_SCRIPT_EXTENSION}, }; /// The representation of a source file. +#[derive(Display, Debug, Clone)] +#[display("{code}")] pub struct SourceCode { + path: Option<PathBuf>, name: Cow<'static, str>, code: Cow<'static, str>, } @@ -23,25 +35,37 @@ pub struct SourceCode { impl SourceCode { /// Create a new source file from a file. pub fn from_path(path: impl AsRef<Path>) -> io::Result<Self> { + let path = path.as_ref(); Ok(Self { name: Cow::Owned( - path.as_ref() - .file_name() + path.file_name() .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "path doesn't have a file name"))? .to_string_lossy() .to_string(), ), - code: Cow::Owned(fs::read_to_string(path.as_ref())?), + path: Some(path.to_path_buf()), + code: Cow::Owned(fs::read_to_string(path)?), }) } /// Create a new source file from memory. pub fn from_memory(code: Cow<'static, str>) -> Self { - Self { name: Cow::Borrowed("--"), code } + Self { name: Cow::Borrowed("--"), code, path: None } } /// Get the name of the source file. pub fn name(&self) -> &str { &self.name } + + /// Get the name of the module. If the source code doesn't have a valid name (e.g. it doesn't + /// end in ".vvs"), returns [None]. + pub fn module(&self) -> Option<&str> { + self.name().strip_suffix(VIVY_SCRIPT_EXTENSION)?.strip_suffix('.') + } + + /// Gets the parent folder of the script if it exists. + pub fn folder(&self) -> Option<&Path> { + self.path.as_deref() + } } -- GitLab