2022-09-07 10:20:34 -06:00
|
|
|
package analyzer
|
|
|
|
|
2022-09-07 13:50:37 -06:00
|
|
|
import "os"
|
2022-09-10 17:50:18 -06:00
|
|
|
import "fmt"
|
2022-09-07 13:50:37 -06:00
|
|
|
import "path/filepath"
|
2022-09-09 18:55:03 -06:00
|
|
|
// import "git.tebibyte.media/arf/arf/types"
|
2022-09-07 13:50:37 -06:00
|
|
|
import "git.tebibyte.media/arf/arf/parser"
|
2022-09-09 18:55:03 -06:00
|
|
|
// import "git.tebibyte.media/arf/arf/infoerr"
|
2022-09-07 13:50:37 -06:00
|
|
|
|
2022-09-07 10:20:34 -06:00
|
|
|
// AnalysisOperation holds information about an ongoing analysis operation.
|
|
|
|
type AnalysisOperation struct {
|
|
|
|
sectionTable SectionTable
|
2022-09-07 13:50:37 -06:00
|
|
|
modulePath string
|
2022-09-08 23:09:17 -06:00
|
|
|
|
2022-09-09 18:55:03 -06:00
|
|
|
currentPosition locator
|
|
|
|
currentSection parser.Section
|
2022-09-07 13:50:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Analyze performs a semantic analyisys on the module specified by path, and
|
|
|
|
// returns a SectionTable that can be translated into C.
|
|
|
|
func Analyze (modulePath string, skim bool) (table SectionTable, err error) {
|
|
|
|
if modulePath[0] != '/' {
|
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
modulePath = filepath.Join(cwd, modulePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
analyzer := AnalysisOperation {
|
|
|
|
sectionTable: make(SectionTable),
|
|
|
|
modulePath: modulePath,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = analyzer.analyze()
|
|
|
|
table = analyzer.sectionTable
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// analyze performs an analysis operation given the state of the operation
|
|
|
|
// struct.
|
|
|
|
func (analyzer *AnalysisOperation) analyze () (err error) {
|
|
|
|
tree, err := parser.Fetch(analyzer.modulePath, false)
|
|
|
|
sections := tree.Sections()
|
|
|
|
|
|
|
|
for !sections.End() {
|
2022-09-08 23:09:17 -06:00
|
|
|
_, err = analyzer.fetchSection(locator {
|
|
|
|
modulePath: analyzer.modulePath,
|
|
|
|
name: sections.Value().Name(),
|
|
|
|
})
|
2022-09-07 13:50:37 -06:00
|
|
|
sections.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2022-09-07 10:20:34 -06:00
|
|
|
}
|
2022-09-08 23:09:17 -06:00
|
|
|
|
|
|
|
// fetchSection returns a section from the section table. If it has not already
|
|
|
|
// been analyzed, it analyzes it first. If the section does not actually exist,
|
|
|
|
// a nil section is returned. When this happens, an error should be created on
|
|
|
|
// whatever syntax tree node "requested" the section be analyzed.
|
|
|
|
func (analyzer *AnalysisOperation) fetchSection (
|
|
|
|
where locator,
|
|
|
|
) (
|
|
|
|
section Section,
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
var exists bool
|
|
|
|
section, exists = analyzer.sectionTable[where]
|
|
|
|
if exists { return }
|
|
|
|
|
|
|
|
// fetch the module. since we already have our main module parsed fully
|
|
|
|
// and not skimmed, we can just say "yeah lets skim stuff here".
|
|
|
|
var tree parser.SyntaxTree
|
|
|
|
tree, err = parser.Fetch(where.modulePath, true)
|
|
|
|
if err != nil {
|
|
|
|
section = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var parsedSection = tree.LookupSection(where.name)
|
|
|
|
if parsedSection == nil {
|
|
|
|
section = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-09 18:55:03 -06:00
|
|
|
previousPosition := analyzer.currentPosition
|
|
|
|
previousSection := analyzer.currentSection
|
|
|
|
analyzer.currentPosition = where
|
|
|
|
analyzer.currentSection = parsedSection
|
2022-09-08 23:53:53 -06:00
|
|
|
|
2022-09-18 00:41:06 -06:00
|
|
|
defer func () {
|
|
|
|
analyzer.currentPosition = previousPosition
|
|
|
|
analyzer.currentSection = previousSection
|
|
|
|
} ()
|
|
|
|
|
2022-09-09 18:55:03 -06:00
|
|
|
// TODO: analyze section. have analysis methods work on currentPosition
|
|
|
|
// and currentSection.
|
|
|
|
//
|
|
|
|
// while building an analyzed section, add it to the section
|
|
|
|
// table as soon as the vital details are acquired, and mark it as
|
|
|
|
// incomplete. that way, it can still be referenced by itself in certain
|
|
|
|
// scenarios.
|
2022-09-08 23:09:17 -06:00
|
|
|
switch parsedSection.Kind() {
|
|
|
|
case parser.SectionKindType:
|
2022-09-18 00:41:06 -06:00
|
|
|
section, err = analyzer.analyzeTypeSection()
|
|
|
|
if err != nil { return}
|
2022-09-08 23:09:17 -06:00
|
|
|
case parser.SectionKindEnum:
|
|
|
|
case parser.SectionKindFace:
|
|
|
|
case parser.SectionKindData:
|
|
|
|
case parser.SectionKindFunc:
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2022-09-09 18:55:03 -06:00
|
|
|
}
|
2022-09-10 17:50:18 -06:00
|
|
|
|
|
|
|
func doIndent (indent int, input ...any) (output string) {
|
|
|
|
for index := 0; index < indent; index ++ {
|
|
|
|
output += "\t"
|
|
|
|
}
|
|
|
|
|
|
|
|
output += fmt.Sprint(input...)
|
|
|
|
return
|
|
|
|
}
|