Easy

From Esolang
Jump to: navigation, search

Easy is an esoteric programming language based on Brainfuck. Unlike most programming languages, all actual code-writing is done at runtime, hence the author's joke that "it is extremely easy to write programs in Easy".

The instructions are mostly the same as those in Brainfuck (actually, input and output are switched), but with an added instruction, :, which lets you add code to the end of the program. The : itself cannot be added to a program, and there is always only one, at the end of the program.

: supplies the only way to end a program: if you type anything that is not a valid Brainfuck instruction (: is not considered valid), the program ends immediately.

The input is the program, which contains both instructions and data interwined. When executing, the interpreter reads one symbol or whole block from the input and executes it. . reads another symbol and puts it into the current cell. , writes a symbol from the current cell to the output.

Examples

Hello world example

The same as in Brainfuck

>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]
<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.

Note that, of course, this is not actual "source code" but rather the input to the program which will produce the output "Hello World!"

Another way

+[.,]Hello, world!

Here the + and [.,] are read and starts to execute. On each iteration in the loop, . reads another symbol (H and ahead), then , prints it. When the loop is finished (e.g. EOF is read), it tries to execute the next instruction. But since it has already exhausted the input, there is no more instructions, and the program terminates.

A third way

.H,.e,.l,.l,.o,.,,.w,.o,.r,.l,.d,.!,

Similar to the previous, but without a loop. Input needs to be provided after each . if executed at the top level.

Quine

+[->.[,>.]<[<]>[,>]]+[->.[,>.]<[<]>[,>]]

(by User:MizardX)

Implementation

C

/*****************************************************************************
Easy Interpreter.

This is not exactly as in the 'specification'. I have improved
compatibility with Brainfuck by switching the the input and output
characters back to 'normal' and altered the ':' command so that it
ignores any non-command character.

If a file is specified on the command line the program will therefor
execute this as a normal brainfuck program. If no file is specified easy
commands (and data) will be read from the standard input.

Because of the brainfuck compatibility newlines will not terminate the
program; instead you must give the ':' command an EOF indication.
(or abort the program with too many ']' commands)

Errors are indicated with the command return status as normal.

As a brainfuck interpreter this implementation has a limited tape of
65536 cells each of which is 16bits, both cells and tape wrap.
EOF uses the read() convention. This interpreter is slow.

This input is "Hello World!"

++++++++++[>+++++++>++++++++++>+++>+<<<<-]
>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

As is this:

,H[.[-],e]llo World!

*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>

FILE *f;
char pgm[4000000];
unsigned short m, mem[65536];

char * bf(char * p, int r) {
    char * s;
    for(;;) {
        switch(*p) {
        case ':': if((*p = getc(f)) == EOF) exit(r); p[1]=':'; continue;
        case '+': mem[m]++; break;
        case '-': mem[m]--; break;
        case '>': m++; break;
        case '<': m--; break;
        case '.': putchar(mem[m]); break;
        case ',': {int a=getchar(); if(a!=EOF) mem[m]=a;} break;
        case ']': return p;
        case '[':
            s=p+1;
            if(mem[m]) do p=bf(s,1); while(mem[m]);
            else {
                int b=0;
                for(p++;*p!=']'||b;) {
                    if (*p!=':') { b += (*p=='[')-(*p==']'); p++; }
                    else if((*p = getc(f)) == EOF) exit(2); else p[1]=':';
                }
            }
            if(!r) { *(p=pgm)=':'; continue; }
            break;
        }
        p++;
    }
}

int main(int argc, char**argv)
{
    if(argc==1) f=stdin; else if(!(f=fopen(argv[1],"r"))) exit(4);
    setbuf(stdout, 0); *pgm=':'; bf(pgm,0); exit(3);
}

(by User:rdebath)

External resources