Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A new, simple language: rename

43 views
Skip to first unread message

Claudio Calvelli

unread,
Jan 7, 2010, 6:24:00 PM1/7/10
to
I just thought of a simple, new language, "rename". A program is a sequence
of opcodes, specified one per line, just like, say, assembler, with the
difference that they are not executed in the order one would expect, and
programs rely on the ability to "rename" opcodes for flow of control.

It is possible to write very compact programs. However the effort involved
is far greater than writing INTERCAL. It is also possible to write very
large programs which do very little.

Internally, opcodes are represented as single bytes, one byte for each
line of the source program. This is important to remember because the
"rename" opcode changes the internal representation, not the source code.

The opcodes operate on a stack. Values stored on the stack can be numbers
or strings: if an opcode expects a string and finds a number, it will
convert the number to its string representation; if it expects a number
and finds a string, it will attempt to interpret the string as a number,
using the value 0 if it fails. So far so normal.

Program execution is essentially the following loop:

while (hell has not yet frozen over) {
find all opcodes with value 0
if none found, exit program
execute the opcode after each 0
}

If the last opcode in the program is 0, the one "after" it is the first
one. However the opcodes are executed in the order the 0s appear in the
program, so the first opcode would be executed last.

Note that if the program does not move the "0" opcodes around this will
repeat the same operation over and over again. Normally, the program will
use one or more "rename" opcodes to modify this loop.

If this seems unclear, the "Hello world" example at the end will make
it completely transparent. Just bear with me.

The following opcodes have been defined:

hex source description
code code
00 (blank line) Execute the next opcode
01 PUSH Push the next opcode to the stack (as a character)
02 POP Discard the top of the stack
03 COPY Duplicate the value on top of the stack
04 APPEND Append the next opcode to the top of the stack (string)
05 INPUT Reads one character from standard input, push to the stack
06 OUTPUT Prints (and discards) the top of the stack
07 SWAP Swap the two values on top of the stack
08 ALTER Pop a string from stack; copy it to program memory after
this opcode (this ALTERs the program)
09 ADD Pop a number, add it to new top
0A SUBTRACT Pop a number, subtract it from new top
0B MULTIPLY Pop a number, multiply new top by this value
0C DIVIDE Pop a number, divide new top by this value
0D NEGATE Negate number on top of stack
0E CONCATENATE Pop top of stack, append it to new top (string)
0F RENAME Add the next opcode to every single opcode in the program
(with wraparound, so 200 + 57 == 1)
10-13 reserved (I know what I want here but I'm not telling yet)
14 ARGUMENT Push next command line argument to stack
15 COUNT Push number of command line arguments remaining
16 DEPTH Push the number of stack elements to stack
17 ROTATE Pops two numbers n, m. Rotate the topmost n elements by m
18 OROTATE Same as rotate, but uses next two opcodes as n, m
19 DIG Pops a number(*) n. Duplicates n-th topmost element to top
1A ODIG Same as dig, but uses next opcode as n
1B-FF reserved

Additionally, a quote character followed by anything results in the
character to be stored in program memory, so

PUSH
"A

Means push the 1-character string "A" to the stack.

Anything after the opcode is ignored, so it can be used for comments,
misleading or otherwise.

Just to see how it works, I've written an interpreter, although it
only implements 14 of the 22 opcodes defined. This is enough to print
"Hello World" using the following (clearly not very optimised) program
(note that it is absolutely important to have exactly one blank line at
the end, or the program stops working):

-------------------------------- cut here --------------------------------
RENAME "Hello World" program
PUSH

PUSH
"o

COPY

PUSH
"l

COPY

CONCATENATE

PUSH
"H

APPEND
"e

SWAP

CONCATENATE

SWAP

CONCATENATE

APPEND
" make sure to have a space after the "

APPEND
"W

SWAP

CONCATENATE

APPEND
"r

APPEND
"l

APPEND
"d

APPEND
SUBTRACT This is a newline. No, really

OUTPUT

-------------------------------- cut here --------------------------------

Writing a more optimised "Hello World" program is left as an exercise to
the reader. I'm sure it's also possible to write an INTERCAL compiler
in "rename", but I haven't tried yet.

The incomplete interpreter can be found at
http://clc.intercal.org.uk/rename.c

For simplicity, the "Hello World" program is at
http://clc.intercal.org.uk/hello.rename

Compile and use as "rename NAME_OF_PROGRAM" for example if you copy the
"Hello World" program to a file "hello.rename" you can say:

rename hello.rename

If your operating system has a "rename" command you may need to specify
the full path.

Claudio, trying to avoid INTERCAL for a while

0 new messages