NCDEvaluator.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @file NCDEvaluator.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_NCDEVALUATOR_H
  30. #define BADVPN_NCDEVALUATOR_H
  31. #include <stddef.h>
  32. #include <misc/debug.h>
  33. #include <structure/Vector.h>
  34. #include <ncd/NCDAst.h>
  35. #include <ncd/NCDStringIndex.h>
  36. #include <ncd/NCDVal.h>
  37. struct NCDEvaluator__Expr {
  38. NCDValMem mem;
  39. NCDValSafeRef ref;
  40. NCDValReplaceProg prog;
  41. };
  42. struct NCDEvaluator__Var {
  43. NCD_string_id_t *varnames;
  44. size_t num_names;
  45. };
  46. #include "NCDEvaluator_var_vec.h"
  47. #include <structure/Vector_decl.h>
  48. struct NCDEvaluator__Call {
  49. NCD_string_id_t func_name_id;
  50. struct NCDEvaluator__Expr *args;
  51. size_t num_args;
  52. };
  53. #include "NCDEvaluator_call_vec.h"
  54. #include <structure/Vector_decl.h>
  55. struct NCDEvaluator__eval_context;
  56. typedef struct {
  57. NCDStringIndex *string_index;
  58. NCDEvaluator__VarVec vars;
  59. NCDEvaluator__CallVec calls;
  60. } NCDEvaluator;
  61. typedef struct {
  62. struct NCDEvaluator__Expr expr;
  63. } NCDEvaluatorExpr;
  64. typedef struct {
  65. struct NCDEvaluator__eval_context const *context;
  66. int call_index;
  67. } NCDEvaluatorArgs;
  68. typedef struct {
  69. void *user;
  70. int (*func_eval_var) (void *user, NCD_string_id_t const *varnames, size_t num_names, NCDValMem *mem, NCDValRef *out);
  71. int (*func_eval_call) (void *user, NCD_string_id_t func_name_id, NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out);
  72. } NCDEvaluator_EvalFuncs;
  73. int NCDEvaluator_Init (NCDEvaluator *o, NCDStringIndex *string_index) WARN_UNUSED;
  74. void NCDEvaluator_Free (NCDEvaluator *o);
  75. int NCDEvaluatorExpr_Init (NCDEvaluatorExpr *o, NCDEvaluator *eval, NCDValue *value) WARN_UNUSED;
  76. void NCDEvaluatorExpr_Free (NCDEvaluatorExpr *o);
  77. int NCDEvaluatorExpr_Eval (NCDEvaluatorExpr *o, NCDEvaluator *eval, NCDEvaluator_EvalFuncs const *funcs, NCDValMem *out_newmem, NCDValRef *out_val) WARN_UNUSED;
  78. size_t NCDEvaluatorArgs_Count (NCDEvaluatorArgs *o);
  79. int NCDEvaluatorArgs_EvalArg (NCDEvaluatorArgs *o, size_t index, NCDValMem *mem, NCDValRef *out_ref) WARN_UNUSED;
  80. int NCDEvaluatorArgs_EvalArgNewMem (NCDEvaluatorArgs *o, size_t index, NCDValMem *out_newmem, NCDValRef *out_ref) WARN_UNUSED;
  81. #endif