Skip to content
Extraits de code Groupes Projets
Valider 90855f48 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

SCRIPT: Implement printing the main program as summary

parent 5855ccb8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #8105 en échec
......@@ -3,24 +3,23 @@
//! The VivyScript cli
use anyhow::{Context as _, Result};
use clap::CommandFactory as _;
use clap::{CommandFactory as _, Parser as _};
use std::{fs, io, path::Path};
use vvs_cli::{
args::Args,
config::{Config, ConfigFile},
};
use vvs_font::{embeded_fonts, Font};
use vvs_lang::*;
use vvs_xdg::{file::TempFile, XDGConfig, XDGConfigMergedSilent};
fn print_font(name: impl AsRef<Path>, font: &[u8]) -> Result<()> {
let font = Font::try_from(font).with_context(|| format!("failed to parse font {}", name.as_ref().display()))?;
let types = Vec::from_iter(font.font_types().into_iter().map(|ty| ty.to_string()));
fn print_font(n: impl AsRef<Path>, f: &[u8]) -> Result<()> {
let f = vvs_font::Font::try_from(f).with_context(|| format!("failed to parse font {}", n.as_ref().display()))?;
let ts = Vec::from_iter(f.font_types().into_iter().map(|ty| ty.to_string()));
println!("###");
println!("# Family Name: {}", font.name().context("failed to get the font name")?);
println!("# Name(s): {}", font.family_names().join(", "));
println!("# Font Type(s): {}", types.join(", "));
println!("# Number of glyph(s): {}", font.number_of_glyphs());
println!("# Family Name: {}", f.name().context("failed to get the font name")?);
println!("# Name(s): {}", f.family_names().join(", "));
println!("# Font Type(s): {}", ts.join(", "));
println!("# Number of glyph(s): {}", f.number_of_glyphs());
Ok(())
}
......@@ -33,7 +32,7 @@ fn main() -> Result<()> {
.file("vvcc.toml")
.read_or_default(),
)
.with_args(<Args as clap::Parser>::parse())
.with_args(Args::parse())
.apply_logger();
// Handle auxiliary stuff
......@@ -47,7 +46,7 @@ fn main() -> Result<()> {
} else if let Some(font_info) = font_info {
return match font_info {
Some(p) => print_font(&p, &fs::read(&p).with_context(|| format!("failed to read font: {}", p.display()))?),
None => embeded_fonts().try_for_each(|(n, f)| print_font(n, f)),
None => vvs_font::embeded_fonts().try_for_each(|(n, f)| print_font(n, f)),
};
}
......@@ -76,11 +75,11 @@ fn main() -> Result<()> {
Some(path) => println!("options file ....... {}", path.display()),
None => println!("options file ....... ø"),
}
println!("{output}");
print!("{output}");
}
let jit = vvs_runtime::JIT::new()?;
let mut file = TempFile::new("vvcc")?;
let jit = vvs_runtime::JIT::new()?;
jit.with_module(
vvs_codegen::lowerer::Lowerer::new(&jit.ctx(), &output.main.name)?
.declare_globals(output.declared_functions())?
......
......@@ -157,9 +157,8 @@ impl fmt::Display for FrontendOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self { imports, main, options, .. } = self;
writeln!(f, "imported modules:")?;
imports.keys().try_for_each(|module| writeln!(f, " - {module}"))?;
writeln!(f, "{options}")?;
writeln!(f, "{main:#?}")
imports.keys().try_for_each(|module| writeln!(f, "\t- {module}"))?;
writeln!(f, "{options}\n{main}")
}
}
......
//! Main program and utilities to be able to complete the work.
use derive_more::{IntoIterator, IsVariant};
use derive_more::{Display, IntoIterator, IsVariant};
use vvs_parser::prelude::ast::Expression;
use vvs_shortstring::ShortString;
......@@ -53,6 +53,16 @@ impl MainProgram {
}
}
impl Display for MainProgram {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, initial, steps, returns } = self;
writeln!(f, "program {name} ({initial}) {{")?;
steps.iter().try_for_each(|step| writeln!(f, "{step}"))?;
writeln!(f, "\t{returns}")?;
write!(f, "}}")
}
}
/// The returned variables from the main program.
#[derive(Debug, Default, IntoIterator)]
pub struct MainProgramReturns {
......@@ -66,6 +76,21 @@ impl FromIterator<ShortString> for MainProgramReturns {
}
}
impl Display for MainProgramReturns {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "return {{")?;
match &self.variables[..] {
[] => write!(f, " }}"),
[item] => write!(f, " {item} }}"),
[head, tail @ ..] => {
write!(f, " {head}")?;
tail.iter().try_for_each(|item| write!(f, ", {item}"))?;
write!(f, " }}")
}
}
}
}
/// A step to do for the main program.
#[derive(Debug)]
pub struct MainProgramStep {
......@@ -111,13 +136,31 @@ impl MainProgramStep {
}
}
impl Display for MainProgramStep {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { destination, job: (module, job), .. } = self;
write!(f, "{destination} = {module}:{job}")?;
match &self.call_list[..] {
[] => write!(f, " {{ }}"),
[item] => write!(f, " {{ {item} }}"),
[head, tail @ ..] => {
write!(f, " {{ {head}")?;
tail.iter().try_for_each(|item| write!(f, ", {item}"))?;
write!(f, " }}")
}
}
}
}
/// An item to pass to a job when we call it.
#[derive(Debug, IsVariant)]
#[derive(Debug, IsVariant, Display)]
pub enum MainProgramCallListItem {
/// We add a variable to process.
#[display("{_0}")]
Variable(ShortString),
/// We set a parameter for the job.
#[display("{_0} = {_1}")]
Parameter(ShortString, Expression),
}
......
use crate::*;
use std::{
collections::hash_map::DefaultHasher,
fs::{self, File, OpenOptions},
fs,
hash::{Hash, Hasher},
io::Error as IoError,
path::Path,
......@@ -55,7 +55,7 @@ impl LockFile {
MaybeFolderList::Empty => unreachable!("failed to find the runtime dir..."),
MaybeFolderList::Folder(path) | MaybeFolderList::Many(path, _) => {
log::debug!("create lock file at: {}", path.display());
let _ = OpenOptions::new()
let _ = fs::OpenOptions::new()
.create_new(true)
.open(&path)
.map_err(Self::handle_io_error)?;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter