fixed examples, fixed bug due to VType changes

This commit is contained in:
mark
2023-06-12 19:06:20 +02:00
parent d94f727eaa
commit 3032d3c115
13 changed files with 76 additions and 483 deletions

2
mers/Cargo.lock generated
View File

@@ -647,7 +647,7 @@ dependencies = [
[[package]]
name = "mers"
version = "0.2.1"
version = "0.2.2"
dependencies = [
"colorize",
"edit",

View File

@@ -1,6 +1,6 @@
[package]
name = "mers"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -636,11 +636,7 @@ impl BuiltinFunction {
VSingleType::Tuple(vec![]),
VSingleType::Tuple(vec![v
.get_any(info)
.expect("cannot use get on this type")
.dereference(info)
.expect(
"running get_any() on &[ ...] should give a reference...",
)]),
.expect("cannot use get on this type")]),
],
}
} else {

View File

@@ -557,7 +557,7 @@ fn statement_adv(
SStatementEnum::For(v, c, b) => {
let mut linfo = linfo.clone();
let container = statement(&c, ginfo, &mut linfo)?;
let inner = container.out(ginfo).inner_types(ginfo);
let inner = container.out(ginfo).inner_types_for_iters(ginfo);
if inner.types.is_empty() {
return Err(ToRunnableError::ForLoopContainerHasNoInnerTypes);
}
@@ -591,7 +591,6 @@ fn statement_adv(
statement(case_action, ginfo, &mut linfo)?,
));
}
if *force {
let types_not_covered = og_type.fits_in(&covered_types, ginfo);
if !types_not_covered.is_empty() {

View File

@@ -35,70 +35,6 @@ pub enum VSingleType {
CustomTypeS(String),
}
impl VSingleType {
/// None => Cannot get, Some(t) => getting can return t or nothing
pub fn get(&self, i: usize, gsinfo: &GlobalScriptInfo) -> Option<VType> {
match self {
Self::Any
| Self::Bool
| Self::Int
| Self::Float
| Self::Function(..)
| Self::Thread(..)
| Self::EnumVariant(..)
| Self::EnumVariantS(..) => None,
Self::String => Some(VSingleType::String.into()),
Self::Tuple(t) => t.get(i).cloned(),
Self::List(t) => Some(t.clone()),
Self::Reference(r) => Some(r.get(i, gsinfo)?.reference()),
Self::CustomType(t) => gsinfo.custom_types[*t].get(i, gsinfo),
&Self::CustomTypeS(_) => {
unreachable!("CustomTypeS instead of CustomType, compiler bug? [get]")
}
}
}
/// None => might not always return t, Some(t) => can only return t
pub fn get_always(&self, i: usize, info: &GlobalScriptInfo) -> Option<VType> {
match self {
Self::Any
| Self::Bool
| Self::Int
| Self::Float
| Self::String
| Self::List(_)
| Self::Function(..)
| Self::Thread(..)
| Self::EnumVariant(..)
| Self::EnumVariantS(..) => None,
Self::Tuple(t) => t.get(i).cloned(),
Self::Reference(r) => r.get_always_ref(i, info),
Self::CustomType(t) => info.custom_types[*t].get_always(i, info),
Self::CustomTypeS(_) => {
unreachable!("CustomTypeS instead of CustomType, compiler bug? [get_always]")
}
}
}
pub fn get_always_ref(&self, i: usize, info: &GlobalScriptInfo) -> Option<VType> {
match self {
Self::Any
| Self::Bool
| Self::Int
| Self::Float
| Self::String
| Self::List(_)
| Self::Function(..)
| Self::Thread(..)
| Self::EnumVariant(..)
| Self::EnumVariantS(..) => None,
Self::Tuple(t) => Some(t.get(i)?.reference()),
Self::Reference(r) => r.get_always_ref(i, info),
Self::CustomType(t) => info.custom_types[*t].get_always_ref(i, info),
Self::CustomTypeS(_) => {
unreachable!("CustomTypeS instead of CustomType, compiler bug? [get_always]")
}
}
}
}
impl VType {
pub fn empty() -> Self {
Self { types: vec![] }
@@ -117,13 +53,6 @@ impl VType {
}
Some(out)
}
pub fn get_always_ref(&self, i: usize, info: &GlobalScriptInfo) -> Option<VType> {
let mut out = VType { types: vec![] };
for t in &self.types {
out.add_types(t.get_always_ref(i, info)?, info); // if we can't use *get* on one type, we can't use it at all.
}
Some(out)
}
/// returns Some(true) or Some(false) if all types are references or not references. If it is mixed or types is empty, returns None.
pub fn is_reference(&self) -> Option<bool> {
let mut noref = false;
@@ -156,6 +85,27 @@ impl VType {
}
impl VSingleType {
/// None => Cannot get, Some(t) => getting can return t or nothing
pub fn get(&self, i: usize, gsinfo: &GlobalScriptInfo) -> Option<VType> {
match self {
Self::Any
| Self::Bool
| Self::Int
| Self::Float
| Self::Function(..)
| Self::Thread(..)
| Self::EnumVariant(..)
| Self::EnumVariantS(..) => None,
Self::String => Some(VSingleType::String.into()),
Self::Tuple(t) => t.get(i).cloned(),
Self::List(t) => Some(t.clone()),
Self::Reference(r) => Some(r.get(i, gsinfo)?.reference()),
Self::CustomType(t) => gsinfo.custom_types[*t].get(i, gsinfo),
&Self::CustomTypeS(_) => {
unreachable!("CustomTypeS instead of CustomType, compiler bug? [get]")
}
}
}
pub fn get_any(&self, info: &GlobalScriptInfo) -> Option<VType> {
match self {
Self::Any
@@ -170,33 +120,40 @@ impl VSingleType {
a
})),
Self::List(t) => Some(t.clone()),
Self::Reference(r) => r.get_any_ref(info),
Self::Reference(r) => Some(VType {
// this is &a/&b/..., NOT &(a/b/...)!
types: r
.get_any(info)?
.types
.iter()
.map(|v| VSingleType::Reference(v.clone().to()))
.collect(),
}),
Self::EnumVariant(_, t) => t.get_any(info),
Self::EnumVariantS(..) => unreachable!(),
Self::CustomType(t) => info.custom_types[*t].get_any(info),
Self::CustomTypeS(_) => unreachable!(),
}
}
pub fn get_any_ref(&self, info: &GlobalScriptInfo) -> Option<VType> {
/// None => might not always return t, Some(t) => can only return t
pub fn get_always(&self, i: usize, info: &GlobalScriptInfo) -> Option<VType> {
match self {
Self::Any
| Self::Bool
| Self::Int
| Self::Float
| Self::String
| Self::List(_)
| Self::Function(..)
| Self::Thread(..) => None,
Self::String => Some(VSingleType::String.into()),
Self::Tuple(t) => Some(t.iter().fold(VType::empty(), |mut a, b| {
a.add_types(b.reference(), info);
a
})),
Self::List(t) => Some(t.reference()),
// TODO: idk if this is right...
Self::Reference(r) => r.get_any_ref(info),
Self::EnumVariant(_, t) => t.get_any_ref(info),
Self::EnumVariantS(..) => unreachable!(),
Self::CustomType(t) => info.custom_types[*t].get_any(info),
Self::CustomTypeS(_) => unreachable!(),
| Self::Thread(..)
| Self::EnumVariant(..)
| Self::EnumVariantS(..) => None,
Self::Tuple(t) => t.get(i).cloned(),
Self::Reference(r) => Some(VSingleType::Reference(r.get_any(info)?).to()),
Self::CustomType(t) => info.custom_types[*t].get_always(i, info),
Self::CustomTypeS(_) => {
unreachable!("CustomTypeS instead of CustomType, compiler bug? [get_always]")
}
}
}
pub fn is_reference(&self) -> bool {
@@ -221,13 +178,6 @@ impl VType {
}
Some(out)
}
pub fn get_any_ref(&self, info: &GlobalScriptInfo) -> Option<VType> {
let mut out = VType { types: vec![] };
for t in &self.types {
out.add_types(t.get_any_ref(info)?, info); // if we can't use *get* on one type, we can't use it at all.
}
Some(out)
}
}
impl VType {
@@ -246,10 +196,10 @@ impl VType {
}
no
}
pub fn inner_types(&self, info: &GlobalScriptInfo) -> VType {
pub fn inner_types_for_iters(&self, info: &GlobalScriptInfo) -> VType {
let mut out = VType { types: vec![] };
for t in &self.types {
out.add_types(t.inner_types(info), info);
out.add_types(t.inner_types_for_iters(info), info);
}
out
}
@@ -307,7 +257,7 @@ impl VSingleType {
pub fn to(self) -> VType {
VType { types: vec![self] }
}
pub fn inner_types(&self, info: &GlobalScriptInfo) -> VType {
pub fn inner_types_for_iters(&self, info: &GlobalScriptInfo) -> VType {
match self {
Self::Tuple(v) => {
let mut out = VType::empty();
@@ -324,7 +274,7 @@ impl VSingleType {
// function that takes no inputs
if let Some(out) = f.iter().find_map(|(args, out)| {
if args.is_empty() {
Some(out.clone())
Some(out.clone().matches(info).1)
} else {
None
}
@@ -334,21 +284,7 @@ impl VSingleType {
VType::empty()
}
}
Self::Reference(r) => r.inner_types(info).reference(),
_ => VType::empty(),
}
}
pub fn inner_types_ref(&self, info: &GlobalScriptInfo) -> VType {
match self {
Self::Tuple(v) => {
let mut out = VType::empty();
for it in v {
out.add_types(it.reference(), info);
}
out
}
Self::List(v) => v.reference(),
Self::Reference(r) => r.inner_types(info).reference(),
Self::Reference(r) => r.inner_types_for_iters(info).reference(),
_ => VType::empty(),
}
}

View File

@@ -83,8 +83,6 @@ impl FormatGs for ScriptError {
}
}
pub const PARSE_VERSION: u64 = 0;
pub struct Error {
pub err: ScriptError,
pub ginfo: GSInfo,
@@ -313,7 +311,6 @@ pub enum ParseErrors {
FoundEofInType,
FoundEofInsteadOfType,
RefTypeWithBracketsNotClosedProperly,
InvalidType(String),
CannotUseFixedIndexingWithThisType(VType),
CannotWrapWithThisStatement(SStatementEnum),
ErrorParsingFunctionArgs(Box<ParseError>),
@@ -354,7 +351,6 @@ impl FormatGs for ParseErrors {
Self::RefTypeWithBracketsNotClosedProperly => {
write!(f, "ref type with brackets &(...) wasn't closed properly.")
}
Self::InvalidType(name) => write!(f, "\"{name}\" is not a type."),
Self::CannotUseFixedIndexingWithThisType(t) => {
write!(f, "cannot use fixed-indexing with type ")?;
t.fmtgs(f, info, form, file)?;
@@ -1166,12 +1162,12 @@ pub mod implementation {
}
Ok((VType { types }, closed_fn_args))
}
fn parse_single_type(file: &mut File) -> Result<VSingleType, ParseError> {
match parse_single_type_adv(file, false) {
Ok((v, _)) => Ok(v),
Err(e) => Err(e),
}
}
// fn parse_single_type(file: &mut File) -> Result<VSingleType, ParseError> {
// match parse_single_type_adv(file, false) {
// Ok((v, _)) => Ok(v),
// Err(e) => Err(e),
// }
// }
fn parse_single_type_adv(
file: &mut File,
in_fn_args: bool,
@@ -1277,8 +1273,11 @@ pub mod implementation {
}
}
file.skip_whitespaces();
let out = parse_type(file)?;
let (out, close) = parse_type_adv(file, true)?;
fn_types.push((args, out));
if close {
break;
};
}
Some(')') => break,
Some(other) => {