GGcode is a high-level programming language specifically designed for generating G-code for CNC machines, 3D printers, and other computer-controlled manufacturing equipment. It combines the simplicity of basic programming with powerful mathematical functions, making it easy to create complex toolpaths and geometric patterns.
Key Features:
Variables: Use let
to declare variables. No semicolons required!
let radius = 10 let center_x = 0 let feed_rate = 150
G-code Commands: Standard G-code commands with variable interpolation
G[0] X[10] Y[5] // Rapid move to position G1 X[20] Y[15] F[100] // Linear move with feed rate G1 X[center_x] Y[center_y] F[feed_rate] // Using variables
Comments: Use //
for single-line or /* */
for multi-line
// This is a single-line comment /* This is a multi-line comment */
GGcode provides a comprehensive library of mathematical functions for complex calculations and geometric operations.
abs(x)
- Absolute value | mod(a, b)
- Modulo (remainder) | floor(x)
- Round down | ceil(x)
- Round up
round(x)
- Round to nearest | min(a, b)
- Minimum value | max(a, b)
- Maximum value | clamp(x, min, max)
- Constrain value
let abs_val = abs(-15) // Returns 15 let mod_val = mod(17, 5) // Returns 2 let floor_val = floor(3.7) // Returns 3 let clamp_val = clamp(15, 0, 10) // Returns 10
sin(x)
- Sine | cos(x)
- Cosine | tan(x)
- Tangent
asin(x)
- Arcsine | acos(x)
- Arccosine | atan(x)
- Arctangent | atan2(y, x)
- Arctangent of y/x
let angle = 45 * DEG_TO_RAD let x = radius * cos(angle) let y = radius * sin(angle) let angle_from_atan = atan2(y, x)
sqrt(x)
- Square root | pow(x, y)
- Power (x^y) | hypot(x, y)
- Hypotenuse | distance(x1, y1, x2, y2)
- Distance between points
let sqrt_val = sqrt(25) // Returns 5 let pow_val = pow(2, 8) // Returns 256 let hyp = hypot(3, 4) // Returns 5 let dist = distance(0, 0, 3, 4) // Returns 5
lerp(a, b, t)
- Linear interpolation | map(x, in_min, in_max, out_min, out_max)
- Re-map value
let lerp_val = lerp(0, 100, 0.5) // Returns 50 let mapped = map(50, 0, 100, 0, 200) // Returns 100
sign(x)
- Sign (-1, 0, 1) | log(x)
- Natural logarithm | exp(x)
- Exponential | noise(x)
- Perlin noise
let sign_val = sign(-5) // Returns -1 let log_val = log(10) // Returns ~2.3 let exp_val = exp(1) // Returns ~2.7 let noise_val = noise(1.5) // Returns noise value
deg(x)
- Radians to degrees | rad(x)
- Degrees to radians
let deg_val = deg(PI) // Returns 180 let rad_val = rad(180) // Returns PI
GGcode provides powerful control structures for creating complex toolpaths and conditional operations.
Basic For Loop: Use for variable = start..end
syntax
for i = 0..10 { G1 X[i] Y[i] F[100] }
For Loops with Step: Use for variable = start..end step increment
for i = 0..10 step 2 { G1 X[i] Y[i] F[100] }
Exclusive Range: Use ..<
for exclusive end (recommended for arrays)
for i = 0..<10 { G1 X[i] Y[i] F[100] }
Nested For Loops: Create complex patterns
for x = 0..<5 { for y = 0..<5 { if (x == y) { G1 X[x*2] Y[y*2] F[200] } } }
Basic While Loop: Use while (condition)
syntax
let i = 0 while (i < 10) { G1 X[i] Y[i] F[100] i = i + 1 }
Complex Conditions: Combine multiple conditions
let k = 0 while (k < 8 && k >= 0) { if (mod(k, 2) == 0) { G1 X[k] Y[5] F[100] } else { G1 X[k] Y[-5] F[100] } k = k + 1 }
If-Else: Use if (condition)
and else
if (x > 5) { G1 X[x] Y[y] F[100] } else { G1 X[x] Y[y] F[50] }
If-Else If-Else: Multiple conditions
if (speed > 100) { return 300 } else if (speed > 50) { return 200 } else { return 100 }
Arrays in GGcode allow you to store and manipulate collections of values, making it easy to work with complex data structures and coordinate points.
Single-dimensional Arrays: Create arrays with initial values
let points = [10, 20, 30, 40, 50] let first = points[0] // Access first element let last = points[4] // Access last element
Multi-dimensional Arrays: Create nested arrays for grids and matrices
let grid = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] let value = grid[1][2] // Access element at row 1, column 2 (returns 5)
3D Arrays: Create complex data structures
let matrix = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] let value = matrix[0][1][0] // Access nested element (returns 3)
Array Assignment: Modify array elements
let maze = [[1, 2], [3, 4]] maze[1][0] = 0 // Change element at position [1][0] to 0
Dynamic Arrays: Build arrays programmatically
let points = [] let count = 0 points[count] = [10, 20] // Add point at index 0 count = count + 1 points[count] = [30, 40] // Add point at index 1 let pt = points[0] // Get first point let x = pt[0] // Get x coordinate let y = pt[1] // Get y coordinate
Array Iteration: Loop through array elements
let values = [10, 20, 30, 40, 50] for i = 0..<5 { G1 X[values[i]] Y[i*10] F100 }
Functions in GGcode allow you to create reusable code blocks with parameters and return values, making your code more modular and maintainable.
Basic Function: Define functions with parameters
function draw_circle(radius, feed_rate) { for i = 0..36 { let angle = (i * 10) * DEG_TO_RAD let x = radius * cos(angle) let y = radius * sin(angle) G1 X[x] Y[y] F[feed_rate] } }
Return Statement: Functions can return calculated values
function calculate_area(radius) { let area = PI * radius * radius return area } function get_feed_rate(speed) { if (speed > 100) { return 300 } else if (speed > 50) { return 200 } else { return 100 } }
Calling Functions: Use functions with parameters
draw_circle(10, 150) // Draw circle with radius 10, feed rate 150 let area = calculate_area(5) // Calculate area of circle with radius 5 let feed = get_feed_rate(75) // Get feed rate for speed 75
Multi-parameter Functions: Functions can take multiple parameters
function draw_rectangle(width, height, center_x, center_y, feed_rate) { let x1 = center_x - width/2 let y1 = center_y - height/2 let x2 = center_x + width/2 let y2 = center_y + height/2 G0 X[x1] Y[y1] G1 X[x2] Y[y1] F[feed_rate] G1 X[x2] Y[y2] F[feed_rate] G1 X[x1] Y[y2] F[feed_rate] G1 X[x1] Y[y1] F[feed_rate] }
GGcode provides a comprehensive set of operators for mathematical calculations, comparisons, and logical operations.
==
- Equal to | !=
- Not equal to | <
- Less than | <=
- Less than or equal
>
- Greater than | >=
- Greater than or equal
let a = 10 let b = 5 let eq_test = a == 10 // Returns true let ne_test = a != b // Returns true let lt_test = b < a // Returns true let le_test = b <= a // Returns true
&&
- Logical AND | ||
- Logical OR | !
- Logical NOT
let and_test = (a > b) && (a == 10) // Returns true let or_test = (a < b) || (a != 5) // Returns true let not_test = !(a < b) // Returns true
+
- Addition | -
- Subtraction | *
- Multiplication | /
- Division
let sum = a + b // Returns 15 let diff = a - b // Returns 5 let product = a * b // Returns 50 let quotient = a / b // Returns 2
&
- Bitwise AND
let bit_test = 5 & 3 // Returns 1 (binary: 101 & 011 = 001)
-
- Negation | !
- Logical NOT
let negative = -5 // Returns -5 let logical_not = !0 // Returns true let double_not = !!1 // Returns true
Modulo: Use the mod(a, b)
function, not the %
operator
let remainder = mod(17, 5) // Returns 2 (correct) // let remainder = 17 % 5 // This will cause an error!
GGcode provides several built-in mathematical constants for common calculations.
PI
- π (3.14159...) | TAU
- 2π (6.28318...) | EU
- Euler's number (2.71828...)
DEG_TO_RAD
- Degrees to radians conversion | RAD_TO_DEG
- Radians to degrees conversion
let pi_val = PI // 3.14159... let tau_val = TAU // 6.28318... let eu_val = EU // 2.71828... let deg_to_rad_val = DEG_TO_RAD // π/180 let rad_to_deg_val = RAD_TO_DEG // 180/π
Use note { }
blocks for file documentation and headers
note { This is a documentation block Can contain multiple lines Useful for file headers and descriptions Author: Your Name Version: 1.0 }
Use //
for single-line comments or /* */
for multi-line comments
// This is a single-line comment let radius = 10 // Inline comment /* This is a multi-line comment Can span multiple lines Useful for detailed explanations */
GGcode includes several special features that make programming more convenient.
Automatic Variable Creation: Writing just an identifier creates an assignment with 0
unexpected_token // Automatically becomes: unexpected_token = 0
Automatic G1: G-code coordinates without explicit command become G1
X[10] Y[20] // Automatically becomes: G1 X[10] Y[20]
Direct Assignment: Assign values directly to array elements
let grid = [[1, 2], [3, 4]] grid[1][0] = 0 // Change element at position [1][0] to 0
Nested Operations: Combine multiple operations in single expressions
let complex_expr = (a + b) * (c - d) / (e + f) let nested_func = sin(cos(tan(45 * DEG_TO_RAD)))
for i = 0..<10
instead of for i = 0..10