Special function for type checking and returning an error in one fell swop

This commit is contained in:
Sasha Koshka 2022-10-11 18:03:44 -04:00
parent d74f3a40dd
commit 67c94fb0e8
4 changed files with 33 additions and 10 deletions

View File

@ -3,7 +3,7 @@ package analyzer
import "os" import "os"
import "fmt" import "fmt"
import "path/filepath" import "path/filepath"
// import "git.tebibyte.media/arf/arf/types" import "git.tebibyte.media/arf/arf/file"
import "git.tebibyte.media/arf/arf/parser" import "git.tebibyte.media/arf/arf/parser"
import "git.tebibyte.media/arf/arf/infoerr" import "git.tebibyte.media/arf/arf/infoerr"
@ -213,6 +213,28 @@ func (analyzer *AnalysisOperation) addSection (section Section) {
return return
} }
// typeCheck checks to see if source can fit as an argument into a slot of type
// destination. If it can, it retunrs nil. If it can't, it returns an error
// explaining why.
func (analyzer *AnalysisOperation) typeCheck (
location file.Location,
source Argument,
destination Type,
) (
err error,
) {
if !source.canBePassedAs(destination) {
err = infoerr.NewError (
location,
typeMismatchErrorMessage (
source.What(),
destination),
infoerr.ErrorKindError)
}
return
}
func doIndent (indent int, input ...any) (output string) { func doIndent (indent int, input ...any) (output string) {
for index := 0; index < indent; index ++ { for index := 0; index < indent; index ++ {
output += "\t" output += "\t"

View File

@ -136,6 +136,10 @@ func (literal StringLiteral) canBePassedAs (what Type) (allowed bool) {
// types that can be reduced to a variable array pointing to numbers at // types that can be reduced to a variable array pointing to numbers at
// a primitive level. // a primitive level.
// we don't check the length of what, becasue when setting a static
// array to a string literal, excess data will be cut off (and if it is
// shorter, the excess space will be filled with zeros).
reduced, worked := what.reduce() reduced, worked := what.reduce()
if worked { if worked {
if !what.isSingular() { return } if !what.isSingular() { return }

View File

@ -14,7 +14,7 @@ var PrimitiveU8 = createPrimitive("U8", Type {})
var PrimitiveU16 = createPrimitive("U16", Type {}) var PrimitiveU16 = createPrimitive("U16", Type {})
var PrimitiveU32 = createPrimitive("U32", Type {}) var PrimitiveU32 = createPrimitive("U32", Type {})
var PrimitiveU64 = createPrimitive("U64", Type {}) var PrimitiveU64 = createPrimitive("U64", Type {})
var PrimitiveObj = createPrimitive("Obj", Type {}) var PrimitiveObj = createPrimitive("Obj", Type {})
var PrimitiveFace = createPrimitive("Face", Type {}) var PrimitiveFace = createPrimitive("Face", Type {})
var PrimitiveFunc = createPrimitive("Func", Type {}) var PrimitiveFunc = createPrimitive("Func", Type {})

View File

@ -82,14 +82,11 @@ func (analyzer AnalysisOperation) analyzeTypeSection () (
if err != nil { return } if err != nil { return }
// type check default value // type check default value
if !outputSection.argument.canBePassedAs(outputSection.what) { err = analyzer.typeCheck (
err = inputSection.Argument().NewError ( inputSection.Argument().Location(),
typeMismatchErrorMessage ( outputSection.argument,
outputSection.argument.What(), outputSection.what)
outputSection.what), if err != nil { return }
infoerr.ErrorKindError)
return
}
} }
// TODO: analyze members // TODO: analyze members