示例#1
0
文件: repl.go 项目: raff/eval
func evalCmd(line string) {
	ctx := &eval.Ctx{line}
	if expr, err := parser.ParseExpr(line); err != nil {
		if pair := eval.FormatErrorPos(line, err.Error()); len(pair) == 2 {
			fmt.Println(pair[0])
			fmt.Println(pair[1])
		}
		fmt.Printf("parse error: %s\n", err)
	} else if cexpr, errs := eval.CheckExpr(ctx, expr, env); len(errs) != 0 {
		for _, cerr := range errs {
			fmt.Printf("%v\n", cerr)
		}
	} else if vals, _, err := eval.EvalExpr(ctx, cexpr, env); err != nil {
		fmt.Printf("eval error: %s\n", err)
	} else if vals == nil {
		fmt.Printf("Kind=nil\nnil\n")
	} else if len(*vals) == 0 {
		fmt.Printf("Kind=Slice\nvoid\n")
	} else if len(*vals) == 1 {
		value := (*vals)[0]
		if value.IsValid() {
			kind := value.Kind().String()
			typ := value.Type().String()
			if typ != kind {
				fmt.Printf("Kind = %v\n", kind)
				fmt.Printf("Type = %v\n", typ)
			} else {
				fmt.Printf("Kind = Type = %v\n", kind)
			}

			n := len(results)
			results = append(results, (*vals)[0].Interface())
			fmt.Printf("results[%d] = %s\n", n, eval.Inspect(value))
		} else {
			fmt.Printf("%s\n", value)
		}
	} else {
		fmt.Printf("Kind = Multi-Value\n")
		size := len(*vals)
		for i, v := range *vals {
			fmt.Printf("%s", eval.Inspect(v))
			if i < size-1 {
				fmt.Printf(", ")
			}
		}
		fmt.Printf("\n")
		results = append(results, (*vals))
	}
}
示例#2
0
文件: gogo.go 项目: raff/gogo
func evalCmd(line string) {
	ctx := &eval.Ctx{line}
	if expr, err := parser.ParseExpr(line); err != nil {
		//
		// error parsing
		//
		if pair := eval.FormatErrorPos(line, err.Error()); len(pair) == 2 {
			fmt.Println(pair[0])
			fmt.Println(pair[1])
		}
		fmt.Printf("parse error: %s\n", err)
	} else if cexpr, errs := eval.CheckExpr(ctx, expr, env); len(errs) != 0 {
		//
		// error ...
		//
		for _, cerr := range errs {
			fmt.Printf("%v\n", cerr)
		}
	} else if vals, _, err := eval.EvalExpr(ctx, cexpr, env); err != nil {
		//
		// error evaluating expression
		//
		fmt.Printf("eval error: %s\n", err)
	} else if vals == nil {
		fmt.Println(vals)
	} else {
		for i, v := range *vals {
			if i > 0 {
				fmt.Printf(", ")
			}

			fmt.Printf("%s", eval.Inspect(v))
		}

		fmt.Printf("\n")

		if len(*vals) == 1 {
			env.Vars["_"] = (*vals)[0]
		} else {
			env.Vars["_"] = reflect.ValueOf(*vals)
		}
	}
}