Counting
Counting is a language invented by User:A whose control flow only contains when blocks.
Concept
The only mutable values is the instruction count and the accumulator. The instruction count increments after every executed when block (skipped blocks aren't counted) and starts at 0. It cannot be assigned. (The counter is incremented every time before the execution of the when block.)
The accumulator, initialized at 0, can only be added by the (potentially processed) instruction count. The accumulator cannot be subtracted or assigned by the instruction count, and so on.
Even if none of the conditions are fullfilled, the language continues to execute without stopping, as well as incrementing the counter by 1. You can use halt
to stop the program.
The following piece of pseudocode shall elucidate the process with a more formal diction:
instructionCount ← 0 { "cnt" variable, or counter. } accumulator ← 0 { "acc" variable. } repeat do hasMatchedNoWhenBlock ← true for whenBlock in whenBlocks do if predicate(whenBlock) is satisfied then execute actions(whenBlock) instructionCount ← instructionCount + 1 hasMatchedNoWhenBlock ← false end if end for if hasMatchedNoWhenBlock then instructionCount ← instructionCount + 1 end if end repeat
Data Types
Three species of objects are capable of distinguishment: Boolean values, integers, and strings.
The Boolean category participates in an indirect manner only, yielded by the application of relational operators, and restricted in the perimeter of its influence to the when block predicate.
Integers occupy the highest echolon in terms of significance, with their presence ubiquitous in all aspects of the language. Generally permitted to inhabit an unbounded signed range, the case of the instruction count assumes only values from the non-negative axis.
Strings, demarcated by double quotation marks, "
, are admitted as currencies of communication, accommodated as such for output purposes only, and may tally an arbitrary account of characters.
Commands
The Counting programming language offers a treble of statements:
Statement | Effect |
---|---|
halt
|
Immediately terminates the program. |
out expression
|
Prints the expression to the standard output, succeeded by a single newline character.
|
read variable
|
Queries the standard input for a signed or unsigned integer number and stores the same in the variable . In the current language rendition, merely the accumulator, acc , is homologated as a target.
|
The arithmetic sign and logical NOT operators intrine to form the unary operation set:
Operator | Operand | Effect |
---|---|---|
+
| ||
Boolean | invalid | |
integer | plus sign | |
string | invalid | |
-
| ||
Boolean | invalid | |
integer | minus sign | |
string | invalid | |
!
| ||
Boolean | logical NOT | |
integer | invalid | |
string | invalid |
The following binary operations, with respect to the possible operand type combinations, exist:
Operator | Left operand | Right operand | Effect |
---|---|---|---|
+
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | addition | |
integer | string | string concatenation | |
string | Boolean | invalid | |
string | integer | string concatenation | |
string | string | string concatenation | |
-
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | subtraction | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | invalid | |
*
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | multiplication | |
integer | string | string repetition | |
string | Boolean | invalid | |
string | integer | string repetition | |
string | string | invalid | |
/
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | division | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | invalid | |
%
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | remainder | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | invalid | |
^
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | exponentiation, power | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | invalid | |
&&
| |||
Boolean | Boolean | logical AND | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | invalid | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | invalid | |
||
| |||
Boolean | Boolean | logical OR | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | invalid | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | invalid | |
==
| |||
Boolean | Boolean | equality | |
Boolean | integer | equality | |
Boolean | string | equality | |
integer | Boolean | equality | |
integer | integer | equality | |
integer | string | equality | |
string | Boolean | equality | |
string | integer | equality | |
string | string | equality | |
!=
| |||
Boolean | Boolean | inequality | |
Boolean | integer | inequality | |
Boolean | string | inequality | |
integer | Boolean | inequality | |
integer | integer | inequality | |
integer | string | inequality | |
string | Boolean | inequality | |
string | integer | inequality | |
string | string | inequality | |
<
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | less than | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | less than (lexicographic) | |
<=
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | less than or equal to | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | less than or equal to (lexicographic) | |
>
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | greater than | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | greater than (lexicographic) | |
>=
| |||
Boolean | Boolean | invalid | |
Boolean | integer | invalid | |
Boolean | string | invalid | |
integer | Boolean | invalid | |
integer | integer | greater than or equal to | |
integer | string | invalid | |
string | Boolean | invalid | |
string | integer | invalid | |
string | string | greater than or equal to (lexicographic) |
Comments
Comment lines may be inserted at any location in the code, introduced via the percentage sign %
; however, they are restricted to commence a line of their own, preceded only, if at all, by spaces, forecause the %
token conflates with the arithmetic remainder operator, and as thus would render the language ambiguous.
A line, for instance, following the pattern
acc += cnt % 5
could both denote a division with remainder, or a comment immediately following the cnt
variable.
Examples
Infinite counter
when cnt > 0 out cnt
Range from 2 to 100.
when cnt > 100 halt when cnt >= 2 out cnt
Count up with a step of 2, starting with 2.
when cnt % 3 == 2 acc += 2 out acc
Digital root calculator
when cnt - 1 == 1 + (acc - 1) % 9 out cnt - 1 halt
Line-based cat program
when 1 read acc out acc
Hello, World!
when 1 out "Hello, World!" halt
Truth-machine
when cnt == 0 read acc out acc when acc == 0 halt when acc == 1 out acc
Fibonacci sequence
when cnt == acc - cnt out acc acc += cnt acc += cnt when cnt < acc - cnt % Pass. when cnt == acc - cnt acc += 0 - cnt
Add Two Numbers
when cnt == 0 read acc when cnt == acc read acc out acc + cnt halt
Triangular Numbers
Outputs in unary.
when cnt == 0 read acc when cnt <= acc out "*" * cnt when cnt > acc halt % 1 + 2 + ... + acc
Factorial Sequence
when cnt == 0 read acc when cnt < acc acc += 0 - acc + acc * cnt out acc
99 Bottles of Beer
This program prints the lyrics to the song “99 Bottles of Beer”:
% Bottles in range [3, 99]. when (99 - cnt) > 2 out (99 - cnt) + " bottles of beer on the wall," out (99 - cnt) + " bottles of beer." out "Take one down, pass it around," out (99 - cnt - 1) + " bottles of beer on the wall." out "" % Two bottles. when (99 - cnt) == 2 out (99 - cnt) + " bottles of beer on the wall," out (99 - cnt) + " bottles of beer." out "Take one down, pass it around," out (99 - cnt - 1) + " bottle of beer on the wall." out "" % One bottle. when (99 - cnt) == 1 out (99 - cnt) + " bottle of beer on the wall," out (99 - cnt) + " bottle of beer." out "Take one down, pass it around," out "No bottles of beer on the wall." halt
Interpreter
- Common Lisp implementation of the Counting programming language.