Packlang

From Esolang
Jump to navigation Jump to search

Packlang is an esolang by User:PythonshellDebugwindow.

Packages

Packages define a function and all variables it will need.

Variables

To define a variable of type Type with name varName:

Type varName;

To initialize it to its type's default value (normally the minimum possible value for that type):

INIT varName;

To increment its value by one:

INCR varName;

To decrement its value by one:

DECR varName;

Array indexing:

myArray(index)

Array length (exactly like this):

myArray(length)

Array myArray, index myIndex:

myArray(myIndex)

Package dependencies

This package has no dependencies:

Package {
  ...
} myPackage;

This one has myDependency as a dependency:

Package : myDependency {
  ...
} myPackage;

This one has dpA, dpB, and dpC as dependencies:

Package : dpA, dpB, dpC {
  ...
} myPackage;

A package can have any number of dependencies. If a package has D as a dependency, then it will have access to all functions defined in D, none of which can use any external variables. Dependencies can have any number of functions and can have other dependencies as dependencies.

IO package

IO is a useful built-in package. It provides two functions, charPut and charGet. charPut takes a variable or value as its argument and prints that as a character to STDOUT, while charGet takes a variable or pointer as its argument and sets that variable/pointer value to a character received from STDOUT. Empty input returns a newline.

Datatypes

  • Integer
  • Integer(min, max, underflowValue, overflowValue)
  • String
  • Char
  • Array(type, length)
  • Pointer(type)

Misc

% This is a comment until end-of-line
%$ This is a
multiline comment %

Examples

Hello, World!

Package : IO {
  Integer main {
    charPut(72);
    charPut(101);
    charPut(108);
    charPut(108);
    charPut(111);
    charPut(44);
    charPut(32);
    charPut(87);
    charPut(111);
    charPut(114);
    charPut(108);
    charPut(100);
    charPut(33);
    charPut(13);
    charPut(10);
    0;
  }
} helloWorld;

Truth-machine

Package : IO {
  Char input;
  Integer main {
    charGet(input);
    If input ^ 48 Then {
      While 1 Do {
        charPut(49);
      }
    }
    charPut(48);
  }
} truthMachine;

Infinite cat program up to 100 chars each input

Package : IO {
  Array(Char, 100) input;
  Char c;
  Integer i;
  Integer j;
  Integer main {
    While 1 Do {
      INIT input;
      INIT c;
      INIT i;
      INIT j;
      charGet(c);
      While c ^ 10 Do {
        While c Do {
          INCR input(i);
          DECR c;
        }
        INCR i;
        charGet(c);
      }
      While j ^ i Do {
        charPut(input(j));
        INCR j;
      }
      charPut(13);
      charPut(10);
    }
    0;
  }
} cat;

Interpret PlusOrMinus

Package : IO {
  String code;
  Integer i;
  Integer(0, 255, 255, 0) acc;
  Integer plusOrMinus : code {
    INIT i;
    INIT acc;
    While i ^ code(length) Do {
      If !(code(i) ^ 101011) Then {
        INCR acc;
      }
      If !(code(i) ^ 101101) Then {
        charPut(acc);
        DECR acc;
      }
      INCR i;
    }
    0;
  }
} plusOrMinus;

Define a dependency and use its functions

Dependency {
  Integer equals : Integer a, Integer b {
    !(a ^ b);
  }
  Integer nequals : Integer a, Integer b {
    !!(a ^ b);
  }
} myDependency;
Package : IO, myDependency {
  Integer main {
    % 48 (1100000) ^ 0 -> 48 '0', 48 ^ 1 -> 49 '1'
    charPut(110000 ^ equals(101, 011)); % 0
    charPut(110000 ^ equals(001, 001)); % 1
    charPut(110000 ^ nequals(101, 011)); % 1
    charPut(110000 ^ nequals(001, 001)); % 0
    0;
  }
} myPackage;