Interpreting mathematical expressions in runtime is a rather standard task in different fields of program engineering. There are different approaches to this problem from creating a specific language-in-a-language with its own parser and interpreter just for that task, to using a fully-fledged embedded compiler. This article is dedicated to a middle-ground solution, implemented in the KMath library. KMath uses Kotlin object builders (DSL) and its own abstractions for mathematical operations to provide a syntax to generate AST for mathematical operations. Then the AST is compiled using just-in-time JVM bytecode generation and other means. A similar approach is tested on other Kotlin platforms
翻译:运行时解析数学表达式是程序工程不同领域的一项相当标准的任务。 解决这个问题有不同的方法, 从创建一种特定语言(一种语言)及其自己的解译员和译员, 直至使用完全的嵌入式编译器。 文章专门用在 KMath 库中执行的中层解决方案。 Kamath 使用 Kotlin 对象构建器( DSL) 及其自己的数学操作抽象化器来提供用于数学操作生成 AST 的语法。 然后, AST 将使用 JVM 字元代码生成和其他方式进行编译。 在其他 Kotlin 平台上测试类似的方法 。