Compiler Documentation for
RScript
(v0.6.0)

Last updated: 2020-11-15


by Robert Rodgers

contactinfo


Table of contents



Script Syntax

Script Syntax



The script syntax is very similar to C and C++. Many keywords and operators have the same meanings, but there are a few differences. The following tables contain information on the supported syntax.

Operators, in order of precedence:

{}Code block.
' 'Literal; substitutes a character for its integer value.
""Constant string. Strings are concatenated where possible.
()Parenthesis.
[]Array indices.
.The dot is used for referencing elements in a structure.
,The comma separates function parameters. Used outside of function parenthesis, it is a sequential evaluation operator (i.e. "i=(g=sqrt(f),g+1);" means "g=sqrt(f); i=g+1;").
!Logical not; '!x' means 'x==0'.
++
--
Increment and decrement; '++x' will increment before value is used, 'x++' will increment after value is used.
^Power; 'x^y' means calculate 'x' to the power of 'y'.
*
/
Multiply.
Divide.
%Modulus; 'x % y' means calculate remainder of 'x' after division by 'y'. With the '%' operator, the result is always positive ('fmod(x,y)+|y|' for 'fmod(x,y)<0').
+
-
Add.
Subtract.
<
<=
>
>=
Less than.
Less than or equal to.
Greater than.
Greater than or equal to.
==
!=
Equal to.
Not equal to.
&&Logical and; 'x && y' is '1' if both values are non-zero.
||Logical or; 'x || y' is '1' if either value is non-zero.
^=
%=
*=
/=
+=
-=
Assignment with calculation. Uses the left hand side as the destination for the calculated value.
=Direct assignment to left hand side.


Keywords:

autoDefines a variable with local extent, using the stack. This is the default. Single variables are implicitly defined on their first use, but arrays must always be explicitly defined.
breakExits the loop it is used in.
continueContinues with the loop it is used in, skipping the code remaining.
doStandard implementation of a 'do' loop.
doubleUsed to define a variable as a double precision floating point number. This is the default type (and currently the only internal type), and may be omitted.
elseUsed with 'if'; specifies a block of code to execute if the condition at 'if' is false.
enumCan define any constant value, and definitions lacking an explicit assignment have the value of the previous constant plus one.
forStandard implementation of a 'for' loop.
gotoContinues execution at the specified label.
ifExecutes the following block of code if the specified condition is true.
returnEnds a function and returns to the caller, with an optional value. Zero is used if no value is supplied.
staticDefines a variable with static extent; it is initialized at compile time, and values are retained in the data section of the script. Can be used globally or locally.
structUsed to define a structure, which can be used as a type prefix for variables.
whileStandard implementation of a 'while' loop.


Internal library functions:
(Note: almost all of these are compiled inline.)

absAlias for 'fabs'. Provided for compatibility.
acosCalculates the inverse of a cosine.
asinCalculates the inverse of a sine.
atanCalculates the inverse of a tangent.
atan2Same as 'atan', but with two parameters forming the ratio.
ceilRounds to the lowest integer not less than the number.
cosCalculates the cosine.
expCalculates 'e^x'.
fabsCalculates the absolute value; |x|.
factCalculates the factorial. (Ken's function; fact(x) = gamma(x+1))
faddForces addition without interference from the optimizer. (Allows fast quantization tricks like: fadd(x,3*2^51)-3*2^51)
fdivFamily of 'fadd'.
floorRounds to the highest integer not more than the number.
fmodCalculates the floating point remainder; like '%', but can give a negative result if either parameter is negative.
fmulFamily of 'fadd'.
fsubFamily of 'fadd'.
intRounds the number towards zero.
logCalculates the logarithm base 'e'. An optional second parameter form can be used to specify the base.
log2Calculates the logarithm base 2.
maxCalculates the greater of two numbers.
minCalculates the smaller of two numbers.
nearRounds the number towards the nearest integer.
noiseContinuous noise function; can have one to three parameters. Original algorithm by Ken Perlin, implemented by Tom Dobrowolski, then further optimized for speed.
nrndReturns a normal random number (avg:0, std:1). Can be used as a variable.
powFunction version of the '^' operator.
rgbCalculates: 'clamp_int(r)*2^16 + clamp_int(g)*2^8 + clamp_int(b)'.
rndReturns a uniform random number in range: 0<=x<1. Can be used as a variable.
sgnReturns the sign of a number. A two parameter form can be used, where the value is compared to the second parameter instead of zero. Returns -1 for numbers less than zero, 0 for equal to zero, 1 for more than zero.
sinCalculates the sine.
sqrtCalculates the square root of a number.
srandProvides a seed for 'rnd', 'nrnd' and 'noise'.
tanCalculates the tangent.
unitSee 'sgn'. Returns: 0 for negative, .5 for zero, 1 for positive.


String support:

String support is limited, but with planned improvements coming up. For example:
   ()
   static a[4] = {"A","B","C","D"};
   static fmt = "Random: %s";
   if (mousx > 500) { fmt = "Hey!"; }
   cls(0);
   printf(fmt, a[rnd * 4]);
   refresh();
   sleep(1000);

Initialization syntax:

Static (and auto) variables can be initialized on declaration. This is supported for both single variables and arrays, and for both numeric and string variables. Arrays can also be partially initialized. The uninitialized elements will remain zero:

Other notes:


Known Bugs and Limits

Known Bugs and Limits


If you see a bug that is not mentioned here, please notify me via email. Make the subject title "RSCR BUG REPORT", and attach the script that caused the alleged error with a brief description of your observations.





Help me find and eliminate bugs, and your name will be mentioned here as well as the number of errors you have helped eliminate.

  1. Ken Silverman: > 40 (and counting...)
  2. Matheus Nabao: 5 (A really helpful beta tester - Thanks!)
  3. Torbjørn Haugen: 3
  4. Marcus B: 1

Note: The above tallies are for reported bugs that I have fixed. Fixes may take a considerable amount of time depending on my other workload, so if you have reported a bug (or more bugs than listed above), do not fear - I will eventually get to it. And thank you for helping to make RScript more stable!


Update History

Update History


When I started this compiler I also just started studying at a university. Now I work at that university and write software for them. As time allows, I will continue to patch bugs found in RScript. Even if it takes a while. ; )


Nov 2020 Return of the King: After 10 years, the compiler has finally received its long-awaited update.

Many accumulated bug reports were dusted off and actually read for a change, and their corresponding fixes were implemented. The foundation for better string support was also added. See the syntax reference page for more details on new features.
Feb 2010 Fixed a few crash bugs, as well as an internal corruption bug. Some modification was also done to increase the robustness of type checking.

Implicit type casting is done when the source and destination fields both have the same size, otherwise a "Type mismatch" error is generated.
Jan 2010 First public release!

Featuring integration with Ken Silverman's Evaldraw, and a fair amount of compatibility with existing Evaldraw scripts.

Bug reports and feature suggestions are welcome. My email address is on the index page.