コード例 #1
0
ファイル: main.go プロジェクト: e8vm/shanhu
// Compile compiles and runs the code and returns the output of running
// in a json string.
func Compile(file, code string) string {
	home := pl.MakeMemHome(pl.Lang(false))
	const pkgName = "toys"
	pkg := home.NewPkg(pkgName)
	pkg.AddFile("toys/"+file, file, code)

	b := builds.NewBuilder(home, home)
	b.RunTests = true
	b.TestCycles = 1000000

	if errs := b.BuildAll(); errs != nil {
		res := &result{Errs: makeResErrors(errs)}
		return jsonStr(res)
	}

	image := home.BinBytes(pkgName)
	if image == nil {
		err := errors.New("missing main() function")
		return jsonStr(newErrorRes(err))
	}

	outBuf := new(bytes.Buffer)
	m := arch.NewMachine(&arch.Config{
		Output: outBuf,
	})
	if err := m.LoadImage(bytes.NewReader(image)); err != nil {
		return jsonStr(newErrorRes(err))
	}

	res := new(result)
	ret, exp := m.Run(1000000)
	res.Output = outBuf.String()
	res.Ncycle = ret
	if exp != nil {
		res.ExcepCore = exp.Core
		res.ExcepCode = exp.Code
		res.ExcepMsg = exp.Error()
	}

	return jsonStr(res)
}
コード例 #2
0
ファイル: snapshot.go プロジェクト: e8vm/shanhu
// Build builds the root package "main"
func (s *Snapshot) Build(
	pkgs []string, opt *builds.Options,
) ([]byte, []*lexing.Error) {
	home := s.MemHome()
	b := builds.NewBuilder(home, home)
	b.Options = opt

	ps := builds.ListPkgs(home, pkgs)
	if len(ps) == 0 {
		return nil, nil
	}

	if errs := b.BuildPkgs(ps); errs != nil {
		return nil, errs
	}

	if len(ps) == 1 && !opt.RunTests {
		thePkg := ps[0]
		return home.BinBytes(thePkg), nil
	}

	return nil, nil
}
コード例 #3
0
ファイル: repo.go プロジェクト: e8vm/shanhu
// Build builds a repo into a repodb build.
// It performs static analysis on the repository.
func Build(src, path string) (*repodb.Build, []error) {
	buildHash, err := git.CommitHash(src)
	if err != nil {
		return nil, []error{err}
	}

	lang := pl.LangGoLike()
	input := builds.NewDirHome(src, lang)
	input.AddLang("asm", asm.Lang())

	output := builds.NewMemHome(lang)
	pkgs := input.Pkgs(".")
	for _, p := range pkgs {
		output.NewPkg(p)
	}
	deps := new(repodb.Deps)
	fileToks := make(map[string][]*lexing.Token)

	b := builds.NewBuilder(input, output)
	b.Verbose = true
	b.InitPC = arch.InitPC
	b.RunTests = false
	b.StaticOnly = true
	b.SaveDeps = func(m *dagvis.Map) {
		deps.Pkgs = dagvis.JSONMap(m)
	}
	b.SaveFileTokens = func(p string, toks []*lexing.Token) {
		fileToks[p] = toks
	}

	es := b.BuildPkgs(pkgs)
	if es != nil {
		return nil, lexErrors(es)
	}

	deps.Files = make(map[string]interface{})
	for _, pkg := range pkgs {
		bs := output.OutputBytes(pkg, "depmap")
		if bs == nil {
			continue
		}
		var m map[string]interface{}
		err := json.Unmarshal(bs, &m)
		if err != nil {
			return nil, []error{err}
		}
		deps.Files[pkg] = m
	}

	files := make(map[string]*repodb.File)
	for _, pkg := range pkgs {
		srcFiles := input.Src(pkg)
		for name, f := range srcFiles {
			p := filepath.Join(pkg, name)
			toks := fileToks[p]
			if toks == nil {
				continue
			}

			res, err := renderFile(f, toks)
			if err != nil {
				return nil, []error{err}
			}

			p = filepath.Join(path, p)
			files[p] = res
		}
	}

	return &repodb.Build{
		Name:  path,
		Build: buildHash,
		Lang:  "g",
		Deps:  deps,
		Files: files,
	}, nil
}