Is

From Esolang
Jump to navigation Jump to search

Is is an esoteric programming language invented by User:A which operates on two separate memory components: a scalar bit value and a potentially infinite bit array.

Commands

i Increment the current bit, contingently wrapping around.
s Set the whole array into the pointed bit's value. At the program's start, all array elements are set to 0. If the modification repeats forever, then it will overflow.

Examples

Truth-machine

For input = 1, the code is:

is

Set into:

111111111111.......(which is the normal print 1 forever)

For input = 0, the code is:

s

set into:

000000000000.......(which is 0.It is impossible to only make one 0, unless the array only had 1 bit)

Computability

It can implement a Truth-machine. But, it has no if statements! And repetition! And I/O! But, it even passed the test!!! I am sure the Truth-machine is useless for testing all those characteristics.

Interpreter

An interpreter in the programming language Common Lisp shall be adduced:

(defclass Memory ()
  ()
  (:documentation
    "Establishes an interface for all classes pursuing the modeling of
     an Is program memory's cell array component."))

;;; -------------------------------------------------------

(defclass Infinite-Memory (Memory)
  ((cells
    :initform      (make-array 0
                     :element-type    'bit
                     :initial-element 0
                     :adjustable      T
                     :fill-pointer    T)
    :type          bit-vector
    :documentation "A resizable bit vector which simulates an
                    \"infinite\" binary array."))
  (:documentation
    "Simulates an infinite vector of bits utilizing a dynamically
     resizing array."))

;;; -------------------------------------------------------

(defclass Fixed-Size-Memory (Memory)
  ((cells
    :initarg       :cells
    :type          simple-bit-vector
    :documentation "A fixed-size array of bits."))
  (:documentation
    "Realizes a fixed-size vector of bits."))

;;; -------------------------------------------------------

(defun make-infinite-memory ()
  "Creates and returns a fresh ``Infinite-Memory instance."
  (the Infinite-Memory (make-instance 'Infinite-Memory)))

;;; -------------------------------------------------------

(defun make-fixed-size-memory (size)
  "Creates and returns a fresh ``Fixed-Size-Memory of the desiderated
   SIZE."
  (declare (type fixnum size))
  (the Fixed-Size-Memory
    (make-instance 'Fixed-Size-Memory :cells
      (make-array size
        :element-type    'bit
        :initial-element 0
        :adjustable      NIL
        :fill-pointer    NIL))))

;;; -------------------------------------------------------

(defgeneric set-memory-cells-to (memory new-value)
  (:documentation
    "Stores the NEW-VALUE in all of the MEMORY's cells, concomitantly
     printing each cell's new state, and returns no value.")
  
  (:method ((memory Infinite-Memory) (new-value integer))
    (declare (type Infinite-Memory memory))
    (declare (type bit             new-value))
    (with-slots (cells) memory
      (declare (type bit-vector cells))
      (loop
        for cell-index of-type fixnum
          = 0 then (mod (1+ cell-index) array-total-size-limit)
        do (when (>= cell-index (length cells))
             (vector-push-extend 0 cells))
           (setf (bit cells cell-index) new-value)
           (format T "~d" (bit cells cell-index))))
    (values))
  
  (:method ((memory Fixed-Size-Memory) (new-value integer))
    (declare (type Fixed-Size-Memory memory))
    (declare (type bit               new-value))
    (with-slots (cells) memory
      (declare (type simple-bit-vector cells))
      (loop for cell-index of-type fixnum from 0 below (length cells) do
        (setf (sbit cells cell-index) new-value)
        (format T "~d" (sbit cells cell-index))))
    (values)))

;;; -------------------------------------------------------

(defun interpret-Is (code &optional (memory (make-infinite-memory)))
  "Interprets the piece of Is source CODE, potentially deploying a
   bespoke MEMORY model, and returns no value."
  (declare (type string code))
  (let ((ip          0)
        (current-bit 0))
    (declare (type fixnum ip)
             (type bit    current-bit))
    (symbol-macrolet
        ((current-character
          (the character
            (char code ip)))
         (program-has-completed-p
          (the boolean
            (not (array-in-bounds-p code ip)))))
      (declare (type character current-character)
               (type boolean   program-has-completed-p))
      (loop until program-has-completed-p do
        (case current-character
          ((#\Space #\Tab #\Newline #\Return)
            NIL)
          (#\i
            (setf current-bit (- 1 current-bit)))
          (#\s
            (set-memory-cells-to memory current-bit))
          (otherwise
            (error "Invalid character \"~c\" at position ~d."
              current-character ip)))
        (incf ip))))
  (values))

;;; -------------------------------------------------------

;; Simulate a truth-machine with an input of "1":
(interpret-is "is")

;; Simulate a truth-machine with an input of "0":
(interpret-is "s" (make-fixed-size-memory 1))