OCOO

From Esolang
Jump to navigation Jump to search

OCOO is an esoteric programming language designed by User:Trap-representation. OCOO is an acronym for One Character One Operation.

This is not a normative reference for OCOO. OCOO has a work-in-progress specification, a link to which will be put here once it's completed.

OCOO only has two operators: the + operator, which operates on blocks, and the ; operator, which modifies the block pointer (BP).

Concept

Operations are performed in the order they are present in the program, unless the control is explicitly transferred to somewhere else. Every operation in OCOO affects either one or (implicitly) more blocks, or the BP.

There are 11 blocks in an implementation of OCOO: BLOCK_OPERAND1, BLOCK_OPERAND2, BLOCK_SWAP, BLOCK_SIGN, BLOCK_ZERO, BLOCK_JUMP, BLOCK_STORE, BLOCK_LOAD, BLOCK_NULL, BLOCK_IMPL1, BLOCK_IMPL2. Among the aforementioned blocks, only BLOCK_OPERAND1, BLOCK_OPERAND2, BLOCK_SIGN, BLOCK_IMPL1, and BLOCK_IMPL2 (called the storage blocks) can store positive numeric values that can be used for computation. Each storage block is also set to 0 before program startup. The maximum value that can be stored in a storage block is implementation-defined, except that the value should be at least greater than or equal to the decimal value 65535. BLOCK_OPERAND1 must be able to represent any value that can be represented by BLOCK_OPERAND2, and vice versa.

There is also a BF-like tape that should have at least 30000 cells, and each cell should be able to represent any value designated by BLOCK_OPERAND1.

The + operator operates on the block pointed to by the BP. It has the following semantics:

  • If the + operator is applied on BLOCK_OPERAND1 and BLOCK_SIGN is set to 0, the value in BLOCK_OPERAND1 is incremented by 1; otherwise, if BLOCK_SIGN is set to 1, the value in BLOCK_OPERAND1 is decremented by 1. If the result is not in the range of values that can be represented by BLOCK_OPERAND1, it shall wrap around.
  • If the + operator is applied on BLOCK_OPERAND2 and BLOCK_SIGN is set to 0, the value in BLOCK_OPERAND1 is incremented by 1; otherwise, if BLOCK_SIGN is set to 1, the value in BLOCK_OPERAND2 is decremented by 1. If the result is not in the range of values that can be represented by BLOCK_OPERAND1, it shall wrap around.
  • If the + operator is applied on BLOCK_SWAP, the values in blocks BLOCK_OPERAND1 and BLOCK_OPERAND2 are swapped.
  • If the + operator is applied on BLOCK_SIGN, and BLOCK_SIGN is 0, BLOCK_SIGN1 will have the value 1. Otherwise, if BLOCK_SIGN is 1, it will have the value 0.
  • If the + operator is applied on BLOCK_ZERO, BLOCK_OPERAND1 is set to the value 0.
  • If the + operator is applied on BLOCK_JUMP, and BLOCK_SIGN is 0, the program control jumps forward n operations, as long as BLOCK_OPERAND2 is not equal to 0 (where n shall equal the value in BLOCK_OPERAND1). If BLOCK_SIGN is 1, the program control jumps backward n operations, as long as BLOCK_OPERAND2 is not equal to 0. After the operation completes, BLOCK_OPERAND1 is set to 0. If the program control fails to jump to the specified location, the behavior is undefined.
  • If the + operator is applied on BLOCK_STORE, the BLOCK_OPERAND2th cell of the tape is set to the value of BLOCK_OPERAND1.
  • If the + operator is applied on BLOCK_LOAD, the value of the BLOCK_OPERAND2th cell of the tape is loaded into BLOCK_OPERAND1. If the BLOCK_OPERAND2th cell of the tape is unset, the behavior is undefined.
  • If the + operator is applied on BLOCK_NULL, there shall be no visible side effects.
  • If the + operator is applied on BLOCK_IMPL1, its value shall increment by 1. If the result is not in the range of values that can be represented by BLOCK_IMPL1, it shall wrap around.
  • If the + operator is applied on BLOCK_IMPL2, its value shall increment by 1. If the result is not in the range of values that can be represented by BLOCK_IMPL2, it shall wrap around.

