This document provides a comprehensive reference for the μHigh programming language syntax and features.
Define values that cannot be changed:
const PI 3.14159
const MAX_COUNT 100Declare variables that can store values:
var counter
var resultAssign values to variables:
counter = 1
result = counter + 41Print strings or expressions:
print("Hello, World!")
print(result)Read input from the user:
input counterConditional statements and loops:
if counter == 1 {
print "Counter is one"
} else {
print "Counter is not one"
}
while counter < 10 {
print counter
counter = counter + 1
}Iterate with for loops:
for i = 0; i < 10; i = i + 1 {
print i
}
// Range-based for loop
for i in range(10) {
print i
}
// For-each loop (for arrays)
var numbers = [1, 2, 3, 4, 5]
for num in numbers {
print num
}Declare and manipulate arrays:
// Array declaration
var numbers = [1, 2, 3, 4, 5]
var names = ["Alice", "Bob", "Charlie"]
var empty = []
// Array access
print numbers[0] // prints 1
names[1] = "Robert"
// Array methods
var length = len(numbers)
append(numbers, 6)
var first = pop(numbers, 0)String manipulation and operations:
var greeting = "Hello"
var name = "World"
var message = greeting + ", " + name + "!"
// String methods
var length = len(message)
var upper = uppercase(message)
var lower = lowercase(message)
var substr = substring(message, 0, 5)Explicit type declarations:
var count: int = 42
var price: float = 19.99
var name: string = "μHigh"
var isActive: bool = true
// Type casting
var num = int("123")
var text = string(456)
var decimal = float(42)Define reusable code blocks:
func add(a, b) {
return a + b
}
func main() {
var sum
sum = add(10, 20)
print sum
}Function overloading and default parameters:
// Default parameters
func greet(name = "World") {
print "Hello, " + name + "!"
}
// Multiple return values
func divide(a, b) {
return a / b, a % b
}
var quotient, remainder = divide(10, 3)Try-catch blocks for error handling:
try {
var result = divide(10, 0)
print result
} catch error {
print "Error: " + error
}Embed MicroASM code directly:
func optimized_add(a, b) {
asm {
LOAD R0, a
ADD R0, b
STORE result, R0
}
return result
}Include other μHigh files:
include "utils.uh"Single-line and multi-line comments:
// This is a single-line comment
/*
This is a
multi-line comment
*/
var x = 42 // Inline comment- Equal: ==
- Not equal: !=
- Less than: <
- Greater than: >
- Less or equal: <=
- Greater or equal: >=
- And: &&
- Or: ||
- Not: !
if x > 0 && y < 10 {
print "Valid range"
}
if !isActive || count == 0 {
print "Inactive or empty"
}- Numbers:
42 - Variables:
x - Arithmetic:
x + y,10 * 5
- Ternary operator:
condition ? value1 : value2 - Increment/Decrement:
x++,--y - Compound assignment:
+=,-=,*=,/=
var result = x > 0 ? "positive" : "non-positive"
counter++
total += amountabs(-5) // Absolute value
sqrt(16) // Square root
pow(2, 3) // Power (2^3)
min(5, 3) // Minimum
max(5, 3) // Maximum
random() // Random number 0-1len("hello") // String length
uppercase("hello") // Convert to uppercase
lowercase("HELLO") // Convert to lowercase
substring("hello", 1, 3) // Extract substringlen(array) // Array length
append(array, item) // Add item to end
pop(array, index) // Remove and return item
sort(array) // Sort array
reverse(array) // Reverse arrayfunc main() {
var x = 42
print("The answer is:")
print(x)
}func main() {
var name = ""
print("Enter your name:")
input name
print("Hello, " + name + "!")
}func main() {
var numbers = [5, 2, 8, 1, 9]
print "Original array:"
for num in numbers {
print num
}
sort(numbers)
print "Sorted array:"
for i = 0; i < len(numbers); i++ {
print numbers[i]
}
}func safe_divide(a, b) {
try {
if b == 0 {
throw "Division by zero"
}
return a / b
} catch error {
print "Error: " + error
return 0
}
}
func main() {
var result = safe_divide(10, 0)
print "Result: " + result
}func calculate_area(length: float, width: float): float {
return length * width
}
func main() {
var l: float = 5.5
var w: float = 3.2
var area: float = calculate_area(l, w)
print "Area: " + string(area)
}Define custom types with properties, fields, and methods:
public class Person {
private field name: string // Field - direct storage
private field age: int = 0 // Field with default value
public var Name: string { // Property - can have logic
get { return this.name }
set { this.name = value }
}
public func constructor(name: string, age: int) {
this.name = name
this.age = age
}
public func greet() {
Console.WriteLine("Hello, I'm " + this.name)
}
}- Fields (
field) - Direct storage, compiled to C# fields - Properties (
var) - Can have getters/setters, compiled to C# properties
public class Example {
private field _data: string // Private field
public field MaxCount: int = 100 // Public field with default
readonly field Id: string // Readonly field
static field Counter: int = 0 // Static field
// Auto-implemented properties
public var Name: string { get; set; }
public var ReadOnlyProp: string { get; }
// Expression-bodied properties
public var FullName: string {
get = this.FirstName + " " + this.LastName
}
// Block-bodied properties
public var Data: string {
get {
return this._data?.ToUpper()
}
set {
this._data = value?.Trim()
}
}
// Property with initializer
public var Status: string = "Active"
}μHigh supports several property accessor patterns:
// Auto-implemented properties
var Name: string { get; set; } // Read-write
var ReadOnly: string { get; } // Read-only
// Expression-bodied accessors
var FullName: string {
get = FirstName + " " + LastName // Computed property
}
// Block-bodied accessors
var Value: int {
get {
return this._value * 2
}
set {
this._value = value / 2
}
}
// Mixed accessor styles
var Mixed: string {
get = this._data?.ToUpper() // Expression-bodied getter
set { // Block-bodied setter
this._data = value?.Trim()
}
}