use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class LocalTypeReasoner method walkPostBody.
@Override
protected void walkPostBody(final AstNode node) {
// Get result from upper EXPRESSION.
final AstNode expressionNode = node.getFirstAncestor(MagikGrammar.EXPRESSION);
if (expressionNode == null) {
// Happens with a return, don't do anything.
return;
}
ExpressionResult result = this.getNodeType(expressionNode);
// BODYs don't always have to result in something.
// Find STATEMENT -> RETURN/EMIT/LEAVE
// TODO: what about _loop _if .. _then _leave _with _endif _leave _with _endloop
AstNode resultingNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.STATEMENT, MagikGrammar.RETURN_STATEMENT);
if (resultingNode == null) {
resultingNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.STATEMENT, MagikGrammar.EMIT_STATEMENT);
}
if (resultingNode == null) {
resultingNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.STATEMENT, MagikGrammar.LEAVE_STATEMENT);
}
if (resultingNode == null) {
// Result can also be an unset, as no resulting statement was found.
// TODO: but... "_block _block _return 1 _endblock _endblock"
final ExpressionResult emptyResult = new ExpressionResult();
final AbstractType unsetType = this.typeKeeper.getType(SW_UNSET);
result = new ExpressionResult(result, emptyResult, unsetType);
}
// Set parent EXPRESSION result.
this.setNodeType(expressionNode, result);
}
use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class LocalTypeReasoner method walkPostLoopbody.
@Override
protected void walkPostLoopbody(final AstNode node) {
final AbstractType unsetType = this.typeKeeper.getType(SW_UNSET);
// Get results.
final AstNode multiValueExprNode = node.getFirstChild(MagikGrammar.TUPLE);
final ExpressionResult result = this.getNodeType(multiValueExprNode);
// Find related node to store at.
final AstNode procMethodDefNode = node.getFirstAncestor(MagikGrammar.PROCEDURE_DEFINITION, MagikGrammar.METHOD_DEFINITION);
// Save results.
if (this.hasNodeType(procMethodDefNode)) {
// Combine types.
final ExpressionResult existingResult = this.getNodeType(procMethodDefNode);
final ExpressionResult combinedResult = new ExpressionResult(existingResult, result, unsetType);
this.setLoopbodyNodeType(procMethodDefNode, combinedResult);
} else {
this.setLoopbodyNodeType(procMethodDefNode, result);
}
}
use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class LocalTypeReasoner method assignAtom.
private void assignAtom(final AstNode node, final ExpressionResult result) {
final AstNode atomNode = node.getParent();
this.setNodeType(atomNode, result);
}
use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class LocalTypeReasoner method walkPostEmitStatement.
@Override
protected void walkPostEmitStatement(final AstNode node) {
// Get results.
final AstNode tupleNode = node.getFirstChild(MagikGrammar.TUPLE);
final ExpressionResult result = this.getNodeType(tupleNode);
// Find related node.
final AstNode bodyNode = node.getFirstAncestor(MagikGrammar.BODY);
final AstNode expressionNode = bodyNode.getFirstAncestor(// for BLOCK etc
MagikGrammar.EXPRESSION, // for METHOD_DEFINITION
MagikGrammar.METHOD_DEFINITION, // for PROC_DEFINITION
MagikGrammar.PROCEDURE_DEFINITION);
// Save results.
this.addNodeType(expressionNode, result);
}
use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class LocalTypeReasoner method walkPostSuper.
@Override
protected void walkPostSuper(final AstNode node) {
// Determine which type we are.
final AbstractType methodOwnerType = this.getMethodOwnerType(node);
if (methodOwnerType == UndefinedType.INSTANCE) {
LOGGER.debug("Unknown type for node: {}", node);
return;
}
// Find specified super, if given.
final AstNode identifierNode = node.getFirstChild(MagikGrammar.IDENTIFIER);
final String identifier = identifierNode != null ? identifierNode.getTokenValue() : null;
final AbstractType superType;
if (identifier != null) {
superType = methodOwnerType.getParents().stream().filter(parentType -> {
final String fullTypeName = parentType.getFullName();
final String typeName = fullTypeName.split(":")[1];
return identifier.equals(typeName);
}).findAny().orElse(null);
} else {
superType = methodOwnerType.getParents().stream().reduce(CombinedType::combine).orElse(null);
}
if (superType == null) {
return;
}
final ExpressionResult result = new ExpressionResult(superType);
this.assignAtom(node, result);
}
Aggregations