Welcome to the Scala Hello World project! This project demonstrates a basic setup for running Scala code using Visual Studio Code and the command line.
To run Scala, you need to install Coursier first, then use it to install Scala. Follow the instructions based on your operating system:
-
Download and set up Coursier:
$ curl -fL "https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz" | gzip -d > cs $ chmod +x cs $ ./cs setup
-
Verify Scala installation:
$ scala -version
-
Add the Coursier bin directory to your PATH (remember to save and reload the .bashrc file)
$ vim ~/.bashrc export PATH="$PATH:/root/.local/share/coursier/bin"
$ scala HelloWorld.scala$ scalac HelloWorld.scala
$ scala HelloWorldThis code(scala_compiler.py) implements a parser for Scala code. The parser takes a list of tokens and processes them according to Scala's syntax to produce an Abstract Syntax Tree (AST). This parser is capable of handling various Scala constructs such as function definitions, variable declarations, string interpolations, pattern matching, collections, and more.
@main def helloWorld(): Unit = {
println("Hello, World!")
}
• Handles @main annotation, function definition, and unit return type.
println(s"Hello, $name! You are $age years old.")
• Parses string interpolation with variable insertion inside strings.
val name: String = "Alice"
var age: Int = 25
• Parses val (immutable) and var (mutable) declarations with type annotations and initial values.
def describe(x: Any): String = x match {
case 0 => "Number is zero"
case 1 => "Number is one"
case _ => "Unknown"
}
println(describe(1))
• Parses Scala's match expressions and case statements.
val numbers = List(1, 2, 3, 4)
val doubled = numbers.map(_ * 2)
• Handles collections (e.g., List) and functional operations like map.
import java.util.Date
val now = new Date()
println(now)
• Parses import statements and variable declarations with instantiated classes.
trait Animal {
def sound: String
}
trait Mammal {
def hasFur: Boolean = true
}
class Dog extends Animal with Mammal {
def sound: String = "Woof"
}
val dog = new Dog
println(dog.sound)
println(dog.hasFur)
• Parses Scala's trait and class inheritance along with method definitions
def add(x: Int)(y: Int): Int = x + y
val add5 = add(5)_
println(add5(10))
val add10 = add(10)_
println(add10(2))
• Handles curried functions, partially applied functions, and function invocations.
def printList[T](list: List[T]): Unit = {
list.foreach(println)
}
val intList = List(1, 2, 3)
println(printList(intList))
val stringList = List("Scala", "Java", "Python")
println(printList(stringList))
• Handles generic functions and the printing of lists.
The Scala Code interpreter provides an interactive environment to execute and test Scala code without requiring prior compilation.
object ScalaExample {
def main(args: Array[String]) : Unit = {
print("Hello World")
}
}
The interpreter identifies the main function and then executes the print statement.
println(s"Hello, $name! You are $age years old.")
The interpreter identifies the variables in the print statement and then properly inserts their values into the statement.
val name: String = "Alice"
var age: Int = 25
The interpreter inputs variables in the environment variable, reflecting the characteristics of val (immutable) and var (mutable).