use std::{error::Error, fmt};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(i32)]
pub enum CompilerErrorCode {
UnexpectedCharacter = -560,
FatalCompilerError = -561,
ProgramCompoundStatementAtStart = -562,
UnexpectedEndCompoundStatement = -563,
AfterCompoundStatementAtEnd = -564,
ParsingVariableList = -565,
UnknownStateInCompiler = -566,
InvalidDeclarationType = -567,
NoLeftBracketOnExpression = -568,
NoRightBracketOnExpression = -569,
BadStartOfStatement = -570,
NoLeftBracketOnArgList = -571,
NoRightBracketOnArgList = -572,
NoSemicolonAfterExpression = -573,
ParsingAssignmentStatement = -574,
BadLValue = -575,
BadConstantType = -576,
IdentifierListFull = -577,
NonIntegerIdForIntegerConstant = -578,
NonFloatIdForFloatConstant = -579,
NonStringIdForStringConstant = -580,
VariableAlreadyUsedWithinScope = -581,
VariableDefinedWithoutType = -582,
IncorrectVariableStateLeftOnStack = -583,
NonIntegerExpressionWhereIntegerRequired = -584,
VoidExpressionWhereNonVoidRequired = -585,
InvalidParametersForAssignment = -586,
DeclarationDoesNotMatchParameters = -587,
LogicalOperationHasInvalidOperands = -588,
EqualityTestHasInvalidOperands = -589,
ComparisonTestHasInvalidOperands = -590,
ShiftOperationHasInvalidOperands = -591,
ArithmeticOperationHasInvalidOperands = -592,
UnknownOperationInSemanticCheck = -593,
ScriptTooLarge = -594,
ReturnStatementHasNoParameters = -595,
NoWhileAfterDoKeyword = -596,
FunctionDefinitionMissingName = -597,
FunctionDefinitionMissingParameterList = -598,
MalformedParameterList = -599,
BadTypeSpecifier = -600,
NoSemicolonAfterStructure = -601,
EllipsisInIdentifier = -602,
FileNotFound = -603,
IncludeRecursive = -604,
IncludeTooManyLevels = -605,
ParsingReturnStatement = -606,
ParsingIdentifierList = -607,
ParsingFunctionDeclaration = -608,
DuplicateFunctionImplementation = -609,
TokenTooLong = -610,
UndefinedStructure = -611,
LeftOfStructurePartNotStructure = -612,
RightOfStructurePartNotFieldInStructure = -613,
UndefinedFieldInStructure = -614,
StructureRedefined = -615,
VariableUsedTwiceInSameStructure = -616,
FunctionImplementationAndDefinitionDiffer = -617,
MismatchedTypes = -618,
IntegerNotAtTopOfStack = -619,
ReturnTypeAndFunctionTypeMismatched = -620,
NotAllControlPathsReturnAValue = -621,
UndefinedIdentifier = -622,
NoFunctionMainInScript = -623,
FunctionMainMustHaveVoidReturnValue = -624,
FunctionMainMustHaveNoParameters = -625,
NonVoidFunctionCannotBeAStatement = -626,
BadVariableName = -627,
NonOptionalParameterCannotFollowOptionalParameter = -628,
TypeDoesNotHaveAnOptionalParameter = -629,
NonConstantInFunctionDeclaration = -630,
ParsingConstantVector = -631,
OperandMustBeAnIntegerLValue = -1594,
ConditionalRequiresSecondExpression = -1595,
ConditionalMustHaveMatchingReturnTypes = -1596,
MultipleDefaultStatementsWithinSwitch = -1597,
MultipleCaseConstantStatementsWithinSwitch = -1598,
CaseParameterNotAConstantInteger = -1599,
SwitchMustEvaluateToAnInteger = -1600,
NoColonAfterDefaultLabel = -1601,
NoColonAfterCaseLabel = -1602,
NoSemicolonAfterStatement = -1603,
BreakOutsideOfLoopOrCaseStatement = -4834,
TooManyParametersOnFunction = -4835,
UnableToOpenFileForWriting = -4836,
UnterminatedStringConstant = -4855,
NoFunctionIntscInScript = -5182,
FunctionIntscMustHaveVoidReturnValue = -5183,
FunctionIntscMustHaveNoParameters = -5184,
JumpingOverDeclarationStatementsCaseDisallowed = -6804,
JumpingOverDeclarationStatementsDefaultDisallowed = -6805,
ElseWithoutCorrespondingIf = -6823,
IfConditionCannotBeFollowedByANullStatement = -10407,
InvalidTypeForConstKeyword = -3741,
ConstKeywordCannotBeUsedOnNonGlobalVariables = -3742,
InvalidValueAssignedToConstant = -3752,
SwitchConditionCannotBeFollowedByANullStatement = -9081,
WhileConditionCannotBeFollowedByANullStatement = -9082,
ForStatementCannotBeFollowedByANullStatement = -9083,
CannotIncludeThisFileTwice = -9155,
ElseCannotBeFollowedByANullStatement = -40104,
VmTooManyInstructions = -632,
VmTooManyLevelsOfRecursion = -633,
VmFileNotOpened = -634,
VmFileNotCompiledSuccessfully = -635,
VmInvalidAuxCode = -636,
VmNullVirtualMachineNode = -637,
VmStackOverflow = -638,
VmStackUnderflow = -639,
VmInvalidOpCode = -640,
VmInvalidExtraDataOnOpCode = -641,
VmInvalidCommand = -642,
VmFakeShortcutLogicalOperation = -643,
VmDivideByZero = -644,
VmFakeAbortScript = -645,
VmIpOutOfCodeSegment = -646,
VmCommandImplementerNotSet = -647,
VmUnknownTypeOnRunTimeStack = -648,
AlreadyPrinted = -1,
}
impl CompilerErrorCode {
#[must_use]
pub fn code(self) -> i32 {
self as i32
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnknownCompilerErrorCode {
code: i32,
}
impl UnknownCompilerErrorCode {
#[must_use]
pub fn new(code: i32) -> Self {
Self {
code,
}
}
#[must_use]
pub fn code(&self) -> i32 {
self.code
}
}
impl fmt::Display for UnknownCompilerErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "unknown NWScript compiler error code: {}", self.code)
}
}
impl Error for UnknownCompilerErrorCode {}
impl TryFrom<i32> for CompilerErrorCode {
type Error = UnknownCompilerErrorCode;
fn try_from(code: i32) -> Result<Self, Self::Error> {
let value = match code {
-560 => Self::UnexpectedCharacter,
-561 => Self::FatalCompilerError,
-562 => Self::ProgramCompoundStatementAtStart,
-563 => Self::UnexpectedEndCompoundStatement,
-564 => Self::AfterCompoundStatementAtEnd,
-565 => Self::ParsingVariableList,
-566 => Self::UnknownStateInCompiler,
-567 => Self::InvalidDeclarationType,
-568 => Self::NoLeftBracketOnExpression,
-569 => Self::NoRightBracketOnExpression,
-570 => Self::BadStartOfStatement,
-571 => Self::NoLeftBracketOnArgList,
-572 => Self::NoRightBracketOnArgList,
-573 => Self::NoSemicolonAfterExpression,
-574 => Self::ParsingAssignmentStatement,
-575 => Self::BadLValue,
-576 => Self::BadConstantType,
-577 => Self::IdentifierListFull,
-578 => Self::NonIntegerIdForIntegerConstant,
-579 => Self::NonFloatIdForFloatConstant,
-580 => Self::NonStringIdForStringConstant,
-581 => Self::VariableAlreadyUsedWithinScope,
-582 => Self::VariableDefinedWithoutType,
-583 => Self::IncorrectVariableStateLeftOnStack,
-584 => Self::NonIntegerExpressionWhereIntegerRequired,
-585 => Self::VoidExpressionWhereNonVoidRequired,
-586 => Self::InvalidParametersForAssignment,
-587 => Self::DeclarationDoesNotMatchParameters,
-588 => Self::LogicalOperationHasInvalidOperands,
-589 => Self::EqualityTestHasInvalidOperands,
-590 => Self::ComparisonTestHasInvalidOperands,
-591 => Self::ShiftOperationHasInvalidOperands,
-592 => Self::ArithmeticOperationHasInvalidOperands,
-593 => Self::UnknownOperationInSemanticCheck,
-594 => Self::ScriptTooLarge,
-595 => Self::ReturnStatementHasNoParameters,
-596 => Self::NoWhileAfterDoKeyword,
-597 => Self::FunctionDefinitionMissingName,
-598 => Self::FunctionDefinitionMissingParameterList,
-599 => Self::MalformedParameterList,
-600 => Self::BadTypeSpecifier,
-601 => Self::NoSemicolonAfterStructure,
-602 => Self::EllipsisInIdentifier,
-603 => Self::FileNotFound,
-604 => Self::IncludeRecursive,
-605 => Self::IncludeTooManyLevels,
-606 => Self::ParsingReturnStatement,
-607 => Self::ParsingIdentifierList,
-608 => Self::ParsingFunctionDeclaration,
-609 => Self::DuplicateFunctionImplementation,
-610 => Self::TokenTooLong,
-611 => Self::UndefinedStructure,
-612 => Self::LeftOfStructurePartNotStructure,
-613 => Self::RightOfStructurePartNotFieldInStructure,
-614 => Self::UndefinedFieldInStructure,
-615 => Self::StructureRedefined,
-616 => Self::VariableUsedTwiceInSameStructure,
-617 => Self::FunctionImplementationAndDefinitionDiffer,
-618 => Self::MismatchedTypes,
-619 => Self::IntegerNotAtTopOfStack,
-620 => Self::ReturnTypeAndFunctionTypeMismatched,
-621 => Self::NotAllControlPathsReturnAValue,
-622 => Self::UndefinedIdentifier,
-623 => Self::NoFunctionMainInScript,
-624 => Self::FunctionMainMustHaveVoidReturnValue,
-625 => Self::FunctionMainMustHaveNoParameters,
-626 => Self::NonVoidFunctionCannotBeAStatement,
-627 => Self::BadVariableName,
-628 => Self::NonOptionalParameterCannotFollowOptionalParameter,
-629 => Self::TypeDoesNotHaveAnOptionalParameter,
-630 => Self::NonConstantInFunctionDeclaration,
-631 => Self::ParsingConstantVector,
-1594 => Self::OperandMustBeAnIntegerLValue,
-1595 => Self::ConditionalRequiresSecondExpression,
-1596 => Self::ConditionalMustHaveMatchingReturnTypes,
-1597 => Self::MultipleDefaultStatementsWithinSwitch,
-1598 => Self::MultipleCaseConstantStatementsWithinSwitch,
-1599 => Self::CaseParameterNotAConstantInteger,
-1600 => Self::SwitchMustEvaluateToAnInteger,
-1601 => Self::NoColonAfterDefaultLabel,
-1602 => Self::NoColonAfterCaseLabel,
-1603 => Self::NoSemicolonAfterStatement,
-4834 => Self::BreakOutsideOfLoopOrCaseStatement,
-4835 => Self::TooManyParametersOnFunction,
-4836 => Self::UnableToOpenFileForWriting,
-4855 => Self::UnterminatedStringConstant,
-5182 => Self::NoFunctionIntscInScript,
-5183 => Self::FunctionIntscMustHaveVoidReturnValue,
-5184 => Self::FunctionIntscMustHaveNoParameters,
-6804 => Self::JumpingOverDeclarationStatementsCaseDisallowed,
-6805 => Self::JumpingOverDeclarationStatementsDefaultDisallowed,
-6823 => Self::ElseWithoutCorrespondingIf,
-10407 => Self::IfConditionCannotBeFollowedByANullStatement,
-3741 => Self::InvalidTypeForConstKeyword,
-3742 => Self::ConstKeywordCannotBeUsedOnNonGlobalVariables,
-3752 => Self::InvalidValueAssignedToConstant,
-9081 => Self::SwitchConditionCannotBeFollowedByANullStatement,
-9082 => Self::WhileConditionCannotBeFollowedByANullStatement,
-9083 => Self::ForStatementCannotBeFollowedByANullStatement,
-9155 => Self::CannotIncludeThisFileTwice,
-40104 => Self::ElseCannotBeFollowedByANullStatement,
-632 => Self::VmTooManyInstructions,
-633 => Self::VmTooManyLevelsOfRecursion,
-634 => Self::VmFileNotOpened,
-635 => Self::VmFileNotCompiledSuccessfully,
-636 => Self::VmInvalidAuxCode,
-637 => Self::VmNullVirtualMachineNode,
-638 => Self::VmStackOverflow,
-639 => Self::VmStackUnderflow,
-640 => Self::VmInvalidOpCode,
-641 => Self::VmInvalidExtraDataOnOpCode,
-642 => Self::VmInvalidCommand,
-643 => Self::VmFakeShortcutLogicalOperation,
-644 => Self::VmDivideByZero,
-645 => Self::VmFakeAbortScript,
-646 => Self::VmIpOutOfCodeSegment,
-647 => Self::VmCommandImplementerNotSet,
-648 => Self::VmUnknownTypeOnRunTimeStack,
-1 => Self::AlreadyPrinted,
_ => return Err(UnknownCompilerErrorCode::new(code)),
};
Ok(value)
}
}