A small library which uses a fork of OpenSCAD which adds Python support: https://pythonscad.org/ to allow writing out DXF files and G-code and modeling how G-code will cut in 3D:
https://github.com/WillAdams/gcodepreview
Writing it in a TeX editor using a Literate Programming system developed in the course of working on it, the PDF should give both a good overview, and provide all the code.
It has greatly expanded/restored my math/geometry and has me looking forward to how to implement Bézier Curves and surfaces using similar math (since G-code and CGAL are fundamentally limited to lines and arcs and what can be easily made from such constructs).