本文整理汇总了Golang中github.com/serulian/compiler/compilergraph.GraphNode类的典型用法代码示例。如果您正苦于以下问题:Golang GraphNode类的具体用法?Golang GraphNode怎么用?Golang GraphNode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GraphNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: scopeLambdaExpression
// scopeLambdaExpression scopes a lambda expression in the SRG.
func (sb *scopeBuilder) scopeLambdaExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
if _, ok := node.TryGetNode(parser.NodeLambdaExpressionBlock); ok {
return sb.scopeFullLambaExpression(node, context)
} else {
return sb.scopeInlineLambaExpression(node, context)
}
}
示例3: AsImplementable
// AsImplementable returns the given node as an SRGImplementable (if applicable).
func (g *SRG) AsImplementable(node compilergraph.GraphNode) (SRGImplementable, bool) {
switch node.Kind() {
case parser.NodeTypeConstructor:
fallthrough
case parser.NodeTypeFunction:
fallthrough
case parser.NodeTypeProperty:
fallthrough
case parser.NodeTypeOperator:
fallthrough
case parser.NodeTypeField:
fallthrough
case parser.NodeTypeVariable:
fallthrough
case parser.NodeTypePropertyBlock:
return SRGImplementable{node, g}, true
default:
return SRGImplementable{}, false
}
}
示例4: 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()
}
示例5: 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()
}
示例6: buildMappingLiteralExpression
// buildMappingLiteralExpression builds the CodeDOM for a mapping literal expression.
func (db *domBuilder) buildMappingLiteralExpression(node compilergraph.GraphNode) codedom.Expression {
mappingScope, _ := db.scopegraph.GetScope(node)
mappingType := mappingScope.ResolvedTypeRef(db.scopegraph.TypeGraph())
eit := node.StartQuery().
Out(parser.NodeMappingLiteralExpressionEntryRef).
BuildNodeIterator()
var entries = make([]codedom.ObjectLiteralEntryNode, 0)
for eit.Next() {
entryNode := eit.Node()
// The key expression must be a string when produced. We either reference it directly (if a string)
// or call .String() (if a Stringable).
keyNode := entryNode.GetNode(parser.NodeMappingLiteralExpressionEntryKey)
keyScope, _ := db.scopegraph.GetScope(keyNode)
keyType := keyScope.ResolvedTypeRef(db.scopegraph.TypeGraph())
var keyExpr = db.buildExpression(keyNode)
if !keyType.HasReferredType(db.scopegraph.TypeGraph().StringType()) {
stringMethod, _ := keyType.ResolveMember("String", typegraph.MemberResolutionInstance)
keyExpr = codedom.MemberCall(
codedom.MemberReference(db.buildExpression(keyNode), stringMethod, node),
stringMethod,
[]codedom.Expression{},
keyNode)
}
// Get the expression for the value.
valueExpr := db.getExpression(entryNode, parser.NodeMappingLiteralExpressionEntryValue)
// Build an object literal expression with the (native version of the) key string and the
// created value.
entryExpr := codedom.ObjectLiteralEntryNode{
codedom.NominalUnwrapping(keyExpr, db.scopegraph.TypeGraph().StringTypeReference(), keyNode),
valueExpr,
entryNode,
}
entries = append(entries, entryExpr)
}
if len(entries) == 0 {
// Empty mapping. Call the Empty() constructor directly.
constructor, _ := mappingType.ResolveMember("Empty", typegraph.MemberResolutionStatic)
return codedom.MemberCall(
codedom.MemberReference(codedom.TypeLiteral(mappingType, node), constructor, node),
constructor,
[]codedom.Expression{},
node)
}
constructor, _ := mappingType.ResolveMember("overObject", typegraph.MemberResolutionStatic)
return codedom.MemberCall(
codedom.MemberReference(codedom.TypeLiteral(mappingType, node), constructor, node),
constructor,
[]codedom.Expression{codedom.ObjectLiteral(entries, node)},
node)
}
示例7: scopeListLiteralExpression
// scopeListLiteralExpression scopes a list literal expression in the SRG.
func (sb *scopeBuilder) scopeListLiteralExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
var isValid = true
var valueType = sb.sg.tdg.VoidTypeReference()
// Scope each of the expressions and determine the list type based on its contents.
vit := node.StartQuery().
Out(parser.NodeListExpressionValue).
BuildNodeIterator()
for vit.Next() {
valueNode := vit.Node()
valueScope := sb.getScope(valueNode, context)
if !valueScope.GetIsValid() {
isValid = false
} else {
valueType = valueType.Intersect(valueScope.ResolvedTypeRef(sb.sg.tdg))
}
}
if valueType.IsVoid() {
valueType = sb.sg.tdg.AnyTypeReference()
}
return newScope().IsValid(isValid).Resolving(sb.sg.tdg.ListTypeReference(valueType)).GetScope()
}
示例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: 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))
}
示例10: 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()
}
示例11: buildLambdaExpressionInternal
func (db *domBuilder) buildLambdaExpressionInternal(node compilergraph.GraphNode, paramPredicate compilergraph.Predicate, body codedom.StatementOrExpression, isGenerator bool) codedom.Expression {
// Collect the generic names and parameter names of the lambda expression.
var generics = make([]string, 0)
var parameters = make([]string, 0)
git := node.StartQuery().
Out(parser.NodePredicateTypeMemberGeneric).
BuildNodeIterator(parser.NodeGenericPredicateName)
for git.Next() {
generics = append(generics, git.GetPredicate(parser.NodeGenericPredicateName).String())
}
pit := node.StartQuery().
Out(paramPredicate).
BuildNodeIterator(parser.NodeLambdaExpressionParameterName)
for pit.Next() {
parameters = append(parameters, pit.GetPredicate(parser.NodeLambdaExpressionParameterName).String())
}
// Check for a generator.
specialization := codedom.NormalFunction
if isGenerator {
specialization = codedom.GeneratorFunction
}
return codedom.FunctionDefinition(generics, parameters, body, false, specialization, node)
}
示例12: 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()
}
示例13: 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()
}
示例14: 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()
}
示例15: 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
}
}