V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
bootvue
V2EX  ›  Kotlin

kotlin 表达式引擎

  •  
  •   bootvue · 2022-07-27 12:26:02 +08:00 · 2434 次点击
    这是一个创建于 611 天前的主题,其中的信息可能已经有所发展或是发生改变。
    字符串表达式 "s1 + ( s2 - s3 )" 集合 s1 s2 s3

    kotlin 中怎么让预定义的字符串作为表达式执行 实现类似 js 中 eval ( "表达式" )的效果 执行完返回集合类型的结果
    4 条回复    2022-07-27 16:33:24 +08:00
    U2FSDGVK
        1
    U2FSDGVK  
       2022-07-27 15:20:31 +08:00
    这个问题我也查过一些,最后找到的是用 kotlin-script 运行字符串
    参考:
    https://stackoverflow.com/questions/56384889/run-kotlin-stored-as-a-string-is-giving-a-runtime-error
    https://stackoverflow.com/questions/49416321/kotlin-dynamically-compile-a-class-from-source-code-at-runtime
    实际用起来 kotlin-script 编译挺慢的
    Belmode
        3
    Belmode  
       2022-07-27 15:59:58 +08:00
    让字符串表达式生成临时 kts 文件,然后再用 kotlin 调用外部命令执行这个 kts ,读取返回结果。
    yazinnnn
        4
    yazinnnn  
       2022-07-27 16:33:24 +08:00
    https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/jsr223/jsr223.md

    resource/META-INF/services/javax.script.ScriptEngineFactory 中写入
    kotlin.script.experimental.jsr223.KotlinJsr223DefaultScriptEngineFactory

    https://gist.github.com/yazinnnn/9c7de3320c63f3f5bd5be2df6cc05665

    不知道你出于啥目的有这个需求, 如果只是学习的话, 不如直接启动一个 repl
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2842 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:20 · PVG 21:20 · LAX 06:20 · JFK 09:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.