Over the weekend, three articles on the analysis of mathematical expressions appeared on the Habré: the Expression
Compiler , the
Parser of Mathematical Expressions and the
Calculation of the Expression Value and one
comment , in which the code on the prologue for brevity and expressiveness breaks the examples in these articles.
Below I will show how to use the standard tools of the platform and language to achieve the same result, namely the calculation of expressions.
At the PDC2008 conference, Anders Hejlsberg gave a talk in which he traced the evolution of the C # language. He reviewed the existing versions and made an overview of the future: in the 4th version of the developers waiting for the opportunity to dilute the strict type system with capabilities from dynamic languages, and by the fifth version the compiler will be completely rewritten under managed code, and it can be used as a service (compiler as a service) .
')
For those programmers who are dreaming to try out this ideology (compiler as a service) right now, it will be good news that they can do it. The fact is that the nemerle compiler is written in nemerle, and it can be used as a simple library. It should also be noted that in this case, in the course of its work, no files will be created on the disk, only if you do not want the opposite. Below is an example of code that the compiler uses as a library and which effectively solves the problem of parsing / evaluating expressions =)
using System;
using System. Console ;
using Nemerle.Evaluation. Evaluator ;
module Program
{
Main() : void
{
def function = EvaluateExpression( "x => x + 1.0" ) :> double -> double ;
WriteLine(function(2.0));
}
}
I think that for any programmer who owns C # this code is understandable, I’ll just note that module is a synonym for static class, double-> double is a function type, analog Func <double, double>, and using System.Console reveals System.Console so that its static methods “turn” into global functions / procedures, that is, a call to WriteLine (function (2.0)) is actually a call to Console.WriteLine (function (2.0)).
Another opportunity to taste the future is the mono platform. I waited a long time for release of version 2.2 and was surprised that no one had yet written on Habré about its output. My expectations were based on a note that Miguel De Icaza published on his blog in September, in which he announced that in the next version of mono (now it’s already running) a REPL will be added, which in turn is based on the compiler as service ideology. Below is the C # code under mono, similar to the code on Nemerle:
using System;
using Mono.CSharp;
public class Program
{
public static void Main(string[] args)
{
var function =
Evaluator .Evaluate ( "new System.Converter<double,double>(x=>x+1.0);" )
as Converter < double , double >;
Console .WriteLine(function(2.0));
}
}
PS
Separately, I want to note that both Nemerle and Mono are open source projects. I used this feature when creating this note, namely, I did not find exhaustive documentation on the use of compile as a service in Nemerle and Mono, but I was able to quickly find all the information I was interested in by studying the source codes.