Category:Evaldraw

From Rosetta Code
Language
Evaldraw
This programming language may be used to instruct a computer to perform a task.
Execution method: Compiled (machine code)
Garbage collected: No
Parameter passing methods: By value
Type safety: Unsafe
Type strength: Weak
Type checking: Static
Lang tag(s): c
See Also:


Listed below are all of the tasks on Rosetta Code which have been solved using Evaldraw.


EVALDRAW is a quirky little programming tool that can be used to make small applications and games, or even do 1D, 2D or 3D graphs, or create custom musical instruments. Evaldraw is technically a tool and not the language, however, since RSCRIPT and Evaldraw are tied together, we can consider Evaldraw a programming language in itself. From this point, this article will refer to the capabilities of RSCRIPT and Evaldraw together.

Compiler modes

  • EVAL the initially bundled compiler, developed by Ken Silverman. Had only doubles and arrays of doubles.
  • RSCRIPT added in 2010 and created by Robert Rodgers. Added support for structs, making entities to handle. Easier to translate to C.

Overview

Evaldraw has syntax similar to C.

Curly Braces

EVALDRAW supports grouping multiple statements in curly braces. They are not needed for single statements.

Semicolons

All statements, except for the last statement in a function must end with a semicolon;

Functions

Functions all return an implicit double value from the last statement in the function. Parameters passed to functions can be doubles, arrays or structs.

Statements

The following statements are supported:

  • IF and IF-ELSE (but no SWITCH-case)
  • Loop constructs such as DO{ codeblock }WHILE(condition); WHILE(cond){ block } and the FOR-loop as in C.
  • goto label and label: declaration similar to C.
  • a RETURN statement can at any point in a function return a floating point number. The last value is implicitly returned.
  • BREAK and CONTINUE statements. Loops can be exited with BREAK, or remainder of code body can be skipped with CONTINUE.
  • ENUMs allow the programmer to give numerical constants names. Dependencies between enum values are possible, allowing the programmer to make one enum a calculation based on previous enum values.
  • Variables can be declared automatically on the local stack, or using the STATIC modifier to make a variable global.

Case insensitive

All names and keywords are case insensitive. This can create some confusion when porting from- or to C.

All you need is 64-bit float

All variables, also those declared in structs are double. If no type is supplied, double is assumed. All functions return a double.

Arrays

Arrays that are power of two are wrapped, non power-of-two arrays set index zero if accessed out of bounds. For example, if we define static xcoords[4]; and try to access xcoords[5] we the returned value is at index 5%4 which is xcoords[1].

Structs

Structs can be declared and be passed as function parameters. A struct may doubles, arrays of doubles and other structs.

For example struct vec{x,y,z;}; defines a struct with 3 fields. struct boid_t{ vec pos; vec dir; } defines a struct that contains substructs pos and dir.

Pass by value or pass by ref

Doubles are passed by value, but can be passed by ref by prefixing a & symbol. All other variables, arrays and structs are passed by ref.

Builtins

There are many built in functions. The EVAL and RSCRIPT compiler modes support the following 1-param functions:

  • ABS - gives the absolute value. Same as fabs in C.
  • ACOS - arcos in radians
  • ASIN - arsin in radians
  • ATAN - arctan in radians
  • ATN - alias for atan
  • CEIL - round a value up to nearest integer.
  • COS - cosine in radians
  • EXP - raise Eulers number to some power. EXP(1) = 2.718281
  • FABS - alias for ABS
  • FACT - factorial. FACT(5) = 5!
  • FLOOR - round a value down to nearest integer.
  • INT - different from floor, truncates (removes decimals).
  • LOG - returns the natural logarithm of x. LOG(x) = ln(x)
  • SGN - returns the sign of a number. -1, 0 or +1
  • SIN - sine in radians
  • SQRT - square root
  • TAN - tan in radians
  • UNIT - returns the unit function of x. Returns 0.0 for x<0, 0.5 for x==0 and 1.0 for x>0.

And the following two param functions:

  • ATAN2(x,y) - gives angle in radians from positive x-axis to some point x,y.
  • FADD - eval and rscript compatibility.
  • FMOD - alternative to modulus % operator.
  • LOG(x,b) - return log of x with base b.
  • MIN(a,b) - return smallest
  • MAX(a,b) - return largest
  • POW(a,b) - raise a to power b

There is no concept of include statements or libraries. It is assumed evaldraw programs are small scripts to try out ideas that later will be integrated into a professional programming language such as C.