Element

From Esolang
Jump to: navigation, search

Element is a simple language by PhiNotPi. It is supposedly designed as "very compact and human-readable". Each instruction is one character long and performs a single function.

Element has two stacks and a hash as memory structures. The two stacks are called the main stack and the control stack. The main stack is where arithmetic, I/O, and hash manipulation occurs. The control stack is where logic operations occur, and it also controls the while and for loops.

Documentation

   OP    the operator.  Each operator is a single character
   STACK tells what stacks are affected and how many are popped or pushed
         "o" stands for "other effect"
   HASH  tells if it involves the hash
   x & y represent two values that are already on the stack, so the effect of
         the operator can be more easily described
         
   OP     STACK  HASH   DESCRIPTION
   text     ->m         --whenever a bare word appears, it pushes that string onto 
                          the main stack
   _       o->m         --inputs a word and pushes onto main stack
   `       m->o         --output.  pops from main stack and prints
   xy;    mm->    yes   --variable assignment.  the top stack element y is assigned 
                          the value x
   ~       m->m   yes   --variable retrieval.  pops from main stack, pushes contents 
                          of the element with that name
   x?      m->c         --test. pops x and pushes 0 onto control stack if x is '0' or 
                          an empty string, else pushes 1
   ><=     m->c         --comparison. pops two numbers off of stack and performs 
                          test, pushes 1 onto control stack if true and 0 if false
   '       m->c         --pops from main stack and pushes onto control stack
   "       c->m         --pops from control stack and pushes onto main stack
   &|     cc->c         --AND/OR. pops two items from control stack, performs and/or 
                          respectively, and pushes result back onto control stack
   !       c->c         --NOT. pops a number off of control stack, pushes 1 if 0 or 
                          empty string, 0 otherwise
   []       c           --FOR statement (view the top number number from control stack 
                          and eval those many times)
   {}       c           --WHILE (loop until top number on control stack is 0, also 
                          does not pop)
   #       m->          --discard. pops from main stack and destroys
   (       m->mm        --pops from main stack, removes first character, pushes the 
                          remaining string onto stack, and pushes the removed character 
                          onto stack
   )       m->mm        --pops from main stack, removes last character, pushes the 
                          remaining string onto stack, and pushes the removed character 
                          onto stack
   +-*/%^ mm->m         --arithmetic. pops two most recent items, adds/negates
                          /multiplies/divides/modulates/exponentiates them, and places 
                          the result on the stack 
   xy@    mm->o         --move. pops x and y and moves xth thing in stack to move to 
                          place y in stack
   x$      m->m         --length. pops x and pushs length of x onto the stack
   xy:    mm->o         --duplication. pops x and y and pushes x onto the stack y times
   xy.    mm->m         --concatination. pops x and y and pushes x concatonated with y
   \        o           --escapes out of next character, so it isn't an operator and can
                          be pushed onto the stack
   ,      m->mm         --character conversion. pops from main stack, coverts it to char
                          and pushes, and converts to num and pushes
   Newlines and spaces separate different elements to be pushed 
   onto the stack individually, but can pushed onto the stack using \

Examples

Hello World

Hello\ World\!`

Cat program

One line of input:

_`

In an infinite loop:

!{_`}

Nth Fibonacci number

1_'[3:~2@+]`

Explanation:

1                  push a 1
 _'                take input then move it to the control stack
   [      ]        FOR loop
   [3:    ]        make two additional copies of the top number (3 is the total count)
   [  ~   ]        turn one copy into a zero
   [   2@ ]        move from position 0 to position 2, behind the old number
   [     +]        add the old and newer number
           `       output the result 

GCD of two numbers

__'{"3:~2@%'}`

Explanation:

__                 input the two numbers
  '                use one of the number as a condition so the WHILE loop starts
   {        }      a WHILE loop. Repeats while the c-stack has a true value on top
   {"       }      get the number back from the c-stack to do operations on it
   { 3:     }      make it so that there are threes copies on the stack
   {   ~    }      takes one of the copies from earlier and converts it to a zero
   {    2@  }      take the top item on the stack and move it behind the other two #s
   {      % }      modulo operation
   {       '}      use this number as the condition
             `     since one number is zero (and on the c-stack) print the 
                   other number, which is on m-stack

N Factorial

1_'['1+2:"*]`

Explanation:

1                  push 1 onto the main stack, representing 0!
 _'                take a line of input and move it to the control stack
   [       ]       FOR loop based on the input value
   ['      ]       put the running product on the control stack for storage
   [ 1+    ]       increment the counter
   [   2:  ]       make a second copy
   [     " ]       move the running product back to the main stack
   [      *]       multiply the running product by the counter
            `      output the result

Nth Triangular Number

_2:1+*2/`          

Explanation:

_                  input
 2:                make a second copy of it
   1+              increment one copy
     *             multiply the two copies
      2/           divide by two
        `          output

Digital root calculator

_1-+9%1+`

Explanation:

_         input a line
 1-+      subtract 1
    9%    mod 9
      1+  add one
        ` output

Truth-machine

_+?{1`}0`

Explanation:

_         input a line
 +        convert to a number
  ?       test for non-zero-ness
   {  }   loop
   {1`}   output 1
       0` output 0

Print "Element" without using letters

2'116 110[2:1-+'101"]7'69[,#`]

Interpret Deadfish

_2:$'[(4:i=d=s=o=0 1@[2:\ .`]"#[2^]"#[1-+]"#[1+]3:1-=256=|[0*]0 1@]

Quine

\ \3\:\$\'\[\\\\\`\(\`\]\#\` 3:$'[\\`(`]#`

This is the classic "string preceded by its quotation" approach. It escapes every character in the string's quotation.

\ \3\:\$\'\[\\\\\`\(\`\]\#\`                the string
                             3:             make two additional copies
                               $'           take the length of the string and move it to the control stack
                                 [     ]    FOR loop
                                 [\\`  ]    output a \
                                 [   (`]    remove a character from the front and output it
                                        #`  remove the empty remnant created by the loop, and output the original string

Interpreter

Perl Try it online!