Skip to content

Commit

Permalink
fix some golint checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiban committed Dec 20, 2021
1 parent 14fbe96 commit 62393e6
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 25 deletions.
14 changes: 9 additions & 5 deletions engine/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func (state *State) SetUserOutput(w io.Writer, opts ...StreamOption) {
state.output = NewStream(readWriteCloser(w), StreamModeWrite, opts...)
}

// Parser creates a new parser from the current State and io.Reader.
// If non-nil, vars will hold the information on variables it parses.
func (state *State) Parser(r io.Reader, vars *[]ParsedVariable) *Parser {
br, ok := r.(*bufio.Reader)
if !ok {
Expand All @@ -96,12 +98,14 @@ func (state *State) Parser(r io.Reader, vars *[]ParsedVariable) *Parser {
)
}

// Repeat repeats the continuation until it succeeds.
func (state *State) Repeat(k func(*Env) *Promise, env *Env) *Promise {
return Repeat(func(ctx context.Context) *Promise {
return k(env)
})
}

// Negation calls goal and returns false if it succeeds. Otherwise, invokes the continuation.
func (state *State) Negation(goal Term, k func(*Env) *Promise, env *Env) *Promise {
return Delay(func(ctx context.Context) *Promise {
ok, err := state.Call(goal, Success, env).Force(ctx)
Expand Down Expand Up @@ -577,7 +581,7 @@ func (state *State) collectionOf(agg func(...Term) Term, template, goal, instanc
body = goal
}

groupingVariables := env.FreeVariables(body).Except(env.FreeVariables(template, qualifier))
groupingVariables := variables(env.FreeVariables(body)).except(env.FreeVariables(template, qualifier))

return Delay(func(ctx context.Context) *Promise {
const (
Expand All @@ -592,7 +596,7 @@ func (state *State) collectionOf(agg func(...Term) Term, template, goal, instanc
}
var solutions []solution

template = hyphen.Apply(vars.Apply(groupingVariables.Terms()...), template)
template = hyphen.Apply(vars.Apply(groupingVariables.terms()...), template)
if _, err := state.FindAll(template, body, answers, func(env *Env) *Promise {
if err := EachList(answers, func(elem Term) error {
answer := elem.(*Compound)
Expand Down Expand Up @@ -1340,7 +1344,7 @@ func (state *State) ReadTerm(streamOrAlias, out, options Term, k func(*Env) *Pro
if err != nil {
var (
unexpectedRune *UnexpectedRuneError
unexpectedToken *UnexpectedTokenError
unexpectedToken *unexpectedTokenError
)
switch {
case errors.Is(err, io.EOF):
Expand Down Expand Up @@ -1953,7 +1957,7 @@ func NumberChars(num, chars Term, k func(*Env) *Promise, env *Env) *Promise {
switch err {
case nil:
break
case ErrNotANumber:
case errNotANumber:
return Error(syntaxErrorNotANumber())
default:
return Error(SystemError(err))
Expand Down Expand Up @@ -2016,7 +2020,7 @@ func NumberCodes(num, codes Term, k func(*Env) *Promise, env *Env) *Promise {
switch err {
case nil:
break
case ErrNotANumber:
case errNotANumber:
return Error(syntaxErrorNotANumber())
default:
return Error(SystemError(err))
Expand Down
2 changes: 1 addition & 1 deletion engine/clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func compile(t Term, env *Env) (clauses, error) {
}

// if-then-else construct
if if_, ok := e.Args[0].(*Compound); ok && if_.Functor == "->" && len(if_.Args) == 2 {
if c, ok := e.Args[0].(*Compound); ok && c.Functor == "->" && len(c.Args) == 2 {
break
}

Expand Down
5 changes: 4 additions & 1 deletion engine/compound.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ func EachList(list Term, f func(elem Term) error, env *Env) error {
}
}

// Slice returns a Term slice containing the elements of list.
// It errors if the given Term is not a list.
func Slice(list Term, env *Env) (ret []Term, err error) {
err = EachList(list, func(elem Term) error {
ret = append(ret, env.Resolve(elem))
Expand All @@ -272,7 +274,7 @@ func Slice(list Term, env *Env) (ret []Term, err error) {
return
}

// Seq returns a sequence of ts separated by seq.
// Seq returns a sequence of ts separated by sep.
func Seq(sep Atom, ts ...Term) Term {
s, ts := ts[len(ts)-1], ts[:len(ts)-1]
for i := len(ts) - 1; i >= 0; i-- {
Expand All @@ -299,6 +301,7 @@ func EachSeq(seq Term, sep Atom, f func(elem Term) error, env *Env) error {
return f(seq)
}

// Each iterates over either a list or comma-delimited sequence.
func Each(any Term, f func(elem Term) error, env *Env) error {
if c, ok := env.Resolve(any).(*Compound); ok && c.Functor == "." && len(c.Args) == 2 {
return EachList(any, f, env)
Expand Down
14 changes: 7 additions & 7 deletions engine/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,18 @@ func (e *Env) Simplify(t Term) Term {
}
}

type Variables []Variable
type variables []Variable

func (vs Variables) Terms() []Term {
func (vs variables) terms() []Term {
res := make([]Term, len(vs))
for i, v := range vs {
res[i] = v
}
return res
}

func (vs Variables) Except(ws Variables) Variables {
ret := make(Variables, 0, len(vs))
func (vs variables) except(ws variables) variables {
ret := make(variables, 0, len(vs))
vs:
for _, v := range vs {
for _, w := range ws {
Expand All @@ -196,15 +196,15 @@ vs:
}

// FreeVariables extracts variables in the given terms.
func (e *Env) FreeVariables(ts ...Term) Variables {
var fvs Variables
func (e *Env) FreeVariables(ts ...Term) []Variable {
var fvs variables
for _, t := range ts {
fvs = e.appendFreeVariables(fvs, t)
}
return fvs
}

func (e *Env) appendFreeVariables(fvs Variables, t Term) Variables {
func (e *Env) appendFreeVariables(fvs variables, t Term) variables {
switch t := e.Resolve(t).(type) {
case Variable:
for _, v := range fvs {
Expand Down
3 changes: 2 additions & 1 deletion engine/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (e *Exception) Error() string {
return e.Term.String()
}

// InstantiationError creates a new instantiation error excdption.
// InstantiationError creates a new instantiation error exception.
func InstantiationError(culprit Term) *Exception {
return &Exception{
Term: &Compound{
Expand Down Expand Up @@ -334,6 +334,7 @@ func syntaxError(detail, info Term) *Exception {
}
}

// SystemError creates a new system error exception.
func SystemError(err error) *Exception {
return &Exception{
Term: &Compound{
Expand Down
19 changes: 11 additions & 8 deletions engine/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (p *Parser) expectationError(k TokenKind, vals []string) error {
if p.current.Kind == TokenEOS {
return ErrInsufficient
}
return &UnexpectedTokenError{
return &unexpectedTokenError{
ExpectedKind: k,
ExpectedVals: vals,
Actual: *p.current,
Expand Down Expand Up @@ -252,7 +252,7 @@ func (p *Parser) Term() (Term, error) {
return t, nil
}

var ErrNotANumber = errors.New("not a number")
var errNotANumber = errors.New("not a number")

// Number parses a number term.
func (p *Parser) Number() (Term, error) {
Expand Down Expand Up @@ -289,7 +289,7 @@ func (p *Parser) number() (Term, error) {
}
}

return nil, ErrNotANumber
return nil, errNotANumber
}

// based on Pratt parser explained in this article: https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html
Expand Down Expand Up @@ -489,8 +489,7 @@ func (p *Parser) More() bool {
type OperatorSpecifier uint8

const (
OperatorSpecifierNone OperatorSpecifier = iota
OperatorSpecifierFX
OperatorSpecifierFX OperatorSpecifier = iota
OperatorSpecifierFY
OperatorSpecifierXF
OperatorSpecifierYF
Expand All @@ -513,7 +512,7 @@ func (s OperatorSpecifier) Term() Term {
}[s]
}

// Operators are a list of operators sorted in a descending order of precedence.
// Operators are a list of operators sorted in descending order of precedence.
type Operators []Operator

func (ops Operators) find(name Atom, arity int) *Operator {
Expand Down Expand Up @@ -573,11 +572,15 @@ func (o *Operator) bindingPowers() (int, int) {
}
}

// DoubleQuotes describes how the parser handles double-quoted strings.
type DoubleQuotes int

const (
// DoubleQuotesCodes parses a double-quoted string into a list of integers.
DoubleQuotesCodes DoubleQuotes = iota
// DoubleQuotesChars parses a double-quoted string into a list of single-character atoms.
DoubleQuotesChars
// DoubleQuotesAtom parses a double-quoted string into an atom.
DoubleQuotesAtom
doubleQuotesLen
)
Expand All @@ -590,14 +593,14 @@ func (d DoubleQuotes) String() string {
}[d]
}

type UnexpectedTokenError struct {
type unexpectedTokenError struct {
ExpectedKind TokenKind
ExpectedVals []string
Actual Token
History []Token
}

func (e UnexpectedTokenError) Error() string {
func (e unexpectedTokenError) Error() string {
return fmt.Sprintf("unexpected token: %s", e.Actual)
}

Expand Down
2 changes: 2 additions & 0 deletions engine/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ type Variable string

var varCounter uint64

// NewVariable creates a new generated variable.
func NewVariable() Variable {
atomic.AddUint64(&varCounter, 1)
return Variable(fmt.Sprintf("_%d", varCounter))
}

var generatedPattern = regexp.MustCompile(`\A_\d+\z`)

// Generated checks if the variable is generated.
func (v Variable) Generated() bool {
return generatedPattern.MatchString(string(v))
}
Expand Down
6 changes: 4 additions & 2 deletions engine/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,13 @@ func (p predicate5) Call(_ *VM, args []Term, k func(*Env) *Promise, env *Env) *P
return p(args[0], args[1], args[2], args[3], args[4], k, env)
}

func Success(_ *Env) *Promise {
// Success is a continuation that leads to true.
func Success(*Env) *Promise {
return Bool(true)
}

func Failure(_ *Env) *Promise {
// Failure is a continuation that leads to false.
func Failure(*Env) *Promise {
return Bool(false)
}

Expand Down

0 comments on commit 62393e6

Please sign in to comment.