From 67c94fb0e893aca440e00452da4dfb3391f9c289 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 11 Oct 2022 18:03:44 -0400 Subject: [PATCH] Special function for type checking and returning an error in one fell swop --- analyzer/analyzer.go | 24 +++++++++++++++++++++++- analyzer/literals.go | 4 ++++ analyzer/primitives.go | 2 +- analyzer/type-section.go | 13 +++++-------- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/analyzer/analyzer.go b/analyzer/analyzer.go index 09c8520..208ade5 100644 --- a/analyzer/analyzer.go +++ b/analyzer/analyzer.go @@ -3,7 +3,7 @@ package analyzer import "os" import "fmt" 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/infoerr" @@ -213,6 +213,28 @@ func (analyzer *AnalysisOperation) addSection (section Section) { 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) { for index := 0; index < indent; index ++ { output += "\t" diff --git a/analyzer/literals.go b/analyzer/literals.go index 50a1f15..3c34f1e 100644 --- a/analyzer/literals.go +++ b/analyzer/literals.go @@ -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 // 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() if worked { if !what.isSingular() { return } diff --git a/analyzer/primitives.go b/analyzer/primitives.go index 87e906a..fb49c07 100644 --- a/analyzer/primitives.go +++ b/analyzer/primitives.go @@ -14,7 +14,7 @@ var PrimitiveU8 = createPrimitive("U8", Type {}) var PrimitiveU16 = createPrimitive("U16", Type {}) var PrimitiveU32 = createPrimitive("U32", Type {}) var PrimitiveU64 = createPrimitive("U64", Type {}) -var PrimitiveObj = createPrimitive("Obj", Type {}) +var PrimitiveObj = createPrimitive("Obj", Type {}) var PrimitiveFace = createPrimitive("Face", Type {}) var PrimitiveFunc = createPrimitive("Func", Type {}) diff --git a/analyzer/type-section.go b/analyzer/type-section.go index 2bc7854..a52fba1 100644 --- a/analyzer/type-section.go +++ b/analyzer/type-section.go @@ -82,14 +82,11 @@ func (analyzer AnalysisOperation) analyzeTypeSection () ( if err != nil { return } // type check default value - if !outputSection.argument.canBePassedAs(outputSection.what) { - err = inputSection.Argument().NewError ( - typeMismatchErrorMessage ( - outputSection.argument.What(), - outputSection.what), - infoerr.ErrorKindError) - return - } + err = analyzer.typeCheck ( + inputSection.Argument().Location(), + outputSection.argument, + outputSection.what) + if err != nil { return } } // TODO: analyze members