Added switch statements, default cases to entity

This commit is contained in:
Sasha Koshka 2024-03-25 01:16:41 -04:00
parent 7e05785c09
commit 1f04c3a593
2 changed files with 72 additions and 6 deletions

View File

@ -414,12 +414,13 @@ type Match struct {
// Syntax
Pos errors.Position
Value Expression
Cases []*Case
Cases []*MatchCase
Default *DefaultCase
// Semantics
Ty Type
CaseOrder []Hash
CaseMap map[Hash] *Case
CaseMap map[Hash] *MatchCase
}
func (*Match) expression(){}
func (this *Match) Position () errors.Position { return this.Pos }
@ -436,6 +437,48 @@ func (this *Match) String () string {
for _, cas := range this.Cases {
out += fmt.Sprint(" ", cas)
}
if this.Default != nil {
out += fmt.Sprint(" ", this.Default)
}
return out
}
var _ Expression = &Switch { }
// Switch is a control flow branching expression that executes one of several
// case expressions depending on the value of the input. It accepts any pointer
// or integer type. If the value of the switch expression is used, a default
// case must be present. It may be assigned to any type that satisfies the
// assignment rules of its first case.
type Switch struct {
// Syntax
Pos errors.Position
Value Expression
Cases []*SwitchCase
Default *DefaultCase
// Semantics
Ty Type
CaseOrder []int64
CaseMap map[uint64] *MatchCase
}
func (*Switch) expression(){}
func (this *Switch) Position () errors.Position { return this.Pos }
func (this *Switch) Type () Type { return this.Ty }
func (this *Switch) HasExplicitType () bool {
if len(this.Cases) == 0 {
return true
} else {
return this.Cases[0].HasExplicitType()
}
}
func (this *Switch) String () string {
out := fmt.Sprint("switch ", this.Value)
for _, cas := range this.Cases {
out += fmt.Sprint(" ", cas)
}
if this.Default != nil {
out += fmt.Sprint(" ", this.Default)
}
return out
}

View File

@ -101,18 +101,41 @@ func (this *Member) String () string {
return fmt.Sprint(this.Name, ":", this.Value)
}
// Case represents a match case.
type Case struct {
// MatchCase represents a case in a match expression.
type MatchCase struct {
Pos errors.Position
Declaration *Declaration
Expression
Scope
}
func (this *Case) Position () errors.Position { return this.Pos }
func (this *Case) String () string {
func (this *MatchCase) Position () errors.Position { return this.Pos }
func (this *MatchCase) String () string {
return fmt.Sprint("| ", this.Declaration, this.Expression)
}
// SwitchCase represents a case in a switch expression.
type SwitchCase struct {
Pos errors.Position
Key Expression
Expression
Scope
}
func (this *SwitchCase) Position () errors.Position { return this.Pos }
func (this *SwitchCase) String () string {
return fmt.Sprint("| ", this.Key, this.Expression)
}
// DefaultCase represents the default case in a switch or match expression.
type DefaultCase struct {
Pos errors.Position
Expression
Scope
}
func (this *DefaultCase) Position () errors.Position { return this.Pos }
func (this *DefaultCase) String () string {
return fmt.Sprint("* ", this.Expression)
}
// Operator determines which operation to apply to a value in an operation
// expression.
type Operator int; const (