Download Compiling Compiling a class Compiling a program Compiled Kotlin

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Comment (computer programming) wikipedia , lookup

Java (programming language) wikipedia , lookup

Scala (programming language) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

History of compiler construction wikipedia , lookup

Compiler wikipedia , lookup

Program optimization wikipedia , lookup

Library (computing) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Class (computer programming) wikipedia , lookup

Java performance wikipedia , lookup

Name mangling wikipedia , lookup

Cross compiler wikipedia , lookup

C Sharp syntax wikipedia , lookup

C++ wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Transcript
CS109
Compiling
We write programs in a high-level programming language like
Kotlin, Scala, Java, C++, or C.
CS109
Compiling a class
The Kotlin compiler compiles each class to an object file.
in file point.kt
class Point
A compiler translates the source code to object code (machine
code).
For C and C++, it is customary to compile to native machine
code. It can be executed directly by the processor.
Native machine code is different for different processors,
operating systems, and can depend on library versions.
Kotlin (like Java and Scala) are normally translated to object
code for the JVM (Java virtual machine). A Java runtime
environment is needed on the computer to execute the
program. The exact same object code works on any system.
JVM is heavily used on servers and on Android.
CS109
Compiling a program
We cannot compile Kotlin scripts:
$ ktc hello.kt
error: expecting a top level declaration
println("Hello World")
^
Only declarations can be compiled:
• global variables with val and var,
• functions with fun,
• class definitions with data class or class.
So how can we write a program? If all we have are
declarations, how can we execute any code?
The magic main function.
ktc point.kt
Point.class
object code
The Point class can now be used in any code that can find
Point.class.
$ ktc
Welcome to Kotlin version 1.0.5-2
>>> val p = Point(7, 5)
JVM finds Point.class
CS109
Compiled Kotlin programs
1. In your source file hello.kt, define a function
main(args: Array<String>) returning nothing:
fun main(args: Array<String>) {
println("Hello World")
}
2. Compile the source file, resulting an a class file
HelloKt.class:
$ ktc hello.kt
3. Run the program:
$ kt HelloKt arguments
This name is generated from the name of the
source file.