<commit_before>= G Code Overview [[cha:Language-Overview]]

The EMC2 G Code language is based on the RS274/NGC language. The G
Code language is based on lines of code. Each line (also called a
"block") may include commands to do several different things. Lines of
code may be collected in a file to make a program.

A typical line of code consists of an optional line number at the
beginning followed by one or more "words". A word consists of a letter
followed by a number (or something that evaluates to a number). A word
may either give a command or provide an argument to a command. For
example, "`G1 X3`" is a valid line of code with two words. "`G1` " is a
command meaning "move in a straight line at the programmed feed
 rate to the programmed end point", and "`X3` " provides an argument
value (the value of X should be 3 at the end of
the move). Most EMC2 G Code commands start with either G or M (for
General and Miscellaneous). The words for these commands are called "G
codes" and "M codes."

The EMC2 language has no indicator for the start of a program. The
Interpreter, however, deals with files. A single program may be in a
single file, or a program may be spread across several files. A file
may demarcated with percents in the following way. The first non-blank
line of a file may contain nothing but a percent sign, "%", possibly
surrounded by white space, and later in the file (normally at the end
of the file) there may be a similar line. Demarcating a file with
percents is optional if the file has an `M2` or `M30`  in it, but is
required if not. An error will be signalled if a file
has a percent line at the beginning but not at the end. The useful
contents of a file demarcated by percents stop after the second percent
line. Anything after that is ignored.

The EMC2 G Code language has two commands (`M2` or `M30` ), either of
which ends a program. A program may end before the end of
a file. Lines of a file that occur after the end of a program are not
to be executed. The interpreter does not even read them.

== Format of a line

A permissible line of input code consists of the following, in order,
with the restriction that there is a maximum (currently 256) to the
number of characters allowed on a line.

 . an optional block delete character, which is a slash "/" . 
 . an optional line number.
 . any number of words, parameter settings, and comments.
 . an end of line marker (carriage return or line feed or both). 

Any input not explicitly allowed is illegal and will cause the
Interpreter to signal an error.

Spaces and tabs are allowed anywhere on a line of code and do not
change the meaning of the line, except inside comments. This makes some
strange-looking input legal. The line "`G0X +0. 12 34Y 7`" is
equivalent to "`G0 x+0.1234 Y7`", for example.

Blank lines are allowed in the input. They are to be ignored.

Input is case insensitive, except in comments, i.e., any letter
outside a comment may be in upper or lower case without changing the
meaning of a line.

== Line Number (((Line Number)))

A line number is the letter N followed by an integer (with no sign)
between 0 and 99999 written with no more than five digits (000009 is
not OK, for example). Line numbers may be repeated or used out of
order, although normal practice is to avoid such usage. Line numbers
may also be skipped, and that is normal practice. A line number is not
required to be used, but must be in the proper place if used.

== Word (((Word)))

A word is a letter other than N followed by a real value.

Words may begin with any of the letters shown in Table 
<<cap:Words-and-their>>. The table includes N for completeness, even
though, as defined above, line numbers are not words. Several letters
(I, J, K, L, P, R) may have different meanings in different contexts.
Letters which refer to axis names are not valid on a machine which does
not have the corresponding axis.

.Words and their meanings[[cap:Words-and-their]]

[width="90%", options="header"]
|========================================
|Letter | Meaning
|A | A axis of machine
|B | B axis of machine
|C | C axis of machine
|D | Tool radius compensation number
|F | Feed rate
|G | General function (See table  <<cap:Modal-Groups>>)
|H | Tool length offset index
|I | X offset for arcs and G87 canned cycles
|J | Y offset for arcs and G87 canned cycles
|K | Z offset for arcs and G87 canned cycles.
| | Spindle-Motion Ratio for G33 synchronized movements.
|M | Miscellaneous function (See table  <<cap:Modal-Groups>>)
|N | Line number
|P | Dwell time in canned cycles and with G4.
| | Key used with G10.
|Q | Feed increment in G73, G83 canned cycles
|R | Arc radius or canned cycle plane
|S | Spindle speed
|T | Tool selection
|U | U axis of machine
|V | V axis of machine
|W | W axis of machine
|X | X axis of machine
|Y | Y axis of machine
|Z | Z axis of machine
|========================================

== Number [[sub:Number]]

