expression_language.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /// This file implements a small language for computing arithmetic expressions,
  2. /// such as (a + b)*3 - sin(y). It allows you freedom in specifying variable
  3. /// dependencies, while avoiding the complexities of a full blown scripting
  4. /// language.
  5. ///
  6. /// The language allows for constants (which are converted to numbers at compile
  7. /// time) and variables (which are evaluated as numbers during run time).
  8. /// The language has a fixed small set of operators and functions (more are easy
  9. /// to add). All constants, variables and stack values used by the language are
  10. /// 32 bit floats.
  11. ///
  12. /// During the compilation phase, the source string is compiled to byte code
  13. /// which is run during the run phase. The virtual machine is a simple stack-based
  14. /// machine with the stack explicitly supplied by the caller.
  15. ///
  16. /// All instructions in the byte code are 32-bit wide. The instruction is either
  17. /// a regular float in which case the operation is BC_PUSH_FLOAT -- the float is
  18. /// pushed to the stack, or it is a NaN. If the float is a NaN, the mantissa is
  19. /// used to encode the type of operation and additional data. The first three bits
  20. /// encode the operation and the remaining 20 bits encode the operation data.
  21. ///
  22. /// NAN_MARKER (9) BC_PUSH_VAR (3) id (20) Pushes the variable with the specified id.
  23. /// NAN_MARKER (9) BC_FUNCTION (3) id (20) Computes the function with the specified id.
  24. /// NAN_MARKER (9) BC_END (3) zero (20) Marks the end of the byte code.
  25. /// float (32) Pushes the float.
  26. #include "config.h"
  27. namespace crown
  28. {
  29. namespace expression_language
  30. {
  31. /// Represents the working stack.
  32. struct Stack
  33. {
  34. float *data;
  35. unsigned size;
  36. unsigned capacity;
  37. Stack(float *data, unsigned capacity)
  38. : data(data)
  39. , size(0)
  40. , capacity(capacity)
  41. {
  42. }
  43. };
  44. /// Runs the @a byte_code using the @a stack as execution stack.
  45. /// @a variables is a list of variable values to use for the execution.
  46. /// They should match the list of variable names supplied to the compile function.
  47. bool run(const unsigned *byte_code, const float *variables, Stack &stack);
  48. } // namespace expression_language
  49. #if CROWN_CAN_COMPILE
  50. namespace expression_language
  51. {
  52. /// Compiles the @a source and stores the result in the @a byte_code.
  53. /// @a variables is a list of variable names. The position of the variable in
  54. /// the list should match the position when @a variables is sent to the run
  55. /// function.
  56. ///
  57. /// @a constants and @a constant_values specifies a list of runtime constants
  58. /// and corresponding values. Constants are expanded to numbers at compile
  59. /// time.
  60. ///
  61. /// Returns the number of compiled unsigned words. If the returned number is
  62. /// greater than @a byte_code_capacity, only the first @a byte_code_capacity
  63. /// words of the byte code are written to @a byte_code.
  64. unsigned compile(const char *source
  65. , unsigned num_variables
  66. , const char **variables
  67. , unsigned num_constants
  68. , const char **constants
  69. , const float *constant_values
  70. , unsigned *byte_code
  71. , unsigned byte_code_capacity
  72. );
  73. } // namespace expression_language
  74. #endif // if CROWN_CAN_COMPILE
  75. } // namespace crown