Exemple #1
0
func (b *Builder) Parse(name string, l lex.Lexer) (ast *AST, err error) {
	ctx := &builderCtx{
		ParseName:  name,
		Lexer:      l,
		Root:       NewRootNode(),
		Tokens:     [3]lex.LexItem{},
		FrameStack: util.NewStack(5),
		Frames:     util.NewStack(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
}
Exemple #2
0
// NewState creates a new State struct
func NewState() *State {
	st := &State{
		opidx:      0,
		pc:         NewByteCode(),
		stack:      util.NewStack(5),
		markstack:  util.NewStack(5),
		framestack: util.NewStack(5),
		frames:     util.NewStack(5),
		vars:       make(Vars),
		warn:       os.Stderr,
	}

	st.Pushmark()
	st.PushFrame()
	return st
}