Przygody z oprogramowaniem
  • Start
  • Szkolenia
    • Szkolenia otwarte
    • Katalog szkoleń
  • Usługi
    • Konsulting
    • Mentoring
    • Research & Development
  • Blog
  • Wiedza
    • Strefa wiedzy
    • BFsharp
    • SaaS
  • Klienci
  • Kontakt
1

Formula – compilation process

12 August, 2009-Formula

Requirements

I need a language for business users. They will be customizing business logic by writing rules (still to come), for example discount formula, whether the customer is gold, default values for fields or validation rules.

Syntax analysis

First step is to design the syntax of the language. Then we need to create a parser. I’ll use ANTLR, a very powerful tool for creating lexers and parsers. A parser returns a parse tree which represents the structure according to a formal grammar, usually it’s big and hard to generate code from. Fortunately, ANTLR also supports AST (abstract syntax tree) – the simplified tree which can be used in the next compilation step with ease. For example, let’s take an expression “5”.
On the left we can see parse tree and on the right there is an AST.

Code generation

Next, I have to generate code from AST. There are several techniques to do so but I’ll use Expressions (maybe DLR in the future, now I want to support .Net 3.5). It’s cool because the heavy work will be done by the Expression infrastructure. All we need to do is to compose Expressions.

Let’s analyze the expression: 2*2. The AST will look like:

We need to walk the tree and generate logically equivalent code:

var e = Expression.Add(
                Expression.Constant(2),
                Expression.Multiply(
                    Expression.Constant(2),
                    Expression.Constant(2)));

var lambda = Expression.Lambda<Func<int>>(e, new ParameterExpression[0]); 
var f = lambda.Compile();

There’s no magic here.
In the next post we’ll look closely at Formula grammar, it’s AST and we try to write code to generate some of the binary operators: + – / * % = != .

Czytaj dalej | Dyskutuj
0

Building your own scripting language for C#

21 July, 2009-C#, Formula

Today I’ll start a series of posts about building your own scripting language + business rule engine which can be used in .Net applications.

Background

Sometimes a custom scripting language can come in handy when we want the end user to be able to configure our system, for example:

  • formulas, business rules
    example: a discount for products/services, some sort of calculations
  • conditions, validation rules
    example: max invoice price

Requirements

  • It should be simple so that power users can use it – maybe similar to excel formulas?
  • It should be callable from .net.
  • It should be extensible.
  • It should allow minor grammar changes.
  • It should be fast and simple to use for the developer.
  • It should be safe – user cannot execute arbitary code.

Maybe something like this:

FormulaCompiler compiler = new FormulaCompiler();
var f = compiler.Compile<bool>("Inner.Value=2 OR Value=4");
Assert( true, f() );

It the next post I’ll present the overall design of my language which is called Formula.

Czytaj dalej | Dyskutuj

Kategorie

Architecture BFsharp Blog Business Framework C# CqRS DDD Debugging DSL EntityFramework Formula JavaScript Linq NHibernate SaaS Silverlight SQL Visual Studio WPF Wzorce

O mnie


RSS Feed

© macmichal.pl 2011 Wszystkie prawa zastrzeżone