Onecode (Fergusq)

From Esolang
Jump to navigation Jump to search

Onecode is a programming language created by User:Fergusq as a part of his Twocode programming language.

Statements

Onecode (one-dimensional code) is used as the one-dimensional part of Twocode. Pure onecode doesn't have any control structures as they must be implemented by the parent language. The "stand-alone" version of Onecode uses GOTO statements.

Syntax

Grammar

stmt := <expr> {";" <expr>}
expr := <logic>
logic := <cond> {("&"|"|"|"^") <cond>}
cond := <add> {("="|"<"|">") <add>}
add := <mul> {("+"|"-") <mul>}
mul := <prim> {("*"|"/") <prim>}
prim := "*" <prim_t> ["~" <expr>] | <var> ["~" <expr>] | <prim_t>
prim_t := "(" <expr> ")" | "*" <prim_t> | "!" <prim> | <function_call> | <value>
function_call := "getc" "(" ")" | "getn" "(" ")" | "putc" "(" <expr> ")" | "putn" "(" <expr> ")"
value := <number> | <var> | "'" <chr> "'" | <string> | "[" [<value> {"," <value>}] "]"

Operators

Precedence Operator Description Associativity
1 * Indirection (Unary) Right-to-left
! Logical NOT (Unary)
- Unary minus
2 * Multiplication Left-to-right
/ Division
3 + Addition Left-to-right
- Subtraction
4 = Equal Left-to-right
< Less than
> Greater than
5 & Logical AND Left-to-right
| Logical OR
^ Logical XOR
6 ~ Assigment Right-to-left

Assigments

In addition to a normal expression, rvalue may be a list or a string.

Lists are groups of expressions. They are used to change many memory slots at the same time.

i~2;        // Memory : [0,0,0,0,0,0,0,...]
*i~[1,2,3]; // Memory : [0,0,1,2,3,0,0,...]

Strings are similar to list, but contain the null terminator automatically:

s~2;
*s~"Hi!";   // Memory: [0,0,'H','i','!',0,0,...]

Statements

The only statement in Onecode is the expression statement. In addition to that the stand-alone version provides following statements:

  1. Label statement: label_name:
  2. Goto statement: goto label_name;
  3. If statement: if (expr) goto label_name;

Sometimes else-keyword is used after the if statement, but it is not required.

Computational class

Computational class depends on the parent language. The stand-alone version and Twocode are Turing complete.

Examples

Hello world

s~0;
*s~"Hello, world\n";
print:
putc(*s);
s~s+1;
if (*s) goto print;

Truth-machine

i~getc();
if (i='1') goto truth;
if (i='0') goto lie;
goto other;
truth:
putc(i);
goto truth;
lie:
putc(i);
other: