Coral is a simple language for learning programming. Coral has a code (text) version and a flowcharts version, representing the same underlying language. This spec is for the code version. Coral’s spec is brief, because the language is simple, but includes the constructs taught in most introductory programming classes (variables, assignments, branches, loops, arrays, functions). Language design decisions are discussed on our philosophy page. See http://corallanguage.org for more.
Declaration and statement constructs
Construct |
Form |
Variable declaration |
[type] [identifier]
|
Input statement |
[var] = Get next input
|
Output statement |
Put [item] to output
|
Assignment statement |
[var] = [arithexpr]
|
Branch statements |
if [condexpr]
[substatements]
elseif [condexpr]
[substatements]
else
[substatements]
|
While loop statement |
while [condexpr]
[substatements]
|
For loop statement |
for [init]; [condexpr]; [update]
[substatements]
|
Declaration/statement examples and notes
Construct |
Examples and notes |
Variable declaration |
integer numCoins
float numInches
- Valid types: integer, float
- Identifier: Letter followed by any letters, numbers, or underscores
- Can only be at top of function
|
Input |
x = Get next input
|
Output |
Put "Hi all" to output
Put x to output
- Output item is usually a string literal or variable (but may be an arithmetic expression)
|
Assignment |
x = (y + z) / 2
- Left must be a variable, right an arithmetic expression. Arithmetic operators: +, -, *, /, %
- Precedence: (), unary -, * / %, + -, left-to-right
|
Branch |
if (y - z) < 10
Put "Close" to output
if x < 10
Put "Small" to output
elseif x < 20
Put "Med" to output
else
Put "Large" to output
- elseif is optional, any number OK
- else is optional, must be last
- Conditional expression’s operators: (), ==, !=, , =, and, or, not
- Precedence (high to low): (), not, * / % + -, < >=, == !=, and, or
|
While loop |
while userInput > 0
Put userInput to output
userInput = Get next input
|
For loop |
for i = 0; i < 4; i = i + 1
Put i to output
|
Other constructs
Construct |
Form |
Comments |
// [any text]
|
Arrays |
[type] array([size]) [identifier]
|
Functions |
Function [identifier]([parameters]) returns [type] [identifier]
[substatements]
|
Built-in math functions |
SquareRoot
RaiseToPower
AbsoluteValue
|
Built-in random functions |
RandomNumber
SeedRandomNumbers
|
More output features |
Put floatvar to output with 3 decimal places
|
Other constructs examples and notes
Construct |
Examples and notes |
Comments |
// Read data
x = Get next input
|
Arrays |
integer array(5) userNums
float array(5) userVals
x = userNums[3]
y = userNums.size
- Type is integer or float, size must be > 0
- Access uses [ ] notation. Indices are 0 to size-1
- userVals.size attribute available.
- size of ? OK, must set size before access. Ex:
integer array(?) userNums
userNums.size = 5
|
Functions |
Function FtInchToCm(float numFt, float numInch) returns float numCm
numCm = ((numFt * 12) + numInch) * 2.54
- 0 or more parameters. 2 or more separated by comma
- 0 or 1 return variable. If 0, use:
returns nothing
- Last value in return variable is returned
|
Built-in math functions |
SquareRoot(9.0) evaluates to 3.0
RaiseToPower(2.0, 4.0) evaluates to 16.0
x = AbsoluteValue(-15.2) evaluates to 15.2 |
Built-in random functions |
SeedRandomNumbers(5)
x = RandomNumber()
- Setting seed first is optional (default seed is 0)
- Seed typically only set once, at start of program (if at all)
|
Output |
float f
f = 99.13579
Put f to output with 3 decimal places
yields: 99.136 |
More language details
- All sub-statements are indented 3 spaces.
- Do not need to specify functions, in which case the program is implicitly in a Main function definition having no parameters and returning nothing. However, if any function is specified, then all code must be in functions, with Main explicitly defined. No global code allowed.
- Arithmetic type conversions
- For an arithmetic operator, if either operand is a float, the other is automatically converted to float, and then a floating-point operation is performed.
- For assignments, the right side type is converted to the left side type.
- integer-to-float conversion is straightforward: 25 becomes 25.0.
- float-to-integer conversion just drops the fraction: 4.9 becomes 4.
- Conditional type conversions are not allowed. Ex:
not 1
is not allowed.
Like this:
Like Loading...