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

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

, 12 Jun 2010
A small program that parses expressions and evaluates them using LINQ Expressions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TinyLisp {
    // the whole expression will read sequentially be by various Expression nodes
    // this class provides some conveniance in do that
    public class CharStream {

        public string Stream { get; private set; }
        public int Position { get; private set; }

        // constructor to create a charstream from a string
        public CharStream(string Expression) {
            Stream = Expression;
            Position = 0;
        }

        // provides the current char in the stream
        public char Current {
            [DebuggerStepThrough()]
            get { return Stream[Position]; }
        }

        // move to the next char in the stream (or throws an error when there isn't one anymore
        [DebuggerStepThrough()]
        public void MoveNext() {
            // move to next character (and keep moving while there's whitespace)
            do {
                Position++;
                if (Position == Stream.Length) throw new Exception("Unexpected end of expression at position " + Position + ". \n\nYou probably forgot a ')' to close the expression.");
            } while (IsWhitespace);
        }

        // used a lot so refactored this into seperate property
        private static readonly char[] WhiteSpace = { ' ' };
        public bool IsWhitespace {
            [DebuggerStepThrough()]
            get { return WhiteSpace.Contains(Current); }
        }
    }
}

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 | Mobile
Web01 | 2.8.140926.1 | Last Updated 12 Jun 2010
Article Copyright 2010 by gjvdkamp
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid