Skip to content

Commit

Permalink
use custom type instead of hash map for native function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jul 11, 2024
1 parent b96eb0c commit e0a7df8
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 153 deletions.
58 changes: 30 additions & 28 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::path::Path;
use std::thread::sleep;
use std::time::{Duration, Instant};

use clap::{Parser, Subcommand, ValueEnum};
use clap::builder::PossibleValue;
use clap::{Parser, Subcommand, ValueEnum};
use console::style;
use notify::event::ModifyKind;
use notify::EventKind::Modify;
Expand All @@ -17,8 +17,8 @@ pub(crate) use error::Error;
use crate::compiler::{FileCompilerSource, HexoCompiler, HexoCompilerContext};

mod error;
use crate::util::logger::LogLevel;
use crate::util::{defer, logger};
use crate::util::logger::{LogLevel};

#[derive(Subcommand)]
enum Commands {
Expand Down Expand Up @@ -61,9 +61,7 @@ pub(crate) struct CliCompilerArguments {

impl CliCompilerArguments {
pub(crate) fn new(safe_mode: bool) -> CliCompilerArguments {
CliCompilerArguments {
safe_mode
}
CliCompilerArguments { safe_mode }
}
}

Expand All @@ -80,8 +78,12 @@ impl Cli {
let compiler_arguments = cli.cli_compiler_arguments();
let cli_result: Result<_, Error> = match cli.command {
None => Err(Error::UnknownCommand),
Some(Commands::Watch { source, output }) => Self::watch(source, output, compiler_arguments),
Some(Commands::Build { source, output }) => Self::build(source, output, compiler_arguments),
Some(Commands::Watch { source, output }) => {
Self::watch(source, output, compiler_arguments)
}
Some(Commands::Build { source, output }) => {
Self::build(source, output, compiler_arguments)
}
};

Self::handle_cli_error_if_required(cli_result, build_started);
Expand All @@ -92,18 +94,16 @@ impl Cli {
}

fn log_debug_interface_arguments(&self) {
logger::debug!("initialized cli interface with arguments:\
logger::debug!(
"initialized cli interface with arguments:\
\n --log-level = {}\
\n --safe = {}",
&self.log_level,
&self.safe
);
}

fn handle_cli_error_if_required(
cli_result: Result<(), Error>,
build_started: Instant,
) {
fn handle_cli_error_if_required(cli_result: Result<(), Error>, build_started: Instant) {
if let Err(e) = cli_result {
Self::print_error(e.into());
} else {
Expand All @@ -121,14 +121,18 @@ impl Cli {
logger::error!("{error}");
}

fn watch(source: String, output: Option<String>, compiler_arguments: CliCompilerArguments) -> Result<(), Error> {
fn watch(
source: String,
output: Option<String>,
compiler_arguments: CliCompilerArguments,
) -> Result<(), Error> {
let source_path_clone = source.clone();
let source_path = source_path_clone.as_ref();

let mut watcher = notify::recommended_watcher(move |event: Result<Event, _>| {
Self::watch_loop(source.clone(), output.clone(), compiler_arguments, event)
})
.map_err(Error::FileWatcher)?;
.map_err(Error::FileWatcher)?;

watcher
.watch(source_path, RecursiveMode::NonRecursive)
Expand All @@ -151,7 +155,9 @@ impl Cli {
Ok(e) => {
if let Modify(ModifyKind::Data(_)) = e.kind {
logger::debug!("rebuilding...");
let _ = catch_unwind(|| Self::build(source.clone(), output.clone(), compiler_arguments));
let _ = catch_unwind(|| {
Self::build(source.clone(), output.clone(), compiler_arguments)
});
logger::debug!(" done!");
}
}
Expand All @@ -164,14 +170,12 @@ impl Cli {
pub(crate) fn build(
source: String,
output: Option<String>,
compiler_arguments: CliCompilerArguments
compiler_arguments: CliCompilerArguments,
) -> Result<(), Error> {
defer!(logger::debug!("BUILDING, done"));
logger::debug!("BUILDING, source: {}, output: {:?}", source, output);

let context = HexoCompilerContext::new(
compiler_arguments.safe_mode
);
let context = HexoCompilerContext::new(compiler_arguments.safe_mode);
let compiler = HexoCompiler::new(context);

let source_path = Path::new(&source);
Expand All @@ -197,15 +201,13 @@ impl ValueEnum for LogLevel {

fn to_possible_value(&self) -> Option<PossibleValue> {
let name = match self {
LogLevel::Debug => { "debug" }
LogLevel::Info => { "info" }
LogLevel::Warn => { "warn" }
LogLevel::Error => { "error" }
LogLevel::None => { "none" }
LogLevel::Debug => "debug",
LogLevel::Info => "info",
LogLevel::Warn => "warn",
LogLevel::Error => "error",
LogLevel::None => "none",
};

Some(
PossibleValue::new(name)
)
Some(PossibleValue::new(name))
}
}
}
2 changes: 1 addition & 1 deletion src/compiler/ast/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::Display;
use crate::compiler::ast::parser::Rule;
use std::fmt::Display;

#[derive(Debug)]
pub(crate) enum Error {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ mod parser;

pub(crate) use error::Error;
pub(crate) use node::{AstNode, AstNodeType};
pub(crate) use parser::{AstParser};
pub(crate) use parser::AstParser;
2 changes: 0 additions & 2 deletions src/compiler/ast/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub(crate) enum AstNodeType {
}

impl AstNodeType {

pub(crate) fn must_capture_value(&self) -> bool {
matches!(
self,
Expand All @@ -52,7 +51,6 @@ pub(crate) struct AstNode {
}

impl AstNode {

pub(crate) fn new(
node_type: AstNodeType,
content: Option<String>,
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ struct AstPestParser;
pub(crate) struct AstParser {}

impl AstParser {

pub(crate) fn parse(&self, source: &str) -> Result<AstNode, Error> {
let pairs = AstPestParser::parse(Rule::file, source)
.map_err(|e| Error::Pest(Box::new(e)))?;
let pairs =
AstPestParser::parse(Rule::file, source).map_err(|e| Error::Pest(Box::new(e)))?;

let children: Result<Vec<AstNode>, _> = pairs
.map(parse_ast_pair)
Expand Down
6 changes: 2 additions & 4 deletions src/compiler/compiler_context.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
pub(crate) struct HexoCompilerContext {
safe_mode: bool
safe_mode: bool,
}

impl HexoCompilerContext {
pub(crate) fn new(safe_mode: bool) -> Self {
HexoCompilerContext {
safe_mode
}
HexoCompilerContext { safe_mode }
}

pub(crate) fn safe_mode(&self) -> bool {
Expand Down
24 changes: 12 additions & 12 deletions src/compiler/compiler_source.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::util::id::HexoId;
use crate::util::logger;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use crate::util::id::HexoId;
use crate::util::logger;

pub(crate) trait CompilerSource {
fn read(&self) -> Result<String, std::io::Error>;
Expand All @@ -16,7 +16,9 @@ pub(crate) struct FileCompilerSource {

impl FileCompilerSource {
pub(crate) fn new(path: &Path) -> FileCompilerSource {
FileCompilerSource { path: path.to_path_buf() }
FileCompilerSource {
path: path.to_path_buf(),
}
}
}

Expand Down Expand Up @@ -54,20 +56,17 @@ impl LiteralCompilerSource {
pub(crate) fn anonymous(content: String) -> LiteralCompilerSource {
LiteralCompilerSource {
content: content,
path: Path::new(
format!("hexo://anonymous/{}", HexoId::next()).as_str()
).to_path_buf(),
path: Path::new(format!("hexo://anonymous/{}", HexoId::next()).as_str()).to_path_buf(),
}
}
}


#[cfg(test)]
pub(crate) mod tests {
use crate::compiler::CompilerSource;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use crate::compiler::CompilerSource;

pub(crate) struct EagerCompilerSource {
content: String,
Expand All @@ -91,9 +90,10 @@ pub(crate) mod tests {
let mut content = String::new();
source_file.read_to_string(&mut content)?;

Ok(
EagerCompilerSource { content: content, path: pat_ref.to_path_buf() }
)
Ok(EagerCompilerSource {
content: content,
path: pat_ref.to_path_buf(),
})
}
}
}
}
3 changes: 2 additions & 1 deletion src/compiler/cst/macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[macro_export] macro_rules! match_ast {
#[macro_export]
macro_rules! match_ast {
($node:expr => $node_expected:ident, $($option:ident => $holder:ident | $transform:expr)+) => {
guard_node_type($node, AstNodeType::$node_expected)?;

Expand Down
5 changes: 2 additions & 3 deletions src/compiler/cst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ pub(crate) use error::*;
pub(crate) use node::*;
pub(crate) use parser::*;

mod node;
mod error;
mod parser;
mod macros;

mod node;
mod parser;
10 changes: 5 additions & 5 deletions src/compiler/cst/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ pub(crate) struct CstFile {

impl CstFile {
pub(super) fn new(path: &Path, main: CstFunctionStatement) -> Self {
CstFile { path: path.to_path_buf(), main: main }
CstFile {
path: path.to_path_buf(),
main: main,
}
}

pub(crate) fn path(&self) -> &Path {
Expand Down Expand Up @@ -62,7 +65,6 @@ pub(crate) struct CstEmitStatement {
}

impl CstEmitStatement {

pub(crate) fn new(atoms: CstAtomVec) -> Self {
CstEmitStatement { atoms }
}
Expand All @@ -79,7 +81,6 @@ pub(crate) struct CstConstantStatement {
}

impl CstConstantStatement {

pub(crate) fn new(name: String, atoms: CstAtomVec) -> Self {
CstConstantStatement { name, atoms }
}
Expand All @@ -102,7 +103,6 @@ pub(crate) struct CstFunctionStatement {
}

impl CstFunctionStatement {

pub(crate) fn new(
name: String,
emits: Vec<CstEmitStatement>,
Expand Down Expand Up @@ -132,4 +132,4 @@ impl CstFunctionStatement {
pub(crate) fn functions(&self) -> &Vec<CstFunctionStatement> {
&self.functions
}
}
}
Loading

0 comments on commit e0a7df8

Please sign in to comment.