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 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 20 | #include "yydefs.h" |
| 21 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 22 | #define MAX_STRING_LEN 1024 |
| 23 | |
| 24 | typedef struct Expr Expr; |
| 25 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 26 | typedef struct { |
| 27 | // Optional pointer to app-specific data; the core of edify never |
| 28 | // uses this value. |
| 29 | void* cookie; |
| 30 | |
| 31 | // The source of the original script. Must be NULL-terminated, |
| 32 | // and in writable memory (Evaluate may make temporary changes to |
| 33 | // it but will restore it when done). |
| 34 | char* script; |
| 35 | |
| 36 | // The error message (if any) returned if the evaluation aborts. |
| 37 | // Should be NULL initially, will be either NULL or a malloc'd |
| 38 | // pointer after Evaluate() returns. |
| 39 | char* errmsg; |
| 40 | } State; |
| 41 | |
| 42 | typedef char* (*Function)(const char* name, State* state, |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 43 | int argc, Expr* argv[]); |
| 44 | |
| 45 | struct Expr { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 46 | Function fn; |
| 47 | char* name; |
| 48 | int argc; |
| 49 | Expr** argv; |
| 50 | int start, end; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 53 | char* Evaluate(State* state, Expr* expr); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 54 | |
| 55 | // Glue to make an Expr out of a literal. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 56 | char* Literal(const char* name, State* state, int argc, Expr* argv[]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 57 | |
| 58 | // Functions corresponding to various syntactic sugar operators. |
| 59 | // ("concat" is also available as a builtin function, to concatenate |
| 60 | // more than two strings.) |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 61 | char* ConcatFn(const char* name, State* state, int argc, Expr* argv[]); |
| 62 | char* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]); |
| 63 | char* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]); |
| 64 | char* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]); |
| 65 | char* SubstringFn(const char* name, State* state, int argc, Expr* argv[]); |
| 66 | char* EqualityFn(const char* name, State* state, int argc, Expr* argv[]); |
| 67 | char* InequalityFn(const char* name, State* state, int argc, Expr* argv[]); |
| 68 | char* SequenceFn(const char* name, State* state, int argc, Expr* argv[]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 69 | |
| 70 | // Convenience function for building expressions with a fixed number |
| 71 | // of arguments. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 72 | Expr* Build(Function fn, YYLTYPE loc, int count, ...); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 73 | |
| 74 | // Global builtins, registered by RegisterBuiltins(). |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 75 | char* IfElseFn(const char* name, State* state, int argc, Expr* argv[]); |
| 76 | char* AssertFn(const char* name, State* state, int argc, Expr* argv[]); |
| 77 | char* AbortFn(const char* name, State* state, int argc, Expr* argv[]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 78 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 79 | |
| 80 | // For setting and getting the global error string (when returning |
| 81 | // NULL from a function). |
| 82 | void SetError(const char* message); // makes a copy |
| 83 | const char* GetError(); // retains ownership |
| 84 | void ClearError(); |
| 85 | |
| 86 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 87 | typedef struct { |
| 88 | const char* name; |
| 89 | Function fn; |
| 90 | } NamedFunction; |
| 91 | |
| 92 | // Register a new function. The same Function may be registered under |
| 93 | // multiple names, but a given name should only be used once. |
| 94 | void RegisterFunction(const char* name, Function fn); |
| 95 | |
| 96 | // Register all the builtins. |
| 97 | void RegisterBuiltins(); |
| 98 | |
| 99 | // Call this after all calls to RegisterFunction() but before parsing |
| 100 | // any scripts to finish building the function table. |
| 101 | void FinishRegistration(); |
| 102 | |
| 103 | // Find the Function for a given name; return NULL if no such function |
| 104 | // exists. |
| 105 | Function FindFunction(const char* name); |
| 106 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 107 | |
| 108 | // --- convenience functions for use in functions --- |
| 109 | |
| 110 | // Evaluate the expressions in argv, giving 'count' char* (the ... is |
| 111 | // zero or more char** to put them in). If any expression evaluates |
| 112 | // to NULL, free the rest and return -1. Return 0 on success. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 113 | int ReadArgs(State* state, Expr* argv[], int count, ...); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 114 | |
| 115 | // Evaluate the expressions in argv, returning an array of char* |
| 116 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 117 | // The caller is responsible for freeing the returned array and the |
| 118 | // strings it contains. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 119 | char** ReadVarArgs(State* state, int argc, Expr* argv[]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 120 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 121 | // Use printf-style arguments to compose an error message to put into |
| 122 | // *state. Returns NULL. |
| 123 | char* ErrorAbort(State* state, char* format, ...); |
| 124 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 125 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 126 | #endif // _EXPRESSION_H |