blob: ac5df1869753b4f15281b67521ef253c45d7b5d7 [file] [log] [blame]
Doug Zongker37bee622009-06-08 17:35:39 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _EXPRESSION_H
18#define _EXPRESSION_H
19
20#define MAX_STRING_LEN 1024
21
22typedef struct Expr Expr;
23
24typedef char* (*Function)(const char* name, void* cookie,
25 int argc, Expr* argv[]);
26
27struct Expr {
28 Function fn;
29 char* name;
30 int argc;
31 Expr** argv;
32};
33
34char* Evaluate(void* cookie, Expr* expr);
35
36// Glue to make an Expr out of a literal.
37char* Literal(const char* name, void* cookie, int argc, Expr* argv[]);
38
39// Functions corresponding to various syntactic sugar operators.
40// ("concat" is also available as a builtin function, to concatenate
41// more than two strings.)
42char* ConcatFn(const char* name, void* cookie, int argc, Expr* argv[]);
43char* LogicalAndFn(const char* name, void* cookie, int argc, Expr* argv[]);
44char* LogicalOrFn(const char* name, void* cookie, int argc, Expr* argv[]);
45char* LogicalNotFn(const char* name, void* cookie, int argc, Expr* argv[]);
46char* SubstringFn(const char* name, void* cookie, int argc, Expr* argv[]);
47char* EqualityFn(const char* name, void* cookie, int argc, Expr* argv[]);
48char* InequalityFn(const char* name, void* cookie, int argc, Expr* argv[]);
49char* SequenceFn(const char* name, void* cookie, int argc, Expr* argv[]);
50
51// Convenience function for building expressions with a fixed number
52// of arguments.
53Expr* Build(Function fn, int count, ...);
54
55// Global builtins, registered by RegisterBuiltins().
56char* IfElseFn(const char* name, void* cookie, int argc, Expr* argv[]);
57char* AssertFn(const char* name, void* cookie, int argc, Expr* argv[]);
58char* AbortFn(const char* name, void* cookie, int argc, Expr* argv[]);
59
60typedef struct {
61 const char* name;
62 Function fn;
63} NamedFunction;
64
65// Register a new function. The same Function may be registered under
66// multiple names, but a given name should only be used once.
67void RegisterFunction(const char* name, Function fn);
68
69// Register all the builtins.
70void RegisterBuiltins();
71
72// Call this after all calls to RegisterFunction() but before parsing
73// any scripts to finish building the function table.
74void FinishRegistration();
75
76// Find the Function for a given name; return NULL if no such function
77// exists.
78Function FindFunction(const char* name);
79
80#endif // _EXPRESSION_H