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


Golang ast.Node类代码示例

本文整理汇总了Golang中vamos/lang/ast.Node的典型用法代码示例。如果您正苦于以下问题:Golang Node类的具体用法?Golang Node怎么用?Golang Node使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: panicEvalError

func panicEvalError(n ast.Node, s string) {
	var loc *token.Location
	if n != nil {
		loc = n.Loc()
	}
	panic(NewEvalError("Evaluation error", s, loc))
}
开发者ID:onlyafly,项目名称:vamos,代码行数:7,代码来源:eval_error.go

示例2: panicApplicationError

func panicApplicationError(n ast.Node, s string) {
	var loc *token.Location
	if n != nil {
		loc = n.Loc()
	}
	panic(NewEvalError("Application panic", s, loc))
}
开发者ID:onlyafly,项目名称:vamos,代码行数:7,代码来源:eval_error.go

示例3: primConcat

func primConcat(e Env, head ast.Node, args []ast.Node) ast.Node {
	var sum ast.Node

	for _, arg := range args {
		if sum == nil {
			sum = arg
		} else {
			switch sumVal := sum.(type) {
			case ast.Coll:
				switch argVal := arg.(type) {
				case ast.Coll:
					var err error
					sum, err = sumVal.Append(argVal)
					if err != nil {
						panicEvalError(arg, err.Error())
					}
				default:
					panicEvalError(arg, "Cannot concat a collection with a non-collection: "+arg.String())
				}
			default:
				panicEvalError(arg, "Cannot concat a non-collection type: "+sum.String())
			}
		}
	}

	if sum == nil {
		return &ast.Nil{}
	} else {
		return sum
	}
}
开发者ID:onlyafly,项目名称:vamos,代码行数:31,代码来源:primitives.go

示例4: ensureList

func ensureList(n ast.Node) *ast.List {
	if v, ok := n.(*ast.List); ok {
		return v
	}

	panic("Expected list: " + n.String())
}
开发者ID:onlyafly,项目名称:vamos,代码行数:7,代码来源:parse.go

示例5: ensureSymbol

func ensureSymbol(n ast.Node) *ast.Symbol {
	if v, ok := n.(*ast.Symbol); ok {
		return v
	}

	panic("Expected symbol: " + n.String())
}
开发者ID:onlyafly,项目名称:vamos,代码行数:7,代码来源:parse.go

示例6: toSymbolName

func toSymbolName(n ast.Node) string {
	switch value := n.(type) {
	case *ast.Symbol:
		return value.Name
	}

	panic("Not a symbol: " + n.String())
}
开发者ID:onlyafly,项目名称:vamos,代码行数:8,代码来源:eval.go

示例7: testInputFile

func testInputFile(sourceFilePath string, t *testing.T) {
	sourceDirPart, sourceFileNamePart := filepath.Split(sourceFilePath)
	parts := strings.Split(sourceFileNamePart, ".")
	testName := parts[0]

	outputFilePath := sourceDirPart + testName + ".out"

	input, errIn := util.ReadFile(sourceFilePath)
	if errIn != nil {
		t.Errorf("Error reading file <" + sourceFilePath + ">: " + errIn.Error())
		return
	}

	expectedRaw, errOut := util.ReadFile(outputFilePath)
	if errOut != nil {
		t.Errorf("Error reading file <" + outputFilePath + ">: " + errOut.Error())
		return
	}

	// Remove any carriage return line endings from .out file
	expectedWithUntrimmed := strings.Replace(expectedRaw, "\r", "", -1)
	expected := strings.TrimSpace(expectedWithUntrimmed)

	nodes, errors := parser.Parse(input, sourceFilePath)
	if errors.Len() != 0 {
		verify(t, sourceFilePath, input, expected, errors.String())
	} else {
		e := interpreter.NewTopLevelMapEnv()

		var outputBuffer bytes.Buffer

		dummyReadLine := func() string {
			return "text from dummy read line"
		}

		var result ast.Node
		var evalError error
		for _, n := range nodes {
			result, evalError = interpreter.Eval(e, n, &outputBuffer, dummyReadLine)
			if evalError != nil {
				break
			}
		}

		actual := (&outputBuffer).String()

		if evalError == nil {
			//DEBUG fmt.Printf("RESULT(%v): %v\n", sourceFilePath, result)
			if result != nil {
				actual = actual + result.String()
			}
		} else {
			actual = actual + evalError.Error()
		}
		verify(t, sourceFilePath, input, expected, actual)
	}
}
开发者ID:onlyafly,项目名称:vamos,代码行数:57,代码来源:suite_test.go

示例8: toListValue

func toListValue(n ast.Node) *ast.List {
	switch value := n.(type) {
	case *ast.List:
		return value
	}

	panicEvalError(n, "Expression is not a list: "+n.String())
	return nil
}
开发者ID:onlyafly,项目名称:vamos,代码行数:9,代码来源:conversion.go

示例9: toSymbolValue

func toSymbolValue(n ast.Node) string {
	switch value := n.(type) {
	case *ast.Symbol:
		return value.Name
	}

	panicEvalError(n, "Expression is not a symbol: "+n.String())
	return ""
}
开发者ID:onlyafly,项目名称:vamos,代码行数:9,代码来源:conversion.go

示例10: toNumberValue

func toNumberValue(n ast.Node) float64 {
	switch value := n.(type) {
	case *ast.Number:
		return value.Value
	}

	panicEvalError(n, "Expression is not a number: "+n.String())
	return 0.0
}
开发者ID:onlyafly,项目名称:vamos,代码行数:9,代码来源:conversion.go

示例11: specialCond

func specialCond(e Env, head ast.Node, args []ast.Node) packet {
	for i := 0; i < len(args); i += 2 {
		predicate := toBooleanValue(trampoline(func() packet {
			return evalNode(e, args[i])
		}))

		if predicate {
			return bounce(func() packet {
				return evalNode(e, args[i+1])
			})
		}
	}

	panicEvalError(head, "No matching cond clause: "+head.String())
	return respond(&ast.Nil{})
}
开发者ID:onlyafly,项目名称:vamos,代码行数:16,代码来源:specials.go

示例12: evalInvokeProcedure

func evalInvokeProcedure(dynamicEnv Env, f *Procedure, head ast.Node, unevaledArgs ast.Nodes, shouldEvalMacros bool) packet {
	defer func() {
		if e := recover(); e != nil {
			switch errorValue := e.(type) {
			case *EvalError:
				fmt.Printf("TRACE: (%v: %v): call to %v\n", head.Loc().Filename, head.Loc().Line, f.Name)
				panic(errorValue)
			default:
				panic(errorValue)
			}
		}
	}()

	// Validate parameters
	isVariableNumberOfParams := false
	for _, param := range f.Parameters {
		switch paramVal := param.(type) {
		case *ast.Symbol:
			if paramVal.Name == "&rest" {
				isVariableNumberOfParams = true
			}
		default:
			panicEvalError(head, "Procedure parameters should only be symbols: "+param.String())
		}
	}
	if !isVariableNumberOfParams {
		if len(unevaledArgs) != len(f.Parameters) {
			panicEvalError(head, fmt.Sprintf(
				"Procedure '%v' expects %v argument(s), but was given %v. Procedure parameter list: %v. Arguments: %v.",
				f.Name,
				len(f.Parameters),
				len(unevaledArgs),
				f.Parameters,
				unevaledArgs))
		}
	}

	// Create the lexical environment based on the procedure's lexical parent
	lexicalEnv := NewMapEnv(f.Name, f.ParentEnv)

	// Prepare the arguments for application
	var args []ast.Node
	if f.IsMacro {
		args = unevaledArgs
	} else {
		args = evalEachNode(dynamicEnv, unevaledArgs)
	}

	// Map arguments to parameters
	isMappingRestArgs := false
	iarg := 0
	for iparam, param := range f.Parameters {
		paramName := toSymbolName(param)
		if isMappingRestArgs {
			restArgs := args[iarg:]
			restList := ast.NewList(restArgs)
			lexicalEnv.Set(paramName, restList)
		} else if paramName == "&rest" {
			isMappingRestArgs = true
		} else {
			lexicalEnv.Set(paramName, args[iparam])
			iarg++
		}
	}

	if f.IsMacro {
		expandedMacro := trampoline(func() packet {
			return evalNode(lexicalEnv, f.Body)
		})

		if shouldEvalMacros {
			return bounce(func() packet {
				// This is executed in the environment of its application, not the
				// environment of its definition
				return evalNode(dynamicEnv, expandedMacro)
			})
		} else {
			return respond(expandedMacro)
		}
	} else {
		// Evaluate the body in the new lexical environment
		return bounce(func() packet {
			return evalNode(lexicalEnv, f.Body)
		})
	}
}
开发者ID:onlyafly,项目名称:vamos,代码行数:86,代码来源:eval.go


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