|
manual
User Manual - 1st draft
minimac - a minimalist macro processor
1 Introduction1.1 SynopsisThanks for you interest in minimac. Minimac is a minimalist macro processor that is intended as a front end preprocessor for little language compilers. Minimac is open source software. The project's home page on Google Code is http://code.google.com/p/minimac/ Minimac is written in C, and distributed under a BSD license. 1.2 Current StatusThis project was started in April 2009. The software is currently in alpha release, it meets the author's current needs. Changes or additions may be made as flaws are found or new user requirements are submitted. This Wiki is an initial draft of the users manual.
All feedback is very welcome, I've set up a Google Group for discussing the project: http://groups.google.com/group/minimac-discuss 1.3 NotationIn this manual all output from minimac is prefixed with '=>'. 1.4 Alternatives to minimac
If minimac is not suited to your needs, the following open source macro-processors are possible alternatives: The Wikipedia page on General Purpose Macro Processors gives an overview of this category of software: http://en.wikipedia.org/wiki/General_purpose_macro_processor 2 Invoking minimacMinimac consists of a single OS-independent Ansi C source file, the naming convention is mm-<version info>.c To compile minimac refer to your C compiler instructions, a typical compile on a Unix based sytem would look something like this: The minimac executable is named 'mm'. Minimac reads from standard input and writes the processed text to standard output, error messages are written to standard error. Minimac currently does not recognize any command-line switches or options. Minimac exits 0 on success, and 1 if an error occurs. Since Minimac is a filter program, a good way to learn minimac is to interactively type the examples in this manual from your shell console into Minimac's standard input. 3 Defining macros3.1 Defining a basic substitution macroA macro definition is composed of a name and a body. A macro definition begins with the backquote character ` (upper left key on most keyboards) followed by the macro name. A second backquote begins the body of the definition, and a third backquote ends the definition. The following example defines the macro 'foo' that expands to `Hello world!' Any text between the first space after end of the name and the body of the definition is ignored. The following examples are equivalent: Macro names are case sensitive: The diagnostic built-in {.m} displays a list of the names and current values of user-defined macros: The {toggle!} built-in can be used to change the reserved character from the default of '`' to another character. 3.2 Redefining a macroA new definition of a macro replaces the previous definition from that point onwards. 3.3 Nested macrosMacro expansion is delayed to the last possible moment. 3.4 Self-referential macrosThe caret '^' character in the body of a macro expands to the latest definition of the same name as the macro which contains it. The {self!} built-in can be used to change the reserved character from the default of '^'. 3.5 Embedded macro namesTo expand a macro name that is adjacent to a non-whitespace character, place a dollar '$' character between the name and the non-whitespace character: The {embed!} built-in can be used to change the reserved character from the default of '$' to another character. 3.6 EscapingThe backslash '\' character is used as an escape character, i.e. the character immediately following the backslash is interpreted literally. [...STD there's more to it than this, a more thorough explanation is required here...] The {escape!} built-in can be used to change the reserved character from the default of '\' to another character. [...STD should we use a character other than '\' as the default escape character?...] 3.7 Ignoring whitespace within a definitionThe left-bracket '[' character within a definition causes runs of whitespace to be replaced by runs of the dollar '$' character. The right-bracket ']' resumes normal whitespace expansion. The {wsoff!} and {wson!} built-ins can be used to change the reserved characters from their defaults of '[' and ']' to other characters. [...STD should we use a character other than brackets as the default characters?...] 3.8 QuotingA string surrounded by the tilde character '~' is output as a literal string (i.e. without expansion, but with escaping). The {quote!} built-in can be used to change the reserved character from the default of '~' to another character. 4 Built-in functions4.1 How to use to built-insBuilt-in are special names that invoke useful behavior or side-effects when expanded. For example the increment built-in, whose name is '{++}', is used to increment a number.
4.2 The macro argument stack
When a built-in requires one or more arguments, these are taken from the macro argument stack. The contents of the argument stack can be displayed at any time for diagnostic purposes by invoking the '{.s}' built-in. To push an item, usually a token, onto the stack, append the ampersand character '&' to it, whitespace immediately following the '&' is ignored. Macro names can be pushed onto the stack: Use the expand `{?}' built-in to pop the top item off and expand it. Literal (i.e. unexpanded) strings can also be pushed onto the stack: The {push!} built-in can be used to change the reserved character from the default of '&' to another character. 4.3 Stack effect notationThe following section is a catalog of the minimac's built-ins, included with each built-in's description is a stack effect diagram, in a notation similar to that used by the Forth language, a stack-based language which inspired much of minimac's philosophy and design.
The stack effect diagram documents the items that are required to be on the stack before the built-in is invoked, and the after-effect on the stack of the built-in's invocation. For example, the stack effect for the '{+}' built-in is '( n1 n2 -- n1+n2 )', meaning that it expects two (decimal) integers on the stack, which it will remove, and leave their sum in their place. In both the before and after sections of the stack diagram the rightmost item is considered to be at the top of the stack. The stack effect diagram '( -- )' denotes a built-in that has no expectations of the stack nor after-effects on the stack. 5 Catalog of built-ins5.1 Integer arithmetic
Example: {++} - increment{++} ( n -- n+1 ) or ( name -- ) If the top stack item is a name, its body is incremented, otherwise the top stack item is treated as a literal and incremented in place. Example:...coming soon {--} - decrement{--} ( n -- n-1 ) or ( name -- ) If the top stack item is a name, its body is decremented, otherwise the top stack item is treated as a literal and decremented in place. Example:...coming soon 5.2 Integer comparisonAll comparison operators return canonical truth values, -1 for true, 0 for false.
5.3 Variables{@} - fetch{@} ( name -- value ) Fetch the value of a macro variable. {!} - store{!} ( value name -- ) Store a value into a macro variable.
5.4 Control flow{case} - case conditional{case} ( x y action -- ???|x ) The {case} built-in is the building block for constructing what are variously known as case or switch statements, where a value x is compared to various values and if a match is found a corresponding block of code is executed.
If x and y are identical, removes x and y from the stack, and expands the action string at the top of stack, the remainder of the enclosing definition is no longer expanded. Conversely, if x and y do not match, x remains on the stack and expansion of the remainder of the enclosing definition resumes. Example: {continue} - conditional expansion{continue} ( x -- x| ) Expands the rest of the definition only if the integer value of x is not zero. Conversely if x is zero, x is dropped from the stack, and the remainder of the enclosing definition is no longer expanded. Example: See {repeat} {exit} - exiting to the OS{exit} ( n -- ) or {exit} ( -- ) Exits to the OS using the top of stack as the exit code. If the stack is empty when {exit} is invoked a default exit code of 0 is used. {?} - expand{?} ( action -- ??? ) Expand the top of stack. See also: {?>} - expand to stack{?>} ( action -- ??? output ) Expands the top of stack, all output intended for stdout (or the current diversion) is instead collated in a buffer which is then pushed onto the stack. Example: See also: {if} - if conditional{if} ( n action -- ???| ) Expands the action string on the top of the stack if n is non-zero. Example: {ifelse} - if-else conditional{ifelse} ( n if-action else-action -- ??? ) Expands the if-action if n is non-zero, otherwise expands the else-action. Example: {repeat} - looping, tail recursion{repeat} ( -- ) Repeats the expansion of the enclosing definition. Example: {?repeat} - conditional looping, tail recursion{?repeat} ( n -- ) Repeats the expansion of the enclosing definition if the top of stack is non-zero. 5.5 Input and Output{.} ( str -- ) Print the top of stack without expansion. See also: {divert} - output diversion{divert} ( filename -- ) Temporarily diverts the output stream to a file. Overwrites the file if it already exists. See also: {divert+} - output diversion append{divert+} ( filename -- ) Temporarily diverts the output stream to a file. Appends to the file if it already exists. See also: {include} - file inclusion{include} ( filename -- ) Causes the file named to be read and expanded. {undivert} - cease current output diversion{undivert} ( -- ) Ceases the most recent output diversion started by {divert} or {divert+}. {verbatim} - file inclusion (without expansion){verbatim} ( filename -- ) Causes the file named to be read and sent directly to the output stream without expansion. 5.6 Parameter Stack
5.7 Auxiliary stack
5.8 Strings{append} - append to a string variable{append} ( str name -- ) {cat} - concatenate strings{cat} ( str1 str2 -- str1str2 ) {len} - string length{len} ( str -- u ) {lower} - convert to lowercase{lower} ( str -- str' ) {replace} - substring replacement{replace} ( str fromStr toStr -- str' ) Replaces all occurences of fromStr within str to toStr. {same?} - test for string equality{same?} ( str1 str2 -- 0|-1 ) {substr} - extract a substring{substr} ( str start length -- str' ) {tr} - translate characters{tr} ( str from to -- str' ) {upper} - convert to uppercase{upper} ( str -- str' ) 5.9 Bitwise
If provided with canonical truth values, i.e. -1 for true and 0 for false, the bitwise operators can also serve as logical operators.
5.10 Diagnostic utilitiesAll diagnostic utilities write their output to the standard error stream. {.a} - display auxiliary stack contents{.a} ( -- ) {.m} - display macro definitions{.m} ( -- ) {.s} - display stack contents{.s} ( -- ) 5.11 Reserved special characters
Operators that for checking and changing minimac's reserved special characters.
5.12 Miscellaneous{#} - comment{#} ( -- ) Treats the remainder of the line as a comment. {env} - get the value of an OS environment variable{env} ( env-var -- value ) {last} - last dictionary definition{last} ( -- name ) Returns the name of the most recent user-defined macro. {remove} - file deletion{remove} ( filename -- ) {shell} - execute an operating system command{shell} ( str -- return-code ) {undefine} - remove a user-defined macro from the dictionary{undefine} ( macroname -- ) {version} - version string{version} ( -- str ) Pushes a string containing the minimac processor's version. 6 Sample ExtensionsExamples of possible extension macros are supplied in the extend-<version info>.mm file available for download at http://code.google.com/p/minimac/downloads/list 7 Sample macrosThe classic '99 bottles of beer' example is available for download in the file 99bottles.mm at: http://code.google.com/p/minimac/downloads/list Examples are also available in the examples wiki page at: http://code.google.com/p/minimac/w/list 8 Quirks and limitations
Appendix A - minimac Quick ReferenceSpecial characters:
Buit-ins:...unfinished see rest of manual... Extensions:...unfinished see rest of manual...
...unfinished - more to come Appendix B - BSD Documentation LicenseRedistribution and use in source (text) and 'compiled' forms (SGML, HTML, PDF, PostScript, RTF and so forth) with or without modification, are permitted provided that the following conditions are met:
THIS DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This notice shall appear on any product containing this material. minimac - a minimalist macro processor Copyright (c) 2009 Mark W. Humphries. All rights reserved. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sign in to add a comment