Coral (zyBooks)
Paradigm(s) | imperative, procedural |
---|---|
Designed by | Frank Vahid, Roman Lysecky, Alex Edgcomb |
Appeared in | 2017 |
Type system | static |
Memory system | variable-based |
Dimensions | one-dimensional |
Computational class | Turing complete |
Reference implementation | https://corallanguage.org/simulator/, https://coral-sim.zybooks.com/ |
Coral is a educational programming language created by Frank Vahid, Roman Lysecky, and Alex Edgcomb for the courseware program zyBooks in 2017.[1] Coral is primarily web-based but supports transpilation to C++.[2] As well as generation of interactive flowchart execution diagrams.
Syntax
Coral's syntax is similar to the C family of imperative languages, with notable inspiration from Python especially. Note the indentation; Coral demarcates blocks with indentation of 3 spaces per level. Additionally, lines and statements are equivalent. One line cannot have multiple statements and one statement cannot span multiple lines.
The following is an overview of the syntax:[3]
Variable declaration
// Comments must be on their own line // Variables are always initialized to 0 // Initial values cannot be assigned on declaration // signed integer integer var // floating point value float var // Arrays type array(length) var // Unspecified array size type array(?) var // Must later be set to a valid size, done with var.size = expr // Arrays cannot be resized once a size is assigned
For later definitions, the valid forms of type are integer
, float
, integer array(n)
, float array(n)
, integer array(?)
, and float array(?)
.
Variables can later be assigned and changed with the syntax var = expr
with expr being an arithmetic expression containing variables, operators, function calls, and so on. A similar but different syntax arr_to = arr_from
allows for the contents of an array arr_from to be copied to arr_to. The arrays must have identical size, except if arr_to has an unspecified size.
The arithmetical operators are (by precedence); grouping, -
(unary minus), *
and /
and %
, +
and -
. Expressions are evaluated left to right. Elements of arrays can be indexed with var[expr]
. Arrays are zero indexed.
Built in functions
// Seed the RNG with the given seed // The seed may be an integer or float // floats will be truncated to integers by the reference implementation SeedRandomNumbers(seed) // Return a random number between min and max inclusive RandomNumber(min, max) // The following can take any numeric expression SquareRoot(x) RaiseToPower(base, exponent) AbsoluteValue(x)
Input and output (they must be in these forms)
var = Get next input Put expr to output Put expr to output with n decimal places
The argument to the first print statement form can be a literal string (enclosed in quotes like "string"
), or an arbitrary arithmetic expression. For the second form, the expression must be an arithmetic expression and the n must be an arithmetic expression which results in a nonnegative integer. The reference implementation further restricts n to be at most 16. The print expressions do not produce a newline at the end. Strings can use the escapes \n
, \t
, \\
, \"
. Other single character escapes do nothing and are removed from the string, and there are no multi character escapes.
Conditionals
// cond is a boolean expression (called a decision expression) // booleans are produced from the following operators: < > == != <= >= // booleans can be operated together with: and or not // comparisons have lower priority than addition and are in the same group // boolean operations have even lower priority and are in the same group // except for not which is higher than multiplication // note the 3-space indents if cond statements elseif cond statements ... else statements
Looping
while cond statements for var = expr; cond; var = expr statements
Function declaration
Function name(args) returns type var statements Function name(args) returns nothing statements
Where args is a comma separated list of declarations, type var, type var, ...
, which can be empty. The function can return a value or no value. All valid types can be used as parameters and return values, including arrays. Arguments are presented to the function body as regular variables. The return value is initialized at the start of the function as a regular variable and its value when the function reaches the end is the return value. Earlier returns cannot be performed as there is no return keyword.
All programs must have a Main() returns nothing
function. If there are no functions defined in a file, then the statements are implicitly set as the body of the Main
function.
Computational class
The specification does not demand bounds for the integer type. This allows for both unbounded counters and unbounded arrays in an idealized implementation, hence Turing completeness.
References
- ↑ About Coral. https://corallanguage.org/about/
- ↑ Advanced. https://corallanguage.org/advanced/
- ↑ Coral language specification. https://corallanguage.org/spec/