In this example we will build our first Visual Lambda module to learn how to build algebraic express tree.
Our first example is a simple module to perform addition of two integer numbers.
Creating Module
First we need to create a new module called "Addition", with three parameters "a", "b" and "c", all are of integer types, and a return type of "int".
The created module is shown below. For more information about creating modules with specified signature, see "Creating a Visual Lambda Module".
Please note in the screen shot, the root node "Module" has an icon of a question mark, while all other nodes have icon of "check mark". The icon of question mark indicates that the expression on that node is not completed.
Adding Action
A module must define an action node. Since this module is a simple addition module, we can simply choose "Return" from the pop up action list.
A new "Return" node will be added to the tree. Note the return node has a question mark with it indicating that the expression is not completed yet.
Add Oprand to Return Expression
Right click the "Return" node, the editor first displays all possible operations you can perform with that expression. For a "Return" expression, the only thing you can do is to "Add ReturnValue". After clicking that item, the editor will guide you to the next level of possible operations.
The editor knows that the module has a return type of "int". So all possible operations it prompts for the user has one thing in common. They all result in an integer value.
In this example we need to return the result of addition, which is a sub-category of "[Built in] Math". So we click that item. The editor will pop up all possible operations you can do within that category. We can click "+ Plus [int]" item.
Working With Addition Expression
An addition node is added to the tree. Note the question mark moved to the addition node indicating that the expression is not completed yet.
Right click the addition node, a pop up menu will be displayed with all possible operations you can perform with this node. In this example we need to add operands to the expression. So we click "Add Oprand 0", which leads us to the next level of possible operations.
Note this time the system displays the same pop up list as the screen shot shown above, which is the list of all possible expressions that return an integer type.
This time we need to choose a parameter to add. So we navigate to "Parameters" and select "@a".
Note: In Visual Lambda, a reference to a parameter will have a "@" prefix symbol. A reference to a variable will have a "$" prefix symbol. "@a" means a reference to parameter "a".
Adding a Second Oprand
Repeat the procedure above, this time we add the parameter "b" to the addition expression.
Note: In this screen shot, the pop up menu in the first step is slight different from that in the screen shot above. The first menu item in the first level of pop up changed from "Add Oprand 0" to "Add Oprand 1". As we mentioned before, the editor is able to display all possible choices. The addition expression have a naming convention to name its operands "Oprand 0", "Oprand 1", "Oprand2" etc. The index is apparently zero based. Since the "Oprand 0" is added. This time user can add a second operand which has a name of "Oprand 1" according to the naming convention we just mentioned. Also a "Set Operand 0" item is added to the menu, which will allow user to modify the first operand we just added (Oprand 0).
Common Operations
Repeating the procedure to add the parameter "c". Now the expression is completed. We have built our first module to calculate the sum of all three integer parameters and return it.
Visual Lambda editor provides some common operations on expression nodes. For example, if we right click on the node "@b", a pop up menu will be displayed with choices of operations.
Choose "Replace With" will replace the selected expression with an expression with the same return type.
Choose "Wrap With" will replace the selected expression with an expression with the same return type. The replacement expression also must support support at least one operand and the type of the first operand must be of the same type. The selected expression will be the first operand of the new replacement expression. "Wrap With" is sometimes useful in customizing Boolean expressions.
Choose "Extract Replace Parent", the expression "@b" will be the return value of the module. The addition expression will be replaced by the single parameter reference expression "@b".
Smart Display
The editor is able to display the customized expression tree in various different ways to help people understand the code.
For example, if we expand the addition expression node, it only shows the type of the expression "+ Plus [int]". If we collapse the node, the editor will display the full expression of that node "@a + @b + @c".
Also on the lower part of the editor view, this editor will display the pseudo code of the selected expression node.