Add match expressions to entity

This commit is contained in:
Sasha Koshka 2024-03-01 21:22:32 -05:00
parent 8d611b6fa6
commit fb7558576e
3 changed files with 44 additions and 1 deletions

View File

@ -256,7 +256,7 @@ If the type of the union matches the type of the declaration in the case, the
expression is executed and the value of the union is made available to it
through the declaration. If the value of the match expression is used, all
possible types in the union must be accounted for. It may be assigned to any
type that satisfied
type that satisfies the assignment rules of its first case.
### 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

View File

@ -356,6 +356,40 @@ func (this *IfElse) String () string {
return out
}
// Match is a control flow branching expression that executes one of several
// case expressions depending on the input. It can be used to check the type of
// a union value. Each case takes the form of a declaration, and an associated
// expression. If the type of the union matches the type of the declaration in
// the case, the expression is executed and the value of the union is made
// available to it through the declaration. If the value of the match expression
// is used, all possible types in the union must be accounted for. It may be
// assigned to any type that satisfies the assignment rules of its first case.
type Match struct {
// Syntax
Position errors.Position
Value Expression
Cases []*Case
// Semantics
Ty Type
}
func (*Match) expression(){}
func (this *Match) Type () Type { return this.Ty }
func (this *Match) HasExplicitType () bool {
if len(this.Cases) == 0 {
return true
} else {
return this.Cases[0].HasExplicitType()
}
}
func (this *Match) String () string {
out := fmt.Sprint("match ", this.Value, " on")
for _, cas := range this.Cases {
out += fmt.Sprint(" ", cas)
}
return out
}
// 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

View File

@ -74,6 +74,15 @@ func (this *Member) String () string {
return fmt.Sprint(this.Name, ":", this.Value)
}
// Case represents a match case.
type Case struct {
Declaration Declaration
Expression
}
func (this *Case) String () string {
return fmt.Sprintf("| ", this.Declaration, this.Expression)
}
// Operator determines which operation to apply to a value in an operation
// expression.
type Operator int; const (