X++

From Esolang
Jump to navigation Jump to search

X++ has just a bool and a stream to store values. X++ was invented in April 2008 by User:Feuermonster.

Instructions

(* may be 1 or 0) (? may be a number from 0 to stream.len)

  • Xor *
  • bool = bool xor *
  • Or *
  • bool = bool or *
  • And *
  • bool = bool and *
  • Not
  • bool = not bool
  • Addr
  • adds the bool to the stream which is actually a string. stream = stream + (string)bool
  • Addl
  • adds the bool to the stream which is actually a string. stream = (string)bool + stream
  • Outc
  • converts the stream to a number using binary representation. Then it prints the number as a character. (65=a)
  • Outn
  • converts the stream to a number using binary representation. Then it prints the number in decimal.
  • Clear
  • clears the stream
  • Clear ?
  • removes stream[?] from the stream
  • Get ?
  • bool = stream[?]
  • Set ?
  • stream[?] = bool
  • XGet ?1:?2
  • bool = stream[bintodec(stream[?1:?1+?2])]
  • XSet ?1:?2
  • stream[bintodec(stream[?1:?1+?2])] = bool
  • XClear ?1:?2
  • removes stream[bintodec(stream[?1:?1+?2])] from the stream
  • In
  • Input. (0 or 1) bool = (bool)input()


stream[?1:?1+?2] means the following:

Clear
Or 1
Addl
XClear 0:8

//This would convert stream[0] + stream[1] ... + stream[7] to a number using binary representation 
//In this case, it would remove the 128th element from the stream.

Loops

  • [...] while(bool=0) { ... }
  • (...) while(bool!=0) { ... }
  • {...} while(stream.Length < 8) { ... }


Examples

Or  1
Addr
And 0
Addr
Or  1
Addr
Outn

This will print 101 as decimal -> 5.


Or 1
Set 1
And 0
Set 1
Set 2
Set 3
Set 4
Set 5
Or 1
Set 6
Outc

this will print 00100001 as character -> A

Implementation

Java implementation by User:Mafi with stream readers (use a for bit 0 in the stream, b=1, c=2 ... h=7). [now all loop types]
Note: You can't use Addl in this implementation!

import java.io.*;

class XppInterpreter {
  private static boolean actbool = false;
  private static boolean[] stream = new boolean[8];
  private static int streamlength = 0;
  
  private static final String CMD_XOR   = "XOR _b_",
                              CMD_OR    = "OR _b_",
                              CMD_AND   = "AND _b_",
                              CMD_NOT   = "NOT",
                              CMD_ADDR  = "ADDR",
                              CMD_OUTC  = "OUTC",
                              CMD_OUTN  = "OUTN",
                              CMD_CLEAR = "CLEAR",
                              CMD_GET   = "GET _i_",
                              CMD_SET   = "SET _i_",
                              CMD_IN    = "IN";
  private static final String[] varnames = {
    "A","B","C","D","E","F","G","H"
  };

  
  private static void initStream(){
    for(int i=0; i<=7; i++){
      stream[i] = false;
    }
    streamlength = 0;
  }
  
  private static void pushOnStream(boolean newb){
    for(int i=1; i<=7; i++){
      stream[i-1] = stream[i];
    }
    stream[7] = newb;
    streamlength++;
    if(streamlength>8)
      streamlength=8;
  }
  
  private static int streamToInt(){
    int t = 128, r = 0;
    for(int i=0; i<=7; i++){
      if(stream[i] == true){
        r += t;
      }
      t /= 2;
    }
    return(r);
  }
  
  private static String goBack(String code, String oldcode){
    return(oldcode.substring(oldcode.length()-code.length()-1));
  }
  
