Skip to content

Commit

Permalink
interpreter.Context
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 17, 2020
1 parent 61b329c commit 8a11afb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
15 changes: 8 additions & 7 deletions tpl/interpreter.util/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ var (

// -----------------------------------------------------------------------------

// Context represents the context of an interpreter.
type Context struct {
Src interface{} // []tpl.Token
Pos token.Pos
End token.Pos
}

// FileLine represents file name and line number.
type FileLine struct {
File string
Expand All @@ -53,6 +46,14 @@ type Engine interface {
Source(src interface{}) []byte
}

// Context represents the context of an interpreter.
type Context struct {
Src interface{} // []tpl.Token
Pos token.Pos
End token.Pos
Engine Engine
}

// Call calls a fn.
func Call(stk Stack, fn interface{}, arity int) error {
tfn := reflect.TypeOf(fn)
Expand Down
19 changes: 13 additions & 6 deletions tpl/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var (
typeIntf = reflect.TypeOf((*interface{})(nil)).Elem()
typeEng = reflect.TypeOf((*interpreter.Engine)(nil)).Elem()
typeCtx = reflect.TypeOf((*interpreter.Context)(nil))
zeroIntfVal = reflect.Zero(typeIntf)
)

Expand Down Expand Up @@ -263,6 +264,8 @@ func New(ipt interpreter.Interface, options *Options) (p *Engine, err error) {
args[1] = reflect.ValueOf(g.Len())
} else {
switch targ1 {
case typeCtx:
args[1] = p.ctxValue(tokens)
case typeIntf:
if len(tokens) == 0 { // use nil as empty source
args[1] = zeroIntfVal
Expand All @@ -281,12 +284,7 @@ func New(ipt interpreter.Interface, options *Options) (p *Engine, err error) {
}
}
if n == 3 {
ctx := &interpreter.Context{Src: tokens}
if len(tokens) > 0 {
ctx.Pos = tokens[0].Pos
ctx.End = tokens[len(tokens)-1].End()
}
args[2] = reflect.ValueOf(ctx)
args[2] = p.ctxValue(tokens)
}
ret := reflect.ValueOf(fn).Call(args)
if len(ret) > 0 {
Expand Down Expand Up @@ -321,6 +319,15 @@ func New(ipt interpreter.Interface, options *Options) (p *Engine, err error) {
return
}

func (p *Engine) ctxValue(tokens []tpl.Token) reflect.Value {
ctx := &interpreter.Context{Src: tokens, Engine: interpreter.Engine(p)}
if len(tokens) > 0 {
ctx.Pos = tokens[0].Pos
ctx.End = tokens[len(tokens)-1].End()
}
return reflect.ValueOf(ctx)
}

// Source returns the source text of src.
func (p *Engine) Source(src interface{}) (text []byte) {
if src == nil {
Expand Down
15 changes: 11 additions & 4 deletions tpl/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ import (
// -----------------------------------------------------------------------------

const codeIf = `
today = 3
today = 5
if today == 1 {
today = "Mon"
} elif today == 2 {
today = "Tue"
} elif today == 3 {
today = "Wed"
} else {
today = 0
for i = 0; i < 10; i++ {
today = today + i
}
}
println(today)
panic("haha")
`

func TestIf(t *testing.T) {
Expand All @@ -32,14 +39,14 @@ func TestIf(t *testing.T) {
}

err = lang.SafeEval(codeIf)
if err != nil {
if err != nil && err.Error() != "line 19: runtime error: haha" {
t.Fatal(err)
}

v, _ := lang.Var("today")
fmt.Println(v)
if v != "Wed" {
t.Fatal("MaxPrime ret:", v)
if v != 45 {
t.Fatal("ret:", v)
}
}

Expand Down
3 changes: 2 additions & 1 deletion tpl/qlang/cl/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func (p *Interpreter) And(e interpreter.Engine) {
evalCode(e, p, "term3", bCode)
}

func (p *Interpreter) If(e interpreter.Engine) {
func (p *Interpreter) If(ctx *interpreter.Context) {

var elseCode interface{}
var e = ctx.Engine

stk := p.stk
elseArity := stk.popArity()
Expand Down

0 comments on commit 8a11afb

Please sign in to comment.