Chapter 2

Quick Start

Your first Nevaarize program in 5 minutes

Your First Program

Create a new file called hello.nva with the following content:

// My first Nevaarize program
print("Hello, Nevaarize!")
print("Native JIT Performance")

Run it:

./bin/nevaarize hello.nva

You should see:

Hello, Nevaarize!
Native JIT Performance

Congratulations! You've just run your first Nevaarize program.

Variables and Types

Nevaarize has dynamic typing with automatic type inference:

// Numbers
age = 25
pi = 3.14159

// Strings
name = "Nevaarize"

// Booleans
isAwesome = true

// Print with types
print("Name:", name, "Type:", type(name))
print("Age:", age, "Type:", type(age))
print("Pi:", pi, "Type:", type(pi))
print("Awesome:", isAwesome, "Type:", type(isAwesome))

Functions

Define functions with the func keyword:

// Simple function
func greet(name) {
    return "Hello, " + name + "!"
}

// Function with condition
func max(a, b) {
    if (a > b) {
        return a
    }
    return b
}

// Use them
print(greet("World"))
print("Max of 5 and 3:", max(5, 3))

Control Flow

Standard control structures work as you'd expect:

// If-elif-else
score = 85

if (score >= 90) {
    print("Excellent!")
} elif (score >= 80) {
    print("Good job!")
} elif (score >= 70) {
    print("Not bad")
} else {
    print("Keep practicing")
}

// For loop with Range
print("Counting:")
for (i in Range(1, 6)) {
    print("  ", i)
}

// While loop
count = 3
while (count > 0) {
    print("Countdown:", count)
    count = count - 1
}
print("Liftoff!")

Arrays

Arrays are first-class citizens:

// Create array
fruits = ["apple", "banana", "cherry"]

// Access elements
print("First fruit:", fruits[0])

// Loop through
for (fruit in fruits) {
    print("I like", fruit)
}

// Array methods
fruits.push("durian")
print("All fruits:", fruits)
print("Length:", len(fruits))

Structs

Define custom data structures:

// Define a struct
struct Person {
    name,
    age,
    city
}

// Create instance
alice = Person("Alice", 30, "Jakarta")

// Access fields
print("Name:", alice.name)
print("Age:", alice.age)
print("City:", alice.city)

// Modify fields
alice.age = 31
print("After birthday:", alice.age)

Using Standard Library

Import and use standard library modules:

// Import time module
import stdlib time as t

// Measure execution time
startTime = t.clock()

// Do some work
sum = 0
for (i in Range(1, 1000001)) {
    sum = sum + i
}

endTime = t.clock()
elapsed = endTime - startTime

print("Sum of 1 to 1,000,000:", sum)
print("Time:", elapsed, "seconds")

Async Functions

For concurrent operations, use async and await:

import stdlib time as t

// Define async function
async func fetchData(source) {
    print("Fetching from:", source)
    t.sleep(100)  // Simulate delay
    return "Data from " + source
}

// Await results
result1 = await fetchData("database")
result2 = await fetchData("api")

print("Result 1:", result1)
print("Result 2:", result2)

What's Next?

Now that you've got the basics, explore these topics: