Skip to content

Commit

Permalink
Merge pull request #225 from ichiban/context-aware-exceptions
Browse files Browse the repository at this point in the history
make errors' Imp_def a term representing a context
  • Loading branch information
ichiban authored May 15, 2022
2 parents f788ec4 + 42bf1e7 commit 72c2134
Show file tree
Hide file tree
Showing 18 changed files with 1,387 additions and 1,143 deletions.
489 changes: 244 additions & 245 deletions engine/builtin.go

Large diffs are not rendered by default.

700 changes: 353 additions & 347 deletions engine/builtin_test.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions engine/clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func compile(t Term) (clauses, error) {
for iter.Next() {
c, err := compileClause(head, iter.Current())
if err != nil {
return nil, TypeErrorCallable(body, nil)
return nil, TypeError(ValidTypeCallable, body, nil)
}
c.raw = t
cs = append(cs, c)
Expand All @@ -116,7 +116,7 @@ func compileClause(head Term, body Term) (clause, error) {
}
if body != nil {
if err := c.compileBody(body); err != nil {
return c, TypeErrorCallable(body, nil)
return c, TypeError(ValidTypeCallable, body, nil)
}
}
c.bytecode = append(c.bytecode, instruction{opcode: opExit})
Expand Down
25 changes: 23 additions & 2 deletions engine/env.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package engine

const (
varContext = Variable("$context")
rootContext = Atom("root")
)

type color uint8

const (
Expand All @@ -21,6 +26,13 @@ type binding struct {
// attributes?
}

var rootEnv = &Env{
binding: binding{
variable: varContext,
value: rootContext,
},
}

// NewEnv creates an empty environment.
func NewEnv() *Env {
return nil
Expand All @@ -29,6 +41,9 @@ func NewEnv() *Env {
// Lookup returns a term that the given variable is bound to.
func (e *Env) Lookup(k Variable) (Term, bool) {
node := e
if node == nil {
node = rootEnv
}
for {
if node == nil {
return nil, false
Expand All @@ -46,7 +61,11 @@ func (e *Env) Lookup(k Variable) (Term, bool) {

// Bind adds a new entry to the environment.
func (e *Env) Bind(k Variable, v Term) *Env {
ret := *e.insert(k, v)
node := e
if node == nil {
node = rootEnv
}
ret := *node.insert(k, v)
ret.color = black
return &ret
}
Expand All @@ -67,7 +86,9 @@ func (e *Env) insert(k Variable, v Term) *Env {
ret.balance()
return &ret
default:
return e
ret := *e
ret.value = v
return &ret
}
}

Expand Down
10 changes: 8 additions & 2 deletions engine/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ func TestEnv_Bind(t *testing.T) {
var env *Env
assert.Equal(t, &Env{
color: black,
right: &Env{
binding: binding{
variable: "A",
value: Atom("a"),
},
},
binding: binding{
variable: "A",
value: Atom("a"),
variable: varContext,
value: Atom("root"),
},
}, env.Bind("A", Atom("a")))
}
Expand Down
Loading

0 comments on commit 72c2134

Please sign in to comment.