简体   繁体   English

访问者的antlr4切换语句语法

[英]antlr4 switch statement grammar to visitor

As a lot of people around the programming community, I am also trying to build my own programming language for a school project in Java. 作为编程社区中的许多人,我还尝试为Java的学校项目构建自己的编程语言。 I am using antlr4 with intellij, as it helps a lot generating the tree at the same time I am writing test code. 我将intlij与antlr4一起使用,因为在编写测试代码的同时,它有助于生成树。 I created a visitor class to add functionality to my code and so far I was able to make an if/else statement and a while statement. 我创建了一个访问者类,以向代码中添加功能,到目前为止,我能够做出if / else语句和while语句。 So I would also like to create a switch statement and implement it to the visitor class. 因此,我还想创建一个switch语句并将其实现到visitor类。 My grammar for the switch statement is the following: 我对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;

So I assume that I start the method like this: 所以我假设我是这样启动方法的:

@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;
}

I want to write the case statement inside the switch method but I do not know how to go from here, and I didn't find any simple example... Can anyone point to the right direction, or provide with some simple code? 我想在switch方法中编写case语句,但是我不知道如何从这里开始,也没有找到任何简单的示例...任何人都可以指向正确的方向或提供一些简单的代码吗? Thanks in advance. 提前致谢。

EDIT: A simple example of the syntax that I want is the following: 编辑:我想要的语法的一个简单的示例如下:

switch(aNumber){
   case 1: print("return 1");
   case 2: print("return 2");
}

From your comment it sounds like you're writing an interpreter that executes the code directly in the visitor without going through any IR or anything like that (please correct me if I misunderstood). 从您的评论看来,您正在编写一个解释器,该解释器直接在访问者中执行代码,而无需经过任何IR或类似的操作(如果我误解了,请纠正我)。

So your problem is that you can't just write a switch statement inside your code if you don't even know how many case statements there are going to be - you can't put a case inside a loop after all. 因此,您的问题是,即使您甚至不知道会有多少个case语句,也不能只在代码内编写一个switch语句-毕竟不能将一个case放入循环中。 And even if that weren't a problem, the other problem would be that case statements need compile-time constants, but you'd need to use them with values you extracted from your parse tree. 即使这不是问题,另一个问题是case语句需要编译时常量,但是您需要将其与从解析树中提取的值一起使用。

If you were generating code, none of that would be a problem because you could just generate the proper amount of cases with the given values and everything would be fine. 如果您正在生成代码,那么这都不是问题,因为您可以使用给定的值生成适当数量的案例,并且一切都会很好。 But you're not, so everything is dynamic and you can't use switch . 但是您不是,所以一切都是动态的,您不能使用switch

But that's not a problem because no one says that you have to implement switch using switch . 但是,因为没有人说你必须执行,这不是一个问题, switch使用switch As you probably know, a switch is just a more convenient (and often more performant) way of writing a series of if statements. 您可能知道,切换只是编写一系列if语句的一种更方便(而且通常更具性能)的方式。 In your example (assuming your language has implicit break s), that'd be: 在您的示例中(假设您的语言包含隐含的break ),则为:

if (aNumber == 1) {
    print("return 1");
} else if (aNumber == 2) {
    print("return 2");
}

What this means for your interpreter is that you can evaluate the expression you're switching on, then loop over all the cases and for each case compare the evaluated number to the value of the case with an if . 这对您的解释器意味着您可以评估要打开的表达式,然后循环遍历所有个案,并针对每种个案将评估的数字与case值进行比较,并使用if As soon as one of the condition matches, you execute the associated code and break out of the loop. 一旦条件之一匹配,就执行关联的代码并退出循环。

If you don't have implicit break s, you should instead only break out of the loop when you encounter a break and otherwise use a flag to remember if you've already encountered a true condition and then execute every subsequent code block until you encounter a break or reach the end of the switch . 如果没有隐含的break ,则应该只在遇到break时才跳出循环,否则使用标志来记住是否已经遇到了真实条件,然后执行每个后续代码块,直到遇到break或到达switch的末端。

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

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