Пример #1
0
func setupFuncMap(fm expressionParsing.FunctionMap, typeDef expressionParsing.FuncTypeDefinition) {
	if ft, ok := typeDef.ReturnType().(expressionParsing.FuncTypeDefinition); ok {
		setupFuncMap(fm, ft)
	}
	var argType expressionParsing.TypeDefinition
	if ptd, ok := typeDef.ArgumentType().(expressionParsing.PrimTypeDefinition); ok {
		argType = expressionParsing.NewArgTypeDefinition(ptd)
	} else {
		argType = typeDef.ArgumentType()
	}
	fm.AddFunction(typeDef.ArgumentName(), argType)
}
Пример #2
0
func (ds *LetStatement) GenerateGo(fm expressionParsing.FunctionMap) (string, expressionParsing.TypeDefinition, parser.SyntaxError) {
	innerCode, returnType, synErr := ds.innerStatement.GenerateGo(fm.NextScopeLayer())

	if synErr != nil {
		return "", nil, synErr
	}

	name, err := fm.AddFunction(ds.varName, returnType)
	if err != nil {
		return "", nil, parser.NewSyntaxError(err.Error(), 0, 0)
	}

	var genCode string
	if _, ok := returnType.(expressionParsing.FuncTypeDefinition); ok {
		if ls, ok := ds.innerStatement.(*LambdaStatement); ok && ls.packageLevel {
			genCode = innerCode
		} else {
			genCode = fmt.Sprintf("var %s %s\n%s = %s", name, returnType.GenerateGo(), name, innerCode)
		}
	} else {
		genCode = fmt.Sprintf("var %s func() %s\n%s = func(){\n\treturn %s\n}", name, returnType.GenerateGo(), name, innerCode)
	}
	return genCode, returnType, nil
}