Example #1
0
// InjectMethods injects some methods into a class.
//
func (p *Compiler) InjectMethods(cls *exec.Class, code []byte) (err error) {

	defer func() {
		if e := recover(); e != nil {
			switch v := e.(type) {
			case string:
				err = errors.New(v)
			case error:
				err = v
			default:
				panic(e)
			}
		}
	}()

	engine, err := ipt.New(p, p.Opts)
	if err != nil {
		return
	}
	p.ipt = engine

	src, err := engine.Tokenize(code, "")
	if err != nil {
		return
	}

	p.cl(engine, "methods", src)
	arity := p.popArity()
	members := p.gstk.PopNArgs(arity)
	p.addMethods(cls, engine, members)
	p.Done()
	return
}
Example #2
0
// Cl compiles a qlang source code.
//
func (p *Compiler) Cl(codeText []byte, fname string) int {

	engine, err := interpreter.New(p, p.Opts)
	if err != nil {
		panic(err)
	}

	p.ipt = engine
	p.gvars["__dir__"] = path.Dir(fname)
	p.gvars["__file__"] = fname
	err = engine.MatchExactly(codeText, fname)
	if err != nil {
		panic(err)
	}
	return p.code.Len()
}