Ejemplo n.º 1
0
func GoEval2(expr *C.char, result **C.char) int {
	var res string
	var ret int
	src := C.GoString(expr)
	world := eval.NewWorld()
	world.DefineVar("remote_expr", eval.NewFuncType([]eval.Type{eval.StringType, eval.StringType}, false, []eval.Type{eval.StringType}), &funcV{&w_remote_expr{}})
	code, err := world.Compile(fset, src)
	if err != nil {
		res = err.String()
		ret = -1
	} else {
		value, err := code.Run()
		if err != nil {
			res = err.String()
			ret = -1
		} else {
			res = value.String()
			ret = 0
		}
	}
	p := C.CString(res)
	defer C.free(unsafe.Pointer(p))
	*result = (*C.char)(C.vimremote_malloc((C.size_t)(len(res) + 1)))
	C.memmove(unsafe.Pointer(*result), unsafe.Pointer(p), (C.size_t)(len(res)+1))
	return ret
}
Ejemplo n.º 2
0
func main() {
	world := eval.NewWorld()
	code, err := world.Compile(nil, `md5.New()`)
	if err == nil {
		fmt.Printf("code == %v\n", code)
	} else {
		fmt.Printf("err == %v\n", err)
	}

}
Ejemplo n.º 3
0
func main() {
world := eval.NewWorld();
	   code, err := world.Compile(`
			   print("go_hello\n");
			   `); // does not supoort package

	   if err != nil {
		   panic(err.String(), "\n");
	   } else {
		   /* go_hello */
		   if value, err := code.Run(); err != nil {
			   panicln(err.String());
		   } else println(value);
		   // Hello
	   }
}
Ejemplo n.º 4
0
func main() {
	w := eval.NewWorld()
	r := bufio.NewReader(os.Stdin)
	for {
		s, e := r.ReadString('\n')
		if e == os.EOF {
			break
		}
		c, err := w.Compile(s)
		if err == nil {
			v, e := c.Run()
			if e == nil && v != nil { // single expression
				fmt.Println(v)
			}
		} else {
			fmt.Println("compile error")
		}
	}
}
Ejemplo n.º 5
0
func Main() {
	world = eval.NewWorld()
	defineFuncs()
	r := bufio.NewReader(os.Stdin)
	for {
		print("; ")
		line, err := r.ReadSlice('\n')
		if err != nil {
			break
		}

		// Try line as a command
		cmd, rest := getCmd(line)
		if cmd != nil {
			err := cmd.handler(rest)
			if err != nil {
				scanner.PrintError(os.Stderr, err)
			}
			continue
		}

		// Try line as code
		code, err := world.Compile(string(line))
		if err != nil {
			scanner.PrintError(os.Stderr, err)
			continue
		}
		v, err := code.Run()
		if err != nil {
			fmt.Fprintf(os.Stderr, err.String())
			continue
		}
		if v != nil {
			println(v.String())
		}
	}
}
Ejemplo n.º 6
0
func main() {
	flag.Parse()
	w := eval.NewWorld()
	if *filename != "" {
		data, err := ioutil.ReadFile(*filename)
		if err != nil {
			println(err.String())
			os.Exit(1)
		}
		file, err := parser.ParseFile(fset, *filename, data, 0)
		if err != nil {
			println(err.String())
			os.Exit(1)
		}
		code, err := w.CompileDeclList(fset, file.Decls)
		if err != nil {
			if list, ok := err.(scanner.ErrorList); ok {
				for _, e := range list {
					println(e.String())
				}
			} else {
				println(err.String())
			}
			os.Exit(1)
		}
		_, err = code.Run()
		if err != nil {
			println(err.String())
			os.Exit(1)
		}
		code, err = w.Compile(fset, "init()")
		if code != nil {
			_, err := code.Run()
			if err != nil {
				println(err.String())
				os.Exit(1)
			}
		}
		code, err = w.Compile(fset, "main()")
		if err != nil {
			println(err.String())
			os.Exit(1)
		}
		_, err = code.Run()
		if err != nil {
			println(err.String())
			os.Exit(1)
		}
		os.Exit(0)
	}

	r := bufio.NewReader(os.Stdin)
	for {
		print("; ")
		line, err := r.ReadString('\n')
		if err != nil {
			break
		}
		code, err := w.Compile(fset, line)
		if err != nil {
			println(err.String())
			continue
		}
		v, err := code.Run()
		if err != nil {
			println(err.String())
			continue
		}
		if v != nil {
			println(v.String())
		}
	}
}