|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ExpressionsAboutExpressions in different programming languages different greatly in precedence structure. As a result, templates use a simple system to convert any Flowgorithm expression into the target language's format. To accomplish this, templates use the same system for both intrinsic function calls (such as Cos, ToInteger, etc...) and operators. Each function/operator has the same fields, namely {1} and {2}. These represent the values passed into an intrinsic function or the left-hand-side and right-hand-side of an operator.
Precedence LevelsEach function/operator also contains shared keys for the precedence of the operator (in the target programming language) and what precedence is needed for {1} and {2}. The higher the numeric value given to the function/operator, the higher its precedence. For example, in most programming languages, multiplication and division are computed before addition and subtraction. In this case, the multiplication and division operators will have a higher precedence value.
Subexpression SectionWhenever the system needs to increase the precedence of either {1} or {2}, it will use the [Subexpression] section. This section differs from the other function/operators given that it contains no flags and only has one field {Expression}. The example below is fairly universal for expressions in all programming languages.
Expressions - Sections and FlagsSectionsThe following sections use the expression format. They call contain the same keys, flags, and basic behavior.
Keys
The following example defines a basic logical-and operator in Java. The needed precedence values for {1} and {2} are typical of left-to-right operators that allow changing (e.g. x && y && z).
FlagsDifferent programming languages often use different operators, or library calls, based on the data type being used. Sometimes an operator is built directly into a programming languages while, in other cases, it requires a library call. A good example of this is the exponent operator. In Flowgorithm and the BASIC family of languages, the caret ^ is used to denote an exponent. Languages in the C-Family (such as Java), tend to use a library function call - namely Pow(). Also, programming languages have different rules that define which data type is returned from a calculation. These various "usual arithmetic conversion" can vary greatly between languages. For example, in the C-Family of languages the expression "1 / 2" will return zero. The rules state that if both operands are integers then integer math is used. In Flowgorithm and the BASIC-Family, floating point is always used for division which results in 0.5. So, to handle all these different scenarios, templates contain a large number of flags so the correct syntax can be selected. The following example defines a basic addition operator in Java. The needed precedence for {1} and {2} are typical of left-to-right operators that allow chaining (e.g. 1 + 2 + 3 + 4). The Type Key is defined to follow Java's "Usual Arithmetic Conversions".
The following flags allow you to check for a certain combination between {1} and {2}. Often these are used with the negation prefix ~ to handle special cases.
The example below shows how Java treats strings and numeric values differently. Note: The "Needed" precedence varies greatly based on whether a string is being compared to another string. In the final line, when .equals is used, {1} needs to have a precedence of 100 (max in this template) and {2} only requires 1 (given it is inside parenthesis).
The type of {1} and {2} can also be checked separately.
|