From ea31b462d83c9ccb2fcc8a5e582f2bcdfb8aadb9 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 23 Jun 2024 15:04:37 +0500 Subject: [PATCH] feat: screw the recompile, push it to the user. --- main.go | 16 +++++++--------- tengo.go | 33 +++++++++++++++++++++++---------- tool.go | 3 +-- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index be3e0ac..db61f1f 100644 --- a/main.go +++ b/main.go @@ -26,10 +26,9 @@ func New(tengo *Tengo ) *Preprocessor { func (pp *Preprocessor) Process( ctx context.Context, - recompile bool, filePath string, data []byte, -) ([]byte, error) { +) (*Compiled, []byte, error) { //var b bytes.Buffer preCode := []byte(nil) texts := [][]byte{} @@ -38,7 +37,7 @@ func (pp *Preprocessor) Process( if bytes.HasPrefix(data, pref) { idxEnd := bytes.Index(data, pp.tags[1]) if idxEnd < 0 { - return nil, UnexpectedError{ + return nil, nil, UnexpectedError{ What: "pre-code start tag", } } @@ -52,14 +51,14 @@ func (pp *Preprocessor) Process( //fmt.Printf("cock %d %d %d\n", last, idxStart, idxEnd) if idxStart < 0 { if idxEnd >= 0 { - return nil, UnexpectedError{ + return nil, nil, UnexpectedError{ What: "end tag", } } texts = append(texts, data) break } else if idxEnd < 0 { - return nil, UnexpectedError{ + return nil, nil, UnexpectedError{ What: "start tag", } } @@ -73,18 +72,17 @@ func (pp *Preprocessor) Process( data = data[idxEnd + len(pp.tags[1]):] } - ret, err := pp.tengo.Eval( + compiled, ret, err := pp.tengo.Eval( ctx, - recompile, filePath, texts, preCode, codes, ) if err != nil { - return nil, err + return nil, nil, err } - return ret, nil + return compiled, ret, nil } diff --git a/tengo.go b/tengo.go index 037bb06..66df5dd 100644 --- a/tengo.go +++ b/tengo.go @@ -8,12 +8,15 @@ import ( "strconv" ) +type CompiledMap map[string] *Compiled + type Script = tengo.Script type Compiled = tengo.Compiled +// The type describes +// customizable way to evaluate +// files. type Tengo struct { - head string - compiledScripts map[string] *Compiled // Functions to modify // preprocessor for // more specific purposes. @@ -44,15 +47,18 @@ func (tengo *Tengo) SetPreCompile(fn func(context.Context, *Script)) *Tengo { return tengo } -// Simple Evaler implementation for the Tengo language func (pp *Tengo) Eval( ctx context.Context, - recompile bool, + // File path to give it. filePath string, + // Static text pieces. texts [][]byte, + // The code that comes before any + // dynamic code. preCode []byte, + // General code. codes [][]byte, -) ([]byte, error) { +) (*Compiled, []byte, error) { var fullCodeBuf, retBuf bytes.Buffer const retHead = ` context := {} @@ -68,8 +74,6 @@ func (pp *Tengo) Eval( const retSeparator = ` __separate__() ` - //rets := [][]byte{} - fmt.Fprint(&fullCodeBuf, retHead) @@ -193,10 +197,19 @@ func (pp *Tengo) Eval( }, }) - _, err := script.RunContext(ctx) + compiled, err := script.Compile() if err != nil { - return nil, err + return nil, nil, err + } + // To keep everything of the changes. + compiled = compiled.Clone() + + _, err = script.RunContext(ctx) + if err != nil { + return nil, nil, err } - return retBuf.Bytes(), nil + return compiled, retBuf.Bytes(), nil } + + diff --git a/tool.go b/tool.go index e34f029..9b41082 100644 --- a/tool.go +++ b/tool.go @@ -42,9 +42,8 @@ var Tool = mtool.T("tpp").Func(func(flags *mtool.Flags){ log.Println("read error:", err) continue } - out, err := pp.Process( + _, out, err := pp.Process( context.Background(), - true, pth, bts, )