Корисник:Михајло Анђелковић/mtc (en)

Из Википедије, слободне енциклопедије
Nuvola apps kmessedwords.png (en) (de) (sr)

MTC (abbr. of Mediawiki Template Compiler) is a small compiler for Mediawiki software templates. Its purpose is to simplify the task of writing large and hard-to-oversee templates.

Syntax[уреди]

Variables[уреди]

Normally, when using variables in templates, one must always write six brackets to open and close the space for variable name ({{{1}}} for instance). In MTC this can be done using two dollar symbols instead of brackets:

 
$a sunny day$

will be compiled like:

 
{{{a sunny day}}}

Constants[уреди]

If an expression or larger chunk of code occurs more times in the template you write, writing it can be simplified by defining it as a constant. In MTC you can define a constant in the following way:

 
$$name content
  • Name must be concatenated with two dollars. It is the name of the constant you define and will be used later to call it in the code. It may consist from alphabet characters only.
  • Content must be separated from the name with at least one space. It may contain whatever you need, but it ends with a new line only. Not before, not after.

In order to use an constant, you need to write an @, with the constant name following. Let show it on an example. The following code:

 
$$diff *($3$ - $2$ + $1$)
@diff@diff

Will be compiled into:

 
{{ #expr: ({{{3}}} - {{{2}}} + {{{1}}}) }}{{ #expr: ({{{3}}} - {{{2}}} + {{{1}}}) }}

Note: when you 'call' a constant with @, you may put more variables in a row. For instance "@diff@diff@diff". But you can't concatenate this call with an alphanumeric character. For example

@diffis the distance between...

is wrong, while

@diff is the distance between...

is right.

Expressions[уреди]

Normally, usage of expressions in templates means using {{ #expr: ... }} form. MTC offers the same result, when using *( ... ). For instance:

 
*($2$ + $3$)

will be compiled as:

 
{{ #expr: ({{{2}}} + {{{3}}}) }}

Templates[уреди]

In MTC source you can call another template with symbol "`", its name, and small brackets () on the end. Additionally, these brackets can be used for putting some arguments for the template. Arguments should be separated with commas.

 
`template name(arg1,arg2,arg3)
`template name()

This will be compiled like:

 
{{template name|arg1|arg2|arg3}}
{{template name}}

The template name can contain spaces.

Conditions[уреди]

MTC makes difference between if and if-else conditions.

if[уреди]

A simple if condition, which can be called as follows:

if(condition)(code)

Which compiles like:

{{ #ifexpr: (condition) | code}}

ifif[уреди]

The if-else condition. The syntax is:

ifif(condition)(code when condition is true)(code when condition is false)

Which compiles like:

{{ #ifexpr: (condition) | code when condition is true | code when condition is false}}

pif[уреди]

Provides the construction for testing of variable existance. For instance:

pif(a variable)(code)

would compile as

{{ #if: {{{a variable|}}} | code }}

pifif[уреди]

Provides code2 form. For instance:

pifif(a variable)(code1)(code2)

would compile as

{{ #if: {{{a variable|}}} | code1 | code2 }}

Nesting[уреди]

Nesting should be fully allowed. Let me know if you notice something strange.

Special characters[уреди]

Since some characters have been used for marking up the text, they became inaccessible in the template text. To elude this, you can use backslash '\' as prefix for any of these characters, which will result in printing the character that followed after. This does not apply to spaces and n. The small letter 'n' put after backslash is interpreted as a new line. An example of use:

\^\`\n\n\$abc

Will be compiled as:

^`
 
$abc

Comments[уреди]

Like in the programming languages Java or C++, in MTC you can add comments wherever you like. There are two types of comments: double-slash (// ...) and slash-star (/* ... */). Here is an example which demonstrate possible usages

$$switch1arg <some robust expression> /* if an expression is too
                                         large, you can define it
                                         separately */
 
// now you can define the switch in the way you already know
$$sw/* impossible? */itch1 {{ #switch: @switch1arg | val1=res1 | val2=res2 | ... | defaultres }}
 
// and then you can use it wherever you like
@switch1

which compiles as just:

{{ #switch: <some robust expression> | val1=res1 | val2=res2 | ... | defaultres }}

An example[уреди]

Here is source of a more advanced program:

$$diff ($3$ - $2$ + 1)
 
*(
  $1$
  + `intcast(
         ifif($1$ > $3$)
           (*($2$ - $1$))
	   (
	    ifif($1$ < $2$)
	      (*($3$ - $1$))
	      (0)
	   )
	   / @diff
     )
     * @diff
)

When compiled, it looks like this (note: the code was wrapped in order not to degenerate the page):

{{ #expr: ( {{{1}}} + {{intcast|{{ #ifexpr: ({{{1}}} > {{{3}}}
) | {{ #expr: ({{{2}}} - {{{1}}}) }} |  {{ #ifexpr: ({{{1}}} <
{{{2}}}) | {{ #expr: ({{{3}}} - {{{1}}}) }} | 0}} }} / ({{{3}}}
- {{{2}}} + 1)}} * ({{{3}}} - {{{2}}} + 1)) }}

Legal notice[уреди]

To compile this tool, I used Borland C++ Compiler 5.5 (freeware tool) for windows, and GNU g++ 3.4.6 for linux. Anyone may use this software on any purpose.

External links[уреди]