fspl/analyzer/literal.go

79 lines
1.6 KiB
Go

package analyzer
// import "fmt"
import "github.com/alecthomas/participle/v2"
import "git.tebibyte.media/sashakoshka/fspl/entity"
func (this *Tree) analyzeLiteralInt (
into entity.Type,
mode strictness,
literal *entity.LiteralInt,
) (
entity.Expression,
error,
) {
if !this.isNumeric(into) {
return nil, participle.Errorf (
literal.Pos, "cannot use integer literal as %v",
into)
}
if this.isInteger(into) && !this.inRange(into, int64(literal.Value)) {
return nil, participle.Errorf (
literal.Pos, "integer literal out of range for type %v",
into)
}
literal.Ty = into
return literal, nil
}
func (this *Tree) analyzeLiteralFloat (
into entity.Type,
mode strictness,
literal *entity.LiteralFloat,
) (
entity.Expression,
error,
) {
if !this.isFloat(into) {
return nil, participle.Errorf (
literal.Pos, "cannot use float literal as %v",
into)
}
literal.Ty = into
return literal, nil
}
func (this *Tree) analyzeLiteralArray (
into entity.Type,
mode strictness,
literal *entity.LiteralArray,
) (
entity.Expression,
error,
) {
base, ok := this.reduceToBase(into).(*entity.TypeArray)
if !ok {
return nil, participle.Errorf (
literal.Pos, "cannot use array literal as %v",
into)
}
if base.Length != len(literal.Elements) {
return nil, participle.Errorf (
literal.Pos, "expected %v elements",
base.Length)
}
for index, element := range literal.Elements {
element, err := this.analyzeExpression(base.Element, strict, element)
if err != nil { return nil, err }
literal.Elements[index] = element
}
literal.Ty = into
return literal, nil
}