Title: Other Languages
1Other Languages
- We wrap up our examination of programming
languages by briefly considering a wide variety - we will look at some
- meaningfully important historic languages of note
- apl to demonstrate an array-based language
- a stack-based language (because some of the
bizarre languages use this approach) - bcpl to see more of how C-languages evolved
- some truly abnormal languages
- a couple of the newer languages of note
- bizarre languages
- esoteric languages
- languages that were developed for fun, or as a
proof of concept, but not meant to be taken as
serious languages - obfuscated languages
- languages that are purposefully very difficult to
understand and use
2APL
- APL a programming language (literally)
- developed in the 1960s
- meant to be a language to express mathematical
ideas - notation consists of a set of symbols and a
syntax to describe the processing of data. - APL has primitive instructions that can
manipulate n-dimensional arrays of data
simplifying a program - these primitives can be strung together in a
single line of code as a combination of array
manipulations - APL is interactive (interpreted)
- useful for rapid prototyping
- APL is one of the most concise, consistent, and
powerful programming languages ever devised
(according to the author of a website I got this
info from)
3APL Operations
- APL programs are typical 1/5 to 1/20 the size of
other programs due to the built-in array and math
operations - sort up (or down) a set of numbers
- raise any number to any power
- take any root of a number
- logarithms and exponentials
- examine separately the integer and decimal parts
of a number - convert number bases
- trigonometric functions
- random numbers
- rearrange arrays of numbers into different size
and shape arrays, i.e. vectors, matrices, and
multi-dimensional "cubes".
DIMENSION X(100),Y(100) READ(5,10)
N,(X(I),I1,N) 10 FORMAT (15/(F10.2)) DO 20
I1,N AX(I) L1 DO 15 J1,N
IF(A-X(J))15,15,12 12 AX(j) LJ 15 CONTINUE
Y(I)A 20 X(L)100000. WRITE(6,30)
(Y(I),I1,N) 30 FORMAT(E15.2) END
APL Array sorting program FORTRAN Array sorting
program
4Sample APL
- A ? 15 23 8 19 21 6
- This creates an array, A with the values as
specified - B ? 2 4 r 1 2 3 4 5 6 7 8
- This creates a 2x4 array with 1..4 being in the
first row and 5..8 being in the second row (r is
the Greek letter rho) - Once you create arrays, here are some operations
to perform on them - /A Computes the sum of the array elements
(returns 92 in this case) - A 1 Adds 1 to each element of array A
- x/A Computes the product of the array elements
- r B Returns the dimensions of the array (in
this case, 2 4) - (/A)? r A Computes the sum over the size of the
array (average)
This final one computes and displays all of the
prime numbers ending at 20 See http//www.users.cl
oud9.net/bradmcc/APL.html for an explanation
(halfway down this page)
5BCPL
- Basic Combined Programming Language, the
precursor of C (also B) - block structured like Algol and syntactically
somewhat similar to Algol but it introduced the
for blocks (although since many early keyboards
lacked these characters, ( and ) was also used,
and - was typeless
- had only 1-D arrays
- was case insensitive for identifiers but all
reserved words start with upper case letters - had all of Cs control structures but included an
Unless-Do and Repeat-Until (which would later be
found in Pascal/Modula) - loop (infinite loop)
- more primitive for loop
- test-then-else rather than if-then-else
- introduced pass-by-value as the only form of
parameter passing, but also had global memory (a
1-D array to store any global values) for
variables that should be changed in a subroutine - had both functions and procedures
6BCPL Examples
GET "LIBHDR" LET START() VALOF LET
LC, WC, CC 0, 0, 0 AND SP TRUE
LET CH RDCH( )
IF CHENDSTREAMCH BREAK IF
CH'N' THEN LC LC 1 TEST
CH'T' CH'N' CH'S' THEN SP TRUE
OR IF SP
WC WC 1 SP FALSE
CC CC 1
REPEAT WRITEF("N N NN",
LC, WC, CC) RESULTIS 0
Manifest declares constants Let declares
functions, procs
GET "LIBHDR" MANIFEST BOTTLES 99 LET
START( ) BE LET BEERS(N, S) BE
TEST N 0 THEN WRITEF("No more bottles")
ELSE WRITEF("N bottleS", N, (N 1)
-gt "", "s")
WRITEF(" of beerS", S) FOR I BOTTLES
TO 1 BY -1 DO BEERS(I, " on the
wall, ") BEERS(I, ".NTake one down,
pass it around.N") BEERS(I - 1, " on
the wall.N") FINISH
GET "LIBHDR" LET START ( ) BE LET F(N)
N0 -gt 1, NF(N-1) FOR I 1 TO 10 DO
WRITEF("F(N), NN", I, F(I))
FINISH
7Forth
- An extensible stack-based language from the 1970s
- Interpreted language
- All commands are part of a dictionary
- defining your own commands adds to the current
dictionary (because its interpreted, you can
continue to add during the current session) - All data interact with a built-in data stack
- this will require that operations be specified in
a postfix notation - example 5 4 .
- pushes 5, then 4, pops 5 and 4, adds them,
pushes 9 onto the stack, . ends the instruction,
9 popped off the stack and returned - Built-in stack operations
- DUP duplicates the top of the stack
- SWAP swaps the top 2 elements
- DROP drops the top element of stack
- ROT Rotate the top 3 numbers
8Variables and Words
- To define a variable use variable name
- this allocates storage for the integer variable
name in the dictionary - When you use the variable name, the address of
the variable is placed on the stack - ! stores stack values into a variable as in
- variable year
- 2005 year !
- _at_ fetches variable and places it on the stack
- year _at_ .
- Arrays and strings are also available where the
index precedes the variable name as in 10 array !
- To define a dictionary entry (word) name
body - This adds to the dictionary a new routine whose
name is name and whose body consists of all
characters after the space following name and
prior to the - For example, square dup defines square as a
routine that will duplicate the top of the stack
and then multiply the top two elements on the
top of stack - You would execute square by doing variable square
. And this would compute variable2 and return
that value
9Control Statements
- Conditional operators pop the top two values off
the stack, perform the comparison and push -1
(true) or 0 (false) onto the stack - A control statement can then use the result
- If statements
- If operator statement(s) then
- If lt _at_ x ! then
- This means if top item on stack lt second item
then retrieve x and multiply it by top of stack
(which is now -1) and push to top of stack in
effect, this does if top lt second x x -1 - If operator statement(s) then statement(s) else
- Loop statements
- Limit Init do var statement(s) loop
- As in 10 0 do i . loop -- which prints 0..9
- There is also an until and while repeat
- begin statement(s) condition until .
- begin condition while statement(s) repeat .
10SNOBOL
- SNOBOL (String-oriented Symbolic Language) dates
back to 1962 but its standard version, SNOBOL 4
was implemented in 67 - The language contains the following features
- string manipulation operations - has several of
these operations which allow a string to be
tested for contents and make replacements in the
string - pattern matching- involves examining substrings,
for the occurrences of specified substrings - dynamically typed - no type declarations,
variables may take on any type at any time - aside from the basic data types, SNOBOL allowed
user defined structures like Simula 67 or Algol
68 - interpreted language with a compiler
- SNOBOL statements are of the form
- label statement (label) -- the () is a goto
statement after this instruction executes and is
optional like the label that precedes the
statement - statements have varying syntax and can look like
message passing or imperative statements
11Sample SNOBOL Program
TRIM 1 WORDPAT BREAK(LCASE UCASE)
SPAN(LCASE UCASE "'-") . WORD COUNT
ARRAY('39',0) READ LINE INPUT
F(DONE) NEXTW LINE WORDPAT F(READ) COUNTltSIZ
E(WORD)gt COUNTltSIZE(WORD)gt 1
(NEXTW) DONE OUTPUT "WORD LENGTH NUMBER OF
OCCURRENCES" I 2 PRINT I I 1 OUTPUT
LPAD(I,5) LPAD(COUNTltIgt,20) S(PRINT) END
This program inputs a text file and reads each
word and counts the number of 3-letter, 4-letter,
, 9-letter words and reports on the number of
each found
12Mouse
- A language originally intended for
microcomputers with a small amount of memory,
interpreted and stack-based, using postfix
notation - the unique aspect of Mouse is that the program is
specified as a single stream of symbols so that a
program will tend to be very short (in terms of
byte length) - variables are single characters and commands are
also typically single characters - Mouse includes conditional branches, loops,
pointers, macros, arrays and code tracing - Rather than displaying the entire table of
symbols for commands, here are some brief
examples
X. Y assign X to Y N. 1 N increment N by
1 P. Q. P Q swap values of P and Q ? A
input a number and store in A P. ! print
variable P
1 N N 1 ( N. N. ! Print N N
N. 10 0 lt If N lt 10 0 then exit N. 1
N N N 1 End
Program
13Groovy
- This is a new language, an extension to Java
- It includes these features
- compares data (whether primitive or the values
that make up an object) and not references, so
for instance if a and b are Strings, a b
compares the two Strings values - the is not needed unless multiple statements
are on the same line the interpreter is able to
judge the end of an instruction if it ends with a
\n - supports operator overloading like C
- includes the tuple type from Python (indexed via
arrays) as in list hello, 5, 3.14 and then
list2 is 3.14 - functions can be passed as parameters, like in
C - uses the Java for loop but also has a simpler
counting loop as in Python for(i in 1..10) - closures have built-in iterator operations
- as with Ruby, automatically generated mutators
and accessors for all private data members
14Lua
- Lua is a dynamically typed scripting language
- Lua is a very small language that is very
extendable - While not overtly OO (for instance, no built-in
support for inheritance) - it can be expanded to include inheritance to make
it OO and to handle AOP - the language also fits the imperative paradigm by
having control statements (similar to
Modula/Pascal) - the functional paradigm borrowing other aspects
from Scheme - and concurrent programming
- The basic unit of storage is known as a table,
which is a heterogeneous associative array - aside from associative arrays, Lua provides local
and global (the default) variables with types
that include numbers, strings, and booleans - traditional arrays, sets, records and lists are
implemented using tables
15First Class Functions
- Lua supports first class functions (functions
that can be passed as parameters) but also allows
- functions to be pointed to and
- functions to be redefined
- For instance, you can do the following in order
to save a function while re-implementing it
do local oldprint print // save the old
function function print(s) // redefine the
function if s foo
then oldprint(bar) // cal the old function
else oldprint(s) end end end
16Metatables
- One of Luas greatest strengths is the inclusion
of metatables and the ability for the programmer
to modify metatables - A metatable is attached to every variable that
contains data describing the variable including
how the variable should be treated under various
conditions - To add or alter a metatable entry, use
name.__entry where name is the name of the
variable and entry is the metatable element - We can then use the metatable entry in situations
where the variable might not normally be usable - For instance, if a variable x is not a string but
we still want to know its length, we can obtain
it by doing - metatable(x).__len
- Hopefully the len metatable entry is present to
either return xs length or compute xs length - Built in metatable entries exist for a number of
useful features such as length, index, concat and
eq
17Go
- A systems programming language from Google that
combines features of C/C and Python - For instance, the semicolon is only used to
separate elements in the for loop - There is a built-in string type where strings are
immutable but not objects - Assignment copies contents of values, not
references (pointers) unless you explicitly
request the address - Go is strongly typed
- Go supports OOP, functional programming
(functions are first class objects), concurrent
programming and procedural programming - The language supports the development of
ontologies (as used for the semantic web)
18Example Code
func sum(a int) int s 0 for i
0 i lt len(a) i s ai
return s
main .. include "sysgo/io.gof".
include "sysgo/stdlib.gof". main() -gt
drink(99) stdout.outLine("Time to buy some
more beer..."). drink(0) -gt . drink(i) -gt
stdout.outLine( bottles(i) ltgt " on the
wall,\n" ltgt bottles(i) ltgt ".\n" ltgt
"take one down, pass it around,\n" ltgt
bottles(i) ltgt " on the wall.\n")
drink(i-1). bottles(0) gt "no bottles of
beer". bottles(1) gt "1 bottle of beer".
bottles(i) gt i0 ltgt " bottles of beer".
func Open(name string, mode int, perm int)
(file File, err os.Error) r, e
syscall.Open(name, mode, perm) if e ! 0
err os.Errno(e) return
newFile(r, name), err
19Obfuscated/Esoteric Languages
- The idea behind these languages is to either (or
both) - Hide the meaning of the instructions, that is, to
make programming as challenging as possible - Make languages as simple as possible by reducing
the number of operators/operations to a minimum
and permit a very small compiler - Im going to break these down into roughly three
categories - Spatial languages languages whose instructions
pertain to physical locations among the data - Stack languages languages like Forth where the
data are implied to be on one or more stacks - Other languages that are just plain weird
20False
- One of the first obfuscated languages, it uses
postfix notation for operations like Lisp uses
prefix notation - operators include _ for unary minus, for not
equal and for not - for instance, a1_ means a ltgt -1 and
a0gta99gt means agt0 and alt99 - data types are limited to integer and character,
which are single quoted like Lisp (e.g., a) - variable names are limited to single letters
- assignment is
- is used to dereference a variable
- example 1a is a 1 and a1b is b a 1
- False uses an implied stack (all operations are
via stack, thats why the language uses postfix) - functions are defined as lambda functions
(unnamed) and placed inside of where
parameters are taken from the stack based on the
number of values that precede the function call - you can declare a functions name by placing
name after the function definition
21False Instructions
- Built-in stack operations include to duplicate
top of stack, to delete top of stack, \ to swap
top two of stack, _at_ to rotate top 3 of stack - The only control instruction are
- if which is denoted using ? after the then or
else clause, for instance - a1hello?
- if a 1 then print hello
- a0gt1b?a00b?-1b?
- if a gt 0 then b 1 else if a 0 then b 0 else
b -1 - while loop which is denoted with a after two
lambda functions, the condition and the body - a12f!
- while (a 1) f(2)
- a0gtaa-bbb-1
- while(agt0) a a b b b 1
- output is simply literal or var or .
(print top of stack as number) or , (print top
of stack as char) - input follows the symbol which means stdin
22False Examples
21! 1 is a function that adds 1 to the
argument passed, so this code returns 3 1i
defines the function as i, and can
be called as valuei! as in 2i! which
returns 3 1_, while((cgetchar( ))
! -1) putc(c) more literally, push the char
on top of stack and then pop it off and print
it 1\1\?1-f!?f this defines a
function f as follows f(x) if(top of stack
1) then return 1 else return f(x 1)
23Brainfck
- Goal create a language with the smallest
compiler ever - Brainfcks original compiler was 240 bytes!
- supposedly a newer compiler is under 200 bytes
- The language manipulates an array of 30,000 bytes
with a single pointer - instructions move the pointer or manipulate what
is being pointed at - instructions (C equivalents given, assume p is
pointer) - gt is p
- lt is --p
- is p
- - is --p
- . is putchar(p)
- , is p getchar( )
- is while(p) that is, while loop while
pointer p 0 - is , that is, ends the while loop started with
24BF Examples
BF Hello World program (creates the integer
values for ASCII Hello World and outputs these
values) gtgtgtgtltltltlt
-gt .gt....gt.ltlt.gt.
.------.--------.gt.gt.
This program multiplies the original input value
by 10 and stores the value back into the
array gtlt-gt move num one right ie
num2num gtgt load 10 into fourth
element ltltltgtgtlt- add num2 to first
and third element gtltgt-gt num2third
element - loop ten
times ltlt-lt clear num2
Input text and output it backwards gt.gt.lt-.lt
Program to compute 4 3 and output the
result ,gtlt--------gt-,,ltgt-,lt.gt.
25Argh!
- BF/Esoteric language where commands are single
letters and control is based on orientation of
commands in a 2D grid - Execution starts with j at position 0, 0
- j indicates go down 1 position
- l means process commands to the right
- p and P are print commands to print the character
below or above the letter - q means quit (end of program)
- Programs are limited to a 80x40 grid of Ascii
characters - Aargh! is an extension to Argh! where programs
can be Nx40 where there is no restriction on N
(that is, programs can have an unlimited length
but no more than 40 columns)
j world lppppppPPPPPPq hello,
Note the type is not lined up exactly,
each letter of world and hello shouold
appear directly above or below a p/P
26Argh Commands
h, j, k, l - set execution direction to "left",
"down", "up", "right" H, J, K, L - move
instruction pointer left/down/up/right to the
next cell whose value matches the value on top
of the stack, set execution direction to "left",
"down", "up", "right" x/X - if value on top of
the stack is positive, turn the execution
direction 90 degrees to the right/left q - quit
end program execution s/S - store (push) value of
the cell below/above the current cell to
stack d/D duplicate/delete top value on
stack a/A - add value of cell below/above current
cell to the value on top of the stack r/R -
reduce the value on top of the stack by the value
of the cell below/above f/F - fetch (pop) value
from top of stack and store to cell
below/above p/P - send value below/above the
current cell to stdout g/G - get one byte from
stdin and store in cell below/above e/E - insert
value of system EOF in cell below/above
Argh! and Aargh! allow self-modifying code
instructions can be used to overwrite
instructions!
27Kipple
- A BF/esoteric language
- the goal is minimalism a language with very few
commands - the language is based on stack storage
- 26 built-in stacks available, called a-z, i and o
are reserved for pre-execution input and
post-execution output only, no other variables - commands are limited to
- pushing/popping off of stacks
- 5 gt a pushes 5 onto stack a, a gt b pops the top
of stack a and pushes the item onto b, a lt b does
the same - adding/subtracting two stack items or one stack
item and one numeric operand - a 5 pushes (top of a 5) onto a, a b pushes
(top of a top of b) onto a, a 5 pushes 5 onto
a if a is empty - clearing a stack (if the top of stack stores 0)
- a? pops all elements off of a if the top of a is
0 - a loop that iterates through the body until a
given stack is empty - (a instructions) repeats the instructions, one
time per element of a until a is empty - strings are converted into individual ascii
values and pushed/popped as ints - abc gt a pushes 97, 98 and 99 onto a in order
(so that the top of stack a is 99) - _at_value gt o pushes the value onto the output
stack, not its ascii character
28Kipple Example 1
Prints the lyrics to the famous "99 bottles of
beer" song 99gtn push 99 onto stack n (n
while n is not empty 10gts 10 is ascii for
new line, push new line onto s n0 ngt_at_ (_at_gtt)
(tgts) slt" bottle" n-1 ngta? (a "s"gts 0gta?) slt" of
beer on the wall" 10gts output 1 line of this
verse to stack s n0 ngt_at_ (_at_gtt) (tgts) slt"
bottle" n-1 ngta? (a "s"gts 0gta?) slt" of beer"
output 1 line of this verse to stack s n-1
10gtslt"Take one down and pass it around" 10gts
compute next n (n-1) and output next line of
verse with new n n0 ngt_at_ (_at_gtt) (tgts) slt"
bottle" n-1 ngta? (a "s"gts 0gta?) slt" of beer on
the wall" 10gts output last line of this
verse to stack s n? clear stack n if top of
stack is 0, that is, we have reached the )
end of the song, and therefore empty stack exits
the loop (sgto) take everything in s and move
it to o (output stack)
29More Kipple Examples
Prints the 25 first Fibonacci numbers 24gtn 0gtt
1gta push fibonacci numbers onto stack t (n-1
a0 tltagtba cltbgtaltc n? ) output
numbers (tgt_at_ (_at_gto) 32gto )
Bubblesort by DeathPing (i d1 (bgtc
0gtb? x?) (x? d0 dgtf) ) (cgti) ) 0gtd?
(fgto)
Reverse (igtr)(rgto) Unix cat (igto)
30Chef
- Like Kipple, Chef is another stack-based
programming language - programs in Chef are natural language statements
that are supposed to be recipes - the natural language-based instructions are
converted into stack commands based on the
following - ingredients represent integer or character values
for data - dry ingredients are interpreted as numeric values
and liquid ingredients are interpreted as
characters (they are stored as int ASCII values
so the interpretation is important to output the
right value) - dry vs. liquid is based on the measurement used
(e.g., g, kg are dry and ml, l are liquid) - ingredients themselves are merely symbols and so
can be anything the programmer desires - recipe steps represent stack operations
- see the next slide
- recipe references to mixing bowls and baking
dishes represent different stacks to manipulate - other information, which is optional, is merely
decorative such as baking time, oven temperature,
and the final statement is how many people the
recipe serves (required, but does nothing in the
program)
31Chef Operations
- Take ingredient from refrigerator. input
statement - Put ingredient into nth mixing bowl. pushes
onto stack n - Fold ingredient into nth mixing bowl. pops
off stack n and places it into variable
ingredient - Add ingredient to nth mixing bowl. performs
top of stack n ingredient and pushes onto stack
n - Remove ingredient from nth mixing bowl.
same but does subtraction - Combine ingredient into nth mixing bowl.
same but multiplies - Divide ingredient into nth mixing bowl. same
but divides - Add dry ingredients to nth mixing bowl.
adds all dry ingredients and pushes onto stack n - Liquefy Liquify ingredient. converts from int
to char - Liquefy Liquify contents of the nth mixing
bowl. same for top of stack n
32Continued
- Stir the nth mixing bowl for number minutes.
rotates number of elements on stack n - Mix the nth mixing bowl well. randomizes
order of items on stack n - Clean nth mixing bowl. removes all elements
from stack n - Pour contents of the nth mixing bowl into the
pth baking dish. copies all elements of stack
n onto stack p - Verb the ingredient. this is followed by
another statement and executes the next statement
based on the numeric value stored in ingredient
(a for loop), where the number of ingredient is
decremented each time throughVerb the
ingredient until verbed. ends the loop. The
verb used is arbitrary. - Serve with auxiliary-recipe. this invokes the
subroutine auxiliary recipe. - Refrigerate for number hours. exits the
recipe or auxiliary recipe, printing out the
number of baking dishes used in the recipe if the
recipe contains a number of hours (cooking time)
33Example Compute Fibonacci
Fibonacci Numbers with Caramel Sauce.Ingredients
.100 g flour250 g butter1 eggMethod.Sift
the flour. Put flour into mixing bowl. Serve with
caramel sauce. Stir for 2 minutes. Remove egg.
Rub the flour until sifted. Stir for 2 minutes.
Fold the butter into the mixing bowl. Pour
contents of the mixing bowl into the baking
dish.Serves 1.
34Continued
Caramel Sauce.Ingredients.1 cup white sugar1
cup brown sugar1 vanilla beanMethod.Fold
white sugar into mixing bowl. Put white sugar
into mixing bowl. Fold brown sugar into mixing
bowl. Clean mixing bowl. Put white sugar into
mixing bowl. Remove vanilla bean. Fold white
sugar into mixing bowl. Melt white sugar. Put
vanilla bean into mixing bowl. Refrigerate. Heat
white sugar until melted. Put white sugar into
mixing bowl. Remove vanilla bean. Fold white
sugar into mixing bowl. Caramelise white sugar.
Put vanilla bean into mixing bowl. Refrigerate.
Cook white sugar until caramelised. Put white
sugar into mixing bowl. Serve with caramel
sauce. Fold brown sugar into mixing bowl. Put
white sugar into mixing bowl. Add vanilla bean.
Serve with caramel sauce. Add brown sugar.
35Shakespeare
- Like Chef but the instructions are coded as if
you are writing a play with characters
(variables) and scenes (subroutines) - The reserved words in this case are based on
whether a word is a verb or noun or adjective or
a character in the play - Characters are declared at the top of the play in
a Dramatis Personae section - each character represents its own stack
- The declaration is charactername, description
(description is ignored) - Characters enter into dialog with each other
the dialog manipulates one or more stacks through
push, pop, topmost operations, conditions, and
arithmetic operations
36Continued
- Acts and scenes
- Each program is broken into procedural units
known as acts and scenes - Each act and scene is given a roman numeral
identifier, these are used as GOTO statement
labels - Act I Hamlets last battle (the text after the
is a comment) - Characters must explicitly enter and exit an act
- At most, only two characters can be on stage at a
time - The exeunt statement calls all listed characters
to leave at the same time - Enter Juliet
- Enter Romeo and Juliet
- Exeunt Romeo and Juliet
- Scenes will consist of lines of dialog
- Lines are the commands and fall into three
categories - Control statements (see the next slide)
- I/O statements, which are prefaced with the
expression Open your heart (input) or Speak
your mind (output) - Assignment statements (see the next slide)
37Dialog/Instructions
- Numbers (literals) are represented using two
combining schemes - A noun is equal to 1, any adjective that precedes
it doubles the value, so for instance, 3
adjectives and a noun is 8 - You can combine numbers with such phrases as the
sum of and or the difference between and
- The difference between a small, tired, dirty
pony and a big boy is nothing this represents 8
2 6 - Assignment statements are dialog between one or
two characters and the direction of the
assignment (which character) is based on whether
the dialog uses first person or second person - The dialog Remember me means to push the latest
value on to that characters stack whereas
Recall means to pop the latest value from the
stack
38Control Statements
- There are two forms of control statements
- Go to statements specify particular scenes as is
Let us return to scene III - Selection statements are broken into a question
(condition) and actions, possibly spoken by
multiple characters - Questions ask is X as good as Y or is X better
than Y or is X worse than Y - example Am I as horrid as a flirt-gill? This
compares the value of the current speaker (their
stacks top value) against a literal computed by
flirt-gill - The action may be a go to statement, assignment
statement or input or output - Notice that there is no loop, you must construct
loops as you would in assembly language, by
combining selection statements and go to
statements
39Example Reversing an Input String
Othello, a stacky man. Lady Macbeth, who pushes
him around till he pops. Act I The one and
only. Scene I In the beginning, there was
nothing. Enter Othello and Lady Macbeth
Othello You are nothing! Scene II Pushing to
the very end. Lady Macbeth Open your mind!
Remember yourself. Othello You are as hard as
the sum of yourself and a stone wall. Am I as
horrid as a flirt-gill? Lady Macbeth If not,
let us return to scene II. Recall your imminent
death! Othello You are as small as the
difference between yourself and a hair! Scene
III Once you pop, you can't stop! Lady Macbeth
Recall your unhappy childhood. Speak your mind!
Othello You are as vile as the sum of yourself
and a toad! Are you better than nothing? Lady
Macbeth If so, let us return to scene III.
Scene IV The end. Exeunt
404DL
- Another stack-based language, but here, the
operators manipulate either the top of stack, or
locations of a 4-D array - all operations are single ascii characters
- the instruction pointer (program counter) moves
not sequentially but based on the direction that
the instruction steers it (much like the change
of directions available in Argh!) - so the current instruction is at some location
(i, j, k, l) in the 4-D array and the instruction
might cause us to go to i, i--, j, j--, k,
k--, l or l-- - Instructions are to
- push items adjacent to the current instruction
pointer location (for instance, push i means
take the character at (i1, j, k, l) and push it
onto the stack) - add or subtract top two items of the stack
- pop off stack and if not zero, skip the next
coordinate in the current direction - skip the next character in the program
- change directions in 4-space (change the
dimension being incremented or decremented, or
change from incr to decr) - input to stack, output from stack
41Example
Hello World program
Shows control flow
P push X, p push X-, B push Y, b push Y-,
D push Z, d push Z-, Q push T, q push
T-, X/x change to X or X- direction (similarly
for Y/y/Z/z/T/t), , - input to stack, ? pop and
if not zero then skip instr, - end, 0 push 0
to stack, . pop and print
42Whitespace
- Most languages (and compilers) ignore white space
(blanks, tabs, return keys) but Whitespace is a
language that uses only these keystrokes as
meaningful elements - Whitespace uses a stack and a heap for storage
and has basic I/O and arithmetic operations, push
and pop, pointers, labels for branches,
conditional and unconditional branches, and
subroutines - definitely an obfuscated language
- arithmetic operations and the stack only operate
on integers (currently) - a program actually looks like empty space because
it is in fact full of spaces, tabs and returns! - programming requires fully commenting your code
because you cant actually see the code - The author claims that Whitespace is the perfect
language for spies since no one can actually see
the code to make sense of it!
43WhitespaceOperations
White Space Parameter Meaning Space Number Push
the number onto the stack LFSpace - Duplicate
the top item on the stack TabSpace Number Cop
y nth item on the stack to the top of the
stack LFTab - Swap the top two items on the
stack LFLF - Discard the top item on the
stack TabLF Number Slide n items off the
stack, keeping the top item SpaceSpace - Addit
ion SpaceTab - Subtraction SpaceLF - Multi
plication TabSpace - Integer
Division TabTab - Modulo Space - Store to
Heap Tab - Retrieve from Heap SpaceSpace La
bel Mark a location in the program SpaceTab La
bel Call a subroutine SpaceLF Label Jump
unconditionally to a label TabSpace Label Jump
to a label if the top of the stack is
zero TabTab Label Jump to a label if the top
of the stack is negative TabLF - End
subroutine, transfer control back to
caller LFLF - End the program SpaceSpace
- Output the character at the top of the
stack SpaceTab - Output the number at the top
of the stack TabSpace - Read character, place
it in location given by the top of the
stack TabTab - Read number, place it in
location given by the top of the stack
44Whitespace Example
SpaceSpaceSpaceTabLF Put a 1 on the
stack LFSpaceSpaceSpaceTabSpaceSpace
SpaceSpaceTabTabLF Set a Label
at this point SpaceLFSpace Duplicate the
top stack item TabLFSpaceTab Output the
current value SpaceSpaceSpaceTabSpaceTa
bSpaceLF Put 10 (newline) on the
stack... TabLFSpaceSpace ...and output
the newline SpaceSpaceSpaceTabLF Put a
1 on the stack TabSpaceSpaceSpace Increme
nts our current value. SpaceLFSpace Duplic
ate that value so we can test it SpaceSpaceSp
aceTabSpaceTabTabLF Push 11 onto the
stack TabSpaceSpaceTab Subtraction, if
we reached the end, we have a zero on the
stack LFTabSpaceSpaceTabSpaceSpace
SpaceTabSpaceTabLF If we have a
zero, jump to the end LFSpaceLFSpaceTab
Space SpaceSpaceSpaceTabTabLF Jump
to start LFSpaceSpaceSpaceTabSpace
SpaceSpaceTabSpaceTabLF Set the
end label SpaceLFLF Discard our
accumulator, to be tidy LFLFLF Finish
45Malbolge
- Designed in 1998 to be the hardest programming
language ever! - based on Intercal and Brainfck
- instructions effects are based on where it is
stored in memory mod 94 - instructions are self-modifying
- both code and pointers are incremented by 1 after
being accessed - only control statement is an unconditional jump
- data are stored in trinary (0, 1, 2) rather than
binary or decimal - no load or store operation, instead memory must
be pre-loaded - The above Hello World program was the most
complex Malbolge program ever written for years,
but a version of the 99 Bottles of Beer program
has been written - it consists of 7773 characters and is about 200
lines long of meaningless characters (see
http//www.99-bottles-of-beer.net/language-malbolg
e-375.html if you are interested)
Hello World Program (lt97lt5YXz7wT.3,O/o'KH"
'Dz_at_bLx8Xmrkpohm-kNigsedcba_
\ZYXWVUTSRQPONMLKJIHGFEDCBA_at_?gtlt9876543sOltoLm
the program is supposed to be on one line