  public static void interpret(String code){
    initStream();
    actbool = false;
    
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    code = code.toUpperCase();
    String oldcode = code;
    boolean noelse = false; //continue
    
    while(code.length() > 0){
      noelse = false;

      //simple Befehle / simple Commands
      //not
      if(code.startsWith(CMD_NOT)){
        actbool = !actbool;
        code = code.substring(CMD_NOT.length());
        noelse = true;
      }
      //addr
      if(code.startsWith(CMD_ADDR)){
        pushOnStream(actbool);
        code = code.substring(CMD_ADDR.length());
        noelse = true;
      }
      //outc
      if(code.startsWith(CMD_OUTC)){
        System.out.print((char) streamToInt());
        code = code.substring(CMD_OUTC.length());
        noelse = true;
      }
      //outn
      if(code.startsWith(CMD_OUTN)){
        System.out.print(streamToInt());
        code = code.substring(CMD_OUTN.length());
        noelse = true;
      }
      //clear
      if(code.startsWith(CMD_CLEAR)){
        initStream();
        code = code.substring(CMD_CLEAR.length());
        noelse = true;
      }
      //in
      if(code.startsWith(CMD_IN)){
        try{
          actbool = ((char)br.read() == ' ');
        }catch(Exception ex){
          actbool = false;
        }
        code = code.substring(CMD_IN.length());
        noelse = true;
      }
      //Bool-Funktionen / bool functions
      for(int i=0; i<=9; i++){
        //true or false
        if(i <= 1)  {
          //xor
          if( code.startsWith(CMD_XOR.replace("_b_",""+i)) ){
            actbool = actbool ^ (1 == i);
            code = code.substring(CMD_XOR.length()-2);
            noelse = true;
          }
          //or
          if( code.startsWith(CMD_OR.replace("_b_",""+i)) ){
            actbool = actbool | (1 == i);
            code = code.substring(CMD_OR.length()-2);
            noelse = true;
          }
          //and
          if( code.startsWith(CMD_AND.replace("_b_",""+i)) ){
            actbool = actbool & (1 == i);
            code = code.substring(CMD_AND.length()-2);
            noelse = true;
          }
        }else{  //stream readers
          String var = varnames[i-2];
          //xor
          if( code.startsWith(CMD_XOR.replace("_b_",var)) ){
            actbool = actbool ^ stream[i-2];
            code = code.substring(CMD_XOR.length()-2);
            noelse = true;
          }
          //or
          if( code.startsWith(CMD_OR.replace("_b_",var)) ){
            actbool = actbool | stream[i-2];
            code = code.substring(CMD_OR.length()-2);
            noelse = true;
          }
          //and
          if( code.startsWith(CMD_AND.replace("_b_",var)) ){
            actbool = actbool & stream[i-2];
            code = code.substring(CMD_AND.length()-2);
            noelse = true;
          }
        }
      }
      //Index-Funktionen / index functions
      for(int i=0; i<=7; i++){
        //get
        if( code.startsWith(CMD_GET.replace("_i_",""+i)) ){
          actbool = stream[i];
          code = code.substring(CMD_GET.length()-2);
          noelse = true;
        }
        //set
        if( code.startsWith(CMD_SET.replace("_i_",""+i)) ){
          stream[i] = actbool;
          code = code.substring(CMD_SET.length()-2);
          noelse = true;
        }
      }
      //Schelife/loop
      //[...]
      if(code.startsWith("[") && actbool == false){
        int inside = 1;
        while(inside >= 1){
          code = code.substring(1);
          if(code.startsWith("[")) inside++;
          if(code.startsWith("]")) inside--;
        }
      }
      if(code.startsWith("]") && actbool == true){
        int inside = 1;
        while(inside >= 1){
          code = goBack(code,oldcode);
          if(code.startsWith("[")) inside--;
          if(code.startsWith("]")) inside++;
        }
      }
      //(...)
      if(code.startsWith("(") && actbool == true){
        int inside = 1;
        while(inside >= 1){
          code = code.substring(1);
          if(code.startsWith("(")) inside++;
          if(code.startsWith(")")) inside--;
        }
      }
      if(code.startsWith(")") && actbool == false){
        int inside = 1;
        while(inside >= 1){
          code = goBack(code,oldcode);
          if(code.startsWith("(")) inside--;
          if(code.startsWith(")")) inside++;
        }
      }
      //{...}
      if(code.startsWith("{") && streamlength >= 8){
        int inside = 1;
        while(inside >= 1){
          code = code.substring(1);
          if(code.startsWith("{")) inside++;
          if(code.startsWith("}")) inside--;
        }
      }
      if(code.startsWith("}") && streamlength < 8){
        int inside = 1;
        while(inside >= 1){
          code = goBack(code,oldcode);
          if(code.startsWith("{")) inside--;
          if(code.startsWith("}")) inside++;
        }
      }

      //sonst... / else...
      if(noelse==false)
        code = code.substring(1); //...ein Zeichen wegmachen / ...remove one char
    }
  }
  
  public static void main(String[] args){
    interpret("or 1 [addr xor a ] outn"); //255
  }
}