Logicode

From Esolang
Jump to navigation Jump to search

Logicode is a minimalistic language designed by user:Qwerp-Derp on August 31, 2016. It is heavily inspired by Logisim.

Introduction

Built-ins

Logicode has three built-in logic gates -

   a&b -> AND of a and b
   a|b -> OR of a and b
   !a  -> NOT of a

...and that's about it. The rest is up to you to build.

DIY Stuff

Circuits

Circuits are basically Logisim's version of functions.

Circuits are built like so:

   circ circ_name(arg1,arg2...)->{insert code here}

Breakdown:

  • "circ" is the circuit declarer
  • "circ_name" is the circuit name
  • "arg1,arg2..." are the arguments
  • Everything after the `->` is the code that is executed. NOTE: you do not need curly brackets.

You can also have multi-line circuits, which are like this:

   circ circ_name(arg1,arg2...)->[
       {code block 1}
       {code block 2}
       etc. etc.
   ]

The square brackets denote the start and end of a circuit. You can declare variables, do conditions, and return things. Return is implicit.

Variables

Variables are the same as always - except, they can only hold 0's and 1's.

Variables are built like so:

   var var_name=var_value

Breakdown:

  • "var" is the variable declarer
  • "var_name" is the variable name
  • "var_value" is the value. The value can consist of function calls, and can only consist of 0's and 1's.

Conditionals

The conditionals here are if-else statements, essentially.

They are made like so:

   cond cond_arg->{if cond_arg = 1}/{else}

Breakdown:

  • "cond" is the conditional declarer
  • "cond_arg" is either 0 or 1.
  • "/" is required to separate the two blocks of code.

I/O

Input

Input can be inserted into any variable, condition or function like so:

   circ foo(a)->input&a (calculates the AND of an input and a)
   var bar=input (sets variable bar to an input)
   cond input->out 1/out 0

Output

Output is declared like so:

   out {variable, string of bits, function calls, etc.}

Where out is the output declarer.

Other stuff

Comments

All comments in Logicode are lines which start with a #.

Comments are like this:

   # This is a comment!

+ operator

The + operator is essentially concatenation between two "strings" of binary digits.

It can be used in both circuits (to output multiple bits) and variables/outputs (to concatenate two strings).

+ in circuits is like this:

   circ foo(a,b)->a+b
   out foo(1,1)
   Outputs: 11

+ in variables/outputs is like this:

   var foo=100
   var bar=foo+1
   out bar+11
   Outputs: 100111

ASCII support

The @ operator is used to convert binary strings to ASCII.

It is used like this:

   out @111111
   # Converts 111111 (63 in decimal) to "?" (character code 63)
   Outputs: ?

Example Code

Semi-truth-machine

This is a semi-truth-machine (i.e. it doesn't output 1 indefinitely):

   circ loop(a)->loop(a)
   var in=input
   cond in->out loop(a)/out 0

The circuit loop calls itself, and is essentially the infinite loop.

The variable in is the input.

The conditional goes into the loop if in is 1, otherwise it outputs 0.

Half-adder

This is a half-adder:

   circ xor(a,b)->(!(a&b))&(a|b)
   circ halfadder(a,b)->(a&b)+(xor(a,b))

The circuit xor is an XOR gate, and the circuit halfadder is the half-adder, which outputs two bits:

  • The bit before the + (which is used to separate output bits for circuits) is a AND b, and
  • The bit after the + is a XOR b.

Interpreters

  • This here is my working interpreter for Logicode as it is now. There may be changes.
  • You can also try it online!