TTM

NAME

ttm - macro processor

SYNOPSIS

ttm [-d string] [-e string] [-f|-p programfile] [-i] [-o file] [-r rsfile] [-V] [-q] [-X tag=value] [--] [arg...]

DESCRIPTION

TTM (or ttm) is a string oriented, general purpose macro processing developed in 1968 by Steven H. Caine and E. Kent Gordon at the California Institute of Technology. The following description is taken from the original TTM reference manual[1]. TTM Is a recursive, interpretive language designed primarily for string manipulation, text editing, macro definition and expansion, and other applications generally classified as systems programming. It is derived, primarily, from GAP [3] and GPM [4].

OPTIONS

Except where indicated, all flags may be repeated multiple times.
-d string
Set a variety of debug options as encoded in the string. Currently, the following debug flags are defined.

-e string
Scan the string and execute any macro invocations. Any output left after execution is discarded.

-p programfile
Read and scan the contents of the program file. Any output is discarded.

-i
After executing any -e or -p options, enter the read-eval loop to execute commands interactively. If not specified, then the interpreter will terminate execution after all other command line arguments have been processed.

-f inputfile
Specify the input file. This file will be scanned and any resulting output will be sent to the -o file, if specified, otherwise to standard output. Note that this is not the same as the file specified by the -r flag.

-o file
Send all output to the specified file. If not specified, then output is sent to standard output.

-r rsfile
Specify a file from which #<rs> will read its input. If not specified, standard input is assumed. Note that this is not the same as the file specified by the -f flag.

-V
Output the version number of ttm and then exit.
-q
Suppress the output remaining after scanning the -f file.

-X tag=value
Set a ttm interpreter resource limit. The tag is a single character indicating what resource to set, and the value is the value to assign to that resource. The legal tag characters are as follows:
TagResourceValueDescription
bBuffersizeinteger>0 Set the internal buffer size. The default is 2^20 characters. The suffix m|M is allowed to indicate multiplying by 2^20. The suffix k|K is allowed to indicate multiplying by 2^10;
sStacksizeinteger>0 Set the internal stack size. The default provides for a maximum depth of 64. It is a good idea to keep this number small so that runaway recursion can be detected quickly.
xExecutionsinteger>0 Limit the number of executions. The default is 2^20. The purpose is to catch tail recursive executions that do not eat up space.

--
Signal the end of options and disable further option processing. Anything after the -- is treated as an argument.

arg...
Specify arguments that are accessible using the #<argv> function.

Java Command Line Format

The java invocation of ttm differs from the C version as follows.

Python Command Line Format

The python invocation of ttm is essentially the same as the C command line.
    python ttm.py <command line arguments>

OVERALL OPERATION

The ttm macro processor starts by scanning any commands specified by the -e or -p options. Any result is discarded. The idea is that these options establish the environment in which to scan the input file.

Next, the interpreter scans the -f input file, if specified, and executes any macro invocations encountered. Any resulting output is sent to the file specified by the -o option. If not specified, the output is sent to standard output.

Finally, if the -i flag is specified, then the interpreter goes into a read-eval loop. It reads "balanced" strings from the standard input, scans the input and repeats. If no -i is specified, the interpreter exits.

The term balanced means that input is read until all instances of '<' have a matching '>'. Once balanced, input reading will stop at the currently defined EOL meta-character.

Any arguments after the file name are provided to ttm using the #<argv;i> function.

It is assumed that TTM is given a text file (via e.g. -f) containing some combination of ordinary text and TTM function calls (i.e. invocations). The text is scanned character by character. Any ordinary text is passed to the output unchanged (taking escapes into consideration). If a TTM function is encountered, it is collected and executed.

SYNTAX AND SEMANTICS

The general form of a TTM function call looks like this.
#<functionname;arg1;arg2;...;argn>
where the functionname and the arguments are arbitrary character strings not characters of significance: '#', '<', '>', and ';'. The function is invoked with the specified arguments and the resulting text is inserted into the original text in place of the function call. If the function call was prefixed by a single '#' character, then scanning will resume just before the inserted text from the function call. If the function call was prefixed by two '#' characters, then scanning resumes just after the inserted text.

During the collection of a function call, additional function calls may be encountered, for example, this.

#<functionname;arg1;#<f2;arg;...>;...;argn>
The nested function call will be invoked when encountered and the result inserted into the text of the outer function call and scanning of the outer function call resumes at the place indicated by the number of '#' characters preceding the nested call.

