changed the error

This commit is contained in:
mark 2023-06-04 20:17:36 +02:00
parent c7642ef911
commit efe8a177dc
2 changed files with 124 additions and 89 deletions

View File

@ -135,7 +135,7 @@ impl RStatement {
o o
} }
pub fn out(&self, info: &GlobalScriptInfo) -> VType { pub fn out(&self, info: &GlobalScriptInfo) -> VType {
// `a = b` evaluates to [] // `a = b` evaluates to [] (don't change this - cloning is cheap but a = b should NEVER return a boolean because that will make if a = b {} errors way too likely.)
if self.output_to.is_some() { if self.output_to.is_some() {
return VType { return VType {
types: vec![VSingleType::Tuple(vec![])], types: vec![VSingleType::Tuple(vec![])],

View File

@ -30,9 +30,7 @@ pub enum ToRunnableError {
UseOfUndefinedVariable(String), UseOfUndefinedVariable(String),
UseOfUndefinedFunction(String), UseOfUndefinedFunction(String),
UnknownType(String), UnknownType(String),
CannotDeclareVariableWithDereference(String),
CannotDereferenceTypeNTimes(VType, usize, VType), CannotDereferenceTypeNTimes(VType, usize, VType),
FunctionWrongArgCount(String, usize, usize),
FunctionWrongArgs(String, Vec<Arc<RFunction>>, Vec<VType>), FunctionWrongArgs(String, Vec<Arc<RFunction>>, Vec<VType>),
InvalidType { InvalidType {
expected: VType, expected: VType,
@ -70,93 +68,130 @@ impl FormatGs for ToRunnableError {
file: Option<&crate::parsing::file::File>, file: Option<&crate::parsing::file::File>,
) -> std::fmt::Result { ) -> std::fmt::Result {
match self { match self {
Self::MainWrongInput => write!( Self::MainWrongInput => write!(
f, f,
"Main function had the wrong input. This is a bug and should never happen." "Main function had the wrong input. This is a bug and should never happen."
), ),
Self::UseOfUndefinedVariable(v) => write!(f, "Cannot use variable \"{v}\" as it isn't defined (yet?)."), Self::UseOfUndefinedVariable(v) => {
Self::UseOfUndefinedFunction(v) => write!(f, "Cannot use function \"{v}\" as it isn't defined (yet?)."), write!(f, "Cannot use variable \"{v}\" as it isn't defined (yet?).")
Self::UnknownType(name) => write!(f, "Unknown type \"{name}\"."),
Self::CannotDeclareVariableWithDereference(v) => write!(f, "Cannot declare a variable and dereference it (variable '{v}')."),
Self::CannotDereferenceTypeNTimes(og_type, derefs_wanted, last_valid_type) => {
write!(f, "Cannot dereference type ")?;
og_type.fmtgs(f, info, form, file)?;
write!(f, " {derefs_wanted} times (stopped at ")?;
last_valid_type.fmtgs(f, info, form, file);
write!(f, ")")?;
Ok(())
},
Self::FunctionWrongArgCount(v, a, b) => write!(f, "Tried to call function \"{v}\", which takes {a} arguments, with {b} arguments instead."),
Self::FunctionWrongArgs(fn_name, possible_fns, given_types) => write!(f, "Wrong args for function \"{fn_name}\": {} (possible fns: {})", given_types.iter().map(|v| format!(" {v}")).collect::<String>(), ""),
Self::InvalidType {
expected,
found,
problematic,
} => {
write!(f, "Invalid type: Expected ")?;
expected.fmtgs(f, info, form, file)?;
write!(f, " but found ")?;
found.fmtgs(f, info, form, file)?;
write!(f, ", which includes ")?;
problematic.fmtgs(f, info, form, file)?;
write!(f, " which is not covered.")?;
Ok(())
}
Self::CaseForceButTypeNotCovered(v) => {
write!(f, "Switch! statement, but not all types covered. Types to cover: ")?;
v.fmtgs(f, info, form, file)?;
Ok(())
}
Self::MatchConditionInvalidReturn(v) => {
write!(f, "match statement condition returned ")?;
v.fmtgs(f, info, form, file)?;
write!(f, ", which is not necessarily a tuple of size 0 to 1.")?;
Ok(())
}
Self::NotIndexableFixed(t, i) => {
write!(f, "Cannot use fixed-index {i} on type ")?;
t.fmtgs(f, info, form, file)?;
write!(f, ".")?;
Ok(())
}
Self::WrongInputsForBuiltinFunction(_builtin, builtin_name, args) => {
write!(f, "Wrong arguments for builtin function \"{}\":", builtin_name)?;
for arg in args {
write!(f, " ")?;
arg.fmtgs(f, info, form, file)?;
}
write!(f, ".")
}
Self::WrongArgsForLibFunction(name, args) => {
write!(f, "Wrong arguments for library function {}:", name)?;
for arg in args {
write!(f, " ")?;
arg.fmtgs(f, info, form, file)?;
}
write!(f, ".")
}
Self::CannotAssignTo(val, target) => {
write!(f, "Cannot assign type ")?;
val.fmtgs(f, info, form, file)?;
write!(f, " to ")?;
target.fmtgs(f, info, form, file)?;
write!(f, ".")?;
Ok(())
},
Self::ForLoopContainerHasNoInnerTypes => {
write!(f, "For loop: container had no inner types, cannot iterate.")
}
Self::StatementRequiresOutputTypeToBeAButItActuallyOutputsBWhichDoesNotFitInA(required, real, problematic) => {
write!(f, "the statement requires its output type to be ")?;
required.fmtgs(f, info, form, file)?;
write!(f, ", but its real output type is ")?;
real.fmtgs(f, info, form, file)?;
write!(f, ", which doesn't fit in the required type because of the problematic types ")?;
problematic.fmtgs(f, info, form, file)?;
write!(f, ".")?;
Ok(())
}
} }
Self::UseOfUndefinedFunction(v) => {
write!(f, "Cannot use function \"{v}\" as it isn't defined (yet?).")
}
Self::UnknownType(name) => write!(f, "Unknown type \"{name}\"."),
Self::CannotDereferenceTypeNTimes(og_type, derefs_wanted, last_valid_type) => {
write!(f, "Cannot dereference type ")?;
og_type.fmtgs(f, info, form, file)?;
write!(f, " {derefs_wanted} times (stopped at ")?;
last_valid_type.fmtgs(f, info, form, file);
write!(f, ")")?;
Ok(())
}
Self::FunctionWrongArgs(fn_name, possible_fns, given_types) => {
write!(f, "Wrong args for function \"{fn_name}\": Found (");
for (i, t) in given_types.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
t.fmtgs(f, info, form, file)?;
}
write!(f, "), but valid are only ")?;
for (i, func) in possible_fns.iter().enumerate() {
if i != 0 {
if i + 1 == possible_fns.len() {
write!(f, ", and ")?;
} else {
write!(f, ", ")?;
}
}
VDataEnum::Function(Arc::clone(func)).fmtgs(f, info, form, file)?;
}
write!(f, ".")?;
Ok(())
}
Self::InvalidType {
expected,
found,
problematic,
} => {
write!(f, "Invalid type: Expected ")?;
expected.fmtgs(f, info, form, file)?;
write!(f, " but found ")?;
found.fmtgs(f, info, form, file)?;
write!(f, ", which includes ")?;
problematic.fmtgs(f, info, form, file)?;
write!(f, " which is not covered.")?;
Ok(())
}
Self::CaseForceButTypeNotCovered(v) => {
write!(
f,
"Switch! statement, but not all types covered. Types to cover: "
)?;
v.fmtgs(f, info, form, file)?;
Ok(())
}
Self::MatchConditionInvalidReturn(v) => {
write!(f, "match statement condition returned ")?;
v.fmtgs(f, info, form, file)?;
write!(f, ", which is not necessarily a tuple of size 0 to 1.")?;
Ok(())
}
Self::NotIndexableFixed(t, i) => {
write!(f, "Cannot use fixed-index {i} on type ")?;
t.fmtgs(f, info, form, file)?;
write!(f, ".")?;
Ok(())
}
Self::WrongInputsForBuiltinFunction(_builtin, builtin_name, args) => {
write!(
f,
"Wrong arguments for builtin function \"{}\":",
builtin_name
)?;
for arg in args {
write!(f, " ")?;
arg.fmtgs(f, info, form, file)?;
}
write!(f, ".")
}
Self::WrongArgsForLibFunction(name, args) => {
write!(f, "Wrong arguments for library function {}:", name)?;
for arg in args {
write!(f, " ")?;
arg.fmtgs(f, info, form, file)?;
}
write!(f, ".")
}
Self::CannotAssignTo(val, target) => {
write!(f, "Cannot assign type ")?;
val.fmtgs(f, info, form, file)?;
write!(f, " to ")?;
target.fmtgs(f, info, form, file)?;
write!(f, ".")?;
Ok(())
}
Self::ForLoopContainerHasNoInnerTypes => {
write!(f, "For loop: container had no inner types, cannot iterate.")
}
Self::StatementRequiresOutputTypeToBeAButItActuallyOutputsBWhichDoesNotFitInA(
required,
real,
problematic,
) => {
write!(f, "the statement requires its output type to be ")?;
required.fmtgs(f, info, form, file)?;
write!(f, ", but its real output type is ")?;
real.fmtgs(f, info, form, file)?;
write!(
f,
", which doesn't fit in the required type because of the problematic types "
)?;
problematic.fmtgs(f, info, form, file)?;
write!(f, ".")?;
Ok(())
}
}
} }
} }