Пример #1
0
// atom_codes/2 see ISO §8.16.5
func BuiltinAtomCodes2(m Machine, args []term.Term) ForeignReturn {

	if !term.IsVariable(args[0]) {
		atom := args[0].(*term.Atom)
		list := term.NewCodeList(atom.Name())
		return ForeignUnify(args[1], list)
	} else if !term.IsVariable(args[1]) {
		runes := make([]rune, 0)
		list := args[1].(term.Callable)
		for {
			switch list.Arity() {
			case 2:
				if list.Name() == "." {
					args := list.Arguments()
					code := args[0].(*term.Integer)
					runes = append(runes, code.Code())
					list = args[1].(term.Callable)
				}
			case 0:
				if list.Name() == "[]" {
					atom := term.NewAtom(string(runes))
					return ForeignUnify(args[0], atom)
				}
			default:
				msg := fmt.Sprintf("unexpected code list %s", args[1])
				panic(msg)
			}
		}
	}

	msg := fmt.Sprintf("atom_codes/2: error with the arguments %s and %s", args[0], args[1])
	panic(msg)
}
Пример #2
0
// var(?X) is semidet.
//
// True if X is a variable.
func BuiltinVar1(m Machine, args []term.Term) ForeignReturn {
	x := args[0]

	if term.IsVariable(x) {
		return ForeignTrue()
	}
	return ForeignFail()
}
Пример #3
0
// downcase_atom(+AnyCase, -LowerCase)
//
// Converts the characters of AnyCase into lowercase and unifies the
// lowercase atom with LowerCase.
func BuiltinDowncaseAtom2(m Machine, args []term.Term) ForeignReturn {
	if term.IsVariable(args[0]) {
		panic("downcase_atom/2: instantiation_error")
	}
	anycase := args[0].(term.Callable)
	if anycase.Arity() != 0 {
		msg := fmt.Sprintf("downcase_atom/2: type_error(atom, %s)", anycase)
		panic(msg)
	}

	lowercase := term.NewAtom(strings.ToLower(anycase.Name()))
	return ForeignUnify(args[1], lowercase)
}
Пример #4
0
// atom_number/2 as defined in SWI-Prolog
func BuiltinAtomNumber2(m Machine, args []term.Term) (ret ForeignReturn) {
	number := args[1]

	if !term.IsVariable(args[0]) {
		atom := args[0].(term.Callable)
		defer func() { // convert parsing panics into fail
			if x := recover(); x != nil {
				ret = ForeignFail()
			}
		}()
		if strings.Contains(atom.Name(), ".") {
			number = term.NewFloat(atom.Name())
		} else {
			number = term.NewInt(atom.Name())
		}
		return ForeignUnify(args[1], number)
	} else if !term.IsVariable(number) {
		atom := term.NewAtom(number.String())
		return ForeignUnify(args[0], atom)
	}

	msg := fmt.Sprintf("atom_number/2: Arguments are not sufficiently instantiated: %s and %s", args[0], args[1])
	panic(msg)
}