Example #1
0
// BuildPkgs builds a list of packages
func (b *Builder) BuildPkgs(pkgs []string) []*lex8.Error {
	for _, p := range pkgs {
		if _, es := b.prepare(p); es != nil {
			return es
		}
	}

	g := &dagvis.Graph{b.deps}
	g = g.Reverse()

	m, err := dagvis.Layout(g)
	if err != nil {
		return lex8.SingleErr(err)
	}
	// TODO: save the package dep map

	nodes := m.SortedNodes()
	for _, node := range nodes {
		if b.Verbose { // report progress
			fmt.Println(node.Name)
		}

		pkg := b.pkgs[node.Name]
		if pkg == nil {
			panic("package not prepared")
		}

		pkg.deps = deps(node)
		if es := b.build(pkg); es != nil {
			return es
		}
	}

	return nil
}
Example #2
0
// BuildBareFunc builds a function body into an image.
func BuildBareFunc(f string, rc io.ReadCloser) ([]byte, []*lex8.Error) {
	fn, es := parse.BareFunc(f, rc)
	if es != nil {
		return nil, es
	}

	// resolving pass
	log := lex8.NewErrorList()
	rfunc := resolveFunc(log, fn)
	if es := log.Errs(); es != nil {
		return nil, es
	}

	// building pass
	b := newBuilder("main")
	fobj := buildFunc(b, rfunc)
	if es := b.Errs(); es != nil {
		return nil, es
	}

	ret, e := link8.LinkBareFunc(fobj)
	if e != nil {
		return nil, lex8.SingleErr(e)
	}

	return ret, nil
}
Example #3
0
// Build builds a package.
func (b *Builder) Build(p string) []*lex8.Error {
	if !b.input.HasPkg(p) {
		return lex8.SingleErr(fmt.Errorf(
			"package %q not found", p,
		))
	}
	return b.BuildPkgs([]string{p})
}
Example #4
0
// Plan returns all the packages required for building the specified
// target packages.
func (b *Builder) Plan(pkgs []string) ([]string, []*lex8.Error) {
	for _, p := range pkgs {
		if pkg, es := prepare(b.context, p); es != nil {
			return nil, es
		} else if pkg.err != nil {
			return nil, lex8.SingleErr(pkg.err)
		}
	}

	g := &dagvis.Graph{b.deps}
	g = g.Reverse()

	ret, err := dagvis.TopoSort(g)
	if err != nil {
		return nil, lex8.SingleErr(err)
	}
	return ret, nil
}
Example #5
0
func (bare bareFunc) Compile(pinfo *build8.PkgInfo, opts *build8.Options) (
	pkg *build8.Package, es []*lex8.Error,
) {
	// parsing
	theFile, e := findTheFile(pinfo)
	if e != nil {
		return nil, lex8.SingleErr(e)
	}
	stmts, es := parse.Stmts(theFile.Path, theFile)
	if es != nil {
		return nil, es
	}

	// building
	b := makeBuilder(pinfo)
	if es = b.Errs(); es != nil {
		return nil, es
	}
	if es := buildBareFunc(b, stmts); es != nil {
		return nil, es
	}
	if es := b.Errs(); es != nil {
		return nil, es
	}

	// logging
	if e := logIr(pinfo, b); e != nil {
		return nil, lex8.SingleErr(e)
	}

	// codegen
	lib, errs := ir.BuildPkg(b.p)
	if errs != nil {
		return nil, errs
	}

	ret := &build8.Package{
		Lang: "g8-barefunc",
		Main: startName,
		Lib:  lib,
	}
	return ret, nil
}
Example #6
0
File: build.go Project: Xslxy/e8vm
func build(c *context, pkgs []string) []*lex8.Error {
	for _, p := range pkgs {
		if pkg, es := prepare(c, p); es != nil {
			return es
		} else if pkg.err != nil {
			return lex8.SingleErr(pkg.err)
		}
	}

	if c.RunTests {
		for _, p := range pkgs {
			c.pkgs[p].runTests = true
		}
	}

	g := &dagvis.Graph{c.deps}
	g = g.Reverse()

	m, err := dagvis.Layout(g)
	if err != nil {
		return lex8.SingleErr(err)
	}
	if c.SaveDeps != nil {
		c.SaveDeps(m)
	}

	nodes := m.SortedNodes()
	for _, node := range nodes {
		if c.Verbose { // report progress
			logln(c, node.Name)
		}

		pkg := c.pkgs[node.Name]
		pkg.deps = deps(node)
		if es := buildPkg(c, pkg); es != nil {
			return es
		}
	}

	return nil
}
Example #7
0
// BuildSingleFile builds a package named "main" from a single file.
func BuildSingleFile(f string, rc io.ReadCloser) ([]byte, []*lex8.Error) {
	path := "_"

	pinfo := build8.SimplePkg(path, f, rc)

	pkg, errs := Lang().Compile(pinfo, new(build8.Options))
	if errs != nil {
		return nil, errs
	}

	secs, err := link8.LinkSinglePkg(pkg.Lib, pkg.Main)
	if err != nil {
		return nil, lex8.SingleErr(err)
	}

	buf := new(bytes.Buffer)
	if err := e8.Write(buf, secs); err != nil {
		return nil, lex8.SingleErr(err)
	}
	return buf.Bytes(), nil
}
Example #8
0
func buildMainPkg(home *build8.MemHome) (
	image []byte, errs []*lex8.Error, log []byte,
) {
	b := build8.NewBuilder(home)
	if errs := b.BuildAll(); errs != nil {
		return nil, errs, nil
	}

	image = home.Bin("main")
	log = home.Log("main", "ir")
	if image == nil {
		err := errors.New("missing main() function, no binary created")
		return nil, lex8.SingleErr(err), log
	}

	return image, nil, log
}
Example #9
0
// FileResult returns a parsing result.
func FileResult(f string, rc io.ReadCloser) (*Result, []*lex8.Error) {
	p, rec := newParser(f, rc)
	parsed := parseFile(p)
	e := rc.Close()

	if e != nil {
		return nil, lex8.SingleErr(e)
	}
	if es := p.Errs(); es != nil {
		return nil, es
	}

	res := &Result{
		File:   parsed,
		Tokens: rec.Tokens(),
	}
	return res, nil
}
Example #10
0
File: file.go Project: Xslxy/e8vm
// File parses a file.
func File(f string, r io.Reader, golike bool) (
	*ast.File, *lex8.Recorder, []*lex8.Error,
) {
	bs, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, nil, lex8.SingleErr(err)
	}

	es := textbox.CheckRect(f, bytes.NewReader(bs), MaxLine, MaxCol)
	if es != nil {
		return nil, nil, es
	}

	p, rec := makeParser(f, bytes.NewReader(bs), golike)
	ret := parseFile(p)
	if es := p.Errs(); es != nil {
		return nil, nil, es
	}
	return ret, rec, nil
}
Example #11
0
// BuildPkgs builds a list of packages
func (b *Builder) BuildPkgs(pkgs []string) []*lex8.Error {
	for _, p := range pkgs {
		if _, es := b.prepare(p); es != nil {
			return es
		}
	}

	if b.RunTests {
		for _, p := range pkgs {
			b.pkgs[p].runTests = true
		}
	}

	g := &dagvis.Graph{b.deps}
	g = g.Reverse()

	m, err := dagvis.Layout(g)
	if err != nil {
		return lex8.SingleErr(err)
	}
	if b.SaveDeps != nil {
		b.SaveDeps(m)
	}

	nodes := m.SortedNodes()
	for _, node := range nodes {
		if b.Verbose { // report progress
			b.logln(node.Name)
		}

		pkg := b.pkgs[node.Name]
		pkg.deps = deps(node)
		if es := b.build(pkg); es != nil {
			return es
		}
	}

	return nil
}
Example #12
0
func (l *lang) Compile(pinfo *build8.PkgInfo, opts *build8.Options) (
	*build8.Package, []*lex8.Error,
) {
	// parsing
	asts, es := l.parsePkg(pinfo)
	if es != nil {
		return nil, es
	}

	// building
	b := makeBuilder(pinfo)
	if es = b.Errs(); es != nil {
		return nil, es
	}

	p := newPkg(asts)
	if es := p.build(b, pinfo); es != nil {
		return nil, es
	}

	g := p.deps
	g, err := g.Rename(func(name string) (string, error) {
		if strings.HasSuffix(name, ".g") {
			return strings.TrimSuffix(name, ".g"), nil
		}
		return name, fmt.Errorf("filename suffix missing: %q", name)
	})
	if err != nil {
		return nil, lex8.SingleErr(err)
	}

	if err := logDeps(pinfo, g); err != nil {
		return nil, lex8.SingleErr(err)
	}

	bs, err := dagvis.LayoutJSON(g.Reverse())
	if err != nil {
		return nil, lex8.SingleErr(err)
	}
	if err := logDepMap(pinfo, bs); err != nil {
		return nil, lex8.SingleErr(err)
	}

	// codegen
	lib, errs := ir.BuildPkg(b.p)
	if errs != nil {
		return nil, errs
	}

	// add debug symbols
	ir.AddDebug(b.p, pinfo.AddFuncDebug)

	// IR logging
	if err := logIr(pinfo, b); err != nil {
		return nil, lex8.SingleErr(err)
	}

	tests := make(map[string]uint32)
	for i, name := range p.testNames {
		tests[name] = uint32(i)
	}

	ret := &build8.Package{
		Lang:     "g8",
		Init:     initName,
		Main:     startName,
		TestMain: testStartName,
		Tests:    tests,
		Lib:      lib,
		Symbols:  p.tops,
	}

	return ret, nil
}