# Syntax

Lox2 features a syntax similar to most C-family programming languages, making it familiar to developers with experience in mainstream languages. Lox2 source files use the same .lox extension as the original Lox language and are compiled to bytecode before being executed by the interpreter.

### Literal Values

Literals are objects created using dedicated syntax, including atomic values such as booleans and numbers, as well as heap-allocated objects like strings and arrays. The table below summarizes the literal values available in Lox2; these will be explored in detail in subsequent sections.

| Literal Type | Example Values                                          | Description                                           |
| ------------ | ------------------------------------------------------- | ----------------------------------------------------- |
| Nil          | nil                                                     | Absence of value.                                     |
| Boolean      | true, false                                             | Binary values of logical true and false.              |
| Int          | 0, 1, -4                                                | Signed 32 bit integers.                               |
| Float        | 2.5, 3.6                                                | 64 bit floating point numbers.                        |
| String       | "Hello World"                                           | Sequences of characters enclosed in double quotes.    |
| Array        | \[  ],  \[2, 3.5, true, nil, "Hello"]                   | Collections of elements with integer indexes.         |
| Dictionary   | \["US": "United States", "EU": "Europe", "JP": "Japan"] | Collections of entries/key-value pairs.               |
| Range        | 1..5                                                    | Collections of integers primarily used for iteration. |
| Lambda       | {\|x\| x \* x }                                         | Anonymous functions with non-local returns.           |

### Identifiers

Identifiers in Lox2 follow naming rules similar to those of most C-family languages. They must consist of alphanumeric characters (`a–z`, `A–Z`, `0–9`) or underscores (`_`), and cannot begin with a digit. The following are examples of valid identifiers in Lox2:

```typescript
hello
camelCase
PascalCase
_under_score
abc123
ALLCAPS
```

### Keywords

Keywords are reserved words or identifiers that cannot be used as names for functions, classes, traits, or sub-namespaces. The complete list of Lox2 keywords is provided below:

```typescript
and as async await break catch class continue default
else extends finally for from fun if or return super 
throw trait try val var void while with yield
```

Note: Reserved keywords can be used as method names by escaping them with backticks, e.g., `` `if` ``, `` `catch` ``.&#x20;

### Comments

Lox2 supports line comments that begin with // and extend to the end of the line.

```typescript
// This is a line comment.
```

Block comments, delimited by /\* and \*/, are also supported. They can span multiple lines and may be nested.

```typoscript
/* 
    This
    is
    a
    block
    comment.
    /* It can be nested */
 */
```

### New Lines and Semicolons

Unlike the original Lox language, newlines (`\n`) are meaningful in Lox2 and can serve as statement terminators, similar to semicolons. The following code snippet is perfectly valid in Lox2:

```typescript
var x = 5
println("Value of x is ${x}")
x = x + 1
println("After incrementing x, it becomes ${x}")
```

However, newlines are parsed as whitespace if the token following the newline naturally follows the token before it. Semicolons may be used to explicitly terminate a statement.

```typescript
val x = 1 
    + 2
println(x) 
// prints 3 

val x = 1;
+ 2
println(x)
// prints 1 (who writes code like this?)
```

When multiple statements appear on the same line, semicolons are required as statement terminators. The first code snippet in this section can be rewritten to fit on a single line by adding semicolons:

```typescript
var x = 5; println("Value of x is ${x}"); x = x + 1; println("After incrementing x, it becomes ${x}")
```

In practice, placing multiple statements on a single line leads to unreadable code, and idiomatic Lox2 code should contain very few semicolons, if any at all.
