Con-Text

From Esolang
Jump to navigation Jump to search

Con-Text is an esoteric programming language created in 2004 created by User:Sphen lee. It is equivalent to C/C++ however it relies on context so much that it can be impossible to determine what a line does without looking at the entire program.

Syntax

Any line beginning with a single # is a declaration. Simple variables are terminated by a newline; arrays are followed by a list of dimensions separated by hashes; functions are followed by a list of parameters separated by hashes. All primitive variables are integers. The type of an array, function, or parameter defaults to integer, unless a class is specified (see below). Characters use C-style character literals (eg. 'a') and are stored in integers. Strings are C-style (eg. "abc") and are stored in integer arrays.

Con-Text Equivalent C
 #var
 #array #2 #2
 #func #x #y #z
 int var;
 int array[2][2];
 int func(int x, int y, int z);

Lines beginning without a hash are statements (usually).

Con-Text Equivalent C
var #array #1 #func #2 #3 #4
array #1 #2 #var
func #1 #2 #3
var = array[1][func(2, 3, 4)];
array[1][2] = var;
func(1, 2, 3);

After declaring a function you have to implement it. Lines beginning with double hash implement functions (except ##start which is the program entry point). Assigning a value to the name of a function from within that function returns that value from the function (sounds complicated but it's the same as QBasic). Calling a function from within that function however is a recursive call. Functions can be overloaded on the number and type of arguments.

#func #param (declare the function) 
##func (implement the function) (body of function here) func #expression (returns a value) &#

An 'if' statement is created by placing an expression after an ampersand, if the condition is non-zero the body of the statement is executed (the body is terminated by "&#").

& #condition
    (body of 'if' statement here)
&#

A While statement is created similarly, except that it is terminated by "&" instead.

Exception Handling

An expression can be thrown by following it with &. A try block looks like an if statement with no condition. Following this is a series of catch blocks, which look like declarations of the type of the value to be caught. Each catch block ends with &#. The first non-declaration marks the end of the catch statements (this implies that you cannot declare a variable directly after a catch block).

# (try)
    (block to try)
    12& (throw integer 12)
&#
#x (catch an integer, call it x)
  (body of catch)
&#
#Exception #e (catch an Exception object, call it e)
  (body of catch)
&#
func #param (non-declaration, end of catch blocks)

Object Orientation

To declare a class use #class-name # (newline). The body of the class follows consisting of variable and function declarations. Constructors are not supported (use factory functions instead). The class body ends with &#. To access members of an object use object #member. To declare an instance of a class use #class-name #object name at any place where an ordinary identifier could be used. For example

#Vector2D#
  #x
  #y
  #normalise #dummy (dummy parameter to make a function)
  #getLength #dummy
&# 
#Vector2D #v
(create a Vector2D called v) v #x #1 (v.x = 1) print #v #getLength #null ( print(v.getLength(null)) )

To implement a member function of a class (a method) used this syntax: ##class name #function name. For example:

##Vector2D #normalise (implements Vector2D's normalise method)
  x #div #x #getLength #null
  y #div #y #getLength #null
&#

Notes

'null' is a built in variable used to fill the dummy parameters of functions (since all functions require at least one parameter).

Comments are enclosed by parentheses.

There are no operators; all operations are done using built in functions (like add, sub, mult and div).

Sample Program

Here is a sample program that calculates one root of a quadratic equation. (Comments are in parentheses)

#root #a #b #c
##root
  #dis
  dis #sub #mult #b #b #mult #mult #4 #a #c
  & #gte #dis #0 (an 'if' statement)
     root #div #sub #neg #b #sqrt #dis #mult #2 #a
  &#
  dis & (throws the discriminant because it was negative)
&#
##start
  #x
  x #root #1 #neg #2 #4 (root of x^2 - 2x + 4 == 0)
&#

Implementation

Unfortunately no interpreter or compiler exists at the moment, as I am unable to create a parser to parse this horrendous language.

Turing-completeness

Assuming unbounded integers (and unbounded while loops), compilation into a Minsky machine is possible.