If a function takes, for example, 2 arguments, any extras are ignored. For user defined functions, if too few arguments are provided, additional one are added with the value of the empty string ("").

As with other applicative programming languages, a TTM function may be recursive and may be defined as the result of the invocation of a sequence of other function calls.

Functions are either built-in or user defined. A large number of built-in functions exist and are defined in the TTM reference manual [1].

Function definition

User defined functions are created using the following two built-in functions.

The first function, ds for "define string", defines a named string in the TTM dictionary. The name is "name" and its value is "text". Invoking this named string will cause it's invocation to be replaced by the value (i.e. "text").

The second function, ss for "segment string", scans the text of a previously defined string looking for occurrences of its arguments: text1, text2, ... textn. When an occurrence is found, it is replaced with a segment mark. All occurrences of each argument are replaced by the same segment mark.

When a segmented string is invoked, each argument to the call is substituted for the corresponding segment mark. Consider this example.

01 #<ds;F;abcxxdefyy>
02 #<ss;F;xx;yy>
03 #<F;11;22>
The string F is defined (line 1) and its body "abcxxdefyy" is segmented on the two strings "xx" and "yy" (line2). When invoked (line 3), it will return the value "abc11def22". In effect, we have a user defined function F with two arguments.

Escaping

It is possible to escape one or more characters using either of two conventions.

If a string is enclosed in <...>, then it is scanned but not interpreted by TTM. In the scanning process, the outer < and > brackets are removed. If there are nested occurrences of <...>, then they are scanned but the '<' and '>' are not removed. The brackets must balance: the number of '<' characters must equal the number of '>' characters. The '\' escape convention causes the interpreter to pass as-is the character after the '\'. The leading '\' is left if it within a <...> escape sequence, otherwise it is removed. One use is to allow unbalanced occurrences of '<' or '>' characters [Note: in the reference implementation, '@' was changed to '\'].

Examples

Example 1: Function Definition

The most basic example involves defining a function that is useful for defining addtional functions. This "meta" function is called def. It is written as:
#<ds;def;<##<ds;name;<text>>##<ss;name;subs>>>
#<ss;def;name;subs;text>

We can, for example, use def to define the string XX as 12345 and then segment XX on 34 by writing this.

#<def;XX;34;12345>

The call

#<XX;0000>
will then produce the string "1200005".

The def function operates by invoking ds to define the function being defined — XX in our example — and then segmenting the body of XX by any specified arguments: "34" in this case. When XX is invoked, its argument is substituted for the segment mark.

Example 2: Factorial

