Element

From Esolang
Jump to navigation Jump to 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 1 onto main stack
 _'          take input and move it to control stack
   [      ]  for loop based on input
   [3:    ]  make two more copies of top value of main stack
   [  ~   ]  convert one copy to a zero
   [   2@ ]  move top value of main stack to bottom
   [     +]  add top two values of main stack
           ` output top value of main stack

GCD of two numbers

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

Explanation:

__             get input twice
  '            pop top value of main stack and push onto control stack
   {        }  while loop based on input
   {"       }  pop top value of control stack and push onto main stack
   { 3:     }  make two more copies of top value of main stack
   {   ~    }  take one of the copies and convert it to a zero
   {    2@  }  move top item of main stack to bottom
   {      % }  modulo top two values of main stack
   {       '}  use top value of main stack as condition
             ` output top value of main stack

N Factorial

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

Explanation:

1             push 1 onto main stack (0! = 1)
 _'           get input and move it to control stack
   [       ]  for loop based on input
   ['      ]  pop top value of main stack and push onto control stack
   [ 1+    ]  increment top value of main stack
   [   2:  ]  make another copy
   [     " ]  pop top value of control stack and push onto main stack
   [      *]  multiply top two values of main stack by eachother
            ` output top value of main stack

Nth Triangular Number

_2:1+*2/`          

Explanation:

_         get input
 2:       make another copy
   1+     increment one copy
     *    multiply both copies by eachother
      2/  divide by two
        ` output

Digital root calculator

_1-+9%1+`

Explanation:

_         get input
 1-+      decrement top value of main stack
    9%    top value of main stack modulo 9
      1+  increment top value of main stack
        ` output top value of main stack

Truth-machine

_'{1`}0`

Explanation:

_        get input
 '       pop top value of main stack (input) and push onto control stack
  {1`}   output 1 until top value of control stack is 0 (which will never happen)
      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