diff --git a/analyzer/analyzer.go b/analyzer/analyzer.go index eeccaa0..1004f3c 100644 --- a/analyzer/analyzer.go +++ b/analyzer/analyzer.go @@ -1,6 +1,7 @@ package analyzer import "os" +import "fmt" import "path/filepath" // import "git.tebibyte.media/arf/arf/types" import "git.tebibyte.media/arf/arf/parser" @@ -104,3 +105,12 @@ func (analyzer *AnalysisOperation) fetchSection ( analyzer.currentSection = previousSection return } + +func doIndent (indent int, input ...any) (output string) { + for index := 0; index < indent; index ++ { + output += "\t" + } + + output += fmt.Sprint(input...) + return +} diff --git a/analyzer/primitives.go b/analyzer/primitives.go index b92f92d..248a47e 100644 --- a/analyzer/primitives.go +++ b/analyzer/primitives.go @@ -2,23 +2,28 @@ package analyzer // This is a global, cannonical list of primitive and built-in types. -var PrimitiveInt = TypeSection { sectionBase: sectionBase { name: "Int" } } -var PrimitiveUInt = TypeSection { sectionBase: sectionBase { name: "UInt" } } -var PrimitiveI8 = TypeSection { sectionBase: sectionBase { name: "I8 " } } -var PrimitiveI16 = TypeSection { sectionBase: sectionBase { name: "I16 " } } -var PrimitiveI32 = TypeSection { sectionBase: sectionBase { name: "I32 " } } -var PrimitiveI64 = TypeSection { sectionBase: sectionBase { name: "I64 " } } -var PrimitiveU8 = TypeSection { sectionBase: sectionBase { name: "U8 " } } -var PrimitiveU16 = TypeSection { sectionBase: sectionBase { name: "U16 " } } -var PrimitiveU32 = TypeSection { sectionBase: sectionBase { name: "U32 " } } -var PrimitiveU64 = TypeSection { sectionBase: sectionBase { name: "U64 " } } +var PrimitiveInt = createPrimitive("Int", Type {}) +var PrimitiveUInt = createPrimitive("UInt", Type {}) +var PrimitiveI8 = createPrimitive("I8", Type {}) +var PrimitiveI16 = createPrimitive("I16", Type {}) +var PrimitiveI32 = createPrimitive("I32", Type {}) +var PrimitiveI64 = createPrimitive("I64", Type {}) +var PrimitiveU8 = createPrimitive("U8", Type {}) +var PrimitiveU16 = createPrimitive("U16", Type {}) +var PrimitiveU32 = createPrimitive("U32", Type {}) +var PrimitiveU64 = createPrimitive("U64", Type {}) +var PrimitiveObjt = createPrimitive("Objt", Type {}) +var PrimitiveFace = createPrimitive("Face", Type {}) -var PrimitiveObjt = TypeSection { sectionBase: sectionBase { name: "Objt" } } -var PrimitiveFace = TypeSection { sectionBase: sectionBase { name: "Face" } } +var BuiltInString = createPrimitive("String", Type { + actual: PrimitiveU8, + kind: TypeKindVariableArray, +}) -var BuiltInString = TypeSection { - inherits: Type { - actual: PrimitiveU8, - kind: TypeKindVariableArray, - }, +// createPrimitive provides a quick way to construct a primitive for the above +// list. +func createPrimitive (name string, inherits Type) (primitive TypeSection) { + primitive.where = locator { name: name } + primitive.inherits = inherits + return } diff --git a/analyzer/table.go b/analyzer/table.go index 478cdac..94e1142 100644 --- a/analyzer/table.go +++ b/analyzer/table.go @@ -6,6 +6,11 @@ type locator struct { name string } +func (where locator) ToString () (output string) { + output += where.modulePath + "." + where.name + return +} + // SectionTable stores a list of semantically analized sections from one module, // and all sections that it requires from other modules. type SectionTable map[locator] Section @@ -32,16 +37,24 @@ type Section interface { // sectionBase is a struct that all sections must embed. type sectionBase struct { - name string + where locator complete bool } // Name returns the name of the section. func (section sectionBase) Name () (name string) { - name = section.name + name = section.where.name return } +// ModulePath returns the full path of the module the section came from. +func (section sectionBase) ModulePath () (name string) { + name = section.where.modulePath + return +} + +// TODO: ModuleName returns the name of the module where the section came from. + // Complete returns wether the section has been completed. func (section sectionBase) Complete () (complete bool) { complete = section.complete diff --git a/analyzer/type.go b/analyzer/type.go index 0c354f8..d60f221 100644 --- a/analyzer/type.go +++ b/analyzer/type.go @@ -1,6 +1,5 @@ package analyzer -import "fmt" import "git.tebibyte.media/arf/arf/types" // TypeKind represents what kind of type a type is. @@ -33,9 +32,20 @@ type ObjectMember struct { // a defaultValue member here. } +func (member ObjectMember) ToString (indent int) (output string) { + output += doIndent ( + indent, + member.name, " ", + member.permission.ToString(), + // TODO: default value + "\n") + return +} + // Type represents a description of a type. It must eventually point to a // TypeSection. type Type struct { + // one of these must be nil. actual Section points *Type @@ -58,7 +68,32 @@ type Type struct { // ToString returns all data stored within the type, in string form. func (what Type) ToString (indent int) (output string) { - output += fmt.Sprint("") + output += doIndent(indent, "type ", what.length) + + if what.mutable { + output += " mutable" + } + + switch what.kind { + case TypeKindBasic: + output += " basic" + case TypeKindPointer: + output += " pointer" + case TypeKindVariableArray: + output += " variableArray" + case TypeKindObject: + output += " object" + } + + if what.points != nil { + output += " {\n" + output += what.points.ToString(indent + 1) + output += doIndent(indent, "}") + } + + if what.actual != nil { + output += what.actual.Name() + } return } @@ -76,5 +111,7 @@ func (section TypeSection) Kind () (kind SectionKind) { // ToString returns all data stored within the type section, in string form. func (section TypeSection) ToString (indent int) (output string) { + output += doIndent(indent, "typeSection ", section.where.ToString(), "\n") + output += section.inherits.ToString(indent + 1) return }