Add breakable interface

This commit is contained in:
Sasha Koshka 2024-03-16 01:43:56 -04:00
parent c576c4022e
commit 197373ee27
4 changed files with 29 additions and 6 deletions

View File

@ -48,6 +48,8 @@ func (this *Tree) analyzeExpression (
return this.analyzeMatch(into, mode, expression)
case *entity.Loop:
return this.analyzeLoop(into, mode, expression)
case *entity.For:
return this.analyzeFor(into, mode, expression)
case *entity.Break:
return this.analyzeBreak(into, mode, expression)
case *entity.Return:

View File

@ -808,6 +808,17 @@ func (this *Tree) analyzeLoop (
return loop, nil
}
func (this *Tree) analyzeFor (
into entity.Type,
mode strictness,
loop *entity.For,
) (
entity.Expression,
error,
) {
// TODO
}
func (this *Tree) analyzeBreak (
into entity.Type,
mode strictness,

View File

@ -16,7 +16,7 @@ func (this *scopeContextManager) popScopeContext () {
*this = (*this)[:len(*this) - 1]
}
func (this *scopeContextManager) pushLoop (loop *entity.Loop) {
func (this *scopeContextManager) pushLoop (loop entity.Breakable) {
this.assertPopulated()
(*this)[len(*this) - 1].pushLoop(loop)
}
@ -26,7 +26,7 @@ func (this *scopeContextManager) popLoop () {
(*this)[len(*this) - 1].popLoop()
}
func (this *scopeContextManager) topLoop () (*entity.Loop, bool) {
func (this *scopeContextManager) topLoop () (entity.Breakable, bool) {
this.assertPopulated()
return (*this)[len(*this) - 1].topLoop()
}
@ -71,11 +71,11 @@ func (this *scopeContextManager) assertPopulated () {
// entities.
type scopeContext struct {
scopes []entity.Scoped
loops []*entity.Loop
loops []entity.Breakable
declaration entity.TopLevel
}
func (this *scopeContext) pushLoop (loop *entity.Loop) {
func (this *scopeContext) pushLoop (loop entity.Breakable) {
this.loops = append(this.loops, loop)
}
@ -84,7 +84,7 @@ func (this *scopeContext) popLoop () {
this.loops = this.loops[:len(this.loops) - 1]
}
func (this *scopeContext) topLoop () (*entity.Loop, bool) {
func (this *scopeContext) topLoop () (entity.Breakable, bool) {
if len(this.loops) < 1 { return nil, false }
return this.loops[len(this.loops) - 1], true
}

View File

@ -439,7 +439,14 @@ func (this *Match) String () string {
return out
}
// Breakable is any expression that can be halted using a break.
type Breakable interface {
Expression
breakable()
}
var _ Expression = &Loop { }
var _ Breakable = &Loop { }
// Loop is a control flow expression that repeats an expression until a break
// statement is called from within it. The break statement must be given a value
// if the value of the loop is used. Otherwise, it need not even have a break
@ -456,6 +463,7 @@ type Loop struct {
Ty Type
}
func (*Loop) expression(){}
func (*Loop) breakable(){}
func (this *Loop) Position () errors.Position { return this.Pos }
func (this *Loop) Type () Type { return this.Ty }
func (this *Loop) HasExplicitType () bool {
@ -468,6 +476,7 @@ func (this *Loop) String () string {
}
var _ Expression = &For { }
var _ Breakable = &For { }
// For is a special kind of loop that evaluates an expression for each element
// of an array or slice. It accepts an index declaration and an element
// declaration, which are scoped to the loop's body and are set to the index of
@ -486,6 +495,7 @@ type For struct {
Ty Type
}
func (*For) expression(){}
func (*For) breakable(){}
func (this *For) Position () errors.Position { return this.Pos }
func (this *For) Type () Type { return this.Ty }
func (this *For) HasExplicitType () bool {
@ -511,7 +521,7 @@ type Break struct {
Value Expression
// Semantics
Loop *Loop
Loop Breakable
}
func (*Break) expression(){}
func (this *Break) Position () errors.Position { return this.Pos }