In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the implementation method of open source pure C # expression compiler". The content of the article is simple and clear, and it is easy to learn and understand. let's go deep together to study and learn what is the implementation method of open source pure C # expression compiler.
I. introduction
One of the main functions of the monitoring screen is to track the changes of the lower computer variables and show these changes as animation. Most of the time, a state of an element component on the interface is bound to a single variable Tag, such as the running state of the motor, binding a MotorRunning signal; but sometimes it is not so simple, such as the thermometer displays red when the temperature is higher than 50 ℃; the alarm of a device may be triggered by one of several conditions; the change of variables triggers a series of chain reactions. And so on. Considering that most of the technicians in the industrial control industry are not computer majors, it will undoubtedly take some thought to solve all kinds of complex variable-animation binding problems with the least coding.
II. Scheme selection
For the problem of variable animation binding, you can choose the following options:
Script compiler
Many large-scale configuration software include powerful script editors that support scripting languages such as VBS, Python and even C. The script comes with a syntax editor, debugger and compiler, and the API calls are all-inclusive, such as database API, communication API, screen configuration API. You can use scripts to implement very complex logic.
However, I did not implement this type of script compiler based on the following considerations:
Unlike most configuration software including an independent interface designer, I use Visual Studio to shoulder the important task of syntax editing, debugging, compilation and interface design, so there is no need to build an independent script compiler.
C # combines Visual Studio to call all kinds of functions of communication and database link. C # contains powerful syntax functions and can do almost anything with .NET class library. at the same time, C # also supports scripting, so there is no need to use other scripting languages.
For complex logic, let C# work with the VS artifact.
Operator overloading.
I have studied a script compilation system written by C #, which can implement four operations and logical operations between two specific sets, such as List1.A+List2.A;List1.A > List2.B. It seems that the collection participates in operations and operations like an ordinary numerical value.
Operator overloading is a powerful syntax feature of C #, and the operators that can be overloaded are as follows:
Operator
Reloadability
+, -,!, ~, +,--, true, false
These unary operators can be overloaded.
The true and false operators must be overloaded in pairs.
+, -, *, /,%, &, |, ^,
These binary operators can be overloaded.
=,!, =
The comparison operator can be overloaded. Must be overloaded in pairs.
& &, | |
Conditional logical operators cannot be overloaded.
But you can use the & and | that can be overloaded to do the calculation.
[]
The array index operator cannot be overloaded, but indexers can be defined.
()
The conversion operator cannot be overloaded, but a new conversion operator can be defined.
+ =,-=, *, / =,% =, & =, | =, ^ =, =
The assignment operator cannot be explicitly overloaded.
When rewriting individual operators such as +, -,%, they are implicitly overridden.
=,.,?:,->, new, is, sizeof, typeof
There is no doubt that the good use of operator overloading can write code with clearer and simpler semantics.
For example, there is a plural type of Complex, which has two coordinates x and y; define that ComplexA is greater than ComplexB: at least one of the XMagi y of An is greater than B. All I need is the overload > operator (the corresponding best overload > =, B.x | | A.y > B.y. Even more gratifying is that after reloading >
{
If (tag1.Value.Boolean & & tag2.Value.Boolean & & tag3.Value.Boolean)
{
/ / execution
}
}
Tag2.ValueChanged + = (s, e) = >
{
If (tag1.Value.Boolean & & tag2.Value.Boolean & & tag3.Value.Boolean)
{
/ / execution
}
}
Tag3.ValueChanged + = (s, e) = >
{
If (tag1.Value.Boolean & & tag2.Value.Boolean & & tag3.Value.Boolean)
{
/ / execution
}
}
}
It doesn't look complicated, does it? If there are 50 animations on the interface, the code will be written 50 times. It is not only a waste of time, it is troublesome to change it, but also troublesome to check it out. To make matters worse, people who don't know how to program can't use it.
Expression compiler
For most PC designers with zero programming foundation, what they need is a simple and intuitive way to bind variables without the cost of learning and understanding.
For example, if the thermometer shows red when the temperature is higher than 50 ℃, there is a sentence [temperature > 50]. If a device displays an alarm, it may be triggered by one of several alarm variables. Just write [Alarm1 | | Alarm2 | | Alarm3]. With the help of Microsoft's powerful expression engine, if the designer can parse this kind of variable expression, the designer only needs to know the logical relationship between the element and the variable, and the function that a few expressions are difficult to reach can be realized with a little knowledge of C #. In this way, it is easy to use and easy to use, and at the same time can meet complex needs.
At the same time, there are several additional benefits:
Minimum amount of coding: there is almost no code in the cs file of an interface. The binding logic is embedded intuitively within XAML:
Duplicate coding can be reduced with functions such as copy, paste, and text replacement
You can make full use of the designer extension of WPF to implement a simple syntax editor that can highlight syntax, automatically complete and perform syntax checking.
It is convenient to find variable logic and modify it.
The main code for this compiler is the Eval class.
Third, implement a compiler by yourself
Compilation principle
All college computers have a course in compiling principles. At that time, I was also holding a textbook, surrounded by "Polish expression" and "inverse Polish expression", but inverse Polish expression was the key to the implementation of the compiler.
The advantage of inverse Polish expression is that any ordinary expression can be solved by using only two simple operations, on-stack and off-stack. The mode of operation is as follows:
If the current character is a variable or a number, press the stack, if it is an operator, pop up the two elements at the top of the stack for the corresponding operation, and then enter the stack. Finally, when the expression is scanned, the result in the stack is the result.
Microsoft has already given everyone a ready-made wheel on how to implement its own compiler. Microsoft's Expression class provides a complete set of methods for stitching and compiling Lambda expressions, which you can easily define your own syntax. For related knowledge, please refer to the do-it-yourself compiler series of blog Park Assembly head article: http://www.cnblogs.com/Ninputer/archive/2011/06/18/2084383.html. Take the SCADA project as an example:
Define syntax
In this version, I have only implemented some of the most basic and commonly used operations, such as four operations (+-* /), logical operations (& |!), inversion of modules, ternary conditions and so on.
The GetOperatorLevel function defines the operation priority according to the operator priority of C #.
Define custom functions that start with @, such as @ date to take the current date, @ App to take the current path, and so on.
The IsConstant method defines system constants, where True/False represents logical constants and string constants use''.
Compilation process
The compilation process converts a string into a function with a return value; the argument to the function is the value of the Tag associated with the expression. The order is as follows:
RpnExpression method: converts an infix expression to an inverse Polish expression. Splits the expression string into an array with keywords, leaves the stack in order of priority, and returns a list of strings that reverse the order of Polish expressions.
ComplieRpnExp method: according to the reverse Polish expression order, pop up the subclasses of operators converted to Expression, such as binary expression BinaryExpression, conditional expression ConditionalExpression, constant expression ConstantExpression, etc.; the parameter first determines whether it is constant, if not, then call the GetTagExpression method, convert the string to method call MethodCallExpression, and finally compile the parameter into a Tag. After processing, a LambdaExpression is finally returned.
The Eval method compiles the LambdaExpression into a delegate; the relevant Tag is added to the list TagList.
IV. Application scenarios
Expression binds to animation
There are almost the same few lines of code in each interface form:
List _ valueChangedList
Private void HMI_Loaded (object sender, RoutedEventArgs e)
{
Lock (this)
{
_ valueChangedList = cvs1.BindingToServer (App.Server)
}
}
Private void HMI_Unloaded (object sender, RoutedEventArgs e)
{
Lock (this)
{
App.Server.RemoveHandles (_ valueChangedList)
}
}
Among them, BindingToServer scans all the elements of the current interface, searches for TagReadText expressions related to each control and compiles them with the Eval class; the compiled results are converted into a function with a return value and a list of related Tag; traverse the Tag list and link its value change event ValueChanged with this function. In this way, the compilation process has been completed when the interface is loaded, and once the value of the relevant variable changes, it will return a value according to the expression. If the value is a Boolean and is bound to the running animation of the motor, the trigger process from the expression to the animation is completed.
Complex alarm condition
Alarm generally includes over-limit alarm, variable trigger alarm, difference alarm and so on. However, there may also be complex alarm conditions, which can not be expressed in simple ways such as out-of-limit, over-error, etc., can be summed up as complex alarm, and its conditions can be described by an expression similar to animation binding, which is loaded and compiled into alarm conditions at the time of system initialization.
Future improvement
Editor improvement: support automatic command completion, syntax highlighting, and better syntax checking. Consider the editing control of Sharpdevelop.
Support for complex syntax: the current syntax is only a simple four operations and logical expressions. Future considerations support complex syntax such as multi-segment expressions, functions (such as sine and cosine), and attribute references.
Thank you for reading, the above is the content of "what is the implementation method of open source pure C # expression compiler". After the study of this article, I believe you have a deeper understanding of what the implementation method of open source pure C # expression compiler is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.