The following rules are used for (explicit) numbers. In these rules a
digit is a single character between 0 and 9.

 -  A number consists of (1) an optional plus or minus sign, followed by
   (2) zero to many digits, followed, possibly, by (3) one decimal point,
   followed by (4) zero to many digits - provided that there is at least
   one digit somewhere in the number.
 -  There are two kinds of numbers: integers and decimals. An integer does
   not have a decimal point in it; a decimal does.
 -  Numbers may have any number of digits, subject to the limitation on
   line length. Only about seventeen significant figures will be retained,
   however (enough for all known applications).
 -  A non-zero number with no sign as the first character is assumed to be
   positive.

Notice that initial (before the decimal point and the first non-zero
digit) and trailing (after the decimal point and the last non-zero
digit) zeros are allowed but not required. A number written with
initial or trailing zeros will have the same value when it is read as
if the extra zeros were not there.

Numbers used for specific purposes in RS274/NGC are often restricted
to some finite set of values or some to some range of values. In many
uses, decimal numbers must be close to integers; this includes the
values of indexes (for parameters and carousel slot numbers, for
example), M codes, and G codes multiplied by ten. A decimal number
which is supposed be close to an integer is considered close enough if
it is within 0.0001 of an integer.

== Numbered Parameters [[sub:Numbered-Parameters]]

A numbered parameter is the pound character `#`  followed by an
integer between 1 and 5399. The parameter is referred
to by this integer, and its value is whatever number is stored in the
parameter.

A value is stored in a parameter with the = operator; for example "`#3
= 15` " means "set parameter 3 to 15." A parameter setting does not
take
effect until after all parameter values on the same line have been
found. For example, if parameter 3 has been previously set to 15 and
 the line "`#3=6 G1 x#3` " is interpreted, a straight move to a point
where x equals 15 will
occur and the value of parameter 3 will be 6.

The `#`  character takes precedence over other operations, so that, for
 example, "`#1+2` " means the number found by adding 2 to the value of
parameter 1, not
 the value found in parameter 3. Of course, `#[1+2]` does mean the
value found in parameter 3. The `#` character may be repeated; for
example `##2`  means the value of the parameter whose index is the
(integer) value
of parameter 2.

The interpreter maintains a number of readonly parameters for a loaded
tool:

 - #5400 tool number 
 - #5401 tool x offset 
 - #5402 tool y offset 
 - #5403 tool z offset 
 - #5404 tool a offset 
 - #5405 tool b offset 
 - #5406 tool c offset 
 - #5407 tool u offset 
 - #5408 tool v offset 
 - #5409 tool w offset 
 - #5410 tool diameter 
 - #5411 tool frontangle 
 - #5412 tool backangle 
 - #5413 tool orientation

== Named Parameters [[sub:Named-Parameters]]

Named parameters work like numbered parameters but are easier to read.
All parameter names are converted to lower case and have spaces and
tabs removed. Named parameters must be enclosed with `< >` marks.

`#<named parameter here>`  is a local named parameter. By default, a
named parameter is local to
the scope in which it is assigned. You can't access a local parameter
outside of its subroutine - this is so that two subroutines can use the
same parameter names without fear of one subroutine overwriting the
values in another.

`#<_global named parameter here>`  is a global named parameter. They
are accessible from within called
subroutines and may set values within subroutines that are accessible
to the caller. As far as scope is concerned, they act just like regular
numeric parameters. They are not stored in files.

Examples:

 - Declaration of named global variable

    `#<_endmill_dia> = 0.049` 

 - Reference to previously declared global variable

    `#<_endmill_rad> = [#<_endmill_dia>/2.0]` 

 - Mixed literal and named parameters

    ` o100 call [0.0] [0.0] [#<_inside_cutout>-#<_endmill_dia>] [#<_Zcut>]
    [#<_feedrate>]`



Notes:

The global parameters _a, _b, _c, ... _z have been reserved for
special use. In the future, they may provide access to the last Aword,
Bword, Cword, etc.

== System Parameters [[sec:System-Parameters]]

Two global read only named parameters are available to check which
version is running from G Code.

#<_vmajor>::
    Major package version. If current version was 2.6.2 would return 2.6.

#<_vminor>::
    Minor package version. If current version was 2.6.2 would return 0.2.

== Expressions [[sub:Expressions]]

An expression is a set of characters starting with a left bracket `[`
and ending with a balancing right bracket `]` . In between the brackets
are numbers, parameter values, mathematical
operations, and other expressions. An expression is evaluated to
produce a number. The expressions on a line are evaluated when the line
is read, before anything on the line is executed. An example of an
expression is `[1 + acos[0] - [#3 ** [4.0/2]]]`.

