From c3e4ce511caed2798246cee36fc194b05c1d2a7c Mon Sep 17 00:00:00 2001 From: Yutaka Ichibangase Date: Sun, 27 Feb 2022 11:07:54 +0900 Subject: [PATCH] add retractall/1 --- bootstrap.pl | 6 ++++++ cmd/1pl/main.go | 3 ++- engine/exception.go | 46 ++++++++++++++++++++++----------------------- engine/vm.go | 2 +- interpreter.go | 2 +- 5 files changed, 33 insertions(+), 26 deletions(-) diff --git a/bootstrap.pl b/bootstrap.pl index 53ca3aa3..5e93f70b 100644 --- a/bootstrap.pl +++ b/bootstrap.pl @@ -214,6 +214,12 @@ :- built_in(at_end_of_stream/0). at_end_of_stream :- current_input(S), at_end_of_stream(S). +:- built_in(retractall/1). +retractall(Head) :- + retract((Head :- _)), + fail. +retractall(_). + %%%% non-ISO predicates :- built_in(false/0). diff --git a/cmd/1pl/main.go b/cmd/1pl/main.go index 299c23ec..74bfbccc 100644 --- a/cmd/1pl/main.go +++ b/cmd/1pl/main.go @@ -188,7 +188,8 @@ func handleLine(ctx context.Context, buf *strings.Builder, p *prolog.Interpreter } if err := sols.Err(); err != nil { - return err + log.Print(err) + return nil } if !exists { diff --git a/engine/exception.go b/engine/exception.go index 28af03b7..fac89c96 100644 --- a/engine/exception.go +++ b/engine/exception.go @@ -97,74 +97,74 @@ func TypeError(validType Atom, culprit Term) *Exception { Functor: "type_error", Args: []Term{validType, culprit}, }, - Atom(fmt.Sprintf("'%s' expected, found '%T'.", validType, culprit)), + Atom(fmt.Sprintf("Expected %s, found %T.", validType, culprit)), }, }, } } func domainErrorFlagValue(culprit Term) *Exception { - return DomainError("flag_value", culprit, "%s is not a flag value.", culprit) + return DomainError("flag_value", culprit) } func domainErrorIOMode(culprit Term) *Exception { - return DomainError("io_mode", culprit, "%s is not an I/O mode.", culprit) + return DomainError("io_mode", culprit) } func domainErrorNotEmptyList(culprit Term) *Exception { - return DomainError("not_empty_list", culprit, "%s is an empty list.", culprit) + return DomainError("not_empty_list", culprit) } func domainErrorNotLessThanZero(culprit Term) *Exception { - return DomainError("not_less_than_zero", culprit, "%s is less than zero.", culprit) + return DomainError("not_less_than_zero", culprit) } func domainErrorOperatorPriority(culprit Term) *Exception { - return DomainError("operator_priority", culprit, "%s is not between 0 and 1200.", culprit) + return DomainError("operator_priority", culprit) } func domainErrorOperatorSpecifier(culprit Term) *Exception { - return DomainError("operator_specifier", culprit, "%s is neither xf, yf, xfx, xfy, yfx, fx, nor fy.", culprit) + return DomainError("operator_specifier", culprit) } func domainErrorPrologFlag(culprit Term) *Exception { - return DomainError("prolog_flag", culprit, "%s is not a prolog flag.", culprit) + return DomainError("prolog_flag", culprit) } func domainErrorReadOption(culprit Term) *Exception { - return DomainError("read_option", culprit, "%s is not a read option.", culprit) + return DomainError("read_option", culprit) } func domainErrorSourceSink(culprit Term) *Exception { - return DomainError("source_sink", culprit, "%s is not a source/sink.", culprit) + return DomainError("source_sink", culprit) } func domainErrorStream(culprit Term) *Exception { - return DomainError("stream", culprit, "%s is not a stream.", culprit) + return DomainError("stream", culprit) } func domainErrorStreamOption(culprit Term) *Exception { - return DomainError("stream_option", culprit, "%s is not a stream option.", culprit) + return DomainError("stream_option", culprit) } func domainErrorStreamOrAlias(culprit Term) *Exception { - return DomainError("stream_or_alias", culprit, "%s is neither a stream nor an alias.", culprit) + return DomainError("stream_or_alias", culprit) } func domainErrorStreamProperty(culprit Term) *Exception { - return DomainError("stream_property", culprit, "%s is not a stream property.", culprit) + return DomainError("stream_property", culprit) } func domainErrorWriteOption(culprit Term) *Exception { - return DomainError("write_option", culprit, "%s is not a write option.", culprit) + return DomainError("write_option", culprit) } func domainErrorOrder(culprit Term) *Exception { - return DomainError("order", culprit, "%s is neither <, =, nor >.", culprit) + return DomainError("order", culprit) } // DomainError creates a new domain error exception. -func DomainError(validDomain Atom, culprit Term, format string, args ...interface{}) *Exception { +func DomainError(validDomain Atom, culprit Term) *Exception { return &Exception{ Term: &Compound{ Functor: "error", @@ -173,26 +173,26 @@ func DomainError(validDomain Atom, culprit Term, format string, args ...interfac Functor: "domain_error", Args: []Term{validDomain, culprit}, }, - Atom(fmt.Sprintf(format, args...)), + Atom(fmt.Sprintf("Invalid value for %s.", validDomain)), }, }, } } func existenceErrorProcedure(culprit Term) *Exception { - return ExistenceError("procedure", culprit, "procedure %s is not defined.", culprit) + return ExistenceError("procedure", culprit) } func existenceErrorSourceSink(culprit Term) *Exception { - return ExistenceError("source_sink", culprit, "file %s doesn't exist.", culprit) + return ExistenceError("source_sink", culprit) } func existenceErrorStream(culprit Term) *Exception { - return ExistenceError("stream", culprit, "stream %s doesn't exist.", culprit) + return ExistenceError("stream", culprit) } // ExistenceError creates a new existence error exception. -func ExistenceError(objectType Atom, culprit Term, format string, args ...interface{}) *Exception { +func ExistenceError(objectType Atom, culprit Term) *Exception { return &Exception{ Term: &Compound{ Functor: "error", @@ -201,7 +201,7 @@ func ExistenceError(objectType Atom, culprit Term, format string, args ...interf Functor: "existence_error", Args: []Term{objectType, culprit}, }, - Atom(fmt.Sprintf(format, args...)), + Atom(fmt.Sprintf("Unknown %s.", objectType)), }, }, } diff --git a/engine/vm.go b/engine/vm.go index 733952c6..e27ec6c5 100644 --- a/engine/vm.go +++ b/engine/vm.go @@ -488,7 +488,7 @@ func piArgs(t Term, env *Env) (ProcedureIndicator, []Term, error) { case *Compound: return ProcedureIndicator{Name: f.Functor, Arity: Integer(len(f.Args))}, f.Args, nil default: - return ProcedureIndicator{}, nil, typeErrorCallable(t) + return ProcedureIndicator{}, nil, typeErrorCallable(f) } } diff --git a/interpreter.go b/interpreter.go index d23ab1da..675aea9a 100644 --- a/interpreter.go +++ b/interpreter.go @@ -260,7 +260,7 @@ func (i *Interpreter) consultOne(file engine.Term, env *engine.Env) error { return nil } - return engine.DomainError("source_sink", file, "%s does not exist.", file) + return engine.DomainError("source_sink", file) default: return engine.TypeError("atom", file) }