Exemple #1
0
package commands

import (
	"code.google.com/p/gelo"
	"code.google.com/p/gelo/extensions"
)

func _condition(vm *gelo.VM, w gelo.Word) bool {
	r, e := vm.API.InvokeWordOrReturn(w)
	if e != nil {
		panic(e)
	}
	return vm.API.BoolOrElse(r).True()
}

var _if_parser = extensions.MakeOrElseArgParser(
	"cond 'then cons ['elif cond 'then cons]* ['else alt]?")

func If(vm *gelo.VM, args *gelo.List, ac uint) (ret gelo.Word) {
	Args := _if_parser(vm, args)
	cond, cons := Args["cond"], Args["cons"]
	if lcond, ok := cond.(*gelo.List); !ok { //no elifs
		if _condition(vm, cond) {
			return vm.API.TailInvokeWordOrReturn(cons)
		}
	} else {
		for lcons := cons.(*gelo.List); lcond != nil; lcond, lcons = lcond.Next, lcons.Next {
			if _condition(vm, lcond.Value) {
				return vm.API.TailInvokeWordOrReturn(lcons.Value)
			}
		}
	}
Exemple #2
0
		_, sndb := vm.Ns.Lookup(snd)
		if fstb && sndb {
			gelo.RuntimeError(vm, "Neither", fst, "nor", snd, "are defined")
		}
		if fstb {
			gelo.VariableUndefined(vm, fst)
		}
		if sndb {
			gelo.VariableUndefined(vm, snd)
		}
		gelo.RuntimeError(vm, "swap failed but both operands were defined")
	}
	return sndw //returned because this is now the "closest"
}

var _up_parser = extensions.MakeOrElseArgParser("[up levels]? args+")

func _export_parser(vm *gelo.VM, args *gelo.List) (int, *gelo.List) {
	Args := _up_parser(vm, args)
	levels, ok := Args["levels"]
	lvl := 1
	var lvl64 int64
	if ok {
		lvl64, ok = vm.API.NumberOrElse(levels).Int()
		if !ok || lvl < 0 {
			gelo.TypeMismatch(vm, "positive integer", "number")
		}
		lvl = int(lvl64)
	}
	return lvl, Args["args"].(*gelo.List)
}
Exemple #3
0
func ArgumentParser(_ *gelo.VM, args *gelo.List, ac uint) gelo.Word {
	parser := extensions.MakeOrElseArgParser(_args_to_spec(args, ac))
	return gelo.Alien(func(vm *gelo.VM, args *gelo.List, _ uint) gelo.Word {
		return gelo.NewDictFrom(parser(vm, args))
	})
}