No specific interpretation of the values in BLOCK_IMPL1 and BLOCK_IMPL2 is specified. It is implementation-defined how (if) specific values in BLOCK_IMPL1 and BLOCK_IMPL2 affect the behavior of a program.

At startup, the BP should point to BLOCK_OPERAND1. Each successive invocation of the ; operator should modify the BP to point to BLOCK_OPERAND2, BLOCK_SWAP, BLOCK_SIGN, BLOCK_ZERO, BLOCK_JUMP, BLOCK_STORE, BLOCK_LOAD, BLOCK_NULL, BLOCK_IMPL1, and BLOCK_IMPL2 (in order). If the BP points to BLOCK_IMPL2, an invocation of the ; operator shall modify the BP to point back to BLOCK_OPERAND1.

Syntax

A colon (:) following a non-terminal introduces its definition. Non-terminals are written in italics. Alternative definitions are listed on separate lines. An optional symbol is indicated by the suffix (opt).

Lexical grammar

token:
+
;

Phrase structure grammar

translation-unit:
operator-sequence

operator:
+
;

operator-sequence:
operator operator-sequence (opt)

Examples

NOTE: In the following examples, anything other than the + and ; constitute comments, and does not affect the behavior of the program in any way.

Implementation considerations

It would have been impossible to, for example, write to the standard output without resorting to implementation- and environment-specific stuff. This is why the following examples assume:

  • the execution environment uses the usual ASCII codes to represent members of the execution character set
  • the implementation defines that if BLOCK_IMPL1 is set to 2 and BLOCK_IMPL2 has the value 1, a character is read from the standard input and the corresponding numeric representation of the character is stored in BLOCK_OPERAND1
  • the implementation defines that if both BLOCK_IMPL1 and BLOCK_IMPL2 are set to 1, the character corresponding to the numeric value in BLOCK_OPERAND1 is written to the standard output

Programs

A program that receives a character from the standard input, and writes the input character back to the standard output, followed by a newline character:

;;;;;;;;;++        store the integer 2 in BLOCK_IMPL1
;+                 store the integer 1 in BLOCK_IMPL2

                   the + operator when applied to BLOCK_IMPL1 and BLOCK_IMPL2 produces implementation-defined results
                   in this case, when BLOCK_IMPL1 has the value 2 and BLOCK_IMPL2 has the value 1, a character from stdin is read, which is then stored in BLOCK_OPERAND1

                   BLOCK_IMPL1 and BLOCK_IMPL2 are now imlpicitly set to 0

;;;;;;;;;;+        set BLOCK_IMPL1 to 1
;+                 set BLOCK_IMPL2 to 1
                   this writes the value stored in BLOCK_OPERAND1 to stdout

                   the newline character can be represented by the integer 10

;;;;;+             apply the + operator on BLOCK_ZERO to set BLOCK_OPERAND1 to 0
;;;;;;;++++++++++  set BLOCK_OPERAND1 to 10
;;;;;;;;;+         set BLOCK_IMPL1 to 1
;+                 set BLOCK_IMPL2 to 1
                   this will write a newline character to stdout

A program that writes the words "Hello, World!" followed by a newline character to the standard output:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ H
;;;;;;;;;+;+
;;;;;+;;;;;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e
;;;;;;;;;+;+
;;;;;+;;;;;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ l
;;;;;;;;;+;+
;;;;;+;;;;;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ l
;;;;;;;;;+;+
;;;;;+;;;;;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o
;;;;;;;;;+;+
;;;;;+;;;;;;;++++++++++++++++++++++++++++++++++++++++++++ ,
;;;;;;;;;+;+
;;;;;+;;;;;;;++++++++++++++++++++++++++++++++ the space character
;;;;;;;;;+;+
;;;;;+;;;;;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ W
;;;;;;;;;+;+
;;;;;+;;;;;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o
;;;;;;;;;+;+
;;;;;+;;;;;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ r
;;;;;;;;;+;+
;;;;;+;;;;;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ l
;;;;;;;;;+;+
;;;;;+;;;;;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ d
;;;;;;;;;+;+
;;;;;+;;;;;;;+++++++++++++++++++++++++++++++++ !
;;;;;;;;;+;+
;;;;;+;;;;;;;++++++++++ the newline character
;;;;;;;;;+;+

External resources