CodeGeneration[LanguageDefinition]
Define
define a target language module
Calling Sequence
Parameters
Description
Examples
CodeGeneration[LanguageDefinition][Define](lang, extend=ext_lang, f1, f2,...)
lang
-
string; name of target language
ext_lang
(optional) string; name of language to extend
f1, f2, ...
function calls with the function name being one of AddFunction, AddFunctions, AddOperator, AddPrintHandler, AddType, or SetLanguageAttribute
The Define function defines a language for use with CodeGeneration.
f1, f2, ... can be any number of function calls, with the function's name (zeroth operand) being one of AddFunction, AddFunctions, AddOperator, AddPrintHandler, AddType, SetLanguageAttribute. These function calls accept exactly the same syntax as the Printer functions of the same name.
When ext_lang is provided, the resulting language definition module is identical to the module for language ext_lang, except where the function calls f1, f2, ... override the data of ext_lang. This provides a convenient mechanism for altering a language in simple ways and testing the result.
Note: In the user-defined procedures added through the function calls f1, f2, ... , it is often necessary to refer to exports of a Printer module, such as Indent, Print, or GetPrecedence. For correct behavior, every occurrence of these Printer exports within user-defined procedures must be explicitly declared as Printer exports by prefixing them with 'Printer:-', for example, Printer:-Indent, Printer:-Print. Otherwise, these references will not be properly handled by Define.
Define a custom language DefineExample, using the pre-defined ANSI C as a base, which uses tab characters for indentation and := for assignment. Translate a simple expression to the language DefineExample.
with⁡CodeGeneration:
LanguageDefinition[Define]("DefineExample", extend = "C", SetLanguageAttribute( "Indent_Char" = " ", # use tab character for indentation "Indent_Base" = 1, "Indent_Increment" = 1 ), AddOperator( Names:-Assignment = ":=" ), AddPrintHandler( Names:-Integer = proc(x) Printer:-Print( convert(x, 'string') ) end proc ) );
Translate⁡sin⁡x,language=DefineExample
cg := sin(x);
See Also
Add
Language Definition Overview
LanguageDefinition
LanguageModule
Printer
Download Help Document