The factorial function can be defined (using the above #<def> function) as follows.
#<def;n!;N;<#<lt;N;2;1;<#<mu;N;#<n!;#<su;N;1>>>>>>>
Notice that the inner computation (#<mu...) is escaped so it will only be evaluated after the #<lt... function is executed and returns that nested computation as its result.

An example call would look like this. #<n!;3> and would return the string 6.

MODIFICATIONS

This ttm processor adheres closely to the specification in the ttm specification documents [1,2].

However, the following general changes to TTM should be noted.

  1. The escape character is changed from '@' to '\'.

  2. The end of input meta character is changed from ''' (i.e. single quote) to '\n' (i.e. newline).

  3. Escape handling is slightly different and adheres more closely to the Unix rules. Specifically, the following escape sequences, when occurring outside of <...>, are translated as follows.
    Escape SequenceTranslation
    \rcarriage return
    \nline feed
    \ttab
    \bbackspace
    \fform feed
    Additionally, if an escape occurs at the end of a line, then that end of line is ignored. This allows one to provide some formatting for function invocations without necessarily having to clutter up the result with new lines.

  4. Only lower case built-in function names are supported.

  5. This version of ttm supports (more or less) utf-8. The meta characters are restricted to be a subset of the US-ASCII 7-bit subset of utf-8. Otherwise, strings may contain any legal utf-8 multi-byte character. This allows for inclusion of utf-8 codepoints, but only limited validation is performed to see if the string is truly utf-8.

    Internally, and in the C implementation, all characters are represented as 32-bit UTF characters. This wastes some space, but makes processing simpler. For Python and Java, characters are represented internally in UTF-16 because they provide native support for that format.

A Note on Segment Marks

In this implementation, segment (and creation) marks are represented as a single 32-bit integer, where the value zero indicates a creation mark and any integer greater than zero indicates the n'th segment mark. The integer has bits set so that is is not a legal UTF character.

One more point about segment marks. It is possible using #<gn>, for example, to output a string with segment marks in it. The original documentation is silent on how such segment marks are to be treated. In this implementation, they are included. If an attempt is made to print them, they are converted to the form '^n' where n is the segment mark.

New Functions

A number of new functions have been implemented and are describe below using a notation similar to that in [1].

ttm
Specification: ttm,2,*,S
Invocation: #<ttm;cmd;arg1;arg2;...>
This function provides a general mechanism for embedding special commands to extract or alter interpreter information. The currently defined commands are as follows.
CommandDescription
#<ttm;meta;metachars> Change the set of meta-characters. It takes one argument that specifies the set of meta characters. Setting the default behavior would use this invocation: #<ttm;meta; \#\<\;\>\\>. Note that this is separate from the CM function.
#<ttm;info;subcommands;...> Dump information about internal ttm structures. The currently defined subcommands for info are as follows.
SubCommandDescription
#<ttm;info;name;name1;name2...> Return info about each namei.
#<ttm;info;class;class1;class2...> Return info about each classi.

include
Specification: include,1,1,S
Invocation: #<include;filename>
Read the contents of the specified file into the buffer and continue processing. For security reasons, the constraint is imposed that the file name must only be accessible through one of the include paths (i.e. using -I on the command line). This has the possibly undesirable consequence that if the user used #<include>, then the user must also specify a -I on the command line. This constraint also implies that the parameter filename must be a relative path. If the result from reading the file is unwanted, then one should use "-e #<include;filename>" on the command line.

argv
Specification: argv,1,1,V
Invocation: #<argv;i>
Return the i'th command line argument starting at 1 (like C). The parameter i should be a a non-negative integer. An argument of zero will return the invoking command's name. If the index is out of bounds, then a fatal error occurs => use the #<argc> function.

argc
Specification: argc,0,0,V
Invocation: #<argc>
Return the number of command line arguments, including the 0'th (like C).

classes
Specification: classes,0,0,V
Invocation: #<classes>
Return the names of all defined character classes arranged in sorted order and separated by commas.

pf
Specification: pf,0,1,S
Invocation: #<pf;stderr>
If no arguments are given, then flush stdout and stderr. If the single argument is the string 'stderr', then flush stderr. If the single argument is the string 'stdout' then flush stdout.

Modifications to Function Semantics

The semantics of the following functions others have been changed.

ad
Specification: ad,2,*,V
Invocation: #<ad;num1;...numn>
This function normally takes only two arguments. It has been extended to take an arbitrary number of arguments and return the sum of all of the arguments.

mu
Specification: mu,2,*,V
Invocation: #<mu;num1;...numn>
This function normally takes only two arguments. It has been extended to take an arbitrary number of arguments and return the product of all of the arguments.

ps
Specification: ps,1,2,S
Invocation: #<ps;string;stderr>
This function normally prints the string on stdout. If the second argument is the string "stderr", however, then it prints to stderr.

psr
Specification: psr,1,1,S
Invocation: #<psr;string>
This function prints its argument to stdout and then reads from stdin up to the meta-character (see the cm function).

cm
Specification: cm,1,1,S
Invocation: #<cm;string>
This function specifies the character that is used to terminate input when using, for example #<rs>. The default meta-character has been changed from "'" to "\n". This means that as a rule, reading will read only a single line of input. One can, however use the escape character at the end of the line to read multiple lines with the escaped '\n' being elided.

cf
Specification: cf,2,2,S
Invocation: #<cf;new-name;old-name>
The original definition said that the body of old-name, including segment marks and beginning at the old-name's residual pointer was copied to the body of new-name. However, trying to figure out how to handle a set of segment marks with gaps is problematic. So, the definition has been modified to copy the whole body of the old-name as the body of the new name, ignoring (but duplicating) the residual pointer.

exit
Specification: exit,0,1,S
Invocation: #<exit;exitcode>
This was changed to allow optional specification of a return code.

names
Specification: exit,0,1,V
Invocation: #<names>
This was changed so that if the second argument is present, then all names are returned, otherwise only non-builtin names are returned.

tn
Specification: tn,0,*,V
Invocation: #<tn;name1;...namen>
This was changed so that only the specified function names will be traced. If there are no names provided, then all function invocations will be traced.

tf
Specification: tf,0,*,V
Invocation: #<tn;name1;...namen>
This was changed so that only the specified function names will have tracing turned off. If there are no names provided, then no function invocations will be traced. Note that turning off all tracing will turn off tracing on all specific function.

ps
Specification: ps,1,2,S
Invocation: #<ps;string;stdxx>
This was changed to an optional allow a second argument that specifies sending the output to either "stdout" or "stderr". If missing, then stdout is assumed.

rs
Specification: rs,0,0,S
Invocation: #<rs>
Read a string of characters until either EOF or the defined "end-of-input" character is reached.

rs
Specification: rs,0,0,S
Invocation: #<rs>
In order to avoid spoofing, the string 'ttm>' is printed before reading.

The time function from the interactive version differs from that of the batch version. The interactive time function corresponds to the batch xtime function and the batch time function returns time of day instead of execution time. The batch versions of time and xtime are implemented. In additon, ctime is implemented to give time of day in Unix ctime format.

time
Specification: time,0,0,V
Invocation: #<time>
Returns the time of day in 100'ths of second from ?.

xtime
Specification: xtime,0,0,V
Invocation: #<time>
Returns the execution time in 100'ths of second.

ctime
Specification: ctime,1,1,V
Invocation: #<ctime;time>
Convert the time (the result of a call to #<time>) to a printable string as defined by the Unix ctime function.

A number of functions (#<gn>, $<cn>, etc.) are supposed to return some fixed number of characters. The original documentation is silent about what should happen if there are not enough characters available to return. In this implementation, whatever characters are available are returned, even if less than the number requested.

IMPLEMENTED FUNCTIONS

The following functions from the original ttm are implemented.
abs,1,1,V ad,2,2,V ap,2,2,S cc,1,1,SV
ccl,2,2,SV cf,2,2,S cm,1,1,S cn,2,2,SV
cp,1,1,SV cr,2,2,S cs,1,1,SV ctime,1,1,V
dcl,2,2,S dncl,2,2,S ds,2,2,S dv,2,2,V
dvr,2,2,V ecl,1,*,S eos,3,3,V eq,4,4,V
eq?,4,4,V es,1,*,S exit,0,0,S flip,1,1,V
gn,2,2,V gt,4,4,V gt?,4,4,V isc,4,4,SV
lt,4,4,V lt?,4,4,V mu,2,2,V names,0,1,V
ndf,3,3,V norm,1,1,V ps,1,2,S psr,1,1,SV
rrp,1,1,S rs,0,0,V sc,2,63,SV scl,2,2,S
scn,3,3,SV sn,2,2,S ss,2,2,S su,2,2,V
tcl,4,4,V tf,0,0,S time,0,0,V tn,0,0,S
xtime,0,0,V zlc,1,1,V zlcp,1,1,V

UNIMPLEMENTED FUNCTIONS

The following functions from the original ttm are not implemented.
rcd,2,2,Sfm,1,*,Stabs,1,8,Sscc,2,2,S
icc,1,1,Soutb,0,3,Sstore,2,2,Sdelete,1,1,S
copy,1,1,Sshow,0,1,Slibs,2,2,Sbreak,0,1,S

The following functions from the batch ttm are not implemented.

insw,2,2,S ttmsw,2,2,S cd,0,0,V cdsw,2,2,S
for,0,0,V forsw,2,2,S pk,0,0,V pksw,2,2,S
ps,1,1,S page,1,1,S sp,1,1,S fm,0,*,S
tabs,1,10,S scc,3,3,S fmsw,2,2,S des,1,1,S

BUGS

LICENSE

This software and documents is released under the terms of the Apache License version 2. For details of the license, see http://www.apache.org/licenses/LICENSE-2.0.

VERSION

ACKNOWLEDGEMENTS

The original ttm was designed and implemented by Steven H. Caine and E. Kent Gordon. (http://www.cfg.com/).

REFERENCES

  1. Caine, S.H. and Gordon, E.K., TTM: An Experimental Interpretive Language. California Institute of Technology, Willis H. Booth Computing Center, Programming Report No. 7, 1968.
  2. Caine, S.H. and Gordon, E.K., TTM: A Macro Language for Batch Processing. California Institute of Technology, Willis H. Booth Computing Center, Programming Report No. 8 May, 1969.
  3. Farber, D. J., 635 Assembly System - GAP. Bell Telephone Laboratories Computation Center (1964).
  4. Strachey, C., A General Purpose Macro Generator. Comput J 8, 3(1965), pp. 225-241..

SOURCE CODE

CHANGELOG

01 Oct. 2013— Added python implementation.
15 Nov. 2012— Version 1.0 release with C and Java implementations.