Exemplo n.º 1
0
Arquivo: bf.go Projeto: BenLubar/bit
func NewWriter(writer io.Writer) *Writer {
	w := &Writer{Writer: bitgen.NewWriter(writer)}
	w.DataPtr = w.ReserveVariable()
	w.ScratchPtr = w.ReserveVariable()
	w.DataIndex = w.ReserveInteger(64)
	w.Scratch64 = w.ReserveInteger(64)
	w.UserStart = w.ReserveHeap()
	return w
}
Exemplo n.º 2
0
func (ast *AST) WriteTo(out io.Writer) (n int64, err error) {
	w := &writer{Writer: bitgen.NewWriter(out), AST: ast}
	defer func() {
		if e := w.Close(); err == nil {
			err = e
		}
	}()

	start := w.Init()

	w.InitJumpTable()

	next := w.ReserveLine()
	w.MethodTables(start, next)
	start = next

	next = w.ReserveLine()
	w.New(start, w.Return, ast.main, next)
	start = next

	next = w.ReserveLine()
	w.BeginStack(start, next)
	start = next

	next = w.ReserveLine()
	w.CopyReg(start, w.This, w.Return, next)
	start = next

	w.StaticCall(start, &StaticCallExpr{
		Name: ID{
			Name:   "Main",
			target: ast.main.methods["Main"],
		},
	}, 0)

	return w.n, w.err
}
Exemplo n.º 3
0
func testOptRaw(t *testing.T, f func(w *bitgen.Writer) (int64, error)) {
	r, w := io.Pipe()

	errs := make(chan error, 2)
	ch := make(chan Program, 1)

	go func() {
		bw := bitgen.NewWriter(w)

		_, err := f(bw)
		if err != nil {
			t.Error(err)
			errs <- err
			return
		}

		err = bw.Close()
		if err != nil {
			t.Error(err)
			errs <- err
			return
		}

		err = w.Close()
		if err != nil {
			t.Error(err)
			errs <- err
			return
		}
	}()

	go func() {
		prog, err := Parse(bufio.NewReader(r))
		if err != nil {
			t.Error(err)
			errs <- err
			return
		}
		ch <- prog
	}()

	select {
	case err := <-errs:
		panic(err)

	case prog := <-ch:
		var buf0, buf1 bytes.Buffer

		ctx0, err := prog.run(nil, bitio.NewWriter(&buf0), nil)
		if err != nil {
			panic(err)
		}

		prog.Optimize()

		trace := func(l *line, c *context) {
			if l.opt != nil {
				t.Logf("%d: %#v", l.num, l.opt)
			} else {
				t.Logf("%d: %#v", l.num, l.stmt)
			}
		}
		ctx1, err := prog.run(nil, bitio.NewWriter(&buf1), trace)
		if err != nil {
			panic(err)
		}

		if !reflect.DeepEqual(ctx0, ctx1) || !bytes.Equal(buf0.Bytes(), buf1.Bytes()) {
			t.Errorf("expected equal:\n%#v\n%q\n%#v\n%q", ctx0, buf0.Bytes(), ctx1, buf1.Bytes())
		}
	}
}
Exemplo n.º 4
0
Arquivo: main.go Projeto: BenLubar/bit
func NewWriter(w io.Writer) *Writer {
	return &Writer{bitgen.NewWriter(w)}
}