exercise1 evaluates a simple literal expression: "Hello, World!" Compile, eval, profit!
()
| 54 | // |
| 55 | // Compile, eval, profit! |
| 56 | func exercise1() { |
| 57 | fmt.Println("=== Exercise 1: Hello World ===\n") |
| 58 | // Create the standard environment. |
| 59 | env, err := cel.NewEnv() |
| 60 | if err != nil { |
| 61 | glog.Exitf("env error: %v", err) |
| 62 | } |
| 63 | // Check that the expression compiles and returns a String. |
| 64 | ast, iss := env.Parse(`"Hello, World!"`) |
| 65 | // Report syntactic errors, if present. |
| 66 | if iss.Err() != nil { |
| 67 | glog.Exit(iss.Err()) |
| 68 | } |
| 69 | // Type-check the expression for correctness. |
| 70 | checked, iss := env.Check(ast) |
| 71 | // Report semantic errors, if present. |
| 72 | if iss.Err() != nil { |
| 73 | glog.Exit(iss.Err()) |
| 74 | } |
| 75 | // Check the output type is a string. |
| 76 | if checked.OutputType() != cel.StringType { |
| 77 | glog.Exitf( |
| 78 | "Got %v, wanted %v result type", |
| 79 | checked.OutputType(), cel.StringType) |
| 80 | } |
| 81 | // Plan the program. |
| 82 | program, err := env.Program(checked) |
| 83 | if err != nil { |
| 84 | glog.Exitf("program error: %v", err) |
| 85 | } |
| 86 | // Evaluate the program without any additional arguments. |
| 87 | eval(program, cel.NoVars()) |
| 88 | fmt.Println() |
| 89 | } |
| 90 | |
| 91 | // exercise2 shows how to declare and use variables in expressions. |
| 92 | // |