Ejemplo n.º 1
0
func (b *Builder) Parse(name string, l lex.Lexer) (ast *AST, err error) {
	ctx := &builderCtx{
		ParseName:  name,
		Lexer:      l,
		Root:       node.NewRootNode(),
		Tokens:     [3]lex.LexItem{},
		FrameStack: stack.New(5),
		Frames:     stack.New(5),
	}

	defer func() {
		if ctx.Error != nil {
			err = ctx.Error
			ast = nil
			// don't let the panic propagate
			recover()
		}
	}()

	b.Start(ctx)
	b.ParseStatements(ctx)
	return &AST{
		Name: name,
		Root: ctx.Root,
	}, nil
}
Ejemplo n.º 2
0
// NewState creates a new State struct
func NewState() *State {
	st := &State{
		opidx:        0,
		pc:           NewByteCode(),
		stack:        stack.New(5),
		markstack:    stack.New(5),
		framestack:   stack.New(5),
		frames:       stack.New(5),
		vars:         make(Vars),
		warn:         os.Stderr,
		MaxLoopCount: 1000,
	}

	st.Pushmark()
	st.PushFrame()
	return st
}
Ejemplo n.º 3
0
func TestFrame_Lvar(t *testing.T) {
	f := New(stack.New(5))
	f.SetLvar(0, 1)
	x, err := f.GetLvar(0)
	if !assert.NoError(t, err, "f.GetLvar(0) should succeed") {
		return
	}

	if !assert.Equal(t, 1, x, "f.GetLvar(0) should be 1") {
		return
	}
}