Example #1
0
func main() {
	flag.Parse()

	// for now, assumes this is only being invoked with a filename to interpret
	file, err := os.Open(flag.Args()[0])
	if err != nil {
		panic(err)
	}

	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		panic(err)
	}

	home := os.Getenv("HOME")
	grubbyHome := filepath.Join(home, ".grubby")

	rubyVM := vm.NewVM(grubbyHome, flag.Args()[0])
	defer rubyVM.Exit()

	_, err = rubyVM.Run(string(bytes))

	switch err.(type) {
	case *vm.ParseError:
		offendingFilename := err.(*vm.ParseError).Filename
		println(fmt.Sprintf("Error parsing ruby script %s", offendingFilename))
		println("last 20 statements from the parser:")
		println("")

		debugStatements := []string{}
		for _, d := range parser.DebugStatements {
			debugStatements = append(debugStatements, d)
		}

		threshold := 61
		debugCount := len(debugStatements)
		if debugCount <= threshold {
			for _, stmt := range debugStatements {
				fmt.Printf("\t%s\n", stmt)
			}
		} else {
			for _, stmt := range debugStatements[debugCount-threshold:] {
				fmt.Printf("\t%s\n", stmt)
			}
		}

		os.Exit(1)
	case nil:
	case error:
		panic(err.Error())
	default:
		panic(fmt.Sprintf("%#v", err))
	}
}
Example #2
0
func main() {
	home := os.Getenv("HOME")
	grubbyHome := filepath.Join(home, ".grubby")

	vm := vm.NewVM(grubbyHome, "(grubby irb")
	defer vm.Exit()

	for {
		result, err := vm.Run(readInput())
		if err != nil {
			fmt.Printf(" => %s", err.Error())
			continue
		}

		if result != nil {
			fmt.Printf("=> %s", result.String())
		} else {
			fmt.Printf("=> %#v", result)
		}
		println("")
	}
}