Beispiel #1
0
func main() {
	r := bufio.NewReaderSize(os.Stdin, 4096)

	for {
		fmt.Print("> ")
		line, _, err := r.ReadLine()
		if err == io.EOF {
			return
		} else if err != nil {
			log.Fatal(err)
		}

		expr, err := eval.Parse(string(line))
		if err != nil {
			fmt.Printf("Invalid expression: %s\n", line)
			continue
		}

		vars := map[eval.Var]bool{}
		err = expr.Check(vars)
		if err != nil {
			fmt.Printf("Invalid expression: %s\n", err)
			continue
		}

		env := eval.Env{}
		for v := range vars {
			for {
				fmt.Printf("%s = ", v)
				line, _, err := r.ReadLine()
				if err == io.EOF {
					return
				} else if err != nil {
					log.Fatal(err)
				}

				f, err := strconv.ParseFloat(string(line), 64)
				if err != nil {
					fmt.Printf("Invalid value: %s\n", err)
					continue
				}
				env[v] = f
				break
			}
		}

		fmt.Printf("%f\n", expr.Eval(env))
	}
}
Beispiel #2
0
func main() {
	handler := func(w http.ResponseWriter, r *http.Request) {
		q := r.URL.Query()
		d := &data{"", 0.0}

		if len(q["expr"]) > 0 {
			d.Expr = q["expr"][0]
			expr, err := eval.Parse(q["expr"][0])
			if err == nil {
				d.Result = expr.Eval(eval.Env{})
			}
		}
		calc.Execute(w, d)
	}

	http.HandleFunc("/", handler)
	log.Println("Running server in localhost:8000 ...")
	log.Fatal(http.ListenAndServe("localhost:8000", nil))
}