Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | typedef struct Expr Expr; |
| 23 | |
| 24 | typedef char* (*Function)(const char* name, void* cookie, |
| 25 | int argc, Expr* argv[]); |
| 26 | |
| 27 | struct Expr { |
| 28 | Function fn; |
| 29 | char* name; |
| 30 | int argc; |
| 31 | Expr** argv; |
| 32 | }; |
| 33 | |
| 34 | char* Evaluate(void* cookie, Expr* expr); |
| 35 | |
| 36 | // Glue to make an Expr out of a literal. |
| 37 | char* 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.) |
| 42 | char* ConcatFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 43 | char* LogicalAndFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 44 | char* LogicalOrFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 45 | char* LogicalNotFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 46 | char* SubstringFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 47 | char* EqualityFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 48 | char* InequalityFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 49 | char* SequenceFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 50 | |
| 51 | // Convenience function for building expressions with a fixed number |
| 52 | // of arguments. |
| 53 | Expr* Build(Function fn, int count, ...); |
| 54 | |
| 55 | // Global builtins, registered by RegisterBuiltins(). |
| 56 | char* IfElseFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 57 | char* AssertFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 58 | char* AbortFn(const char* name, void* cookie, int argc, Expr* argv[]); |
| 59 | |
| 60 | typedef 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. |
| 67 | void RegisterFunction(const char* name, Function fn); |
| 68 | |
| 69 | // Register all the builtins. |
| 70 | void RegisterBuiltins(); |
| 71 | |
| 72 | // Call this after all calls to RegisterFunction() but before parsing |
| 73 | // any scripts to finish building the function table. |
| 74 | void FinishRegistration(); |
| 75 | |
| 76 | // Find the Function for a given name; return NULL if no such function |
| 77 | // exists. |
| 78 | Function FindFunction(const char* name); |
| 79 | |
| 80 | #endif // _EXPRESSION_H |