source-academy / java-slang

Implementation of the Java language in TypeScript
Apache License 2.0
0 stars 0 forks source link

compiler/JVM: type long not working #55

Closed martin-henz closed 3 months ago

martin-henz commented 3 months ago
class Test { 
    public static void main(String[] args) {       
        long x = 1;
    }
}

gives

Screenshot 2024-04-16 at 1 50 01 PM
Chang-CH commented 3 months ago

image

compiled with javac Main.java -target 8 -source 8.

  Last modified 16 Apr 2024; size 384 bytes
  Compiled from "Main.java"
class Main
  minor version: 0
  major version: 52
  flags: (0x0020) ACC_SUPER
  this_class: #19                         // Main
  super_class: #2                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 2, attributes: 1
Constant pool:
   #1 = Methodref          #2.#3          // java/lang/Object."<init>":()V
   #2 = Class              #4             // java/lang/Object
   #3 = NameAndType        #5:#6          // "<init>":()V
   #4 = Utf8               java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Fieldref           #8.#9          // java/lang/System.out:Ljava/io/PrintStream;
   #8 = Class              #10            // java/lang/System
   #9 = NameAndType        #11:#12        // out:Ljava/io/PrintStream;
  #10 = Utf8               java/lang/System
  #11 = Utf8               out
  #12 = Utf8               Ljava/io/PrintStream;
  #13 = Methodref          #14.#15        // java/io/PrintStream.println:(J)V
  #14 = Class              #16            // java/io/PrintStream
  #15 = NameAndType        #17:#18        // println:(J)V
  #16 = Utf8               java/io/PrintStream
  #17 = Utf8               println
  #18 = Utf8               (J)V
  #19 = Class              #20            // Main
  #20 = Utf8               Main
  #21 = Utf8               Code
  #22 = Utf8               LineNumberTable
  #23 = Utf8               main
  #24 = Utf8               ([Ljava/lang/String;)V
  #25 = Utf8               SourceFile
  #26 = Utf8               Main.java
{
  Main();
    descriptor: ()V
    flags: (0x0000)
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=3, locals=3, args_size=1
         0: lconst_1
         1: lstore_1
         2: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
         5: lload_1
         6: invokevirtual #13                 // Method java/io/PrintStream.println:(J)V
         9: return
      LineNumberTable:
        line 3: 0
        line 4: 2
        line 5: 9
}
SourceFile: "Main.java"

@1001mei is the compiler output the same?

1001mei commented 3 months ago

From the compiler perspective, the literal 1 is treated as of type int and type conversion is currently not supported, thus this behavior. long x = 1L; should work.

martin-henz commented 3 months ago

Got it. Thanks!