add a print handler to Printer module - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Code Generation Package : Language Definition : Printer : CodeGeneration/LanguageDefinition/Printer/AddPrintHandler

Printer:-AddPrintHandler - add a print handler to Printer module

Printer:-GetPrintHandler - get a print handler from a Printer module

Calling Sequence

Printer:-AddPrintHandler(icname=handler [,icname1=handler2,...])

Printer:-AddPrintHandler(icname [,icname2,...])

Printer:-GetPrintHandler(icname)

Parameters

Printer

-

a Printer module

icname

-

name; Intermediate Code name

handler

-

procedure; procedure to print icname numeric=nprec

Description

• 

The procedure AddPrintHandler defines a handler procedure for the intermediate code symbol icname.  The handler procedure is called to print any intermediate form expression for which icname is the zeroth operand. Note that multiple arguments of the form icname=handler (or icname) may be passed in a single call to Printer:-AddPrintHandler.

• 

To clear (that is, remove) an existing print handler for a particular intermediate code symbol icname, use the calling sequence Printer:-AddPrintHandler(icname).

• 

A print handler procedure accepts an intermediate form expression, and issues Print statements necessary to properly print the intermediate form.  A print handler procedure's return value is ignored.

• 

The GetPrintHandler command returns the print handler procedure for the intermediate code name icname.

• 

The module returned by DefaultPrinter has default print handlers for every intermediate code symbol.  For simplicity, overriding these default definitions with AddPrintHandler should be done only if the form of the printed expression differs significantly from the form suggested by the default print handler.

  

Instead, include language-specific information by overriding language attributes with SetLanguageAttribute.

  

In some cases, the desired appearance of icname may differ significantly from what is possible with default print handlers. In these cases, AddPrintHandler should be used.

Examples

Define a custom language extending Java, which prints floating-point numbers in standard decimal notion if possible. (By default, the Java translator uses scientific notation.)

withCodeGeneration:

LanguageDefinition[Define]("AddPrintHandler_Example1", extend="Java",
     AddPrintHandler(
         Names:-Float = proc(mantissa, exponent)
            Printer:-Print( sprintf("%g", mantissa*10^exponent) );
         end proc
     )
);
Translate(3.14159, language="Java");

cg = 0.314159e1;

Translate3.14159,language=AddPrintHandler_Example1

cg0 = 3.14159;

Next, suppose we have a set of C functions, called addInteger, addNumeric, multiplyInteger, and multiplyNumeric, that we wish to use for integer or numeric addition or subtraction, instead of using the built-in C operators. Here we see how an extension language can be defined to achieve this.

LanguageDefinition[Define]("AddPrintHandler_Example2", extend="C",
    AddPrintHandler(
        Names:-Product = proc()
            local t, i;
            t := Printer:-GetExpressionType( Names:-Product(args) );
            if t = Names:-Type('numeric') then
                Printer:-Print( "multiplyNumeric(" );
            else
                Printer:-Print( "multiplyInteger(" );
            end if;
            for i from 1 to nargs-1 do
                Printer:-Print( args[i], ", " );
            end do;
            Printer:-Print( args[nargs], ")" );
        end proc,
        Names:-Sum = proc()
            local t, i;
            t := Printer:-GetExpressionType( Names:-Sum(args) );
            if t = Names:-Type('numeric') then
                Printer:-Print( "addNumeric(" );
            else
                Printer:-Print( "addInteger(" );
            end if;
            for i from 1 to nargs-1 do
                Printer:-Print( args[i], ", " );
            end do;
            Printer:-Print( args[nargs], ")" );
        end proc,
        Names:-Coercion = proc(x) Printer:-Print(x) end proc
    )
);
Translate( proc(x) (2*x+1)*3.5 end proc, language="AddPrintHandler_Example2");

double cg1 (int x)
{
  return(multiplyNumeric(0.35e1, addInteger(multiplyInteger(2, x), 1)));
}

See Also

DefaultPrinter, LanguageAttribute, Print, Printer


Download Help Document

Copyright © MathResources Inc. All Rights Reserved.

www.mathresources.com

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam