scope.mj

来自「编译器mips版后端」· MJ 代码 · 共 53 行

MJ
53
字号
/**
 * This program is used to test whether your compiler can correctly
 * deal with variable scopes.
 * The following program is correct and will display nothing.
 */
 
class Program {
    static int i1 = 2;
    static int i2 = 4;
    
    static void main() {
        if (i1!=2) print("error1\n");  // refer to the global variable i1
        if (i2!=4) print("error2\n");  // refer to the global variable i2
        
        {
            int i2 = 8;
            if (i2!=8) print("error3\n");  // refer to the local variable
                                        // i2 in this block
        }
        
        if (i2!=4) print("error4\n");  // refer to the global variable i2
        
        f();
        i1();//I add
        return;
    }
    
    static void f() {
        {
            int i3 = 16;
            if (i3!=16)  print("error5\n"); // refer to the local variable
                                         // i3 in this block
        }
        {
            int i3 = 32;
            if (i3!=32)  print("error6\n"); // refer to the local variable
                                         // i3 in this block
        }
        
        int i3 = 64;
        if (i3!=64)  print("error7\n");  // refer to the local variable
                                      // i3 in this block
    }
    
    static void i1() {  // The method can have the same name with a
                        // global variable.
        if (i1!=2)  print("error8\n");  // refer to the global variable i1
        
        int i1 = 128;  // The local variable can hava the same name
                       // with the method.
        if (i1==128) print("error9\n");
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?