Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

TinyLisp: A Language and Parser to See LINQ Expressions in Action

, 12 Jun 2010 CPOL
A small program that parses expressions and evaluates them using LINQ Expressions
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using le = System.Linq.Expressions;

namespace TinyLisp {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e) {

            string sExpression = Input.Text;
            try {
                CharStream s = new CharStream(sExpression);
                CodeNode nExpression = new ExpressionNode(s);

                // Show in tree
                List<CodeNode> ln = new List<CodeNode>() { nExpression };
                tvAst.ItemsSource = ln;

                // convert to Linq.Expression
                le.Expression eExpression = nExpression.ToExpression();
                
                // put it into a Lambda
                le.Expression<Func<int>> lExpression = le.Expression.Lambda<Func<int>>(eExpression);
                
                // Compile the lambda
                Func<int> fExpression = lExpression.Compile();             
           
                
                // display the Linq Expression
                // wrap expression in ExpressionAdapter and then List<ExpressionAdapter> (because a Treeview.ItemSource expects an IEnumerable) and bind TreeView to it
                List<ExpressionAdapter> l = new List<ExpressionAdapter>() { new ExpressionAdapter(lExpression, "Expression") };
                tvExpr.ItemsSource = l;

                // and execute the compiled lambda to see the result
                MessageBox.Show("the answer is " + fExpression(), "That's easy!");

            } catch (Exception ex) { MessageBox.Show(ex.Message, "Hmmm, you might want to check that again"); }
        }

        

       
    }   
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

gjvdkamp
Leaseplan Corporation
Netherlands Netherlands
Gert-Jan is a Senior Quantitative Risk Manager at Leaseplan Corporation. In that job he doesn't get to code much he does these little projects to keep his skills up and feed the inner geek.
Follow on   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150301.1 | Last Updated 12 Jun 2010
Article Copyright 2010 by gjvdkamp
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid