当前位置: 首页>>代码示例>>Golang>>正文


Golang GraphNode.TryGetNode方法代码示例

本文整理汇总了Golang中github.com/serulian/compiler/compilergraph.GraphNode.TryGetNode方法的典型用法代码示例。如果您正苦于以下问题:Golang GraphNode.TryGetNode方法的具体用法?Golang GraphNode.TryGetNode怎么用?Golang GraphNode.TryGetNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/serulian/compiler/compilergraph.GraphNode的用法示例。


在下文中一共展示了GraphNode.TryGetNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: 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
}
开发者ID:Serulian,项目名称:compiler,代码行数:31,代码来源:build_statements.go

示例2: 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
	}
}
开发者ID:Serulian,项目名称:compiler,代码行数:34,代码来源:build_statements.go

示例3: buildMatchStatement

// buildMatchStatement builds the CodeDOM for a match statement.
func (db *domBuilder) buildMatchStatement(node compilergraph.GraphNode) (codedom.Statement, codedom.Statement) {
	// Retrieve (or generate) the name of a variable to hold the value being matched against.
	var matchExprVarName = ""
	if namedValue, hasNamedValue := node.TryGetNode(parser.NodeStatementNamedValue); hasNamedValue {
		matchExprVarName = namedValue.Get(parser.NodeNamedValueName)
	} else {
		matchExprVarName = db.generateScopeVarName(node)
	}

	// Set the match expression's value into the variable.
	startStatement := codedom.VarDefinitionWithInit(matchExprVarName, db.getExpression(node, parser.NodeMatchStatementExpression), node)

	getCheckExpression := func(caseTypeRefNode compilergraph.GraphNode) codedom.Expression {
		caseTypeLiteral, _ := db.scopegraph.ResolveSRGTypeRef(
			db.scopegraph.SourceGraph().GetTypeRef(caseTypeRefNode))

		return codedom.NominalWrapping(
			codedom.RuntimeFunctionCall(codedom.IsTypeFunction,
				[]codedom.Expression{
					codedom.LocalReference(matchExprVarName, caseTypeRefNode),
					codedom.TypeLiteral(caseTypeLiteral, caseTypeRefNode),
				},
				caseTypeRefNode),
			db.scopegraph.TypeGraph().BoolType(),
			caseTypeRefNode)
	}

	return db.buildJumpingCaseStatement(node, parser.NodeMatchStatementCase,
		parser.NodeMatchStatementCaseStatement, parser.NodeMatchStatementCaseTypeReference,
		startStatement, getCheckExpression)
}
开发者ID:Serulian,项目名称:compiler,代码行数:32,代码来源:build_statements.go

示例4: 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)
	}
}
开发者ID:Serulian,项目名称:compiler,代码行数:8,代码来源:scope_lambda_expr.go

示例5: scopeImplementedMember

// scopeImplementedMember scopes an implemented type member.
func (sb *scopeBuilder) scopeImplementedMember(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
	if body, hasBody := node.TryGetNode(parser.NodePredicateBody); hasBody {
		scope := sb.getScope(body, context)
		return *scope
	} else {
		return newScope().GetScope()
	}
}
开发者ID:Serulian,项目名称:compiler,代码行数:9,代码来源:scope_members.go

示例6: tryGetExpression

// tryGetExpression attempts to retrieve the predicate on the given node and, if found, build it as an
// expression.
func (db *domBuilder) tryGetExpression(node compilergraph.GraphNode, predicate compilergraph.Predicate) (codedom.Expression, bool) {
	childNode, hasChild := node.TryGetNode(predicate)
	if !hasChild {
		return nil, false
	}

	return db.buildExpression(childNode), true
}
开发者ID:Serulian,项目名称:compiler,代码行数:10,代码来源:dombuilder.go

示例7: tryGetStatements

// tryGetStatements attempts to retrieve the predicate on the given node and, if found, build it as
// start and end statements.
func (db *domBuilder) tryGetStatements(node compilergraph.GraphNode, predicate compilergraph.Predicate) (codedom.Statement, codedom.Statement, bool) {
	childNode, hasChild := node.TryGetNode(predicate)
	if !hasChild {
		return nil, nil, false
	}

	start, end := db.buildStatements(childNode)
	return start, end, true
}
开发者ID:Serulian,项目名称:compiler,代码行数:11,代码来源:dombuilder.go

示例8: buildSliceExpression

// buildSliceExpression builds the CodeDOM for a slicer or indexer expression.
func (db *domBuilder) buildSliceExpression(node compilergraph.GraphNode) codedom.Expression {
	// Check if this is a slice vs an index.
	_, isIndexer := node.TryGetNode(parser.NodeSliceExpressionIndex)
	if isIndexer {
		return db.buildIndexerExpression(node)
	} else {
		return db.buildSlicerExpression(node)
	}
}
开发者ID:Serulian,项目名称:compiler,代码行数:10,代码来源:build_op_expr.go

示例9: scopeSliceExpression

// scopeSliceExpression scopes a slice expression in the SRG.
func (sb *scopeBuilder) scopeSliceExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
	// Check if this is a slice vs an index.
	_, isIndexer := node.TryGetNode(parser.NodeSliceExpressionIndex)
	if isIndexer {
		return sb.scopeIndexerExpression(node, context)
	} else {
		return sb.scopeSlicerExpression(node, context)
	}
}
开发者ID:Serulian,项目名称:compiler,代码行数:10,代码来源:scope_op_expr.go

示例10: scopeLoopStatement

// scopeLoopStatement scopes a loop statement in the SRG.
func (sb *scopeBuilder) scopeLoopStatement(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
	// Scope the underlying block.
	blockNode := node.GetNode(parser.NodeLoopStatementBlock)
	blockScope := sb.getScope(blockNode, context.withContinuable(node))

	// If the loop has no expression, it is most likely an infinite loop, so we know it is valid and
	// returns whatever the internal type is.
	loopExprNode, hasExpr := node.TryGetNode(parser.NodeLoopStatementExpression)
	if !hasExpr {
		loopScopeBuilder := newScope().
			IsValid(blockScope.GetIsValid()).
			ReturningTypeOf(blockScope).
			LabelSetOfExcept(blockScope, proto.ScopeLabel_BROKEN_FLOW)

		// If the loop contains a break statement somewhere, then it isn't an infinite
		// loop.
		if !blockScope.HasLabel(proto.ScopeLabel_BROKEN_FLOW) {
			loopScopeBuilder.IsTerminatingStatement()
		}

		// for { ... }
		return loopScopeBuilder.GetScope()
	}

	// Otherwise, scope the expression.
	loopExprScope := sb.getScope(loopExprNode, context)
	if !loopExprScope.GetIsValid() {
		return newScope().
			Invalid().
			GetScope()
	}

	loopExprType := loopExprScope.ResolvedTypeRef(sb.sg.tdg)

	// If the loop has a variable defined, we'll check above that the loop expression is a Stream or Streamable. Otherwise,
	// it must be a boolean value.
	varNode, hasVar := node.TryGetNode(parser.NodeStatementNamedValue)
	if hasVar {
		if !sb.getScope(varNode, context).GetIsValid() {
			return newScope().Invalid().GetScope()
		}

		return newScope().Valid().LabelSetOf(blockScope).GetScope()
	} else {
		if !loopExprType.IsDirectReferenceTo(sb.sg.tdg.BoolType()) {
			sb.decorateWithError(node, "Loop conditional expression must be of type 'bool', found: %v", loopExprType)
			return newScope().
				Invalid().
				GetScope()
		}

		return newScope().Valid().LabelSetOfExcept(blockScope, proto.ScopeLabel_BROKEN_FLOW).GetScope()
	}
}
开发者ID:Serulian,项目名称:compiler,代码行数:55,代码来源:scope_statements.go

示例11: buildLambdaExpression

// buildLambdaExpression builds the CodeDOM for a lambda expression.
func (db *domBuilder) buildLambdaExpression(node compilergraph.GraphNode) codedom.Expression {
	if blockNode, ok := node.TryGetNode(parser.NodeLambdaExpressionBlock); ok {
		blockStatement, _ := db.buildStatements(blockNode)
		bodyScope, _ := db.scopegraph.GetScope(blockNode)
		isGenerator := bodyScope.HasLabel(proto.ScopeLabel_GENERATOR_STATEMENT)
		return db.buildLambdaExpressionInternal(node, parser.NodeLambdaExpressionParameter, blockStatement, isGenerator)
	} else {
		bodyExpr := db.getExpression(node, parser.NodeLambdaExpressionChildExpr)
		return db.buildLambdaExpressionInternal(node, parser.NodeLambdaExpressionInferredParameter, bodyExpr, false)
	}
}
开发者ID:Serulian,项目名称:compiler,代码行数:12,代码来源:build_lamda_expr.go

示例12: scopeResolveStatement

// scopeResolveStatement scopes a resolve statement in the SRG.
func (sb *scopeBuilder) scopeResolveStatement(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
	destinationScope := sb.getScope(node.GetNode(parser.NodeAssignedDestination), context)
	sourceScope := sb.getScope(node.GetNode(parser.NodeResolveStatementSource), context)

	isValid := sourceScope.GetIsValid() && destinationScope.GetIsValid()

	if rejection, ok := node.TryGetNode(parser.NodeAssignedRejection); ok {
		rejectionScope := sb.getScope(rejection, context)
		isValid = isValid && rejectionScope.GetIsValid()
	}

	return newScope().IsValid(isValid).GetScope()
}
开发者ID:Serulian,项目名称:compiler,代码行数:14,代码来源:scope_statements.go

示例13: buildWithStatement

