【发布时间】:2018-05-23 15:58:28
【问题描述】:
作为编程社区的许多人,我也在尝试为 Java 的学校项目构建自己的编程语言。我将 antlr4 与 intellij 一起使用,因为它有助于在我编写测试代码的同时生成树。我创建了一个访问者类来为我的代码添加功能,到目前为止,我能够创建一个 if/else 语句和一个 while 语句。所以我还想创建一个 switch 语句并将其实现到访问者类。我的 switch 语句语法如下:
switch_rule: SWITCH LPAREN any_var RPAREN LBRACKET case_rule* RBRACKET;
case_rule: CASE any_var PRES statement;
statement:
expression
| rule_ifset
| rule_for
| method_call
| rule_whiledo
| rule_dowhile
| switch_rule
| assign
|var_declaration;
any_var :INT # NumericConst
| DOUBLE # NumericConst
| IDENTIFIER # NumericVariable
|boolean_var # BooleanConst;
所以我假设我开始这样的方法:
@Override
public InputValue visitSwitch_rule(AdamantParser.Switch_ruleContext ctx) {
InputValue value = this.visit(ctx.any_var().getChild(0));
if(!value.isInteger()){throw new RuntimeException("switch value is not an integer");}
else{
//code to write here
}
return InputValue.VOIDval;
}
我想在switch方法中编写case语句,但我不知道如何从这里开始,我没有找到任何简单的例子......任何人都可以指出正确的方向,或者提供一些简单的代码?提前致谢。
编辑:我想要的语法的一个简单示例如下:
switch(aNumber){
case 1: print("return 1");
case 2: print("return 2");
}
【问题讨论】:
-
我不确定问题是什么。你的语法有问题吗?还是您在问
//code to write here部分应该包含什么?后者在很大程度上取决于您的访问者应该做什么。 -
我的语法和解析树工作正常!我不知道在 //code to write here 部分写什么,这样它会查看 switch 和 case 语句的内部,并根据我将给出的值执行代码。
-
您的源语言是否有隐含的
breaks?也就是说,如果aNumber == 1,您的示例代码应该打印“return 1”还是"return 1return 2"(在Java中,由于缺少breaks,它会打印后者)?
标签: java intellij-idea antlr4 intellij-plugin