== Binary Operators [[sub:Binary-Operators]]

Binary operators only appear inside expressions. There are four basic
mathematical operations: addition (`+`), subtraction (`-`),
multiplication (`*`), and division (`/`). There are three logical
operations: non-exclusive or (`OR`), exclusive or (`XOR`), and logical
and (`AND`). The eighth operation is the modulus operation (`MOD`). The
ninth operation is the "power" operation (`**` ) of raising the number
on the left of the operation to the power on
 the right. The relational operators are equality (`EQ`), inequality
(`NE`), strictly greater than (`GT`), greater than or equal to (`GE`),
strictly less than (`LT`), and less than or equal to (`LE`).

The binary operations are divided into several groups according to
their precedence. (see table  <<cap:Operator-Precedence>>) If
operations in different precedence groups are strung together (for
example in the expression `[2.0 / 3 * 1.5 - 5.5 / 11.0]` ), operations
in a higher group are to be performed before operations
in a lower group. If an expression contains more than one operation
 from the same group (such as the first `/` and `*`  in the example),
the operation on the left is performed first. Thus,
 the example is equivalent to: `[[[2.0 / 3] * 1.5] - [5.5 / 11.0]]` ,
which is equivalent to to `[1.0 - 0.5]` , which is `0.5`.

The logical operations and modulus are to be performed on any real
numbers, not just on integers. The number zero is equivalent to logical
false, and any non-zero number is equivalent to logical true.

.Operator Precedence[[cap:Operator-Precedence]](((operator precedence)))

[width="90%", options="header"]
|========================================
|Operators | Precedence
|** | 'highest'
|* / MOD | 
|+ - | 
|EQ NE GT GE LT LE | 
|AND OR XOR | 'lowest'
|========================================

== Functions [[sub:Unary-Operation-Value]]

A function is either "`ATAN` " followed by one expression divided by
another expression (for
 example "`ATAN[2]/[1+3]`") or any other function name followed by an
expression (for example "`SIN[90]` "). The available functions are
shown in table  <<cap:Functions>>.
 Arguments to unary operations which take angle measures (`COS`, `SIN`,
and `TAN` ) are in degrees. Values returned by unary operations which
return
angle measures (`ACOS`, `ASIN`, and `ATAN`) are also in degrees.

.Functions[[cap:Functions]]

[width="90%", options="header"]
|========================================
|Function Name | Function result
|ATAN[Y]/[X] | Four quadrant inverse tangent
|ABS[arg] | Absolute value
|ACOS[arg] | Inverse cosine
|ASIN[arg] | Inverse sine
|COS[arg] | Cosine
|EXP[arg] | e raised to the given power
|FIX[arg] | Round down to integer
|FUP[arg] | Round up to integer
|ROUND[arg] | Round to nearest integer
|LN[arg] | Base-e logarithm
|SIN[arg] | Sine
|SQRT[arg] | Square Root
|TAN[arg] | Tangent
|EXISTS[arg] | Check named Parameter
|========================================

The `FIX`  function rounds towards the left (less positive or more
negative) on
 a number line, so that `FIX[2.8] =2` and `FIX[-2.8] = -3`, for
example. The `FUP`  operation rounds towards the right (more positive
or less negative)
on a number line; `FUP[2.8] = 3` and `FUP[-2.8] = -2`, for example.

The EXISTS function checks for the existence of a single named
parameter. It takes only one named parameter and returns 1 if it exists
and 0 if it does not exist. It is an error if you use a numbered
parameter or an expression.

== Repeated Items

A line may have any number of G words, but two G words from the same
modal group (see Section  <<sec:Modal-Groups>>) may not appear on the
same line.

A line may have zero to four M words. Two M words from the same modal
group may not appear on the same line.

For all other legal letters, a line may have only one word beginning
with that letter.

If a parameter setting of the same parameter is repeated on a line,
"`#3=15 #3=6` ", for example, only the last setting will take effect.
It is silly,
but not illegal, to set the same parameter twice on the same line.

If more than one comment appears on a line, only the last one will be
used; each of the other comments will be read and its format will be
checked, but it will be ignored thereafter. It is expected that putting
more than one comment on a line will be very rare.

== Item order

The three types of item whose order may vary on a line (as given at
the beginning of this section) are word, parameter setting, and
comment. Imagine that these three types of item are divided into three
groups by type.

The first group (the words) may be reordered in any way without
changing the meaning of the line.

If the second group (the parameter settings) is reordered, there will
be no change in the meaning of the line unless the same parameter is
set more than once. In this case, only the last setting of the
parameter will take effect. For example, after the line "`#3=15 #3=6` "
has been interpreted, the value of parameter 3 will be 6. If the
 order is reversed to "`#3=6 #3=15`" and the line is interpreted, the
value of parameter 3 will be 15.

If the third group (the comments) contains more than one comment and
is reordered, only the last comment will be used.

If each group is kept in order or reordered without changing the
meaning of the line, then the three groups may be interleaved in any
way without changing the meaning of the line. For example, the line
"`g40 g1 #3=15 (foo) #4=-7.0` " has five items and means exactly the
same thing in any of the 120
 possible orders (such as "`#4=-7.0 g1 #3=15 g40 (foo)`") for the five
items.

== Commands and Machine Modes

Many commands cause the controller to change from one mode to another,
and the mode stays active until some other command changes it
implicitly or explicitly. Such commands are called "modal". For
example, if coolant is turned on, it stays on until it is explicitly
turned off. The G codes for motion are also modal. If a G1 (straight
move) command is given on one line, for example, it will be executed
again on the next line if one or more axis words is available on the
line, unless an explicit command is given on that next line using the
axis words or canceling motion.

"Non-modal" codes have effect only on the lines on which they occur.
For example, G4 (dwell) is non-modal.

== Modal Groups (((Modal Groups)))[[sec:Modal-Groups]]

Modal commands are arranged in sets called "modal groups", and only
one member of a modal group may be in force at any given time. In
general, a modal group contains commands for which it is logically
impossible for two members to be in effect at the same time - like
measure in inches vs. measure in millimeters. A machining center may be
in many modes at the same time, with one mode from each modal group
being in effect. The modal groups are shown in Table 
<<cap:Modal-Groups>>.

.Modal Groups[[cap:Modal-Groups]]

[width="90%", options="header"]
|========================================
|Modal Group Meaning | Member Words
|Motion | G0, G1, G2, G3, G33, G38.x, G73, G76, G80, G81
| | G82, G83, G84, G85, G86, G87, G88, G89
|Plane selection | G17.x, G18.x, G19.x
|Distance Mode | G90, G91
|Arc IJK Distance Mode | G90.1, G91.1
|Feed Rate Mode | G93, G94, G95
|Units | G20, G21
|Cutter Diameter Compensation | G40, G41, G42, G41.1, G42.1
|Tool Length Offset | G43, G43.1, G49
|Return Mode in Canned Cycles | G98, G99
|Coordinate System Selection | G54, G55, G56, G57, G58
| | G59, G59.1, G59.2, G59.3
|Control Mode | G61, G61.1, G64
|Spindle Speed Mode | G96, G97
|Lathe Diameter Mode | G7, G8
|Stopping | M0, M1, M2, M30, M60
|Tool Change | M6 Tn
|Spindle | M3, M4, M5
|Coolant | (M7 M8 can both be on), M9
|Override Switches | M48, M49
|Flow Control | O-
|Non-modal codes | G4, G10 G28, G30, G53 G92,
| | G92.1, G92.2, G92.3, M1xx
|========================================

For several modal groups, when a machining center is ready to accept
commands, one member of the group must be in effect. There are default
settings for these modal groups. When the machining center is turned on
or otherwise re-initialized, the default values are automatically in
effect.

Group 1, the first group on the table, is a group of G codes for
motion. One of these is always in effect. That one is called the
current motion mode.

It is an error to put a G-code from group 1 and a G-code from group 0
on the same line if both of them use axis words. If an axis word-using
G-code from group 1 is implicitly in effect on a line (by having been
activated on an earlier line), and a group 0 G-code that uses axis
words appears on the line, the activity of the group 1 G-code is
suspended for that line. The axis word-using G-codes from group 0 are
G10, G28, G30, and G92.

It is an error to include any unrelated words on a line with `O-` flow
control.

== Comments

Comments can be added to lines of G code to help clear up the
intention of the programmer. Comments can be embedded in a line using
parentheses () or for the remainder of a line using a semi-colon. The<commit_msg>Docs: adjust columns in table<commit_after>= G Code Overview [[cha:Language-Overview]]

The EMC2 G Code language is based on the RS274/NGC language. The G
Code language is based on lines of code. Each line (also called a
"block") may include commands to do several different things. Lines of
code may be collected in a file to make a program.

A typical line of code consists of an optional line number at the
beginning followed by one or more "words". A word consists of a letter
followed by a number (or something that evaluates to a number). A word
may either give a command or provide an argument to a command. For
example, "`G1 X3`" is a valid line of code with two words. "`G1` " is a
command meaning "move in a straight line at the programmed feed
 rate to the programmed end point", and "`X3` " provides an argument
value (the value of X should be 3 at the end of
the move). Most EMC2 G Code commands start with either G or M (for
General and Miscellaneous). The words for these commands are called "G
codes" and "M codes."

The EMC2 language has no indicator for the start of a program. The
Interpreter, however, deals with files. A single program may be in a
single file, or a program may be spread across several files. A file
may demarcated with percents in the following way. The first non-blank
line of a file may contain nothing but a percent sign, "%", possibly
surrounded by white space, and later in the file (normally at the end
of the file) there may be a similar line. Demarcating a file with
percents is optional if the file has an `M2` or `M30`  in it, but is
required if not. An error will be signalled if a file
has a percent line at the beginning but not at the end. The useful
contents of a file demarcated by percents stop after the second percent
line. Anything after that is ignored.

The EMC2 G Code language has two commands (`M2` or `M30` ), either of
which ends a program. A program may end before the end of
a file. Lines of a file that occur after the end of a program are not
to be executed. The interpreter does not even read them.

== Format of a line

A permissible line of input code consists of the following, in order,
with the restriction that there is a maximum (currently 256) to the
number of characters allowed on a line.

 . an optional block delete character, which is a slash "/" . 
 . an optional line number.
 . any number of words, parameter settings, and comments.
 . an end of line marker (carriage return or line feed or both). 

Any input not explicitly allowed is illegal and will cause the
Interpreter to signal an error.

Spaces and tabs are allowed anywhere on a line of code and do not
change the meaning of the line, except inside comments. This makes some
strange-looking input legal. The line "`G0X +0. 12 34Y 7`" is
equivalent to "`G0 x+0.1234 Y7`", for example.

Blank lines are allowed in the input. They are to be ignored.

Input is case insensitive, except in comments, i.e., any letter
outside a comment may be in upper or lower case without changing the
meaning of a line.

== Line Number (((Line Number)))

A line number is the letter N followed by an integer (with no sign)
between 0 and 99999 written with no more than five digits (000009 is
not OK, for example). Line numbers may be repeated or used out of
order, although normal practice is to avoid such usage. Line numbers
may also be skipped, and that is normal practice. A line number is not
required to be used, but must be in the proper place if used.

== Word (((Word)))

A word is a letter other than N followed by a real value.

Words may begin with any of the letters shown in Table 
<<cap:Words-and-their>>. The table includes N for completeness, even
though, as defined above, line numbers are not words. Several letters
(I, J, K, L, P, R) may have different meanings in different contexts.
Letters which refer to axis names are not valid on a machine which does
not have the corresponding axis.

.Words and their meanings[[cap:Words-and-their]]

[width="90%", options="header"]
|========================================
|Letter | Meaning
|A | A axis of machine
|B | B axis of machine
|C | C axis of machine
|D | Tool radius compensation number
|F | Feed rate
|G | General function (See table  <<cap:Modal-Groups>>)
|H | Tool length offset index
|I | X offset for arcs and G87 canned cycles
|J | Y offset for arcs and G87 canned cycles
|K | Z offset for arcs and G87 canned cycles.
| | Spindle-Motion Ratio for G33 synchronized movements.
|M | Miscellaneous function (See table  <<cap:Modal-Groups>>)
|N | Line number
|P | Dwell time in canned cycles and with G4.
| | Key used with G10.
|Q | Feed increment in G73, G83 canned cycles
|R | Arc radius or canned cycle plane
|S | Spindle speed
|T | Tool selection
|U | U axis of machine
|V | V axis of machine
|W | W axis of machine
|X | X axis of machine
|Y | Y axis of machine
|Z | Z axis of machine
|========================================

== Number [[sub:Number]]

The following rules are used for (explicit) numbers. In these rules a
digit is a single character between 0 and 9.

 -  A number consists of (1) an optional plus or minus sign, followed by
   (2) zero to many digits, followed, possibly, by (3) one decimal point,
   followed by (4) zero to many digits - provided that there is at least
   one digit somewhere in the number.
 -  There are two kinds of numbers: integers and decimals. An integer does
   not have a decimal point in it; a decimal does.
 -  Numbers may have any number of digits, subject to the limitation on
   line length. Only about seventeen significant figures will be retained,
   however (enough for all known applications).
 -  A non-zero number with no sign as the first character is assumed to be
   positive.

Notice that initial (before the decimal point and the first non-zero
digit) and trailing (after the decimal point and the last non-zero
digit) zeros are allowed but not required. A number written with
initial or trailing zeros will have the same value when it is read as
if the extra zeros were not there.

Numbers used for specific purposes in RS274/NGC are often restricted
to some finite set of values or some to some range of values. In many
uses, decimal numbers must be close to integers; this includes the
values of indexes (for parameters and carousel slot numbers, for
example), M codes, and G codes multiplied by ten. A decimal number
which is supposed be close to an integer is considered close enough if
it is within 0.0001 of an integer.

== Numbered Parameters [[sub:Numbered-Parameters]]

A numbered parameter is the pound character `#`  followed by an
integer between 1 and 5399. The parameter is referred
to by this integer, and its value is whatever number is stored in the
parameter.

A value is stored in a parameter with the = operator; for example "`#3
= 15` " means "set parameter 3 to 15." A parameter setting does not
take
effect until after all parameter values on the same line have been
found. For example, if parameter 3 has been previously set to 15 and
 the line "`#3=6 G1 x#3` " is interpreted, a straight move to a point
where x equals 15 will
occur and the value of parameter 3 will be 6.

The `#`  character takes precedence over other operations, so that, for
 example, "`#1+2` " means the number found by adding 2 to the value of
parameter 1, not
 the value found in parameter 3. Of course, `#[1+2]` does mean the
value found in parameter 3. The `#` character may be repeated; for
example `##2`  means the value of the parameter whose index is the
(integer) value
of parameter 2.

The interpreter maintains a number of readonly parameters for a loaded
tool:

 - #5400 tool number 
 - #5401 tool x offset 
 - #5402 tool y offset 
 - #5403 tool z offset 
 - #5404 tool a offset 
 - #5405 tool b offset 
 - #5406 tool c offset 
 - #5407 tool u offset 
 - #5408 tool v offset 
 - #5409 tool w offset 
 - #5410 tool diameter 
 - #5411 tool frontangle 
 - #5412 tool backangle 
 - #5413 tool orientation

== Named Parameters [[sub:Named-Parameters]]

Named parameters work like numbered parameters but are easier to read.
All parameter names are converted to lower case and have spaces and
tabs removed. Named parameters must be enclosed with `< >` marks.

`#<named parameter here>`  is a local named parameter. By default, a
named parameter is local to
the scope in which it is assigned. You can't access a local parameter
outside of its subroutine - this is so that two subroutines can use the
same parameter names without fear of one subroutine overwriting the
values in another.

`#<_global named parameter here>`  is a global named parameter. They
are accessible from within called
subroutines and may set values within subroutines that are accessible
to the caller. As far as scope is concerned, they act just like regular
numeric parameters. They are not stored in files.

Examples:

 - Declaration of named global variable

    `#<_endmill_dia> = 0.049` 

 - Reference to previously declared global variable

    `#<_endmill_rad> = [#<_endmill_dia>/2.0]` 

 - Mixed literal and named parameters

    ` o100 call [0.0] [0.0] [#<_inside_cutout>-#<_endmill_dia>] [#<_Zcut>]
    [#<_feedrate>]`



Notes:

The global parameters _a, _b, _c, ... _z have been reserved for
special use. In the future, they may provide access to the last Aword,
Bword, Cword, etc.

== System Parameters [[sec:System-Parameters]]

Two global read only named parameters are available to check which
version is running from G Code.

#<_vmajor>::
    Major package version. If current version was 2.6.2 would return 2.6.

#<_vminor>::
    Minor package version. If current version was 2.6.2 would return 0.2.

== Expressions [[sub:Expressions]]

An expression is a set of characters starting with a left bracket `[`
and ending with a balancing right bracket `]` . In between the brackets
are numbers, parameter values, mathematical
operations, and other expressions. An expression is evaluated to
produce a number. The expressions on a line are evaluated when the line
is read, before anything on the line is executed. An example of an
expression is `[1 + acos[0] - [#3 ** [4.0/2]]]`.

