Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typecheck parameter to *LENGTH #241

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import org.amshove.natparse.ReadOnlyList;
import org.amshove.natparse.lexing.SyntaxKind;
import org.amshove.natparse.natural.*;
import org.amshove.natparse.natural.builtin.BuiltInFunctionTable;
import org.amshove.natparse.natural.builtin.IBuiltinFunctionDefinition;
import org.amshove.natparse.natural.builtin.SystemVariableDefinition;
import org.amshove.natparse.natural.builtin.*;
import org.amshove.natparse.natural.conditionals.ISpecifiedCriteriaNode;

import java.util.ArrayList;
Expand Down Expand Up @@ -79,6 +77,36 @@ private void checkNode(ISyntaxNode node)
{
checkVariableReference(variableReference);
}

if (node instanceof ISystemFunctionNode sysFuncNode)
{
checkSystemFunctionParameter(sysFuncNode);
}
}

private void checkSystemFunctionParameter(ISystemFunctionNode sysFuncNode)
{
if (sysFuncNode.systemFunction() == SyntaxKind.SV_LENGTH)
{
for (var parameter : sysFuncNode.parameter())
{
var type = inferDataType(parameter);
if (type == null)
{
continue;
}

if (type.format() != DataFormat.ALPHANUMERIC && type.format() != DataFormat.UNICODE && type.format() != DataFormat.BINARY)
{
report(ParserErrors.typeMismatch("Parameter to *LENGTH must be of type A, B or U", parameter));
}

if (!type.hasDynamicLength())
{
report(ParserErrors.typeMismatch("Parameter to *LENGTH must have dynamic length (e.g. A DYNAMIC)", parameter));
}
}
}
}

private void checkDecideOnBranches(IDecideOnNode decideOn)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
DEFINE DATA LOCAL
1 #ADYN (A) DYNAMIC
1 #BDYN (B) DYNAMIC
1 #UDYN (U) DYNAMIC
1 #A10 (A10)
1 #B10 (B10)
1 #U10 (U10)
1 #N10 (N10)
1 #I (I4)
END-DEFINE

#I := *LENGTH(#ADYN)
#I := *LENGTH(#BDYN)
#I := *LENGTH(#UDYN)
#I := *LENGTH(#A10) /* !{D:ERROR:NPP037}
#I := *LENGTH(#B10) /* !{D:ERROR:NPP037}
#I := *LENGTH(#U10) /* !{D:ERROR:NPP037}
#I := *LENGTH(#N10) /* !{D:ERROR:NPP037}

END