Beispiel #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)
}
Beispiel #2
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)
}
Beispiel #3
0
// parse all list items after the first one
func (r *TermReader) listItems(i *lex.List, o **lex.List, t *term.Term) bool {
	var arg, rest term.Term
	if r.tok(',', i, o) && r.term(999, *o, o, &arg) && r.listItems(*o, o, &rest) {
		*t = term.NewCallable(".", arg, rest)
		return true
	}
	if r.tok('|', i, o) && r.term(999, *o, o, &arg) && r.tok(']', *o, o) {
		*t = arg
		return true
	}
	if r.tok(']', i, o) {
		*t = term.NewAtom("[]")
		return true
	}
	return false
}
Beispiel #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)
}
Beispiel #5
0
// parse a single term
func (r *TermReader) term(p priority, i *lex.List, o **lex.List, t *term.Term) bool {
	var op, f string
	var t0, t1 term.Term
	var opP, argP priority
	//  fmt.Printf("seeking term with %s\n", i.Value.Content)

	// prefix operator
	if r.prefix(&op, &opP, &argP, i, o) && opP <= p && r.term(argP, *o, o, &t0) {
		opT := term.NewCallable(op, t0)
		return r.restTerm(opP, p, *o, o, opT, t)
	}

	// list notation for compound terms §6.3.5
	if r.tok('[', i, o) && r.term(999, *o, o, &t0) && r.listItems(*o, o, &t1) {
		list := term.NewCallable(".", t0, t1)
		return r.restTerm(0, p, *o, o, list, t)
	}
	if r.tok('[', i, o) && r.tok(']', *o, o) {
		list := term.NewAtom("[]")
		return r.restTerm(0, p, *o, o, list, t)
	}

	// parenthesized terms
	if r.tok('(', i, o) && r.term(1200, *o, o, &t0) && r.tok(')', *o, o) {
		//      fmt.Printf("open paren %s close paren\n", t0)
		return r.restTerm(0, p, *o, o, t0, t)
	}

	switch i.Value.Type {
	case lex.Int: // integer term §6.3.1.1
		n := term.NewInt(i.Value.Content)
		*o = i.Next()
		return r.restTerm(0, p, *o, o, n, t)
	case lex.Float: // float term §6.3.1.1
		f := term.NewFloat(i.Value.Content)
		*o = i.Next()
		return r.restTerm(0, p, *o, o, f, t)
	case lex.Atom: // atom term §6.3.1.3
		a := term.NewAtomFromLexeme(i.Value.Content)
		*o = i.Next()
		return r.restTerm(0, p, *o, o, a, t)
	case lex.String: // double quated string §6.3.7
		cl := term.NewCodeListFromDoubleQuotedString(i.Value.Content)
		*o = i.Next()
		return r.restTerm(0, p, *o, o, cl, t)
	case lex.Variable: // variable term §6.3.2
		v := term.NewVar(i.Value.Content)
		*o = i.Next()
		return r.restTerm(0, p, *o, o, v, t)
	case lex.Void: // variable term §6.3.2
		v := term.NewVar("_")
		*o = i.Next()
		return r.restTerm(0, p, *o, o, v, t)
	case lex.Comment:
		*o = i.Next()              // skip the comment
		return r.term(p, *o, o, t) // ... and try again
	}

	// compound term - functional notation §6.3.3
	if r.functor(i, o, &f) && r.tok('(', *o, o) {
		var args []term.Term
		var arg term.Term
		for r.term(999, *o, o, &arg) { // 999 priority per §6.3.3.1
			args = append(args, arg)
			if r.tok(')', *o, o) {
				break
			}
			if r.tok(',', *o, o) {
				continue
			}
			panic("Unexpected content inside compound term arguments")
		}
		f := term.NewTermFromLexeme(f, args...)
		return r.restTerm(0, p, *o, o, f, t)
	}

	*t = term.NewError("Syntax error", i.Value)
	return false
}