项目作者: macthomasengineering

项目描述 :
B4X Expression Compiler and Eval Library
高级语言: Visual Basic
项目地址: git://github.com/macthomasengineering/MteEVAL-b4x-library.git
创建时间: 2016-08-22T01:10:56Z
项目社区:https://github.com/macthomasengineering/MteEVAL-b4x-library

开源协议:Other

下载


MteEVAL - B4X Expression Compiler and Eval Library

MteEVAL is a library for compiling and evaluating expressions at runtime. Expressions are converted to bytecode and then executed on demand with a simple virtual machine.

There are four editions of the library: Android (B4A), iOS (B4i), Java (B4J), JavaS2 (B4A/B4J).

JavaS2 is our stage 2 performance edition of the library in native Java.

See Anywhere Software to learn more about B4A, B4i, and B4J cross-platform development tools.

Application

Creating expressions at runtime is a powerful tool allowing calculations and program flow to be modified after installation, which otherwise would require a physical update or a custom build of an application. For example, any application designed to manage a sales compensation plan could benefit from runtime expressions, where the end-user may want to customize the plan’s formulas by team members, product mixes and sales goals.

Codeblocks

MteEVAL implements a single class named Codeblock. MteEVAL’s codeblock adopts the syntax from the venerable 1990’s xBase compiler Clipper 5) where the construct began. Codeblocks start with an open brace, followed by an optional parameter list between pipes, then the expression, and end with a closing brace.

  1. {|<parameters>|<expression>}

Examples

You only need to compile a Codeblock once. Once compiled you can evaluate it as many times as needed, all while supplying different arguments.

Example 1: Codeblock without parameters

  1. Dim cb as Codeblock
  2. cb.Initialize
  3. cb.Compile( "{||5 + 3}" )
  4. Result = cb.Eval 'Result=8

Example 2: Codeblock with parameters

  1. Dim cb as Codeblock
  2. cb.Initialize
  3. cb.Compile( "{|length,width|length*width}" )
  4. Area = cb.Eval2( Array( 3, 17 ) ) 'Area=51

When evaluating a Codeblock with parameters, use the Eval2 method.

Example 3: Codeblock compile, eval and repeat

  1. Dim cb as Codeblock
  2. cb.Initialize
  3. cb.Compile( "{|sales,r1,r2| r1*sales + iif( sales > 100000, (sales-100000)*r2, 0 ) }" )
  4. Commission1 = cb.Eval2( Array( 152000, .08, .05 ) ) 'Commission1=14760
  5. Commission2 = cb.Eval2( Array( 186100, .08, .07 ) ) 'Commission2=20915
  6. Commission3 = cb.Eval2( Array( 320000, .08, .05 ) ) 'Commission3=36600

Operator support

The library supports C/Java style operators along side a growing list of B4X native functions.

  • Math operators: +-*/%
  • Relational: > < >= <= != ==
  • Logical: || && !
  • Bitwise: << >> & ^ |
  • Assignment: =
  • Functions: abs(), ceil(), floor(), iif(), if(), min(), max(), sqrt(), power(), round()
  • Trig Functions: acos(), acosd(), asin(), asind(), atan(), atand(), cos(), cosd(), sin(), sind(), tan(), tand()

Linking to your project

  • To use the Android or Java editions, add the .JAR and .XML files to your Additional Libraries folder and check the MteEVAL library in the Libraries Manager of the IDE.
  • For iOS, copy the modules Codeblock.bas, Codegen.bas, PCODE.bas, and Run.bas to your project folder or place them in the Shared Modules folder. Then add the modules to the project through the IDE.