Exemplo n.º 1
0
// initGameSession if a load file exists load the old game otherwise
// create a new game session.
func initGameSession(a *area.Area) (sav *save.Save, err error) {
	path, err := goutil.SrcDir("github.com/karlek/reason/")
	if err != nil {
		return nil, errutil.Err(err)
	}
	sav, err = save.New(path + "debug.save")
	if err != nil {
		return nil, errutil.Err(err)
	}

	err = initGameLibs()
	if err != nil {
		return nil, errutil.Err(err)
	}

	// If save exists load old game session.
	// Otherwise create a new game session.
	if sav.Exists() {
		err = load(sav, a)
	} else {
		err = newGame(a)
	}
	if err != nil {
		return nil, errutil.Err(err)
	}
	// Initalize turn priority queue.
	turn.Init(a)

	return sav, nil
}
Exemplo n.º 2
0
Arquivo: alpha.go Projeto: karlek/seer
func init() {
	dir, err := goutil.SrcDir("github.com/karlek/seer/cmd/alpha")
	if err != nil {
		log.Fatalln(err)
	}
	filename = path.Join(dir, "alpha.json")
}
Exemplo n.º 3
0
// parseSubs parses the graph representations of the given high-level control
// flow primitives. If unable to locate a subgraph, a second attempt is made by
// prepending $GOPATH/src/decomp.org/decomp/cmd/restructure/primitives/ to the
// subgraph path.
func parseSubs(subPaths []string) (subs []*graphs.SubGraph, err error) {
	// Prepend $GOPATH/src/decomp.org/decomp/cmd/restructure/primitives/ to the path
	// of missing subgraphs.
	subDir, err := goutil.SrcDir("decomp.org/decomp/cmd/restructure/primitives")
	if err != nil {
		return nil, errutil.Err(err)
	}
	for i, subPath := range subPaths {
		if ok, _ := osutil.Exists(subPath); !ok {
			subPath = filepath.Join(subDir, subPath)
			subPaths[i] = subPath
		}
	}

	// Parse subgraphs representing control flow primitives.
	for _, subPath := range subPaths {
		sub, err := graphs.ParseSubGraph(subPath)
		if err != nil {
			return nil, errutil.Err(err)
		}
		subs = append(subs, sub)
	}

	return subs, nil
}
Exemplo n.º 4
0
Arquivo: iso.go Projeto: decomp/decomp
// locate parses the provided graphs and tries to locate isomorphisms of the
// subgraph in the graph.
func locate(graphPath, subPath string) error {
	// Parse graph.
	graph, err := dot.ParseFile(graphPath)
	if err != nil {
		return errutil.Err(err)
	}

	// Search for subgraph in GOPATH if not found.
	if ok, _ := osutil.Exists(subPath); !ok {
		dir, err := goutil.SrcDir("decomp.org/decomp/graphs/testdata/primitives")
		if err != nil {
			return errutil.Err(err)
		}
		subPath = filepath.Join(dir, subPath)
	}
	sub, err := graphs.ParseSubGraph(subPath)
	if err != nil {
		return errutil.Err(err)
	}

	// Locate isomorphisms.
	found := false
	if len(flagStart) > 0 {
		// Locate an isomorphism of sub in graph which starts at the node
		// specified by the "-start" flag.
		m, ok := iso.Isomorphism(graph, flagStart, sub)
		if ok {
			found = true
			printMapping(graph, sub, m)
		}
	} else {
		// Locate all isomorphisms of sub in graph.
		var names []string
		for name := range graph.Nodes.Lookup {
			names = append(names, name)
		}
		sort.Strings(names)
		for _, name := range names {
			m, ok := iso.Isomorphism(graph, name, sub)
			if !ok {
				continue
			}
			found = true
			printMapping(graph, sub, m)
		}
	}
	if !found {
		fmt.Println("not found.")
	}

	return nil
}
Exemplo n.º 5
0
// loadResources loads the images required to render the grid and the markers.
func loadResources() (err error) {
	dataDir, err := goutil.SrcDir("github.com/mewmew/playground/archive/vaga/data")
	if err != nil {
		return err
	}
	imgGrid, err = imgutil.ReadFile(dataDir + "/grid.png")
	if err != nil {
		return err
	}
	imgO, err = imgutil.ReadFile(dataDir + "/o.png")
	if err != nil {
		return err
	}
	imgX, err = imgutil.ReadFile(dataDir + "/x.png")
	if err != nil {
		return err
	}
	return nil
}
Exemplo n.º 6
0
// DirFiles returns all filenames in a folder.
func DirFiles(srcDir string) (filnames []string, err error) {
	folder, err := goutil.SrcDir(srcDir)
	if err != nil {
		return nil, errutil.Err(err)
	}
	f, err := os.Open(folder)
	if err != nil {
		return nil, errutil.Err(err)
	}
	fi, err := f.Readdir(0)
	if err != nil {
		return nil, errutil.Err(err)
	}

	var filenames []string
	for _, v := range fi {
		filename := folder + "/" + v.Name()
		filenames = append(filenames, filename)
	}
	return filenames, nil
}
Exemplo n.º 7
0
// checkFile performs a static semantic analysis check on the given file.
func compileFile(path string, outputPath string, goccLexer bool) error {
	// Lexical analysis
	// Syntactic analysis
	// Semantic analysis
	// Intermediate representation generation

	// Create lexer for the input.
	buf, err := ioutilx.ReadFile(path)
	if err != nil {
		return errutil.Err(err)
	}
	if path == "-" {
		path = "<stdin>"
	}

	fmt.Fprintf(os.Stderr, "Compiling %q\n", path)

	var s parser.Scanner
	if goccLexer {
		s = goccscanner.NewFromBytes(buf)
	} else {
		s = handscanner.NewFromBytes(buf)
	}

	// Parse input.
	p := parser.NewParser()
	f, err := p.Parse(s)
	if err != nil {
		if err, ok := err.(*goccerrors.Error); ok {
			// Unwrap Gocc error.
			return parser.NewError(err)
		}
		return errutil.Err(err)
	}
	file := f.(*ast.File)
	input := string(buf)
	src := semerrors.NewSource(path, input)
	info, err := sem.Check(file)
	if err != nil {
		if err, ok := err.(*errutil.ErrInfo); ok {
			// Unwrap errutil error.
			if err, ok := err.Err.(*semerrors.Error); ok {
				// Unwrap semantic analysis error, and add input source information.
				err.Src = src
				return err
			}
		}
		return errutil.Err(err)
	}

	// Generate LLVM IR module based on the syntax tree of the given file.
	module := irgen.Gen(file, info)

	// Add path to uc lib.
	lib, err := goutil.SrcDir("github.com/mewmew/uc/testdata")
	if err != nil {
		return errutil.Err(err)
	}
	lib = filepath.Join(lib, "uc.ll")

	// Link and create binary through clang
	clang := exec.Command("clang", "-o", outputPath, "-x", "ir", lib, "-")
	clang.Stdin = strings.NewReader(module.String())
	clang.Stderr = os.Stderr
	clang.Stdout = os.Stdout
	if err := clang.Run(); err != nil {
		return errutil.Err(err)
	}
	return nil
}
Exemplo n.º 8
0
// locateAndMerge parses the provided graphs and tries to merge isomorphisms of
// the subgraph in the graph into single nodes.
func locateAndMerge(graphPath, subPath string) error {
	// Parse graph.
	graph, err := dot.ParseFile(graphPath)
	if err != nil {
		return errutil.Err(err)
	}

	// Search for subgraph in GOPATH if not found.
	if ok, _ := osutil.Exists(subPath); !ok {
		dir, err := goutil.SrcDir("decomp.org/decomp/graphs/testdata/primitives")
		if err != nil {
			return errutil.Err(err)
		}
		subPath = filepath.Join(dir, subPath)
	}
	sub, err := graphs.ParseSubGraph(subPath)
	if err != nil {
		return errutil.Err(err)
	}

	// Merge isomorphisms.
	found := false
	if len(flagStart) > 0 {
		// Merge an isomorphism of sub in graph which starts at the node
		// specified by the "-start" flag.
		m, ok := iso.Isomorphism(graph, flagStart, sub)
		if ok {
			found = true
			printMapping(graph, sub, m)
			_, err := merge.Merge(graph, m, sub)
			if err != nil {
				return errutil.Err(err)
			}
		}
	} else {
		// Merge all isomorphisms of sub in graph.
		for {
			m, ok := iso.Search(graph, sub)
			if !ok {
				break
			}
			found = true
			printMapping(graph, sub, m)
			_, err := merge.Merge(graph, m, sub)
			if err != nil {
				return errutil.Err(err)
			}
		}
	}

	// Store DOT and PNG representation of graph.
	if found {
		err = dump(graph)
		if err != nil {
			return errutil.Err(err)
		}
	} else {
		fmt.Println("not found.")
	}

	return nil
}