﻿ Flowgorithm - Templates - Expressions

# Expressions

Expressions 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.

 Fields Contents {1} The expression used in the left-hand-side of an operator or the first argument into an intrinsic function. {2} The expression used in the right-hand-side of an operator or the second argument into an intrinsic function.

## Precedence Levels

Each 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.

 Java Example [Cos] Precedence = 100 Needed 1 = 0 Text = Math.cos({1})

## Subexpression Section

Whenever 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.

 Java Example [Subexpression] Precedence = 100 Text = ({Expression})

# Expressions - Sections and Flags

## Sections

The following sections use the expression format. They call contain the same keys, flags, and basic behavior.

 Operator Sections [Add] [Greater Than] [Multiply] [Power] [And] [Greater Equal Than] [Negate] [Subtract] [Concatenate] [Less Than] [Not] [Divide] [Less Equal Than] [Not Equals] [Equals] [Modulus] [Or]
 Intrinsic Function Sections [Abs] [Cos] [Pi] [Sqrt] [ToInteger] [ArcCos] [Int] [Random] [Tan] [ToReal] [ArcSin] [Len] [Sin] [ToChar] [ToString] [ArcTan] [Log] [Sgn] [ToCode] [Char] [Log10] [Size] [ToFixed]

## Keys

 Fields Contents Precedence A numeric value indicating the precedence level of this function/operator. Type If the data type, created by this function/operator, differs from Flowgorithm, then this field can specify the new type. For example, in Java, 1 / 2 returns a integer. Flowgorithm returns a real. Valid values are: Integer, Real, String, and Boolean. Needed 1 The needed precedence level for {1}. Typically this is equal to or greater than the value specified in the Precedence Key. Needed 2 The needed precedence level for {2}. Typically this is equal to or greater than the value specified in the Precedence Key. Text The syntax of the function/operator.

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).

 Java Example - And Operator [And] Precedence = 2 Needed 1 = 2 Needed 2 = 3 Text = {1} && {2}

## Flags

Different 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".

 Java Example - Add Operator [Add] Type = integer | integer-integer = real | ~integer-integer Precedence = 5 Needed 1 = 5 Needed 2 = 6 Text = {1} + {2}

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.

 Combination Flag When True string-string {1} is a string, {2} is a string string-integer {1} is a string, {2} is an integer string-real {1} is a string, {2} is a real string-boolean {1} is a string, {2} is a boolean integer-string {1} is an integer, {2} is a string integer-integer {1} is an integer, {2} is an integer integer-real {1} is an integer, {2} is a real integer-boolean {1} is an integer, {2} is a Boolean real-string {1} is a real, {2} is a string real-integer {1} is a real, {2} is an integer real-real {1} is a real, {2} is a real real-boolean {1} is a real, {2} is a Boolean boolean-string {1} is a boolean, {2} is a string boolean-integer {1} is a boolean, {2} is a string boolean-real {1} is a boolean, {2} is a string boolean-boolean {1} is a boolean, {2} is a string

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).

 Java Example [Equals] Precedence = 3 | ~string-string = 100 | string-string Needed 1 = 4 | ~string-string = 100 | string-string Needed 2 = 4 | ~string-string = 1 | string-string Text = {1} == {2} | ~string-string = {1}.equals({2}) | string-string

The type of {1} and {2} can also be checked separately.

 Flag When True string-1 {1} is a string integer-1 {1} is an integer real-1 {1} is a real boolean-1 {1} is a Boolean string-2 {2} is a string integer-2 {2} is an integer real-2 {2} is a real boolean-2 {2} is a Boolean