本文整理汇总了Golang中github.com/serulian/compiler/compilergraph.GraphNode.GetNode方法的典型用法代码示例。如果您正苦于以下问题:Golang GraphNode.GetNode方法的具体用法?Golang GraphNode.GetNode怎么用?Golang GraphNode.GetNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/serulian/compiler/compilergraph.GraphNode
的用法示例。
在下文中一共展示了GraphNode.GetNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: scopeAssignStatement
// scopeAssignStatement scopes a assign statement in the SRG.
func (sb *scopeBuilder) scopeAssignStatement(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
// TODO: Handle tuple assignment once we figure out tuple types
// Scope the name.
nameScope := sb.getScope(node.GetNode(parser.NodeAssignStatementName), context.withAccess(scopeSetAccess))
// Scope the expression value.
exprScope := sb.getScope(node.GetNode(parser.NodeAssignStatementValue), context)
if !nameScope.GetIsValid() || !exprScope.GetIsValid() {
return newScope().Invalid().GetScope()
}
// Check that we have a named item.
namedScopedRef, found := sb.getNamedScopeForScope(nameScope)
if !found {
sb.decorateWithError(node, "Cannot assign to non-named value")
return newScope().Invalid().GetScope()
}
// Check that the item is assignable.
if !namedScopedRef.IsAssignable() {
sb.decorateWithError(node, "Cannot assign to non-assignable %v %v", namedScopedRef.Title(), namedScopedRef.Name())
return newScope().Invalid().GetScope()
}
// Ensure that we can assign the expr value to the named scope.
if serr := exprScope.ResolvedTypeRef(sb.sg.tdg).CheckSubTypeOf(nameScope.AssignableTypeRef(sb.sg.tdg)); serr != nil {
sb.decorateWithError(node, "Cannot assign value to %v %v: %v", namedScopedRef.Title(), namedScopedRef.Name(), serr)
return newScope().Invalid().GetScope()
}
return newScope().Valid().GetScope()
}
示例2: scopeUnaryExpression
// scopeUnaryExpression scopes a unary expression in the SRG.
func (sb *scopeBuilder) scopeUnaryExpression(node compilergraph.GraphNode, opName string, predicate compilergraph.Predicate, context scopeContext) *scopeInfoBuilder {
// Get the scope of the sub expression.
childScope := sb.getScope(node.GetNode(predicate), context)
// Ensure that the child scope is valid.
if !childScope.GetIsValid() {
return newScope().Invalid()
}
// Ensure that the operator exists under the resolved type.
childType := childScope.ResolvedTypeRef(sb.sg.tdg)
module := compilercommon.InputSource(node.Get(parser.NodePredicateSource))
operator, rerr := childType.ResolveAccessibleMember(opName, module, typegraph.MemberResolutionOperator)
if rerr != nil {
sb.decorateWithError(node, "Operator '%v' is not defined on type '%v'", opName, childType)
return newScope().Invalid()
}
returnType, _ := operator.ReturnType()
// Check for nullable values.
if childType.NullValueAllowed() {
sb.decorateWithError(node, "Cannot invoke operator '%v' on nullable type '%v'", opName, childType)
return newScope().Invalid().CallsOperator(operator).Resolving(returnType.TransformUnder(childType))
}
return newScope().Valid().CallsOperator(operator).Resolving(returnType.TransformUnder(childType))
}
示例3: scopeIndexerExpression
// scopeIndexerExpression scopes an indexer expression (slice with single numerical index) in the SRG.
func (sb *scopeBuilder) scopeIndexerExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
// Lookup the indexing operator.
var opName = "index"
if context.accessOption == scopeSetAccess {
opName = "setindex"
}
operator, childType, found := sb.scopeSliceChildExpression(node, opName, context)
if !found {
return newScope().Invalid().GetScope()
}
// Scope the index expression.
exprScope := sb.getScope(node.GetNode(parser.NodeSliceExpressionIndex), context)
if !exprScope.GetIsValid() {
return newScope().Invalid().GetScope()
}
// Ensure the index expression type matches that expected.
exprType := exprScope.ResolvedTypeRef(sb.sg.tdg)
parameterType := operator.ParameterTypes()[0].TransformUnder(childType)
if serr := exprType.CheckSubTypeOf(parameterType); serr != nil {
sb.decorateWithError(node, "Indexer parameter must be type %v: %v", parameterType, serr)
return newScope().Invalid().GetScope()
}
if context.accessOption == scopeSetAccess {
return newScope().Valid().ForNamedScopeUnderType(sb.getNamedScopeForMember(operator), childType, context).GetScope()
} else {
returnType, _ := operator.ReturnType()
return newScope().Valid().CallsOperator(operator).Resolving(returnType.TransformUnder(childType)).GetScope()
}
}
示例4: buildResolveStatement
// buildResolveStatement builds the CodeDOM for a resolve statement.
func (db *domBuilder) buildResolveStatement(node compilergraph.GraphNode) (codedom.Statement, codedom.Statement) {
sourceExpr := db.getExpression(node, parser.NodeResolveStatementSource)
destinationNode := node.GetNode(parser.NodeAssignedDestination)
destinationScope, _ := db.scopegraph.GetScope(destinationNode)
destinationName := destinationNode.Get(parser.NodeNamedValueName)
var destinationStatement codedom.Statement = nil
if !destinationScope.GetIsAnonymousReference() {
destinationStatement = codedom.VarDefinitionWithInit(destinationName, sourceExpr, node)
} else {
destinationStatement = codedom.ExpressionStatement(sourceExpr, node)
destinationName = ""
}
// If the resolve statement has a rejection value, then we need to wrap the source expression
// call to catch any rejections *or* exceptions.
rejectionNode, hasRejection := node.TryGetNode(parser.NodeAssignedRejection)
if hasRejection {
rejectionName := rejectionNode.Get(parser.NodeNamedValueName)
rejectionScope, _ := db.scopegraph.GetScope(rejectionNode)
if rejectionScope.GetIsAnonymousReference() {
rejectionName = ""
}
empty := codedom.EmptyStatement(node)
return codedom.ResolveExpression(sourceExpr, destinationName, rejectionName, empty, node), empty
} else {
// Otherwise, we simply execute the expression, optionally assigning it to the
// destination variable.
return destinationStatement, destinationStatement
}
}
示例5: scopeInlineLambaExpression
// scopeInlineLambaExpression scopes an inline lambda expression node in the SRG.
func (sb *scopeBuilder) scopeInlineLambaExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
var returnType = sb.sg.tdg.AnyTypeReference()
// Scope the lambda's internal expression.
exprScope := sb.getScope(node.GetNode(parser.NodeLambdaExpressionChildExpr), context)
if exprScope.GetIsValid() {
returnType = exprScope.ResolvedTypeRef(sb.sg.tdg)
}
// Build the function type.
var functionType = sb.sg.tdg.FunctionTypeReference(returnType)
// Add the parameter types.
pit := node.StartQuery().
Out(parser.NodeLambdaExpressionInferredParameter).
BuildNodeIterator()
for pit.Next() {
parameterType, hasParameterType := sb.inferredParameterTypes.Get(string(pit.Node().NodeId))
if hasParameterType {
functionType = functionType.WithParameter(parameterType.(typegraph.TypeReference))
} else {
functionType = functionType.WithParameter(sb.sg.tdg.AnyTypeReference())
}
}
return newScope().IsValid(exprScope.GetIsValid()).Resolving(functionType).GetScope()
}
示例6: buildCastExpression
// buildCastExpression builds the CodeDOM for a cast expression.
func (db *domBuilder) buildCastExpression(node compilergraph.GraphNode) codedom.Expression {
childExpr := db.getExpression(node, parser.NodeCastExpressionChildExpr)
// Determine the resulting type.
scope, _ := db.scopegraph.GetScope(node)
resultingType := scope.ResolvedTypeRef(db.scopegraph.TypeGraph())
// If the resulting type is a structural subtype of the child expression's type, then
// we are accessing the automatically composited inner instance.
childScope, _ := db.scopegraph.GetScope(node.GetNode(parser.NodeCastExpressionChildExpr))
childType := childScope.ResolvedTypeRef(db.scopegraph.TypeGraph())
if childType.CheckStructuralSubtypeOf(resultingType) {
return codedom.NestedTypeAccess(childExpr, resultingType, node)
}
// Otherwise, add a cast call with the cast type.
typeLiteral := codedom.TypeLiteral(resultingType, node)
allowNull := codedom.LiteralValue("false", node)
if resultingType.NullValueAllowed() {
allowNull = codedom.LiteralValue("true", node)
}
return codedom.RuntimeFunctionCall(codedom.CastFunction, []codedom.Expression{childExpr, typeLiteral, allowNull}, node)
}
示例7: buildArrowStatement
// buildArrowStatement builds the CodeDOM for an arrow statement.
func (db *domBuilder) buildArrowStatement(node compilergraph.GraphNode) (codedom.Statement, codedom.Statement) {
sourceExpr := codedom.RuntimeFunctionCall(
codedom.TranslatePromiseFunction,
[]codedom.Expression{db.getExpression(node, parser.NodeArrowStatementSource)},
node)
destinationNode := node.GetNode(parser.NodeArrowStatementDestination)
destinationScope, _ := db.scopegraph.GetScope(destinationNode)
var destinationTarget codedom.Expression = nil
var rejectionTarget codedom.Expression = nil
// Retrieve the expression of the destination variable.
if !destinationScope.GetIsAnonymousReference() {
destinationTarget = db.buildAssignmentExpression(destinationNode, codedom.LocalReference("resolved", node), node)
}
// Retrieve the expression of the rejection variable.
rejectionNode, hasRejection := node.TryGetNode(parser.NodeArrowStatementRejection)
if hasRejection {
rejectionScope, _ := db.scopegraph.GetScope(rejectionNode)
if !rejectionScope.GetIsAnonymousReference() {
rejectionTarget = db.buildAssignmentExpression(rejectionNode, codedom.LocalReference("rejected", node), node)
}
}
empty := codedom.EmptyStatement(node)
promise := codedom.ArrowPromise(sourceExpr, destinationTarget, rejectionTarget, empty, node)
return promise, empty
}
示例8: buildStructuralNewExpression
// buildStructuralNewExpression builds the CodeDOM for a structural new expression.
func (db *domBuilder) buildStructuralNewExpression(node compilergraph.GraphNode) codedom.Expression {
// Collect the full set of initializers, by member.
initializers := map[string]codedom.Expression{}
eit := node.StartQuery().
Out(parser.NodeStructuralNewExpressionChildEntry).
BuildNodeIterator()
for eit.Next() {
entryScope, _ := db.scopegraph.GetScope(eit.Node())
entryName, _ := db.scopegraph.GetReferencedName(entryScope)
entryMember, _ := entryName.Member()
initializers[entryMember.Name()] = db.getExpression(eit.Node(), parser.NodeStructuralNewEntryValue)
}
childScope, _ := db.scopegraph.GetScope(node.GetNode(parser.NodeStructuralNewTypeExpression))
nodeScope, _ := db.scopegraph.GetScope(node)
if nodeScope.HasLabel(proto.ScopeLabel_STRUCTURAL_UPDATE_EXPR) {
// Build a call to the Clone() method followed by assignments.
resolvedTypeRef := childScope.ResolvedTypeRef(db.scopegraph.TypeGraph())
return db.buildStructCloneExpression(resolvedTypeRef, initializers, node)
} else {
// Build a call to the new() constructor of the type with the required field expressions.
staticTypeRef := childScope.StaticTypeRef(db.scopegraph.TypeGraph())
return db.buildStructInitializerExpression(staticTypeRef, initializers, node)
}
}
示例9: scopeRootTypeExpression
// scopeRootTypeExpression scopes a root-type expression in the SRG.
func (sb *scopeBuilder) scopeRootTypeExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
// Get the scope of the sub expression.
childScope := sb.getScope(node.GetNode(parser.NodeUnaryExpressionChildExpr), context)
// Ensure that the child scope is valid.
if !childScope.GetIsValid() {
return newScope().Invalid().GetScope()
}
childType := childScope.ResolvedTypeRef(sb.sg.tdg)
// Ensure the child type is not void.
if childType.IsVoid() || childType.IsNull() {
sb.decorateWithError(node, "Root type operator (&) cannot be applied to value of type %v", childType)
return newScope().Invalid().GetScope()
}
// Ensure the child type is nominal, interface or any.
if !childType.IsAny() {
referredType := childType.ReferredType()
if referredType.TypeKind() == typegraph.NominalType {
// The result of the operator is the nominal type's parent type.
return newScope().Valid().Resolving(referredType.ParentTypes()[0]).GetScope()
}
if referredType.TypeKind() != typegraph.ImplicitInterfaceType && referredType.TypeKind() != typegraph.GenericType {
sb.decorateWithError(node, "Root type operator (&) cannot be applied to value of type %v", childType)
return newScope().Invalid().GetScope()
}
}
// The result of the operator is a value of any type.
return newScope().Valid().Resolving(sb.sg.tdg.AnyTypeReference()).GetScope()
}
示例10: buildBinaryOperatorExpression
// buildBinaryOperatorExpression builds the CodeDOM for a binary operator.
func (db *domBuilder) buildBinaryOperatorExpression(node compilergraph.GraphNode, modifier exprModifier) codedom.Expression {
scope, _ := db.scopegraph.GetScope(node)
operator, _ := scope.CalledOperator(db.scopegraph.TypeGraph())
if operator.IsNative() {
return db.buildNativeBinaryExpression(node, operatorMap[node.Kind()])
}
leftExpr := db.getExpression(node, parser.NodeBinaryExpressionLeftExpr)
rightExpr := db.getExpression(node, parser.NodeBinaryExpressionRightExpr)
leftScope, _ := db.scopegraph.GetScope(node.GetNode(parser.NodeBinaryExpressionLeftExpr))
parentType := leftScope.ResolvedTypeRef(db.scopegraph.TypeGraph())
optimized, wasOptimized := db.buildOptimizedBinaryOperatorExpression(node, parentType, leftExpr, rightExpr)
if wasOptimized {
return optimized
}
callExpr := codedom.MemberCall(codedom.StaticMemberReference(operator, parentType, node), operator, []codedom.Expression{leftExpr, rightExpr}, node)
if modifier != nil {
return modifier(callExpr)
}
return callExpr
}
示例11: scopeTaggedTemplateString
// scopeTaggedTemplateString scopes a tagged template string expression in the SRG.
func (sb *scopeBuilder) scopeTaggedTemplateString(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
var isValid = true
// Scope the tagging expression.
tagScope := sb.getScope(node.GetNode(parser.NodeTaggedTemplateCallExpression), context)
if !tagScope.GetIsValid() {
isValid = false
}
// Scope the template string.
templateScope := sb.getScope(node.GetNode(parser.NodeTaggedTemplateParsed), context)
if !templateScope.GetIsValid() {
isValid = false
}
// Ensure that the tagging expression is a function of type function<(any here)>(slice<string>, slice<stringable>).
if tagScope.GetIsValid() {
tagType := tagScope.ResolvedTypeRef(sb.sg.tdg)
if !tagType.IsDirectReferenceTo(sb.sg.tdg.FunctionType()) ||
tagType.ParameterCount() != 2 ||
tagType.Parameters()[0] != sb.sg.tdg.SliceTypeReference(sb.sg.tdg.StringTypeReference()) ||
tagType.Parameters()[1] != sb.sg.tdg.SliceTypeReference(sb.sg.tdg.StringableTypeReference()) {
isValid = false
sb.decorateWithError(node, "Tagging expression for template string must be function with parameters ([]string, []stringable). Found: %v", tagType)
}
return newScope().IsValid(isValid).Resolving(tagType.Generics()[0]).GetScope()
} else {
return newScope().IsValid(isValid).Resolving(sb.sg.tdg.AnyTypeReference()).GetScope()
}
}
示例12: scopeSliceLiteralExpression
// scopeSliceLiteralExpression scopes a slice literal expression in the SRG.
func (sb *scopeBuilder) scopeSliceLiteralExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
var isValid = true
declaredTypeNode := node.GetNode(parser.NodeSliceLiteralExpressionType)
declaredType, rerr := sb.sg.ResolveSRGTypeRef(sb.sg.srg.GetTypeRef(declaredTypeNode))
if rerr != nil {
sb.decorateWithError(node, "%v", rerr)
return newScope().Invalid().Resolving(sb.sg.tdg.SliceTypeReference(sb.sg.tdg.AnyTypeReference())).GetScope()
}
// Scope each of the expressions and ensure they match the slice type.
vit := node.StartQuery().
Out(parser.NodeSliceLiteralExpressionValue).
BuildNodeIterator()
for vit.Next() {
valueNode := vit.Node()
valueScope := sb.getScope(valueNode, context)
if !valueScope.GetIsValid() {
isValid = false
} else {
if serr := valueScope.ResolvedTypeRef(sb.sg.tdg).CheckSubTypeOf(declaredType); serr != nil {
isValid = false
sb.decorateWithError(node, "Invalid slice literal value: %v", serr)
}
}
}
return newScope().IsValid(isValid).Resolving(sb.sg.tdg.SliceTypeReference(declaredType)).GetScope()
}
示例13: scopeConditionalExpression
// scopeConditionalExpression scopes a conditional expression in the SRG.
func (sb *scopeBuilder) scopeConditionalExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
conditionalExprNode := node.GetNode(parser.NodeConditionalExpressionCheckExpression)
thenContext := sb.inferTypesForConditionalExpressionContext(context, conditionalExprNode, inferredDirect)
elseContext := sb.inferTypesForConditionalExpressionContext(context, conditionalExprNode, inferredInverted)
// Scope the child expressions.
checkScope := sb.getScope(conditionalExprNode, context)
thenScope := sb.getScope(node.GetNode(parser.NodeConditionalExpressionThenExpression), thenContext)
elseScope := sb.getScope(node.GetNode(parser.NodeConditionalExpressionElseExpression), elseContext)
if !checkScope.GetIsValid() || !thenScope.GetIsValid() || !elseScope.GetIsValid() {
return newScope().Invalid().GetScope()
}
// Intersect the then and else types.
thenType := thenScope.ResolvedTypeRef(sb.sg.tdg)
elseType := elseScope.ResolvedTypeRef(sb.sg.tdg)
resultType := thenType.Intersect(elseType)
// Ensure that the check is a boolean expression.
checkType := checkScope.ResolvedTypeRef(sb.sg.tdg)
if !checkType.IsDirectReferenceTo(sb.sg.tdg.BoolType()) {
sb.decorateWithError(node, "Conditional expression check must be of type 'bool', found: %v", checkType)
return newScope().Invalid().GetScope()
}
return newScope().Valid().Resolving(resultType).GetScope()
}
示例14: scopeBooleanBinaryExpression
// scopeBooleanBinaryExpression scopes a boolean binary operator expression in the SRG.
func (sb *scopeBuilder) scopeBooleanBinaryExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
// Get the scope of the left and right expressions.
leftScope := sb.getScope(node.GetNode(parser.NodeBinaryExpressionLeftExpr), context)
rightScope := sb.getScope(node.GetNode(parser.NodeBinaryExpressionRightExpr), context)
// Ensure that both scopes are valid.
if !leftScope.GetIsValid() || !rightScope.GetIsValid() {
return newScope().Invalid().GetScope()
}
// Ensure that both scopes have type boolean.
var isValid = true
leftType := leftScope.ResolvedTypeRef(sb.sg.tdg)
rightType := rightScope.ResolvedTypeRef(sb.sg.tdg)
if !leftType.IsDirectReferenceTo(sb.sg.tdg.BoolType()) {
sb.decorateWithError(node, "Boolean operator requires type Boolean for operands. Left hand operand has type: %v", leftType)
isValid = false
}
if !rightType.IsDirectReferenceTo(sb.sg.tdg.BoolType()) {
sb.decorateWithError(node, "Boolean operator requires type Boolean for operands. Right hand operand has type: %v", rightType)
isValid = false
}
return newScope().IsValid(isValid).Resolving(sb.sg.tdg.BoolTypeReference()).GetScope()
}
示例15: scopeInCollectionExpression
// scopeInCollectionExpression scopes an 'in' collection expression in the SRG.
func (sb *scopeBuilder) scopeInCollectionExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
// Get the scope of the left and right expressions.
leftScope := sb.getScope(node.GetNode(parser.NodeBinaryExpressionLeftExpr), context)
rightScope := sb.getScope(node.GetNode(parser.NodeBinaryExpressionRightExpr), context)
// Ensure that both scopes are valid.
if !leftScope.GetIsValid() || !rightScope.GetIsValid() {
return newScope().Invalid().GetScope()
}
// Ensure that the right side has a 'contains' operator defined.
rightType := rightScope.ResolvedTypeRef(sb.sg.tdg)
module := compilercommon.InputSource(node.Get(parser.NodePredicateSource))
operator, rerr := rightType.ResolveAccessibleMember("contains", module, typegraph.MemberResolutionOperator)
if rerr != nil {
sb.decorateWithError(node, "Operator 'contains' is not defined on type '%v'", rightType)
return newScope().Invalid().GetScope()
}
// Ensure the right side is not nullable.
if rightType.NullValueAllowed() {
sb.decorateWithError(node, "Cannot invoke operator 'in' on nullable value of type '%v'", rightType)
return newScope().Invalid().GetScope()
}
// Ensure that the left side can be used as the operator's parameter.
parameterType := operator.ParameterTypes()[0].TransformUnder(rightType)
leftType := leftScope.ResolvedTypeRef(sb.sg.tdg)
if serr := leftType.CheckSubTypeOf(parameterType); serr != nil {
sb.decorateWithError(node, "Cannot invoke operator 'in' with value of type '%v': %v", leftType, serr)
return newScope().Invalid().GetScope()
}
return newScope().Valid().CallsOperator(operator).Resolving(sb.sg.tdg.BoolTypeReference()).GetScope()
}