// buildWithStatement builds the CodeDOM for a with statement.
func (db *domBuilder) buildWithStatement(node compilergraph.GraphNode) codedom.Statement {
	namedVar, hasNamed := node.TryGetNode(parser.NodeStatementNamedValue)

	var resourceVar = ""
	if hasNamed {
		resourceVar = namedVar.Get(parser.NodeNamedValueName)
	} else {
		resourceVar = db.generateScopeVarName(node)
	}

	resourceExpr := db.getExpression(node, parser.NodeWithStatementExpression)
	withStatement, _ := db.getStatements(node, parser.NodeWithStatementBlock)

	return codedom.ResourceBlock(resourceVar, resourceExpr, withStatement, node)
}
开发者ID:Serulian,项目名称:compiler,代码行数:16,代码来源:build_statements.go

示例14: scopeConditionalStatement

// scopeConditionalStatement scopes a conditional statement in the SRG.
func (sb *scopeBuilder) scopeConditionalStatement(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
	var returningType = sb.sg.tdg.VoidTypeReference()
	var valid = true
	var labelSet = newLabelSet()

	conditionalExprNode := node.GetNode(parser.NodeConditionalStatementConditional)

	// Scope the child block(s).
	statementBlockContext := sb.inferTypesForConditionalExpressionContext(context, conditionalExprNode, inferredDirect)
	statementBlockScope := sb.getScope(node.GetNode(parser.NodeConditionalStatementBlock), statementBlockContext)
	labelSet.AppendLabelsOf(statementBlockScope)
	if !statementBlockScope.GetIsValid() {
		valid = false
	}

	var isSettlingScope = false

	elseClauseNode, hasElseClause := node.TryGetNode(parser.NodeConditionalStatementElseClause)
	if hasElseClause {
		elseClauseContext := sb.inferTypesForConditionalExpressionContext(context, conditionalExprNode, inferredInverted)
		elseClauseScope := sb.getScope(elseClauseNode, elseClauseContext)
		labelSet.AppendLabelsOf(elseClauseScope)

		if !elseClauseScope.GetIsValid() {
			valid = false
		}

		// The type returned by this conditional is only non-void if both the block and the
		// else clause return values.
		returningType = statementBlockScope.ReturnedTypeRef(sb.sg.tdg).
			Intersect(elseClauseScope.ReturnedTypeRef(sb.sg.tdg))
		isSettlingScope = statementBlockScope.GetIsSettlingScope() && elseClauseScope.GetIsSettlingScope()
	}

	// Scope the conditional expression and make sure it has type boolean.
	conditionalExprScope := sb.getScope(conditionalExprNode, context)
	if !conditionalExprScope.GetIsValid() {
		return newScope().Invalid().Returning(returningType, isSettlingScope).WithLabelSet(labelSet).GetScope()
	}

	conditionalExprType := conditionalExprScope.ResolvedTypeRef(sb.sg.tdg)
	if !conditionalExprType.IsDirectReferenceTo(sb.sg.tdg.BoolType()) {
		sb.decorateWithError(node, "Conditional expression must be of type 'bool', found: %v", conditionalExprType)
		return newScope().Invalid().Returning(returningType, isSettlingScope).WithLabelSet(labelSet).GetScope()
	}

	return newScope().IsValid(valid).Returning(returningType, isSettlingScope).WithLabelSet(labelSet).GetScope()
}
开发者ID:Serulian,项目名称:compiler,代码行数:49,代码来源:scope_statements.go

示例15: scopeReturnStatement

// scopeReturnStatement scopes a return statement in the SRG.
func (sb *scopeBuilder) scopeReturnStatement(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
	var actualReturnType typegraph.TypeReference = sb.sg.tdg.VoidTypeReference()
	exprNode, found := node.TryGetNode(parser.NodeReturnStatementValue)
	if found {
		exprScope := sb.getScope(exprNode, context)
		if !exprScope.GetIsValid() {
			return newScope().
				Invalid().
				GetScope()
		}

		actualReturnType = exprScope.ResolvedTypeRef(sb.sg.tdg)
	}

	// Ensure the return types match.
	expectedReturnType, _ := sb.sg.tdg.LookupReturnType(context.parentImplemented)
	if expectedReturnType.IsVoid() {
		if !actualReturnType.IsVoid() {
			sb.decorateWithError(node, "No return value expected here, found value of type '%v'", actualReturnType)
			return newScope().
				Invalid().
				Returning(actualReturnType, true).
				GetScope()
		}
	} else if actualReturnType.IsVoid() {
		sb.decorateWithError(node, "Expected non-void resolved value")
		return newScope().
			Invalid().
			Returning(actualReturnType, true).
			GetScope()
	} else {
		if serr := actualReturnType.CheckSubTypeOf(expectedReturnType); serr != nil {
			sb.decorateWithError(node, "Expected return value of type '%v': %v", expectedReturnType, serr)
			return newScope().
				Invalid().
				Returning(actualReturnType, true).
				GetScope()
		}
	}

	return newScope().
		IsTerminatingStatement().
		Valid().
		Returning(actualReturnType, true).
		GetScope()
}
开发者ID:Serulian,项目名称:compiler,代码行数:47,代码来源:scope_statements.go


注:本文中的github.com/serulian/compiler/compilergraph.GraphNode.TryGetNode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。