From 2e79d9c834f7f98a58ac72c39b376082d71a3d63 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 5 Sep 2022 13:31:38 -0400 Subject: [PATCH] Parse -> Fetch, now tries to hit cache --- parser/cache.go | 2 +- parser/parser.go | 30 +++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/parser/cache.go b/parser/cache.go index a2881c3..a07dbd6 100644 --- a/parser/cache.go +++ b/parser/cache.go @@ -2,7 +2,7 @@ package parser // cacheItem stores an item of the parser cache. type cacheItem struct { - section Section + tree SyntaxTree skimmed bool } diff --git a/parser/parser.go b/parser/parser.go index 215a679..9e4a994 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -18,15 +18,28 @@ type ParsingOperation struct { } // TODO: -// - implement parser cache -// - have this try to hit the cache, and actually parse on miss +// * implement parser cache +// * have this try to hit the cache, and actually parse on miss // - rename this to Fetch // - add `skim bool` argument. when this is true, don't parse any code or data // section initialization values, just definitions and their default values. -// Parse reads the files located in the module specified by modulePath, and -// converts them into an abstract syntax tree. -func Parse (modulePath string) (tree SyntaxTree, err error) { +// Fetch returns the parsed module located at the specified path, and returns an +// abstract syntax tree. If the module has not yet been parsed, it parses it +// first. +func Fetch (modulePath string) (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 { + tree = cached.tree + return + } + + // miss, so parse the module. parser := ParsingOperation { modulePath: modulePath, tree: SyntaxTree { @@ -56,6 +69,13 @@ func Parse (modulePath string) (tree SyntaxTree, err error) { } tree = parser.tree + + // cache tree + cache[modulePath] = cacheItem { + tree: tree, + skimmed: false, + } + return }