Reading data from standard input (stdin)

王林
Release: 2024-08-07 08:30:52
Original
444 people have browsed it

Lendo dados da entrada padrão (stdin)

For those who are not following POJ (Pascal on the JVM) it is a compiler that transforms a subset from Pascal to JASM (Java Assembly) so that we can use the JVM as an execution environment.

In the last post, contexts (from parser) and nested sentences were discussed. In this publication we will talk about the changes necessary to make it possible to read data from standard input (stdin), using the read/readln function from Pascal.

As we are compiling for the JVM, it is necessary to detail the functioning of various points of this incredible virtual machine. Therefore, at various times I detail the internal functioning of the JVM as well as some of its instructions (opcodes).

Reading data from stdin (standard input)

Standard input (stdin) is the stream from which a program reads its input data. Until now we only supported stdout (standard output).

In this commit a Java program was implemented to understand how the JVM deals with stdin:

public class InputData {
    public static String name;
    public static int age;

    public static void main(String[] args) {
        name = System.console().readLine();
        age = Integer.parseInt(System.console().readLine());
        System.out.println("You entered string " + name);
    }
Copy after login

When we disassemble the file class we get the assembly below. Irrelevant snippets were omitted, and the original snippet (in Java) that gave rise to assembly was inserted with ";;":

 1: public class InputData {
 2:     ;; public static String name;
 3:     public static name java/lang/String
 4: 
 5:     ;; public static int age;
 6:     public static age I
 7:
 8:     public static main([java/lang/String)V {
 9:         ;; name = System.console().readLine();
10:         invokestatic java/lang/System.console()java/io/Console
11:         invokevirtual java/io/Console.readLine()java/lang/String
12:         putstatic InputData.name java/lang/String
13:
14:         ;; age = Integer.parseInt(System.console().readLine());
15:         invokestatic java/lang/System.console()java/io/Console
16:         invokevirtual java/io/Console.readLine()java/lang/String
17:         invokestatic java/lang/Integer.parseInt(java/lang/String)I
18:         putstatic InputData.age I
19:
20:         ;; System.out.println("You entered string " + name);
21:         getstatic java/lang/System.out java/io/PrintStream
22:         getstatic InputData.name java/lang/String
23:         invokedynamic makeConcatWithConstants(java/lang/String)java/lang/String {
                invokestatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants(java/lang/invoke/MethodHandles$Lookup, java/lang/String, java/lang/invoke/MethodType, java/lang/String, [java/lang/Object)java/lang/invoke/CallSite
                ["You entered string "]
            }
24:
25:         invokevirtual java/io/PrintStream.println(java/lang/String)V
26:
27:         return
    }
}
Copy after login

With this example it was possible to identify that to read data from stdin it was necessary to use the System.console().readLine() instruction (lines 11 and 16). And since readLine() returns a string, to read numbers it was necessary to convert using the function Integer.parseInt (line 17).

That said, from the Pascal program below:

program NameAndAge;
var
    myname: string;
    myage: integer;
begin
    write('What is your name? '); readln(myname);
    write('How old are you? '); readln(myage);
    writeln;
    writeln('Hello ', myname);
    writeln('You are ', myage, ' years old');
end.
Copy after login

POJ has been adjusted to generate the following JASM:

// Code generated by POJ 0.1
public class name_and_age {
    ;; var myname: string;
    public static myname java/lang/String

    ;; var myage: integer;
    public static myage I

    ;; procedure main
    public static main([java/lang/String)V {
        ;; write('What is your name? ');
        getstatic java/lang/System.out java/io/PrintStream
        ldc "What is your name? "
        invokevirtual java/io/PrintStream.print(java/lang/String)V

        ;; readln(myname);
        invokestatic java/lang/System.console()java/io/Console
        invokevirtual java/io/Console.readLine()java/lang/String
        putstatic name_and_age.myname java/lang/String

        ;; write('How old are you? ');
        getstatic java/lang/System.out java/io/PrintStream
        ldc "How old are you? "
        invokevirtual java/io/PrintStream.print(java/lang/String)V

        ;; readln(myage);
        invokestatic java/lang/System.console()java/io/Console
        invokevirtual java/io/Console.readLine()java/lang/String
        invokestatic java/lang/Integer.parseInt(java/lang/String)I
        putstatic name_and_age.myage I

        ;; writeln;
        getstatic java/lang/System.out java/io/PrintStream
        invokevirtual java/io/PrintStream.println()V

        ;; writeln('Hello ', myname);
        getstatic java/lang/System.out java/io/PrintStream
        ldc "Hello "
        invokevirtual java/io/PrintStream.print(java/lang/String)V
        getstatic java/lang/System.out java/io/PrintStream
        getstatic name_and_age.myname java/lang/String
        invokevirtual java/io/PrintStream.print(java/lang/String)V
        getstatic java/lang/System.out java/io/PrintStream
        invokevirtual java/io/PrintStream.println()V

        ;; writeln('You are ', myage, ' years old');
        getstatic java/lang/System.out java/io/PrintStream
        ldc "You are "
        invokevirtual java/io/PrintStream.print(java/lang/String)V
        getstatic java/lang/System.out java/io/PrintStream
        getstatic name_and_age.myage I
        invokevirtual java/io/PrintStream.print(I)V
        getstatic java/lang/System.out java/io/PrintStream
        ldc " years old"
        invokevirtual java/io/PrintStream.print(java/lang/String)V
        getstatic java/lang/System.out java/io/PrintStream
        invokevirtual java/io/PrintStream.println()V

        return
    }
}
Copy after login

This commit implements the necessary changes to the POJ parser.

Here is the full PR.

Next steps

In the next publication we will complete one of the objectives of this project: calculating the factorial recursively.

Complete project code

The repository with the project's complete code and documentation is here.

The above is the detailed content of Reading data from standard input (stdin). For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!