Skip to content

Commit

Permalink
feat: replace operators clauses by ordered list
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Jul 12, 2024
1 parent 7d87fa8 commit cfc8a8b
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 184 deletions.
5 changes: 3 additions & 2 deletions engine/atom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"bytes"
"github.com/stretchr/testify/assert"
orderedmap "github.com/wk8/go-ordered-map/v2"
"testing"
)

Expand All @@ -23,8 +24,8 @@ func TestAtom_WriteTerm(t *testing.T) {
{name: `{}`, opts: WriteOptions{quoted: false}, output: `{}`},
{name: `{}`, opts: WriteOptions{quoted: true}, output: `{}`},
{name: `-`, output: `-`},
{name: `-`, opts: WriteOptions{ops: operators{atomPlus: {}, atomMinus: {}}, left: operator{specifier: operatorSpecifierFY, name: atomPlus}}, output: ` (-)`},
{name: `-`, opts: WriteOptions{ops: operators{atomPlus: {}, atomMinus: {}}, right: operator{name: atomPlus}}, output: `(-)`},
{name: `-`, opts: WriteOptions{ops: &operators{OrderedMap: orderedmap.New[Atom, [_operatorClassLen]operator](orderedmap.WithInitialData(orderedmap.Pair[Atom, [_operatorClassLen]operator]{Key: atomPlus, Value: [3]operator{}}, orderedmap.Pair[Atom, [_operatorClassLen]operator]{Key: atomMinus, Value: [3]operator{}}))}, left: operator{specifier: operatorSpecifierFY, name: atomPlus}}, output: ` (-)`},
{name: `-`, opts: WriteOptions{ops: &operators{OrderedMap: orderedmap.New[Atom, [_operatorClassLen]operator](orderedmap.WithInitialData(orderedmap.Pair[Atom, [_operatorClassLen]operator]{Key: atomPlus, Value: [3]operator{}}, orderedmap.Pair[Atom, [_operatorClassLen]operator]{Key: atomMinus, Value: [3]operator{}}))}, right: operator{name: atomPlus}}, output: `(-)`},
{name: `X`, opts: WriteOptions{quoted: true, left: operator{name: NewAtom(`F`)}}, output: ` 'X'`}, // So that it won't be 'F''X'.
{name: `X`, opts: WriteOptions{quoted: true, right: operator{name: NewAtom(`F`)}}, output: `'X' `}, // So that it won't be 'X''F'.
{name: `foo`, opts: WriteOptions{left: operator{name: NewAtom(`bar`)}}, output: ` foo`}, // So that it won't be barfoo.
Expand Down
22 changes: 11 additions & 11 deletions engine/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,11 @@ func Op(vm *VM, priority, specifier, op Term, k Cont, env *Env) *Promise {
}

for _, name := range names {
if class := spec.class(); vm.operators.definedInClass(name, spec.class()) {
vm.operators.remove(name, class)
if class := spec.class(); vm.getOperators().definedInClass(name, spec.class()) {
vm.getOperators().remove(name, class)
}

vm.operators.define(p, spec, name)
vm.getOperators().define(p, spec, name)
}

return k(env)
Expand All @@ -574,13 +574,13 @@ func Op(vm *VM, priority, specifier, op Term, k Cont, env *Env) *Promise {
func validateOp(vm *VM, p Integer, spec operatorSpecifier, name Atom, env *Env) *Promise {
switch name {
case atomComma:
if vm.operators.definedInClass(name, operatorClassInfix) {
if vm.getOperators().definedInClass(name, operatorClassInfix) {
return Error(permissionError(operationModify, permissionTypeOperator, name, env))
}
case atomBar:
if spec.class() != operatorClassInfix || (p > 0 && p < 1001) {
op := operationCreate
if vm.operators.definedInClass(name, operatorClassInfix) {
if vm.getOperators().definedInClass(name, operatorClassInfix) {
op = operationModify
}
return Error(permissionError(op, permissionTypeOperator, name, env))
Expand All @@ -592,11 +592,11 @@ func validateOp(vm *VM, p Integer, spec operatorSpecifier, name Atom, env *Env)
// 6.3.4.3 There shall not be an infix and a postfix Operator with the same name.
switch spec.class() {
case operatorClassInfix:
if vm.operators.definedInClass(name, operatorClassPostfix) {
if vm.getOperators().definedInClass(name, operatorClassPostfix) {
return Error(permissionError(operationCreate, permissionTypeOperator, name, env))
}
case operatorClassPostfix:
if vm.operators.definedInClass(name, operatorClassInfix) {
if vm.getOperators().definedInClass(name, operatorClassInfix) {
return Error(permissionError(operationCreate, permissionTypeOperator, name, env))
}
}
Expand Down Expand Up @@ -653,9 +653,9 @@ func CurrentOp(vm *VM, priority, specifier, op Term, k Cont, env *Env) *Promise
}

pattern := tuple(priority, specifier, op)
ks := make([]func(context.Context) *Promise, 0, len(vm.operators)*int(_operatorClassLen))
for _, ops := range vm.operators {
for _, op := range ops {
ks := make([]func(context.Context) *Promise, 0, vm.getOperators().Len()*int(_operatorClassLen))
for ops := vm.getOperators().Oldest(); ops != nil; ops = ops.Next() {
for _, op := range ops.Value {
op := op
if op == (operator{}) {
continue
Expand Down Expand Up @@ -1463,7 +1463,7 @@ func WriteTerm(vm *VM, streamOrAlias, t, options Term, k Cont, env *Env) *Promis
}

opts := WriteOptions{
ops: vm.operators,
ops: vm.getOperators(),
priority: 1200,
}
iter := ListIterator{List: options, Env: env}
Expand Down
Loading

0 comments on commit cfc8a8b

Please sign in to comment.