Analyzer now understands type section default values

This commit is contained in:
Sasha Koshka 2022-09-30 00:04:28 -04:00
parent d117e2727c
commit 47cb89206a
7 changed files with 67 additions and 2 deletions

View File

@ -63,6 +63,9 @@ func (analyzer *AnalysisOperation) fetchSection (
err error, err error,
) { ) {
var exists bool var exists bool
section, exists = analyzer.resolvePrimitive(where)
if exists { return }
section, exists = analyzer.sectionTable[where] section, exists = analyzer.sectionTable[where]
if exists { return } if exists { return }
@ -156,6 +159,45 @@ func (analyzer *AnalysisOperation) fetchSectionFromIdentifier (
return return
} }
// resolvePrimitive checks to see if the locator is in the current module, and
// refers to a primitive. If it does, it returns a pointer to that primitive
// and true for exists. If it doesn't, it returns nil and false.
func (analyzer *AnalysisOperation) resolvePrimitive (
where locator,
) (
section Section,
exists bool,
) {
// primitives are scoped as if they are contained within the current
// module, so if the location refers to something outside of the current
// module, it is definetly not referring to a primitive.
if where.modulePath != analyzer.currentPosition.modulePath {
return
}
exists = true
switch where.name {
case "Int": section = &PrimitiveInt
case "UInt": section = &PrimitiveUInt
case "I8": section = &PrimitiveI8
case "I16": section = &PrimitiveI16
case "I32": section = &PrimitiveI32
case "I64": section = &PrimitiveI64
case "U8": section = &PrimitiveU8
case "U16": section = &PrimitiveU16
case "U32": section = &PrimitiveU32
case "U64": section = &PrimitiveU64
case "Objt": section = &PrimitiveObjt
case "Face": section = &PrimitiveFace
case "Func": section = &PrimitiveFunc
case "String": section = &BuiltInString
default:
exists = false
}
return
}
// addSection adds a section to the analyzer's section table. If a section with // addSection adds a section to the analyzer's section table. If a section with
// that name already exists, it panics because the parser should not have given // that name already exists, it panics because the parser should not have given
// that to us. // that to us.

View File

@ -26,6 +26,7 @@ func (analyzer AnalysisOperation) analyzeArgument (
inputArgument parser.Argument, inputArgument parser.Argument,
) ( ) (
outputArgument Argument, outputArgument Argument,
err error,
) { ) {
switch inputArgument.Kind() { switch inputArgument.Kind() {
case parser.ArgumentKindNil: case parser.ArgumentKindNil:

View File

@ -14,6 +14,7 @@ var PrimitiveU32 = createPrimitive("U32", Type {})
var PrimitiveU64 = createPrimitive("U64", Type {}) var PrimitiveU64 = createPrimitive("U64", Type {})
var PrimitiveObjt = createPrimitive("Objt", Type {}) var PrimitiveObjt = createPrimitive("Objt", Type {})
var PrimitiveFace = createPrimitive("Face", Type {}) var PrimitiveFace = createPrimitive("Face", Type {})
var PrimitiveFunc = createPrimitive("Func", Type {})
var BuiltInString = createPrimitive("String", Type { var BuiltInString = createPrimitive("String", Type {
actual: PrimitiveU32, actual: PrimitiveU32,

View File

@ -9,6 +9,7 @@ type TypeSection struct {
sectionBase sectionBase
what Type what Type
complete bool complete bool
argument Argument
// TODO: do not add members from parent type. instead have a member // TODO: do not add members from parent type. instead have a member
// function to discern whether this type contains a particular member, // function to discern whether this type contains a particular member,
// and have it recurse all the way up the family tree. it will be the // and have it recurse all the way up the family tree. it will be the
@ -42,6 +43,9 @@ func (member ObjectMember) ToString (indent int) (output string) {
func (section TypeSection) ToString (indent int) (output string) { func (section TypeSection) ToString (indent int) (output string) {
output += doIndent(indent, "typeSection ", section.where.ToString(), "\n") output += doIndent(indent, "typeSection ", section.where.ToString(), "\n")
output += section.what.ToString(indent + 1) output += section.what.ToString(indent + 1)
if section.argument != nil {
output += section.argument.ToString(indent + 1)
}
return return
} }
@ -66,7 +70,15 @@ func (analyzer AnalysisOperation) analyzeTypeSection () (
outputSection.what, err = analyzer.analyzeType(inputSection.Type()) outputSection.what, err = analyzer.analyzeType(inputSection.Type())
if err != nil { return } if err != nil { return }
if !inputSection.Argument().Nil() {
outputSection.argument,
err = analyzer.analyzeArgument(inputSection.Argument())
if err != nil { return }
// TODO: type check default value. possibly add a method to
// Argument that takes in a type and determines whether the
// argument can fit to it.
}
// TODO: analyze members // TODO: analyze members

View File

@ -6,5 +6,6 @@ func TestTypeSection (test *testing.T) {
checkTree ("../tests/analyzer/typeSection", false, checkTree ("../tests/analyzer/typeSection", false,
`typeSection ../tests/analyzer/typeSection.basicInt `typeSection ../tests/analyzer/typeSection.basicInt
type 1 basic Int type 1 basic Int
arg uint 5
`, test) `, test)
} }

View File

@ -63,8 +63,10 @@ func (what Type) ToString (indent int) (output string) {
} }
if what.actual != nil { if what.actual != nil {
output += what.actual.Name() output += " " + what.actual.Name()
} }
output += "\n"
return return
} }

View File

@ -115,6 +115,12 @@ func (argument Argument) Kind () (kind ArgumentKind) {
return return
} }
// Nil returns true if the argument is nil, and false if it isn't.
func (argument Argument) Nil () (isNil bool) {
isNil = argument.kind == ArgumentKindNil
return
}
// Value returns the underlying value of the argument. You can use Kind() to // Value returns the underlying value of the argument. You can use Kind() to
// find out what to cast this to. // find out what to cast this to.
func (argument Argument) Value () (value any) { func (argument Argument) Value () (value any) {