== Binary Operators [[sub:Binary-Operators]]

Binary operators only appear inside expressions. There are four basic
mathematical operations: addition (`+`), subtraction (`-`),
multiplication (`*`), and division (`/`). There are three logical
operations: non-exclusive or (`OR`), exclusive or (`XOR`), and logical
and (`AND`). The eighth operation is the modulus operation (`MOD`). The
ninth operation is the "power" operation (`**` ) of raising the number
on the left of the operation to the power on
 the right. The relational operators are equality (`EQ`), inequality
(`NE`), strictly greater than (`GT`), greater than or equal to (`GE`),
strictly less than (`LT`), and less than or equal to (`LE`).

The binary operations are divided into several groups according to
their precedence. (see table  <<cap:Operator-Precedence>>) If
operations in different precedence groups are strung together (for
example in the expression `[2.0 / 3 * 1.5 - 5.5 / 11.0]` ), operations
in a higher group are to be performed before operations
in a lower group. If an expression contains more than one operation
 from the same group (such as the first `/` and `*`  in the example),
the operation on the left is performed first. Thus,
 the example is equivalent to: `[[[2.0 / 3] * 1.5] - [5.5 / 11.0]]` ,
which is equivalent to to `[1.0 - 0.5]` , which is `0.5`.

The logical operations and modulus are to be performed on any real
numbers, not just on integers. The number zero is equivalent to logical
false, and any non-zero number is equivalent to logical true.

.Operator Precedence[[cap:Operator-Precedence]](((operator precedence)))

[width="90%", options="header"]
|========================================
|Operators | Precedence
|** | 'highest'
|* / MOD | 
|+ - | 
|EQ NE GT GE LT LE | 
|AND OR XOR | 'lowest'
|========================================

== Functions [[sub:Unary-Operation-Value]]

A function is either "`ATAN` " followed by one expression divided by
another expression (for
 example "`ATAN[2]/[1+3]`") or any other function name followed by an
expression (for example "`SIN[90]` "). The available functions are
shown in table  <<cap:Functions>>.
 Arguments to unary operations which take angle measures (`COS`, `SIN`,
and `TAN` ) are in degrees. Values returned by unary operations which
return
angle measures (`ACOS`, `ASIN`, and `ATAN`) are also in degrees.

.Functions[[cap:Functions]]

[width="90%", options="header"]
|========================================
|Function Name | Function result
|ATAN[Y]/[X] | Four quadrant inverse tangent
|ABS[arg] | Absolute value
|ACOS[arg] | Inverse cosine
|ASIN[arg] | Inverse sine
|COS[arg] | Cosine
|EXP[arg] | e raised to the given power
|FIX[arg] | Round down to integer
|FUP[arg] | Round up to integer
|ROUND[arg] | Round to nearest integer
|LN[arg] | Base-e logarithm
|SIN[arg] | Sine
|SQRT[arg] | Square Root
|TAN[arg] | Tangent
|EXISTS[arg] | Check named Parameter
|========================================

The `FIX`  function rounds towards the left (less positive or more
negative) on
 a number line, so that `FIX[2.8] =2` and `FIX[-2.8] = -3`, for
example. The `FUP`  operation rounds towards the right (more positive
or less negative)
on a number line; `FUP[2.8] = 3` and `FUP[-2.8] = -2`, for example.

The EXISTS function checks for the existence of a single named
parameter. It takes only one named parameter and returns 1 if it exists
and 0 if it does not exist. It is an error if you use a numbered
parameter or an expression.

== Repeated Items

A line may have any number of G words, but two G words from the same
modal group (see Section  <<sec:Modal-Groups>>) may not appear on the
same line.

A line may have zero to four M words. Two M words from the same modal
group may not appear on the same line.

For all other legal letters, a line may have only one word beginning
with that letter.

If a parameter setting of the same parameter is repeated on a line,
"`#3=15 #3=6` ", for example, only the last setting will take effect.
It is silly,
but not illegal, to set the same parameter twice on the same line.

If more than one comment appears on a line, only the last one will be
used; each of the other comments will be read and its format will be
checked, but it will be ignored thereafter. It is expected that putting
more than one comment on a line will be very rare.

== Item order

The three types of item whose order may vary on a line (as given at
the beginning of this section) are word, parameter setting, and
comment. Imagine that these three types of item are divided into three
groups by type.

The first group (the words) may be reordered in any way without
changing the meaning of the line.

