Expressions are fundamental

<< Click to Display Table of Contents >>

Navigation:  Introduction > A quick primer on parsing >

Expressions are fundamental

Previous pageReturn to chapter overviewNext page

Expressions like + b + c are used (or can be used) in many places in Gekko command files, and of course in model files, too.

 

With a basic understanding of how the parser and the so-called AST trees work in Gekko, it should be possible to understand how expressions are represented and handled in Gekko. AST trees are tree-like structures, where the 'nodes' can have children nodes (and parent nodes), somewhat like a file system with folders and sub-folders (and parent folders). For example, if the parser recognizes a time period in an option field (like <2010 2015> in the timeseries statement <2010 2015> = 100;), a (sub)tree representing this logical structure is produced, with the parent name being for instance "ASTDATES" to indicate that the following sub-nodes represent a time period, and with two children nodes containing the actual dates 2010 and 2015. (The very curious may jump forwards to see an AST tree with such an "ASTDATES" node here).

 

Expressions can be time series expressions like = a + b;, but may also involve scalar expressions like %= %+ %b;, or collection expressions like #= #+ #b;.

 

String manipulations are also possible (for instance %= replace(%s1, 'x', 'y');, and the user may define his/her own functions and procedures. Almost anything in Gekko can be an expression. Because of that, and because of the complexities involved in parsing and interpreting expressions (and ensuring that they run fast enough in Gekko), it has been deemed important to focus quite a bit on those in the current source code documentation.

 

Gekko expressions make use of the parser to split up the expression into some machine-understandable structure (AST tree), and with some proper corresponding classes/objects and methods, these Gekko expressions can be translated into sequences of C# methods that call each other, and where the exact type of variable each method returns is not known before the Gekko command file is actually run (that is, the types are only known at run-time and are hence 'dynamic' regarding type). An example could be an Add() method that adds the two variables a and b together, like we also saw in the preceding section on parsing + b + c. This Add() method will act differently according to the types of the incoming variables. If the variables are string types, the strings are appended (%= '1' + '2'; will result in %= '12'), whereas if the variables are numerical values, the values are added mathematically (%= 1 + 2; will result in %= 3). The user may also mix the types: for instance you may add the value 2 to the date 2012q3 and obtain the date 2013q1 corresponding to two quarters later (but two dates cannot be added; in that case a run-time error will occur).

 

So with the right data structures, and with a tree representation (AST) of the Gekko command file, it is a relatively straightforward task to turn that tree representation into corresponding C# code. There is a tight interaction between the parser and its tree creation, and the data structures that are referred to while emitting C# code corresponding to the Gekko command file. Therefore, it makes sense to present parsing and object structures in the same section (here).