Previous Up Next

Chapter 15  Using Tom

15.1  The Tom compiler

The Tom compiler is a non-intrusive pattern matching compiler. It takes a Tom program, combination of a host programming language (Java, C or Caml) extended by Tom constructs. Each Tom program can define its own data structures and manipulate them in order to write pattern-matching based algorithms.

15.2  Development process

As mentioned earlier, Tom generates host language code. The command tom invokes the compiler on a Tom source file and generates a new source file (with .java, .tom.c, or .tom.ml extension, depending on the compilation command-line). The generated code has to be compiled/interpreted by a tool which can handle the corresponding host language: javac for Java, cc for C and ocamlc for Caml.

A typical development cycle is the following:

As cc and javac, tom can compile several input files. By default, the generated files are saved in the same directory as the corresponding input files. When compiling a Java file (i.e. a Tom file where Java is the host language), Tom is smart enough to parse the package definition and generate the pure-Java file in the same package architecture. Similarly to javac, tom supports a -d option which allows the user to indicate where the files have to be generated. To be compatible with cc and classical Makefile, Tom also supports a -o option which can be used to specify the name of the generated file.

Note: Only if a manual installation was performed (without using the kit), Windows users can use the provided shell script javacForTom.bat in order to compile the Java files that are generated by Tom. Another script, javaForTom.bat can be used to execute the Java bytecode. We recommend the use of these scripts since they update the CLASSPATH according to the ’*.jar’ files that are in the distribution. If you used the install kit, the CLASSPATH was updated accordingly at the install time.

15.3  Command line tool

tom [options] filename[.t]

The command takes only one file argument, the name of the input file to be compiled. By convention the input file name is expected to be of the form filename.t, whatever the used host language is.

Options:


--config | -X <file>Define an alternate XML configuration file
 Allow to define the plugins instantiated by the Tom platform
--cCode | -cGenerate C code (default is Java)
 The output file has the extension .tom.c
--camlCodeGenerate Caml code (default is Java)
--camlSemanticsVerify with caml semantics for match)
--compileActivate the compiler (by default)
--csCodeGenerate C# code
--destdir | -dSpecify where to place generated files.
 By default, Tom generates files close to the input file. However (like javac), this option allows to specify a directory where generated files have to be put. When compiling a Java file, Tom is smart enough to parse the package definition and generate the pure-Java file in the same package architecture.
--eclipseActivate Eclipse mode
--encoding <charset> | -eSpecify the character encoding
--expandActivate the expander (by default)
--genIntrospector | -gi Generate a class that implements Introspector to apply strategies on non visitable terms
--help | -hShow the help
 Give a brief description of each available options
--import <path> | -IPath to included files
 Even if inclusion can be performed using relative path, this option specifies list of path where Tom look for when an inclusion has to be done by %include construct
--inlineInline mapping
--inlineplusForce inlining, even if no $ is used
--intermediate | -iGenerate intermediate files
 The compiler manipulates Abstract Syntax Trees. This option dumps the AST after each phase (parsing, checking, expansion, compilation) This option is useful to analyze the compilation process
--jCode | -jGenerate Java code (by default)
--lazyType | -lUse universal type
 This option makes Tom using a less restrictive backend. In Java, the universal sort Object is used more often. This reduces static typing but allows to manipulate inherited data-structures
--noDeclaration | -DDo not generate code for declarations
 Avoid multiple declaration of symbols when structures are inherited or when multiple inclusion of common data structures are performed
--noOutputDo not generate code
 No output file is generated. It allows to see warnings and errors without generating the result
--noReduceDo not simplify extracted constraints (depends on –verify)
--noStaticGenerate non static functions
--noSyntaxCheckDo not perform syntax checking
--noTypeCheckDo not perform type checking
--optimize | -OOptimize generated code
 Add an optimization phase to the compilation process. This removes unused variables and performs some inlining.
--optimize2 | -O2Further optimize generated code (does not imply -O)
--output | -oSet output file name
 By default, Tom infers the name of the generated file by replacing the initial file extension by .java, .tom.c or .tom.ml, depending on the chosen target language. This option allows to explicitly give the name of the generated file.
--pCodeGenerate Python code
--parseActivate the parser (by default)
--pretty | -pGenerate readable code with indentation
 By default, the generated code is synchronized with the source code. This simplifies error reporting but makes the code more difficult to read. This option beautifies the generated code.
--prettyPIL | -pilPrettyprint the intermediate language
--protectedGenerate protected functions
 In Java, this option forces the generation of protected functions, instead of private ones, for symbol manipulation functions
--typeTyper (activated by default)
--verbose | -vSet verbose mode on
 Give duration information about each compilation passes
--verifyVerify correctness of match compilation
--version | -VPrint the version of Tom
--wallPrint all warnings
 Useful in debugging phase

15.4  Ant task

Tom provides an ant task for running the Tom compiler within the apache ant build system.

The Tom ant task is very close in use to the javac ant task. Since this task is not part of the official ant tasks, you have first to declare this task in your buildfile, in order to use it.

This is done by the following code:

<taskdef name="tom" classname="tom.engine.tools.ant.TomTask">
  <classpath refid="tom.classpath"/>
</taskdef>

where tom.classpath is the path reference containing all the jar’s in Tom’s distribution predefined in the file ${TOM_HOME}/lib/tom-common.xml.

This task is used to produce Java code from Tom programs. A typical use of the Tom ant task is:

<tom config="${tom.configfile}"
     srcdir="${src.dir}"
     destdir="${gen.dir}"
     options="-I ${mapping.dir}">
  <include name="**/*.t"/>
</tom>

Here, we want to compile all Tom source files in {src.dir}, having the generated code in {gen.dir}, and we configure the compiler to use the {tom.configfile} config file, and pass the options we want like, for example {-I} to indicate the mapping needed for compilation, just as we do for Tom in command line.

Another use of this task is:

<tom config="${tom.configfile}"
     srcdir="${src.dir}"
     outputfile="${gen.dir}/Example.java"
     options="-I ${mapping.dir}">
  <include name="**/Example.t"/>
</tom>

Here we compile only one Tom file, and specify directly the output file name we want to use.

The main usecases for the Tom ant task can be found in Tom’s own buildfile.

The Tom ant task takes the following arguments:

AttributeDescriptionRequired
classpathThe classpath to use, given as a reference to a path defined elsewhere. This variable is not used currently.No
configLocation of the Tom configuration file.No
destdirLocation of the Java files.No
failonerrorIndicates whether the build will continue even if there are compilation errors; defaults to trueNo
logfileWhich log file to use.No
nowarnAsks the compiler not to report all warnings; defaults to noNo
optimizeIndicates whether source should be compiled with optimization; defaults to offNo
optionsCustom options to pass to the Tom compilerNo
outputfileDestination file to compile the source. This require to have only one source file.No
prettyAsks the compiler to generate more human readable code; defaults to noNo
srcdirLocation of the Tom files.Yes, unless nested <src> elements are present
verboseAsks the compiler for verbose output; defaults to noNo

Previous Up Next