Fahrenheit 0.4.0

Syntax

Syntax & Basic Types

Comments


// various types of comments
x = 10; # Inline comment
/* This is a
   block comment */
    

Data Types

Variables & Constants

Variables are statically typed in Fahrenheit. You must specify a type (or multiple types) upon definition. Fahrenheit checks types at runtime and throws a ScriptException if an incompatible value is assigned. If you omit the type and assign a value on the first encounter, the type defaults to any.


# Mandatory typed variables
int count = 0;
String name = "Fahrenheit";
boolean isValid = true;

# Union types (allow multiple acceptable types)
String|int id = 5;
id = "five"; # Valid

# 'any' type (accepts any value)
any data = 10;
data = "hello"; # Valid

# Defaulting to 'any' by assigning on first use
x = 10; # x is of type 'any'

# Constants (immutable, must be typed)
constant double PI = 3.14159;
constant String VERSION = "0.4.0";
# PI = 3.14; # Error: Cannot assign to constant
    

Scope & Visibility

Fahrenheit uses block scoping. Variables declared within { } are local to that block. Modifiers control where variables are stored and their visibility:


global String APP_NAME = "Fahrenheit App"; # Accessible everywhere
local int temp = 100;                      # Local to this script runner

structure Secure {
    private String key = "123";
    protected String secret = "abc";
    function String getKey() { return this.key; }
}
    

Operators

Standard arithmetic (+, -, *, /, %), comparison (==, !=, >, <, >=, <=), and logical (&&, ||, !) operators are supported.

Compound Assignment: +=, -=, *=, /= (works on numbers, strings, array elements, and struct fields)

Ternary Operator: condition ? val1 : val2 (right-associative, nestable)

Bitwise Operators: &, |, ^, ~, <<, >>, >>>

Operator Precedence (highest to lowest)

  1. ( ) [ ] .
  2. ! ~ - (unary)
  3. * / %
  4. + -
  5. > >= < <= == !=
  6. &&
  7. ||
  8. ? : (Ternary)
  9. =

Strings

Single and Double Quotes: Strings can be delimited with either double quotes ("...") or single quotes ('......'). Mixed nesting is supported (a single-quoted string can contain double quotes and vice versa).

Interpolation: Embed expressions using """ + "$" + """expression syntax inside any string.

Multiline: Use triple-double-quotes ("""...""") or triple-single-quotes ('''...'''). Preserves whitespace and newlines. Supports interpolation.


name = "World";
print("Hello ${name}!");    // Output: Hello World!
print('Hi ${name}!');       // Output: Hi World!

# Multiline string
html = """
<div>
  <h1>Hello """ + "$" + """{name}</h1>
</div>
""";
    

Enumerations

Use the enumerated keyword for fixed sets of constants.


enumerated Status { PENDING, ACTIVE, INACTIVE }

global enumerated AppState { INIT, RUNNING, STOPPED }

status = Status.ACTIVE;
if (status == Status.ACTIVE) {
    print("User is active");
}
    

Control Flow

If-Else


if (x > 5) {
    print("High");
} else {
    print("Low");
}
    

Loops


# While
i = 0;
while (i < 5) { i = i + 1; }

# For
for (i = 0; i < 10; i = i + 1) { print(i); }

# Foreach (over arrays, strings, or maps)
foreach (item : list) { print(item); }
foreach (key : myMap) { print(key + " -> " + myMap[key]); }
    

Break and Continue

Use break to exit a loop early, and continue to skip to the next iteration. Both work inside while, for, and foreach loops.


for (i = 0; i < 10; i = i + 1) {
    if (i % 2 == 0) { continue; } # Skip even numbers
    if (i > 7) { break; }         # Stop early
    print(i);
}
    

Switch


switch (val) {
    case 1: print("One"); break;
    default: print("Other");
}
    

Exception Handling


try {
    throw "Error";
} catch (e) {
    print("Caught: " + e);
} finally {
    print("Cleanup");
}