本文整理汇总了Golang中github.com/raoulvdberge/risp/runtime.NewRuntimeError函数的典型用法代码示例。如果您正苦于以下问题:Golang NewRuntimeError函数的具体用法?Golang NewRuntimeError怎么用?Golang NewRuntimeError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewRuntimeError函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: builtinMath
func builtinMath(context *runtime.FunctionCallContext) (*runtime.Value, error) {
if err := runtime.ValidateArguments(context, runtime.NumberValue, runtime.NumberValue); err != nil {
return nil, err
}
base := big.NewRat(0, 1)
var callback func(*big.Rat, *big.Rat) *big.Rat
switch context.Name {
case "+":
callback = base.Add
case "-":
callback = base.Sub
case "*":
callback = base.Mul
case "/":
if context.Args[1].Number.Cmp(base) == 0 {
return nil, runtime.NewRuntimeError(context.Pos, "division by zero")
}
callback = base.Quo
}
return runtime.NewNumberValueFromRat(callback(context.Args[0].Number, context.Args[1].Number)), nil
}
示例2: listMap
func listMap(context *runtime.MacroCallContext) (*runtime.Value, error) {
list, err := context.Block.EvalNode(context.Nodes[0])
if err != nil {
return nil, err
}
if list.Type != runtime.ListValue {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "expected a list")
}
ident := context.Nodes[1].(*parser.IdentifierNode).Token.Data
callback := context.Nodes[2]
mappedList := runtime.NewListValue()
for _, item := range list.List {
b := runtime.NewBlock([]parser.Node{callback}, runtime.NewScope(context.Block.Scope))
b.Scope.SetSymbolLocally(ident, runtime.NewSymbol(item))
result, err := b.Eval()
if err != nil {
return nil, err
}
mappedList.List = append(mappedList.List, result)
}
return mappedList, nil
}
示例3: stringsCharacterCheck
func stringsCharacterCheck(context *runtime.FunctionCallContext) (*runtime.Value, error) {
if err := runtime.ValidateArguments(context, runtime.StringValue); err != nil {
return nil, err
}
s := context.Args[0].Str
if len(s) != 1 {
return nil, runtime.NewRuntimeError(context.Pos, "expected string that has 1 character, got %d character(s)", len(s))
}
var callback func(rune) bool
switch context.Name {
case "is-digit":
callback = unicode.IsDigit
case "is-letter":
callback = unicode.IsLetter
case "is-lower":
callback = unicode.IsLower
case "is-upper":
callback = unicode.IsUpper
}
return runtime.BooleanValueFor(callback(rune(s[0]))), nil
}
示例4: builtinFor
func builtinFor(context *runtime.MacroCallContext) (*runtime.Value, error) {
l, err := context.Block.EvalNode(context.Nodes[0])
if err != nil {
return nil, err
}
if l.Type != runtime.ListValue {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "expected a list to iterate over")
}
var args []string
for _, nameNode := range context.Nodes[1].(*parser.ListNode).Nodes {
ident, isIdent := nameNode.(*parser.IdentifierNode)
if isIdent {
args = append(args, ident.Token.Data)
} else {
return nil, runtime.NewRuntimeError(nameNode.Pos(), "expected an identifier")
}
}
if len(args) > 2 {
return nil, runtime.NewRuntimeError(context.Nodes[1].Pos(), "too many arguments provided")
}
callbackBlock := runtime.NewBlock([]parser.Node{context.Nodes[2]}, runtime.NewScope(context.Block.Scope))
for i, item := range l.List {
if len(args) >= 1 {
callbackBlock.Scope.SetSymbol(args[0], runtime.NewSymbol(item))
}
if len(args) == 2 {
callbackBlock.Scope.SetSymbol(args[1], runtime.NewSymbol(runtime.NewNumberValueFromInt64(int64(i))))
}
_, err := callbackBlock.Eval()
if err != nil {
return nil, err
}
}
return runtime.Nil, nil
}
示例5: builtinCall
func builtinCall(context *runtime.FunctionCallContext) (*runtime.Value, error) {
if len(context.Args) < 1 || context.Args[0].Type != runtime.FunctionValue {
return nil, runtime.NewRuntimeError(context.Pos, "expected a function")
}
function := context.Args[0].Function
return function.Call(context.Block, context.Args[1:], context.Pos)
}
示例6: builtinDefmacro
func builtinDefmacro(context *runtime.MacroCallContext) (*runtime.Value, error) {
name := context.Nodes[0].(*parser.IdentifierNode).Token.Data
if name == "_" {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "disallowed macro name")
}
if context.Block.Scope.GetSymbol(name) != nil && context.Block.Scope.GetSymbol(name).Const {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "%s is a constant and cannot be modified", name)
}
argNodes := context.Nodes[1].(*parser.ListNode)
var args []string
callback := context.Nodes[2].(*parser.ListNode)
if len(callback.Nodes) == 0 {
return nil, runtime.NewRuntimeError(callback.Pos(), "empty macro body")
}
for _, argNode := range argNodes.Nodes {
ident, ok := argNode.(*parser.IdentifierNode)
if !ok {
return nil, runtime.NewRuntimeError(argNode.Pos(), "expected an identifier")
}
args = append(args, ident.Token.Data)
}
macro := runtime.NewMacro(func(handlerContext *runtime.MacroCallContext) (*runtime.Value, error) {
block := runtime.NewBlock([]parser.Node{callback}, runtime.NewScope(handlerContext.Block.Scope))
for i, arg := range args {
block.Scope.SetSymbolLocally(arg, runtime.NewSymbol(runtime.NewQuotedValue(handlerContext.Nodes[i])))
}
return block.Eval()
}, false)
context.Block.Scope.SetMacro(name, macro)
return runtime.Nil, nil
}
示例7: builtinExport
func builtinExport(context *runtime.MacroCallContext) (*runtime.Value, error) {
for _, node := range context.Nodes {
ident, isIdent := node.(*parser.IdentifierNode)
if !isIdent {
return nil, runtime.NewRuntimeError(node.Pos(), "expected an identifier")
} else {
name := ident.Token.Data
if !context.Block.Scope.HasSymbol(name) {
return nil, runtime.NewRuntimeError(node.Pos(), "unknown symbol '%s'", name)
}
context.Block.Scope.GetSymbol(name).Exported = true
}
}
return runtime.Nil, nil
}
示例8: listReduce
func listReduce(context *runtime.MacroCallContext) (*runtime.Value, error) {
list, err := context.Block.EvalNode(context.Nodes[0])
if err != nil {
return nil, err
}
if list.Type != runtime.ListValue {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "expected a list")
}
if len(list.List) == 0 {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "empty list")
}
identLeft := context.Nodes[1].(*parser.IdentifierNode).Token.Data
identRight := context.Nodes[2].(*parser.IdentifierNode).Token.Data
callback := context.Nodes[3]
reduced := list.List[0]
for _, item := range list.List[1:] {
b := runtime.NewBlock([]parser.Node{callback}, runtime.NewScope(context.Block.Scope))
b.Scope.SetSymbolLocally(identLeft, runtime.NewSymbol(reduced))
b.Scope.SetSymbolLocally(identRight, runtime.NewSymbol(item))
result, err := b.Eval()
if err != nil {
return nil, err
}
reduced = result
}
return reduced, nil
}
示例9: stringsFormat
func stringsFormat(context *runtime.FunctionCallContext) (*runtime.Value, error) {
if len(context.Args) < 1 {
return nil, runtime.NewRuntimeError(context.Pos, "missing format specifier")
}
if context.Args[0].Type != runtime.StringValue {
return nil, runtime.NewRuntimeError(context.Pos, "format specifier should be a string")
}
format := context.Args[0].Str
args := strings.Count(format, "~")
if len(context.Args)-1 != args {
return nil, runtime.NewRuntimeError(context.Pos, "format specifier expected %d arguments, got %d", args, len(context.Args)-1)
}
for _, item := range context.Args[1:] {
format = strings.Replace(format, "~", item.String(), 1)
}
return runtime.NewStringValue(format), nil
}
示例10: builtinDef
func builtinDef(context *runtime.MacroCallContext) (*runtime.Value, error) {
name := context.Nodes[0].(*parser.IdentifierNode).Token.Data
value, err := context.Block.EvalNode(context.Nodes[1])
if name == "_" {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "disallowed symbol name")
}
if err != nil {
return nil, err
}
if context.Block.Scope.GetSymbol(name) != nil && context.Block.Scope.GetSymbol(name).Const {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "%s is a constant and cannot be modified", name)
}
sym := runtime.NewSymbol(value)
sym.Const = context.Name == "defconst"
context.Block.Scope.SetSymbol(name, sym)
return value, nil
}
示例11: builtinFun
func builtinFun(context *runtime.MacroCallContext) (*runtime.Value, error) {
argNodes := context.Nodes[0].(*parser.ListNode)
var args []string
callback := context.Nodes[1].(*parser.ListNode)
if len(callback.Nodes) == 0 {
return nil, runtime.NewRuntimeError(callback.Pos(), "empty function body")
}
for _, argNode := range argNodes.Nodes {
ident, ok := argNode.(*parser.IdentifierNode)
if !ok {
return nil, runtime.NewRuntimeError(argNode.Pos(), "expected an identifier")
}
args = append(args, ident.Token.Data)
}
function := runtime.NewLambdaFunction([]parser.Node{callback}, args)
return runtime.NewFunctionValue(function), nil
}
示例12: listGet
func listGet(context *runtime.FunctionCallContext) (*runtime.Value, error) {
if err := runtime.ValidateArguments(context, runtime.ListValue, runtime.NumberValue); err != nil {
return nil, err
}
size := int64(len(context.Args[0].List))
index := context.Args[1].NumberToInt64()
if index < 0 || index > size-1 {
return nil, runtime.NewRuntimeError(context.Pos, "index %d out of bounds (list size is %d)", index, size)
}
return context.Args[0].List[index], nil
}
示例13: builtinDefun
func builtinDefun(context *runtime.MacroCallContext) (*runtime.Value, error) {
name := context.Nodes[0].(*parser.IdentifierNode).Token.Data
if name == "_" {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "disallowed function name")
}
if context.Block.Scope.GetSymbol(name) != nil && context.Block.Scope.GetSymbol(name).Const {
return nil, runtime.NewRuntimeError(context.Nodes[0].Pos(), "%s is a constant and cannot be modified", name)
}
argNodes := context.Nodes[1].(*parser.ListNode)
var args []string
callback := context.Nodes[2].(*parser.ListNode)
if len(callback.Nodes) == 0 {
return nil, runtime.NewRuntimeError(callback.Pos(), "empty function body")
}
for _, argNode := range argNodes.Nodes {
ident, ok := argNode.(*parser.IdentifierNode)
if !ok {
return nil, runtime.NewRuntimeError(argNode.Pos(), "expected an identifier")
}
args = append(args, ident.Token.Data)
}
function := runtime.NewDeclaredFunction([]parser.Node{callback}, name, args)
functionValue := runtime.NewFunctionValue(function)
context.Block.Scope.SetSymbol(name, runtime.NewSymbol(functionValue))
return functionValue, nil
}
示例14: listDropLeft
func listDropLeft(context *runtime.FunctionCallContext) (*runtime.Value, error) {
if err := runtime.ValidateArguments(context, runtime.ListValue); err != nil {
return nil, err
}
l := context.Args[0]
if len(l.List) == 0 {
return nil, runtime.NewRuntimeError(context.Pos, "empty list")
}
l.List = l.List[1:]
return context.Args[0], nil
}
示例15: builtinIfel
func builtinIfel(context *runtime.MacroCallContext) (*runtime.Value, error) {
conditionNode := context.Nodes[0]
condition, err := context.Block.EvalNode(conditionNode)
if err != nil {
return nil, err
}
if condition.Type != runtime.BooleanValue {
return nil, runtime.NewRuntimeError(conditionNode.Pos(), "expected a boolean")
}
if condition.Boolean == true {
return context.Block.EvalNode(context.Nodes[1])
} else {
return context.Block.EvalNode(context.Nodes[2])
}
}