If the second group (the parameter settings) is reordered, there will
be no change in the meaning of the line unless the same parameter is
set more than once. In this case, only the last setting of the
parameter will take effect. For example, after the line "`#3=15 #3=6` "
has been interpreted, the value of parameter 3 will be 6. If the
 order is reversed to "`#3=6 #3=15`" and the line is interpreted, the
value of parameter 3 will be 15.

If the third group (the comments) contains more than one comment and
is reordered, only the last comment will be used.

If each group is kept in order or reordered without changing the
meaning of the line, then the three groups may be interleaved in any
way without changing the meaning of the line. For example, the line
"`g40 g1 #3=15 (foo) #4=-7.0` " has five items and means exactly the
same thing in any of the 120
 possible orders (such as "`#4=-7.0 g1 #3=15 g40 (foo)`") for the five
items.

== Commands and Machine Modes

Many commands cause the controller to change from one mode to another,
and the mode stays active until some other command changes it
implicitly or explicitly. Such commands are called "modal". For
example, if coolant is turned on, it stays on until it is explicitly
turned off. The G codes for motion are also modal. If a G1 (straight
move) command is given on one line, for example, it will be executed
again on the next line if one or more axis words is available on the
line, unless an explicit command is given on that next line using the
axis words or canceling motion.

"Non-modal" codes have effect only on the lines on which they occur.
For example, G4 (dwell) is non-modal.

== Modal Groups (((Modal Groups)))[[sec:Modal-Groups]]

Modal commands are arranged in sets called "modal groups", and only
one member of a modal group may be in force at any given time. In
general, a modal group contains commands for which it is logically
impossible for two members to be in effect at the same time - like
measure in inches vs. measure in millimeters. A machining center may be
in many modes at the same time, with one mode from each modal group
being in effect. The modal groups are shown in Table 
<<cap:Modal-Groups>>.

.Modal Groups[[cap:Modal-Groups]]

[width="90%", cols="1,2", options="header"]
|========================================
|Modal Group Meaning | Member Words
|Motion | G0, G1, G2, G3, G33, G38.x, G73, G76, G80, G81
| | G82, G83, G84, G85, G86, G87, G88, G89
|Plane selection | G17.x, G18.x, G19.x
|Distance Mode | G90, G91
|Arc IJK Distance Mode | G90.1, G91.1
|Feed Rate Mode | G93, G94, G95
|Units | G20, G21
|Cutter Diameter Compensation | G40, G41, G42, G41.1, G42.1
|Tool Length Offset | G43, G43.1, G49
|Return Mode in Canned Cycles | G98, G99
|Coordinate System Selection | G54, G55, G56, G57, G58, G59, G59.1, G59.2, G59.3
|Control Mode | G61, G61.1, G64
|Spindle Speed Mode | G96, G97
|Lathe Diameter Mode | G7, G8
|Stopping | M0, M1, M2, M30, M60
|Tool Change | M6 Tn
|Spindle | M3, M4, M5
|Coolant | (M7 M8 can both be on), M9
|Override Switches | M48, M49
|Flow Control | O-
|Non-modal codes | G4, G10 G28, G30, G53 G92, G92.1, G92.2, G92.3, M1xx
|========================================

For several modal groups, when a machining center is ready to accept
commands, one member of the group must be in effect. There are default
settings for these modal groups. When the machining center is turned on
or otherwise re-initialized, the default values are automatically in
effect.

Group 1, the first group on the table, is a group of G codes for
motion. One of these is always in effect. That one is called the
current motion mode.

It is an error to put a G-code from group 1 and a G-code from group 0
on the same line if both of them use axis words. If an axis word-using
G-code from group 1 is implicitly in effect on a line (by having been
activated on an earlier line), and a group 0 G-code that uses axis
words appears on the line, the activity of the group 1 G-code is
suspended for that line. The axis word-using G-codes from group 0 are
G10, G28, G30, and G92.

It is an error to include any unrelated words on a line with `O-` flow
control.

== Comments

Comments can be added to lines of G code to help clear up the
intention of the programmer. Comments can be embedded in a line using
parentheses () or for the remainder of a line using a semi-colon. The
{'fraction_non_alphanumeric': 0.08788945378698997, 'fraction_numerical': 0.023934298005475165, 'mean_word_length': 4.024364684307048, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2237958', 'n_tokens_mistral': 11437, 'n_tokens_neox': 10542, 'n_words': 6226}