Some ToString functionality

This commit is contained in:
Sasha Koshka 2022-09-10 19:50:18 -04:00
parent b3e2d9f822
commit 3635eef15a
4 changed files with 86 additions and 21 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}