aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/apps/eval_expr/eval_expr.cpp
blob: 12c94c6e68ecffdbf24561214c8459f3a42df4f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/eval/eval/function.h>
#include <vespa/eval/eval/interpreted_function.h>
#include <vespa/eval/eval/tensor_spec.h>

using namespace vespalib::eval;

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "usage: %s <expr>\n", argv[0]);
        fprintf(stderr, "  the expression must be self-contained (no arguments)\n");
        fprintf(stderr, "  quote the expression to make it a single parameter\n");
        return 1;
    }
    auto function = Function::parse({}, argv[1]);
    if (function->has_error()) {
        fprintf(stderr, "expression error: %s\n", function->get_error().c_str());
        return 1;
    }
    auto result = TensorSpec::from_expr(argv[1]);
    auto type = ValueType::from_spec(result.type());
    if (type.is_error()) {
        fprintf(stdout, "error\n");
    } else if (type.is_double()) {
        fprintf(stdout, "%.32g\n", result.as_double());
    } else {
        fprintf(stdout, "%s\n", result.to_string().c_str());
    }
    return 0;
}