API Reference
GScript provides a comprehensive API for programmatic access to the compiler, validator, and other tools.
Overview
The GScript API is organized into several modules:
- Operations: High-level CNC operations (face, drill, pocket, contour)
- Backends: G-code generation for different CNC controllers
- Libraries: Tool, material, and feed/speed definitions
- Validation: Type checking and safety validation
- Optimization: G-code optimization passes
Using the API
Rust
Add GScript as a dependency:
toml
[dependencies]
gscript = "0.1.0"Basic usage:
rust
use gscript::{compile, Backend, OptimizationLevel};
let source = r#"
tool endmill {
diameter: 6.0,
flutes: 4,
max_rpm: 18000,
}
face {
area: rect(0, 0, 100, 100),
tool: endmill,
depth: 2.0,
}
"#;
let gcode = compile(source, Backend::GRBL, OptimizationLevel::O2)?;
println!("{}", gcode);Module Documentation
Operations
Define and execute CNC operations:
Backends
Target different CNC controllers:
Libraries
Reusable definitions:
Type Definitions
Tool
rust
pub struct Tool {
pub name: String,
pub diameter: f64,
pub flutes: u32,
pub max_rpm: u32,
pub max_feedrate: f64,
}Operation
rust
pub enum Operation {
Face(FaceOperation),
Drill(DrillOperation),
Pocket(PocketOperation),
Contour(ContourOperation),
}Backend
rust
pub enum Backend {
GRBL,
Marlin,
Fanuc,
LinuxCNC,
TinyG,
}Error Handling
GScript uses Result types for error handling:
rust
use gscript::{compile, GScriptError};
match compile(source, Backend::GRBL, OptimizationLevel::O2) {
Ok(gcode) => println!("{}", gcode),
Err(GScriptError::SyntaxError(msg)) => eprintln!("Syntax error: {}", msg),
Err(GScriptError::ValidationError(msg)) => eprintln!("Validation error: {}", msg),
Err(e) => eprintln!("Error: {}", e),
}Examples
See the Examples section for complete API usage examples.
Next Steps
- Operations API - Detailed operation documentation
- Backends API - Backend-specific features
- Libraries API - Working with libraries