Merge pull request 'parser-cache' (#4) from parser-cache into main
Reviewed-on: arf/arf#4
This commit is contained in:
commit
c8181f93fa
Binary file not shown.
Before Width: | Height: | Size: 115 KiB After Width: | Height: | Size: 114 KiB |
11
parser/cache.go
Normal file
11
parser/cache.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
// cacheItem stores an item of the parser cache.
|
||||||
|
type cacheItem struct {
|
||||||
|
tree SyntaxTree
|
||||||
|
skimmed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// cache stores all modules that have been parsed so far. They are indexed with
|
||||||
|
// their full path on the filesystem, starting with '/'.
|
||||||
|
var cache = make(map[string] cacheItem)
|
@ -18,15 +18,28 @@ type ParsingOperation struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// - implement parser cache
|
// * implement parser cache
|
||||||
// - have this try to hit the cache, and actually parse on miss
|
// * have this try to hit the cache, and actually parse on miss
|
||||||
// - rename this to Fetch
|
// * rename this to Fetch
|
||||||
// - add `skim bool` argument. when this is true, don't parse any code or data
|
// - add `skim bool` argument. when this is true, don't parse any code or data
|
||||||
// section initialization values, just definitions and their default values.
|
// section initialization values, just definitions and their default values.
|
||||||
|
|
||||||
// Parse reads the files located in the module specified by modulePath, and
|
// Fetch returns the parsed module located at the specified path, and returns an
|
||||||
// converts them into an abstract syntax tree.
|
// abstract syntax tree. If the module has not yet been parsed, it parses it
|
||||||
func Parse (modulePath string) (tree SyntaxTree, err error) {
|
// first.
|
||||||
|
func Fetch (modulePath string, skim bool) (tree SyntaxTree, err error) {
|
||||||
|
if modulePath[0] != '/' {
|
||||||
|
panic("module path did not begin at filesystem root")
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to hit cache
|
||||||
|
cached, exists := cache[modulePath]
|
||||||
|
if exists && !(!skim && cached.skimmed){
|
||||||
|
tree = cached.tree
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// miss, so parse the module.
|
||||||
parser := ParsingOperation {
|
parser := ParsingOperation {
|
||||||
modulePath: modulePath,
|
modulePath: modulePath,
|
||||||
tree: SyntaxTree {
|
tree: SyntaxTree {
|
||||||
@ -56,6 +69,13 @@ func Parse (modulePath string) (tree SyntaxTree, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tree = parser.tree
|
tree = parser.tree
|
||||||
|
|
||||||
|
// cache tree
|
||||||
|
cache[modulePath] = cacheItem {
|
||||||
|
tree: tree,
|
||||||
|
skimmed: false,
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import "io"
|
import "io"
|
||||||
|
import "os"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "testing"
|
import "testing"
|
||||||
// import "git.tebibyte.media/arf/arf/types"
|
import "path/filepath"
|
||||||
|
|
||||||
func checkTree (modulePath string, correct string, test *testing.T) {
|
func checkTree (modulePath string, correct string, test *testing.T) {
|
||||||
tree, err := Parse(modulePath)
|
cwd, _ := os.Getwd()
|
||||||
|
modulePath = filepath.Join(cwd, modulePath)
|
||||||
|
println(modulePath)
|
||||||
|
tree, err := Fetch(modulePath, false)
|
||||||
|
|
||||||
treeString := tree.ToString(0)
|
treeString := tree.ToString(0)
|
||||||
treeRunes := []rune(treeString)
|
treeRunes := []rune(treeString)
|
||||||
|
Reference in New Issue
Block a user