5,699,997 members and growing! (19,312 online)
Email Password   helpLost your password?
Languages » C# » Samples License: The Code Project Open License (CPOL)

C# for MS-DOS: Expression trees compiled into 16-bit MS-DOS binary

By Ivan Krivyakov

C# arithmetic expressions compiled into 8086 machine code (yes, you can run it on Vista :-))
C#, .NET (.NET, .NET 3.5)

Posted: 4 Sep 2008
Updated: 4 Sep 2008
Views: 3,408
Bookmarked: 18 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 4.79 Rating: 4.60 out of 5
1 vote, 9.1%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
10 votes, 90.9%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Download ExpressionCompiler86_Src.zip - 13.91 KB

What Does The Sample Do

It compiles C# lambda expressions into 16-bit executable files.

Why Is It Interesting?

C# expression trees allow you to analyze C# code and reinterpret it in a different way. E.g. LINQ-to-SQL takes expressions written in C# and turns them into SQL statements.

This sample turns (a small subset of) C# into 8086 machine code.

Background

I have read many blogs about LINQ expression trees and how "cool" they are. However, very few posts explained what actually expression trees are for. This sample demonstrated one possible usage. Some day I may expand this article to further disucss the place of the expression trees in life.

Why .Com Files 

I chose .com files because they are as simple as executable format gets. Just an array of 8086 machine codes with absolutely no headers or metadata. A compiler writer heaven. And it is still executable on all versions of Windows.

NB: "com" here is merely a file extensions, as in xy.com. It has nothing to do with Component Object Model which appeared 10 or so years after .com files.  

Compiling Expressions

An expression may contain variables and three arithmetic operations: +, -, and *. All arithmetic is done in unsigned 16-bit integers.

When the generated COM file executes, it prints a banner, prompts the user for parameter values (if any), calculates the result and displays it. COM files shall be executed in a console window, e.g. from cmd.exe.

E.g. expression (x,y) => 2*x + 3*y when compiled and executed produces the following output (user input in bold):

C# for MS-DOS, version 1.0
x? 10
y? 20
((2 * x) + (3 * y)) = 80

Calling Expression Compiler

The sample provides class ExpressionCompiler86 with the method Compile(Expression expr). Here is how main class Program compiles three expressions:

    // a little wrapper to makes things simpler
    static void Compile<T>(string file, Expression<T> expr)
    {
        using (ExpressionCompiler86 compiler = new
ExpressionCompiler86(file))
        {
            compiler.Compile(expr);
        }
    }

    ...

    Compile<Func<int>>( ''constant.com'', () => 42);
    Compile<Func<int>>( ''noparams.com&'', () => 1+2+3*4);
    Compile<Func<int,int,int>>( ''xy.com'', (x,y) => 2*x + 3*y );

NOTE: quotation marks did not make it through the submission wizard. I had to replace them with double apostrophes ''. Codeproject team has been notified

Disclaimer 

Please be lenient to the quality of code. It is not production code. It's a sample, or, rather, a proof of concept. It was written in one day. It contains absolutely no automated tests. There absolutely no overflow checks or anything like that. Class names may be questionable. Bugs are likely.  

Enjoy! 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ivan Krivyakov



Occupation: Architect
Company: Sungard Consulting Services
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralGot my 5memberLee Humphries14:16 8 Sep '08  
GeneralRe: Got my 5memberIvan Krivyakov16:32 9 Sep '08  
GeneralRe: Got my 5memberLee Humphries19:07 9 Sep '08  
GeneralExcellentmembertorial5:57 5 Sep '08  
GeneralNice...memberRob Philpott2:43 5 Sep '08  
GeneralgeniusmemberUnruled Boy21:20 4 Sep '08  
GeneralAwesomememberTarnacious Barfish20:37 4 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Sep 2008
Editor: Sean Ewington
Copyright 2008 by Ivan Krivyakov
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project