added macro support, syntax is !(macro-type ...), i.e. !(mers my_file.mers) or !(mers { "value to compute at compile-time".regex("\\S+").assume_no_enum() })

This commit is contained in:
Dummi26 2023-04-29 01:45:00 +02:00
parent 0a2c67c2d5
commit 09d0d29138
8 changed files with 821 additions and 700 deletions

View File

@ -121,7 +121,10 @@ impl Lib {
fn_signature.next(); fn_signature.next();
} else { } else {
loop { loop {
let mut t = match parse::parse_type_adv(&mut fn_signature, true) { let mut t = match parse::implementation::parse_type_adv(
&mut fn_signature,
true,
) {
Ok(v) => v, Ok(v) => v,
Err(e) => panic!("{e}"), Err(e) => panic!("{e}"),
}; };
@ -132,7 +135,8 @@ impl Lib {
} }
} }
} }
let mut fn_out = match parse::parse_type(&mut fn_signature) { let mut fn_out = match parse::implementation::parse_type(&mut fn_signature)
{
Ok(v) => v, Ok(v) => v,
Err(e) => panic!("{e}"), Err(e) => panic!("{e}"),
}; };

View File

@ -1,16 +1,20 @@
use std::path::PathBuf; use std::path::PathBuf;
pub fn path_from_string(path: &str, script_directory: &PathBuf) -> Option<PathBuf> { pub fn path_from_string(path: &str, script_path: &PathBuf) -> Option<PathBuf> {
let path = PathBuf::from(path); let path = PathBuf::from(path);
if path.is_absolute() { if path.is_absolute() {
return Some(path); return Some(path);
} }
if let Some(p) = script_directory if let Some(p) = script_path
.canonicalize() .canonicalize()
.unwrap_or_else(|_| script_directory.clone()) .unwrap_or_else(|_| script_path.clone())
.parent() .parent()
{ {
#[cfg(debug_assertions)]
eprintln!("path: parent path: {p:?}");
let p = p.join(&path); let p = p.join(&path);
#[cfg(debug_assertions)]
eprintln!("path: joined: {p:?}");
if p.exists() { if p.exists() {
return Some(p); return Some(p);
} }

View File

@ -108,12 +108,27 @@ impl File {
} }
pub fn skip_whitespaces(&mut self) { pub fn skip_whitespaces(&mut self) {
loop { loop {
match self.chars.get(self.pos.current_char_index) { match self.peek() {
Some(ch) if ch.1.is_whitespace() => _ = self.next(), Some(ch) if ch.is_whitespace() => _ = self.next(),
_ => break, _ => break,
} }
} }
} }
pub fn collect_to_whitespace(&mut self) -> String {
let mut o = String::new();
loop {
if let Some(ch) = self.next() {
if ch.is_whitespace() {
self.set_pos(*self.get_ppos());
break;
}
o.push(ch);
} else {
break;
}
}
o
}
pub fn path(&self) -> &PathBuf { pub fn path(&self) -> &PathBuf {
&self.path &self.path
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
use std::fmt::{self, Display, Formatter, Pointer}; use std::fmt::{self, Display, Formatter, Pointer};
use super::{global_info::GSInfo, val_data::VData, val_type::VType}; use super::{code_macro::Macro, global_info::GSInfo, val_data::VData, val_type::VType};
pub enum SStatementEnum { pub enum SStatementEnum {
Value(VData), Value(VData),
@ -17,6 +17,7 @@ pub enum SStatementEnum {
Match(String, Vec<(SStatement, SStatement)>), Match(String, Vec<(SStatement, SStatement)>),
IndexFixed(SStatement, usize), IndexFixed(SStatement, usize),
EnumVariant(String, SStatement), EnumVariant(String, SStatement),
Macro(Macro),
} }
impl SStatementEnum { impl SStatementEnum {
pub fn to(self) -> SStatement { pub fn to(self) -> SStatement {
@ -172,6 +173,9 @@ impl SStatementEnum {
write!(f, "{variant}: ")?; write!(f, "{variant}: ")?;
inner.fmtgs(f, info) inner.fmtgs(f, info)
} }
Self::Macro(m) => {
write!(f, "!({m})")
}
} }
} }
} }

View File

@ -1,4 +1,5 @@
pub mod builtins; pub mod builtins;
pub mod code_macro;
pub mod code_parsed; pub mod code_parsed;
pub mod code_runnable; pub mod code_runnable;
pub mod global_info; pub mod global_info;

View File

@ -16,6 +16,7 @@ use crate::{
use super::{ use super::{
builtins::BuiltinFunction, builtins::BuiltinFunction,
code_macro::Macro,
code_parsed::{SBlock, SFunction, SStatement, SStatementEnum}, code_parsed::{SBlock, SFunction, SStatement, SStatementEnum},
code_runnable::{RBlock, RFunction, RScript, RStatement, RStatementEnum}, code_runnable::{RBlock, RFunction, RScript, RStatement, RStatementEnum},
}; };
@ -351,7 +352,7 @@ fn statement(
}); });
} }
} else { } else {
return Err(ToRunnableError::UseOfUndefinedFunction(v.clone())); return Err(ToRunnableError::UseOfUndefinedFunction(v.clone()));
} }
} }
} }
@ -565,6 +566,9 @@ fn statement(
v v
} }
}, statement(s, ginfo, linfo)?), }, statement(s, ginfo, linfo)?),
SStatementEnum::Macro(m) => match m {
Macro::StaticMers(val) => RStatementEnum::Value(val.clone()),
},
} }
.to(); .to();
// if force_output_type is set, verify that the real output type actually fits in the forced one. // if force_output_type is set, verify that the real output type actually fits in the forced one.

View File

@ -47,6 +47,9 @@ impl PartialEq for VDataEnum {
} }
impl VData { impl VData {
pub fn safe_to_share(&self) -> bool {
self.data.safe_to_share()
}
pub fn out(&self) -> VType { pub fn out(&self) -> VType {
VType { VType {
types: vec![self.out_single()], types: vec![self.out_single()],
@ -82,6 +85,15 @@ impl VDataEnum {
// get() // get()
impl VDataEnum { impl VDataEnum {
pub fn safe_to_share(&self) -> bool {
match self {
Self::Bool(_) | Self::Int(_) | Self::Float(_) | Self::String(_) | Self::Function(_) => {
true
}
Self::Tuple(v) | Self::List(_, v) => v.iter().all(|v| v.safe_to_share()),
Self::Thread(..) | Self::Reference(..) | Self::EnumVariant(..) => false,
}
}
pub fn noenum(self) -> VData { pub fn noenum(self) -> VData {
match self { match self {
Self::EnumVariant(_, v) => *v, Self::EnumVariant(_, v) => *v,