syntax cheat sheet for .mgs scripts
name = "MagmaCrunch"
version = 2
pi = 3.14159
flag = true
nothing = none
Values print with MagmaScript spelling: none not None, true/false not True/False.
Only f"..." interpolates. Plain strings treat { as a literal character.
print(f"Hello, {name} v{version}!") // interpolated
print("use {braces} safely") // plain — {braces} printed literally
// named
fn greet(name) {
return f"Hello, {name}!"
}
// anonymous
double = fn(x) { x * 2 }
// arrow
triple = x -> x * 3
// default parameters
fn greet(name, greeting="hello") {
return f"{greeting}, {name}!"
}
greet("Jake") // "hello, Jake!"
greet("Jake", "hey") // "hey, Jake!"
if x > 10 {
print("big")
} else if x > 5 {
print("medium")
} else {
print("small")
}
for i in range(5) {
print(i)
}
for item in [1, 2, 3] {
print(item)
}
while x > 0 {
x = x - 1
}
break // exit loop early
continue // skip to next iteration
return // exit function
// lists
numbers = [1, 2, 3, 4, 5]
first = numbers[0]
sliced = numbers[1:3] // [2, 3]
reversed = numbers[::-1] // [5, 4, 3, 2, 1]
// dicts
scores = {"Pong": 12, "Tetris": 45}
print(scores["Tetris"])
// list comprehensions
evens = [x for x in numbers if x % 2 == 0]
doubled = [x * 2 for x in numbers]
// index assignment
numbers[0] = 99
scores["Pong"] = 100
numbers[0] += 10
a, b = 1, 2
x, y, z = 10, 20, 30
a, b = [1, 2] // list unpacking
// arithmetic: + - * / %
// comparison: == != < > <= >=
// logical: and or not
// membership: in, not in
if "key" in {"name": "Jake"} { ... }
if 5 not in [1, 2, 3] { ... }
Falsy values: none, false, 0, "", [], {}. Everything else is truthy.
class Dog {
fn init(name) {
self.name = name
}
fn bark(self) {
return self.name + " says woof!"
}
}
rex = Dog("Rex")
print(rex.bark()) // "Rex says woof!"
try {
result = risky_operation()
} haunter (e) {
print(f"Error: {e.message}")
}
throw fire toad("something went wrong")
error vocabulary:
| Keyword | Description |
|---|---|
haunter | syntax / parse errors |
fire toad | runtime errors |
devastate | undefined variable errors |
contemplate | type errors |
spooked | warnings (non-fatal, stderr) |
// import a module
intent "utils.mgs"
result = utils.greet("World")
// import with alias
intent "utils.mgs" as u
result = u.greet("World")
// import specific names
intent { greet, farewell } from "utils.mgs"
result = greet("World")
// file I/O
content = quarry("data.txt") // read
litho("output.txt", "hello") // write
// HTTP requests
response = http.get("https://api.example.com/data")
print(response.status)
print(response.json)
// shell commands
result = exec("ls -la")
print(result.stdout)
print(result.exit_code)
| Function | Description |
|---|---|
print(...) | print to stdout |
echo(...) | print to stdout (alias) |
len(x) | length of string, list, or dict |
type(x) | type name as string |
range(n) | generate integer ranges |
str(x) / int(x) / float(x) | type conversions |
abs(x) / min(...) / max(...) / sum(...) | math utilities |
keys(d) / values(d) | dict operations |
args() | get script arguments from CLI |
quarry(path) | read file contents |
litho(path, content) | write content to file |
exec(command) | execute shell command |
| Method | Description |
|---|---|
s.split(sep) | split string by separator |
s.join(list) | join list with string separator |
s.upper() / s.lower() | case conversion |
s.contains(sub) | check if substring exists |
s.replace(old, new) | replace substring |
s.length() | get string length |
s.startswith(prefix) | check prefix |
s.endswith(suffix) | check suffix |
s.strip() | remove leading/trailing whitespace |
s.match(pattern) | regex match, return groups |
s.findall(pattern) | find all regex matches |
Explicit memory below the dynamic language: fixed-width ints that wrap like C, a real byte arena, and structs with visible layout. floorplan is the only new keyword, the rest are builtins.
// fixed-width ints: i8 i16 i32 i64 u8 u16 u32 u64 f32 f64
n = u8(255)
n = n + 1 // wraps to 0, and says so (spooked)
i32(-7) / i32(2) // -3: truncates toward zero, like C
i32(1) + u8(1) // error: no promotion — use osmosis(x, i32)
// the arena: garrison claims bytes, scorch frees them
p = garrison(16)
p.poke(i32, 10) // write; p.peek(i32) reads back
p[8] = 72 // raw byte access
bathysphere(p) // annotated hex dump
scorch(p)
// floorplans: structs with C layout
floorplan Point {
x: i32
y: i32
label: u8[16]
}
layout(Point) // prints the field table, padding included
pt = garrison(Point)
pt.x = i32(10)
scorch(pt)
// a pine field points at another floorplan — linked lists work
floorplan Node {
value: i32
next: pine[Node]
}
// faults, all caught by try / haunter:
// quicksand — reading ground you already scorched
// area does not exist — reading outside a block
// ancient weeds — anything never scorched, reported at exit