Skip to content

Commit

Permalink
Merge pull request #241 from MarkusAmshove/natparse/check-length-type
Browse files Browse the repository at this point in the history
Typecheck parameter to *LENGTH
  • Loading branch information
MarkusAmshove authored May 11, 2023
2 parents 4e6685e + 583b007 commit f0cf474
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
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

0 comments on commit f0cf474

Please sign in to comment.