-
Notifications
You must be signed in to change notification settings - Fork 2
Language Reference
Variables must be declared before they can be used. Declarations comprise a type followed by the variable identifier.
<typename> <identifier>
Examples:
int i
real r
text t
list a
C.run currently handles the following data types:
-
int
- for integer values (e.g. 1,2,-3) -
real
- for floating point numbers (e.g. -3.14, .10, 1.0) -
text
- for character strings (e.g. "See Spot Run") -
list
- for heterogeneous arrays (e.g. [1, 3.14, "Spot", ["Nested", "List"]])
Variables generally follow the C-style variable id convention: A letter or underscore followed by any number of letters/underscores/numbers:
[a-zA-Z_][a-zA-Z_0-9]*
In other words: variable names cannot start with a digit.
Example:
int myVar
int myVar02
int _02_myVar
Values can be assigned to variables during declaration (as an initialization) or after declaration, using an assignment statement.
To initialize a variable during declaration just append the assignment operator and value to the end of a declaration:
<typename> <identifier> = <value>
int i = 3
real r = 355 / 113
text t = "See Spot Run!"
list a = [i, 3.14159, t, "Go Spot!"]
The assignment statement proper begins with the let
keyword:
let <identifier> = <value>
Example:
text name
let name = "Spot"
Some variable types have built-in methods. They are called using the following syntax:
<id>.<method>
Note: Methods don't use empty parentheses. Only parameterized methods have parentheses.
Variables of type text
have some useful built-in methods:
length
lowercase
reverse
uppercase
Example:
text t = "Spot."
print t # Outputs: Spot.
print t.length # Outputs: 4
print t.lowercase # Outputs: spot.
print t.reverse # Outputs: .topS
print t.uppercase # Outputs: SPOT.
Simple output is handled via the print
statement. It's composed of the keyword print
followed by a string expression.
print <print string>
Example:
# Simple string:
# Output: See Spot catch the frisbee!
print "See Spot catch the frisbee!"
# Concatenated string expression:
# Output: See Spot play piano.
text activity = "play piano"
print "See Spot "+ activity + "."
As C.run is a web-based language, input is handled through 'prompt' dialog pop-ups (like alerts, with an input field). Therefore, C.run input is handled through the prompt
expression, which returns the user's input.
prompt <prompt string>
Example:
# Using the prompt expression in an assignment:
# User input is coerced to an integer value to match variable type.
int i = prompt "Please enter a number:"
At the time of writing, C.run has very few control structures implemented, including the if
statement, the if
-else
statement, and the while
loop.
Basic conditional execution is handled with the if
statement. It has the following form:
if <condition>
<statements>
end if
Example:
if 2 > 1
print "I thought it was."
end if
The if
-else
structure adds an else
clause to the if
statement, making its form:
if <condition>
<statements>
else
<statements>
end if
Example:
text s = "Spot"
if s = "Fido"
print "Wrong dog!"
else
print "You found her!"
end if
Note: While there are plans for a simplified else if
statement structure, it has not yet been implemented.
This construct is in lieu of a for
loop (Thanks to Andy Stefik for revealing how absolutely not intuitive the keyword for
is.) The while-to construct allows the user to initialize a variable and a range; it then auto-increments (if ending bound is greater than beginning bound) or auto-decrements (if ending bound is less than beginning bound). If the bounds are equal, it throws a syntax error.
Note: If a counting loop is initialized with a previously undeclared variable, it will automatically be declared and initialized in the symbol table as an integer.
while <id> = <expr> to <expr>
<statements>
repeat
Example:
# Total output: 1, 2, 3, 4, 5, 6
# Outputs: 1, 2, 3, 4, 5,
while i = 1 to 5
print i + ", "
repeat
# Outputs: 6
print i
# Outputs: 5 4 3 2 1
while i = 5 to 1
print i + " "
repeat
Traditionally, loops are handled by a simple while
statement:
while <condition>
<statements>
repeat
Example:
# Iterative loop:
int i = 0
while i < 3
print i
let i = i + 1
repeat
# Using input for a sentinel value:
text again = "Y"
while again = "Y"
print "You said again!"
let text = prompt "Do it again?"
repeat
C.run has support for rudimentary procedures. Note: At this time, all variables are in the global scope.
Procedures are defined with the proc
keyword:
proc <procedure name>:
<statements>
end proc
Note: the colon (':') following the <procedure name> is likely to be deprecated in upcoming releases.
Example:
proc fetch:
print "See Spot fetch the ball!"
end proc
Procedures are invoked (called) using the do
keyword:
do <procedure name>
Example:
# This loop calls the 'fetch' procedure (defined above) 3 times.
int i = 0
while i < 3
do fetch
repeat