From Esolang
Jump to navigation Jump to search

Glass is an esoteric programming language developed by Gregor Richards in 2005. It combines an unintuitive postfix notation with heavy object-orientation, requiring extensive juggling of a main stack combined with its object-oriented structure. No other language that the author knows of is implemented like this, because it would be idiotic.


The language is parsed into distinctive elements, each one character long, with the exception of these groupings:

  • (name) or (number) is parsed as a single variable name or stack reference number.
  • "string" is parsed as a string value.
  • <number> is parsed as a numeric value (integer or floating-point).
  • 'comment' isn't parsed at all, it's a comment (non-nestable).

Parentheses () are optional around a single letter A-Z, a-z or digit 0-9. Therefore, the variable name a could be represented by both a and (a), but the name abc could only be represented as (abc).

A program consists of a sequence of class definitions.

Each class definition has the form

{class-name function-definitions}

The class-name must be the name of a global variable which becomes a reference to the class.

A function definition has the form

[function-name content]

The function-name must be the name of a class-local variable which becomes a reference to the function. The content is an arbitrary sequence of commands.

For example:


would declare a class M with a function named m which does ...


would declare a class Main with a function named main which does ...


Execution starts by creating an instance of the class M and running the function M.m. That is, it is equivalent to the following sequence of commands:



There are four types of variables, partly separated by the format of their name:

  • Global variables, with names beginning with upper-case characters.
  • Variables local to a class, with names beginning with lower-case characters. These are immutable and must be looked up with the . command rather than the * command.
  • Variables local to an object, with names beginning with lower-case characters.
  • Variables local to the current invocation of a function, with names beginning with an underscore. (Note that names beginning with an underscore must be parenthesized.)

Variable names are only resolved in the context where they are actually used - that is, if you introduce the name _a in function a, and pass it back to the parent function, which uses the returned name, it will not refer to the variable _a of function a, but to the _a of the parent function. Autogenerated global variables are available to alleviate this issue. The contextual-ness of variable names is also why the . operator (seen later) works even though you push the name of a class-local variable - it resolves that variable in the context of the called class, not the caller.


Each command is listed in this form:

command : stack beforestack afterwards

In this notation (due to Forth), stack elements are separated by spaces, and the top of the stack (i.e. the most recently-pushed element) is listed last.

Command list

(name) : — name
Push a variable name onto the stack.
If name is a single letter from a-z, A-Z, then the parentheses are optional.
(number) : snumber ... s1 s0 — snumber ... s1 s0 snumber
Duplicate an element number positions down the stack to the top of the stack.
If number is a single digit 0-9, then the parentheses are optional.
"string" : — string
Push the string onto the stack.
<number> : — number
Push the number onto the stack.
, : value
Pop a value from the stack.
^ : —
Return (from function).
= : name value
Assign value to variable name. For composite values, only references are copied.
! : name cname
Assign a new instance of the class in variable cname to the variable name. Pop the names from the stack, then run the c__ (constructor) function of the newly instantiated object.
. : oname fnamefunction
Retrieve the function fname defined in the class of the object oname. The object becomes the current object of the retrieved function.
The variable oname is looked up in the current context, while fname is looked up in the context of the class of the object.
? : stack-before functionstack-afterwards
Pop the function from the stack, then run it. The effect on the stack depends on the function.
* : namevalue
Retrieve the value of variable name (in the current context).
$ : name
Assign the current object to variable name.
/(name)commands\ : stack-beforestack-afterwards
Repeat commands while the variable name has a "true" value. The effect on the stack depends on content.
Values are considered "true" if they are non-zero numbers or non-empty strings.

As an example of using commands, here is a segment of code to instantiate a class O into the variable _o, then run the function _o.o with "Hello World!" on the stack:

(_o)O!"Hello World!"(_o)o.?

So, to make a hello world program (assuming that the O class is built in to the system), do the following:

{M[m(_o)O!"Hello World!"(_o)o.?]}

Isn't that easy?!

Special definable functions

Constructor function of a class, run whenever an object is created.
Destructor function of a class, run when an object is garbage collected.
Function in class M, run to execute the whole program.

Built-in classes

Each built-in function is listed in the form

Class.function : stack beforestack afterwards

Note that Class.function is not legal Glass syntax for using the function, you need to do something like


Class A (Arithmetic)

A.a : x yx+y
A.s : x yx-y
A.m : x yx*y
A.d : x yx/y
A.mod : x y — (x mod y)
A.f : x — floor(x)
A.e : x yx==y
Equality : x yx!=y
Inequality : x yx<y
Less than
A.le : x yx<=y
Less than or equal to : x yx>y
Greater than : x yx>=y
Greater than or equal to

Class S (Strings)

S.l : stringlength
Retrieve the length of string.
S.i : string ncharacter
Retrieve the nth character of string. : string n charnewstring
Replace the nth character of string with char.
S.a : s1 s2s1s2
Concatenate strings.
S.d : string poss1 s2
divide string at point pos, s1 is the first part, s2 is the second.
S.e : s1 s2s1==s2
String equality.
S.ns : numbercharacter
Convert a number to a character. : characternumber
Convert a character to a number.

Class V (autogenerated Variables)

V.n : — newname
Push name of a new global variable.
V.d : name
Delete an autogenerated variable.

Class O (Output)

O.o : string/name
Output string or name.
O.on : number
Output number.

Class I (Input)

I.l : — string
Retrieve a line of input.
I.c : — char
Retrieve a character from input.
I.e : — is-eof
Push 1 if end of input, 0 otherwise.


Hello, world! program

{M[m(_o)O!"Hello World!"(_o)o.?]}

Fibonacci sequence

<1>=/(_nlm)(_n)*(_f)f.?(_o)(on).?" "(_o)o.?(_n)(_n)*<1>(_a)a.?=(_nlm)(_n)*<20>

Randomizer class






99 bottles of beer

?=(_y)<1>=/(_x)"No more"oo.?(_x)0=(_y)0=\/(_y)(_m)*o(on).
?(_y)0=\" bottle"oo.?(_x)<1>(_m)*ae.?=/(_x)^(_x)0=\"s"oo.
?" of beer on the wall,\n"pp.?n*pb.?qe" of beer,\n"pp.?
"Take one down, pass it around\n"pp.?ln*<1>as.?=l*pb.?wu
" of beer on the wall.\n\n"pp.?]}{M[moO!cC!bB!bb.?cx.?fc

brainfuck interpreter


External resources