简体   繁体   English

java中非常简单的脚本解析器

[英]Very simple script parser in java

I'm currently working on a small project in java where I need to process a very lightweight scripting language for a robot that I've made up. 我目前正在研究java中的一个小项目,我需要为我组建的机器人处理一个非常轻量级的脚本语言。

It doesn't need to deal with arithmetic or any complicated functionality, it only needs to run certain commands and deal with if and while statements. 它不需要处理算术或任何复杂的功能,它只需要运行某些命令并处理if和while语句。

It could look something like this: 它可能看起来像这样:

turn(90,cw)
move(25)
sleep(5000)
get(temp)
turn(90,ccw)
get(image)

This is something that I've had no problem with and was able to get it working very quickly. 这是我没有遇到过的问题,并且能够让它快速运行。 However once I started considering if and while statements, I realised that things might be a lot more complex. 然而,一旦我开始考虑if和while语句,我意识到事情可能会复杂得多。

For instance, It would be quite simple to manage something like this: 例如,管理这样的事情会非常简单:

while(temp > 50)
    move(25)
    turn(90,cw)

But I'm getting very confused as to how I can start to process statements like this: 但是我对如何开始处理这样的语句感到非常困惑:

while(temp > 50)
    if(light < 100)
        move(25)
    turn(90,cw)
    move(10)

Looking at it, I can see things getting very confusing very quickly! 看着它,我可以很快看到事情变得非常混乱!

Is there a preferred method for processing "lightweight scripts" like these? 有没有像这样处理“轻量级脚本”的首选方法? Or would I be better off just sticking with it and working out some kind of manual parser? 或者我会更好地坚持下去并制定某种手动解析器?

Thanks for any help you can give me! 感谢你给与我的帮助!

EDIT - Thanks for the help everyone, I got it working really quickly using the Java Scripting Engine! 编辑 - 感谢大家的帮助,我使用Java Scripting Engine快速工作了!

I would consider using BeanShell and not parsing at all. 我会考虑使用BeanShell而不是解析。 You just need to write your statements as beanshell commands. 您只需要将您的语句编写为beanshell命令。

Writing custom language is hard, seriously. 编写自定义语言很难,认真。

Since 6.0 Java has a built-in JavaScript engine. 从6.0开始,Java就有了内置的JavaScript引擎。 Use it instead, and your users will be very happy that you use syntax they are familiar with. 请改用它,您的用户会非常高兴您使用他们熟悉的语法。 Other alternatives are Groovy, JRuby, or even Lua. 其他替代品是Groovy,JRuby,甚至是Lua。

See also 也可以看看

Groovy has good support for writing domain-specific languages like this, if you're prepared to use Java-like braces for nesting rather than Python-like indentation levels. 如果您准备使用类似Java的括号进行嵌套而不是类似Python的缩进级别,那么Groovy对编写像这样的特定于域的语言有很好的支持。

while(temp > 50) {
  if(light < 100) {
    move(25)
  }
  turn(90,cw)
  move(10)
}

This could be implemented something like 这可以实现类似的东西

GroovyShell gs = new GroovyShell()
Script script = gs.parse(stringContainingTheScript)
Binding b = script.binding
// utility values
b.cw = true
b.ccw = false

b.light = theRobot.getLight()
b.temp = theRobot.getTemperature()

def mc = script.metaClass

mc.move = { distance ->
  theRobot.move(distance)
  // update from sensors
  b.light = theRobot.getLight()
  b.temp = theRobot.getTemperature()
}

mc.turn = { angle, direction ->
  if(direction) theRobot.turnClockwise(angle)
  else theRobot.turnCounterclockwise(angle)
}
mc.sleep = { time ->
  sleep(time) // Groovy builtin
  // update from sensors
  b.light = theRobot.getLight()
  b.temp = theRobot.getTemperature()
}

script.run()

I have used Jython (Python ported on JVM) for a similar task. 我使用Jython(在JVM上移植的Python)来完成类似的任务。 The key benefits include strong interoperability between the "host" (Java environment) and the Jython interpreter (invoked from Java). 主要优点包括“主机”(Java环境)和Jython解释器(从Java调用)之间的强大互操作性。 You can access your JVM universe from the script: classes, methods, fields, defined in the Java env. 您可以从脚本中访问JVM Universe:Java环境中定义的类,方法,字段。 are all available to manipulate from your scripts. 都可以从脚本中操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM