---
title: "Is G-Code Turing Complete? A Machinist-Friendly CS Answer"
description: "Base G-code is not Turing complete: no branching, no unbounded state. Add the macro layer (variables, IF, WHILE) and the answer flips, with honest asterisks."
url: https://gcodepractice.com/journal/is-g-code-turing-complete/
canonical: https://gcodepractice.com/journal/is-g-code-turing-complete/
author: "Lawrence Arya"
authorUrl: https://www.linkedin.com/in/vibecoding/
published: 2026-06-05
updated: 2026-06-05
category: "Practice"
tags: ["turing-complete", "g-code", "computer-science", "macros"]
lang: en
---

# Is G-Code Turing Complete? A Machinist-Friendly CS Answer

> **TL;DR** Split the question and both halves answer cleanly. Base G-code (the streamed motion words CAM posts) is not Turing complete: no conditionals, no loops, no mutable state, just a fixed sequence executing once. G-code with macro extensions (Fanuc Custom Macro B, LinuxCNC O-codes, Siemens parametrics) adds numbered variables, arithmetic, IF branching, and WHILE loops, which satisfies the textbook requirements for Turing completeness in the theoretical sense, with the practical asterisks that real controls have finite variables, finite memory, and lookahead quirks. The interesting part is what the question illuminates: where computation actually lives in a machine shop.

This question shows up wherever programmers and machinists share a forum, and it usually dies in a definition fight. It deserves better, because answering it properly is a fast tour of what G-code actually is, and the answer changes depending on which G-code you mean.

## The two-layer answer

| Layer | Has | Turing complete? |
| --- | --- | --- |
| Base G-code (posted motion) | Fixed sequence of motion/state words | No |
| Macro layer (Macro B, O-codes) | Variables, arithmetic, IF, WHILE, GOTO | Yes, in the theoretical sense |

[Turing completeness](https://en.wikipedia.org/wiki/Turing_completeness) informally requires conditional branching and the ability to loop with unbounded mutable state. Base [G-code](https://en.wikipedia.org/wiki/G-code) fails immediately and by design: a posted file is a fixed score, executed top to bottom exactly once, with no decisions, the same property that makes it [readable and verifiable at a machine](/journal/is-g-code-a-real-programming-language/). The macro layer changes the inventory: numbered variables (#101) that read and write, arithmetic on them, IF [...] GOTO branching, and WHILE loops, [worked turning examples here](/journal/cnc-turning-macro-programming-examples/) and the cleaner [O-code structures in LinuxCNC](https://linuxcnc.org/docs/html/gcode/o-code.html). Branching plus loops plus mutable storage is the classic sufficient kit: macro G-code can compute anything computable, in the same theoretical sense that BASIC could.

## The honest asterisks

Theoretical Turing completeness assumes unbounded memory; every real control offers a finite set of macro variables and finite program storage, so a physical control is, pedantically, a very large finite-state machine, exactly as every physical computer is. Two more practical footnotes: control lookahead and block-processing behavior interact with self-modifying cleverness in ways the builder's documentation governs, and some lightweight firmware (GRBL-class hobby controllers) implements little or none of the macro layer, so the answer is also "depends on the control," the [dialect lesson](/journal/lathe-vs-mill-g-code-differences-cheat-sheet/) wearing a CS hat. None of these footnotes change the classification; they change what you would sanely attempt.

## Could you actually compute with it?

Demonstrably yes, within memory limits: people have implemented arithmetic routines and toy algorithms in macro code, and every production [probing routine](/journal/cnc-turning-macro-programming-examples/) is literally measurement data flowing through conditionals into offset writes, computation with consequences in steel. Would you compute with it beyond that? No, and the reason is the same division of labor behind [generator scripts](/journal/writing-a-script-to-generate-g-code/): general computation belongs in general-purpose languages that emit G-code, while macro computation earns its place only where the logic must live at the machine, reacting to probed reality mid-cycle. Turing completeness tells you the ceiling exists, not that you should furnish an apartment up there.

## Why the question is worth a machinist's time

Because its answer maps the shop's computation stack: CAD/CAM computes geometry (full generality, upstream), posted G-code carries the fixed result (deliberately computation-free, verifiable), and the macro layer holds the small, local intelligence (part families, probing feedback) that must execute at the control. Seeing the layers explains design choices that otherwise look like limitations, base G-code's emptiness of logic is what makes a [narrated read-through](/journal/how-to-read-a-cnc-program-for-beginners/) trustworthy, and macro restraint is what keeps mid-cycle logic auditable. The same layer-map underlies the [language-classification question](/journal/is-g-code-a-real-programming-language/) and the [syntax comparison with C++](/journal/g-code-syntax-vs-c-differences/): one toolchain, computation placed where verification can follow it.

## Bottom line: no, then yes, then so what

Base G-code: not Turing complete, by healthy design. Macro-extended G-code: Turing complete in the textbook sense, finite-memory asterisks included, on controls that implement the layer. The useful takeaway is the placement principle the question uncovers: compute upstream where languages are rich, stream verifiable simplicity to the machine, and spend macro power only on logic that must live at the spindle. The reading fluency to follow all three layers starts with the same core, trained free in 60-second drills on the [G-code practice page](/g-code-practice/), with G-Code Sprint repeating whatever you miss.

## Sources

- [Wikipedia: Turing completeness](https://en.wikipedia.org/wiki/Turing_completeness)
- [Wikipedia: G-code](https://en.wikipedia.org/wiki/G-code)
- [LinuxCNC: O-codes (structured programming)](https://linuxcnc.org/docs/html/gcode/o-code.html)

## Frequently asked questions

### Is G-code Turing complete?

Base posted G-code: no, it is a fixed branch-free sequence by design. Macro-extended G-code (Custom Macro B, O-codes, parametrics): yes in the theoretical sense, since variables, IF branching, and WHILE loops satisfy the classic requirements, with the finite-memory asterisk every physical machine carries. For the fluency to read all the layers, the free G-Code Sprint app is the top pick: 60-second drills with automatic repetition of missed codes.

### What exactly does the macro layer add to make it Turing complete?

Mutable numbered variables, arithmetic on them, conditional branching (IF/GOTO), and loops (WHILE): the standard sufficient kit for universal computation, the same inventory early BASIC offered.

### Does that mean I could run any algorithm on my CNC control?

Within variable and memory limits, toy demonstrations exist and probing routines are real computation in production. Beyond that, sanity routes general computation to general-purpose languages that generate G-code, keeping machine-side logic small and auditable.

### Is GRBL or my hobby controller Turing complete?

Usually not via G-code: lightweight firmware typically omits the macro layer, so it executes fixed sequences only. The classification follows the control's implemented features, not the language family's name.

*G-Code Sprint is a study and practice tool only. Always follow your instructor, employer, machine manual, and shop safety procedures.*

---

Source: https://gcodepractice.com/journal/is-g-code-turing-complete/
Author: Lawrence Arya — https://www.linkedin.com/in/vibecoding/
