{{language|MoonRock
|exec=machine
|site=http://www.rowan.sensation.net.au/moonrock.html}}
{{language programming paradigm|Imperative}}
{{implementation|BASIC}}
'''MoonRock''' is a compiler for a [[BASIC]]-like programming language, developed by Rowan Crowe. Released for [[MS-DOS]] and compatible systems, MoonRock enables compilation to assembly language and linking to COM executable files. The compiler is known for producing exceptionally concise executables; for example, a "[[Hello world|Hello, World!]]" program can be compiled into a COM file as small as 276 bytes.

MoonRock is a programming language specifically designed to facilitate the creation of programs that can be easily translated into assembly language. This design choice emphasizes simplicity and direct mapping to low-level operations, making it particularly suitable for environments where performance and control over hardware are critical. The language's syntax and features are structured to align closely with assembly language constructs while benefiting from some higher-level abstractions provided by BASIC.

== MoonRock Version 0.50 ==
=== Program Structure ===
The definitions of arrays must be included within the <code>BEGIN DEF</code> section of the program.

=== Control Structures ===
The ''[[Loops/For|for]]'' loop requires that both the start and end values be variables, constants, or literals; expressions are not permitted. The language does not implement a ''[[Loops/Do-while|repeat-until]]'' loop.

=== Arithmetic Operations ===
MoonRock 0.50 does not support [[Floating point|floating-point numbers]] and operations. Division using the <code>/</code> operator produces rounded [[Arithmetic/Integer|integer]] results. For integer division, the <code>\</code> operator must be used. For example:
<syntaxhighlight lang="basic">
A% = 1 / 3
PRINT A% + "\n"
A% = 2 / 3
PRINT A% + "\n"
A% = 3 / 3
PRINT A% + "\n"
</syntaxhighlight>
produces the output:
<pre>
0
1
1
</pre>
The <code>MOD</code> function is not implemented for 32-bit integers (doublewords).

=== Comparison Operations ===
Both operands in comparison operations must be literals, constants, or variables; expressions are not allowed. Only variables of the same type can be compared. Attempting to compare variables of different types, such as <code>A& > B%</code>, may produce incorrect results.

=== Output ===
To output the value of a named constant, it must be passed as an argument to the <code>STR</code> function:
<syntaxhighlight lang="basic">
PRINT "Up to " + STR(%MaxNumber) + "\n"
</syntaxhighlight>
Formatted output is not supported. Users may create custom subroutines as a workaround, such as:
<syntaxhighlight lang="basic">
SUB PrintStr(S$, Width%)
  IF LEN(S$) > Width% THEN
    PRINT S$
  ELSE
    S$ = SPACE(Width%) + S$
    PRINT RIGHT(S$, Width%)
  ENDIF
END SUB
</syntaxhighlight>

=== Abnormal Behaviors ===
The manual states: ''Keywords are shown in all upper case to aid readability. They may be of any case in the source code''. However, some statements function solely when written in lowercase. For example, the statement:
<syntaxhighlight lang="basic">
if A@ = 0 then print B% + " "
</syntaxhighlight>
operates correctly only when <code>if</code> and <code>then</code> are in lowercase. Conversely, the following formulation works regardless of case:
<syntaxhighlight lang="basic">
IF A@ = 0 THEN 
  PRINT B% + " "
ENDIF
</syntaxhighlight>

== External links ==
* [https://github.com/DosWorld/moonrock MoonRock on GitHub]