views:

99

answers:

1

Hi, I've made a basic interpreter before in C with a preprocessor that took a lot of load off of parsing and such. I would like to port this preprocessor for use in C# now and I'm having trouble, as I am still quite new to C#.

My old preprocessor made it so things like

var $mine=        this;
      //weird intendtation
var       $something        + $a=$b;

would come out to something fairly machine readable as

var\0$mine\0=this\0;\0var\0$something\0+$a\0=$b\0;\0

(with \0 being NULL so that I could get the names of variables and identifiers very very easily)

Well, with my old code I read it in one byte at a time and depending on if its whitespace and what the last character and such was, then it would either insert the character, insert NULL or ignore the character.

Well I'm having trouble turning this into C# code. I'm using the StringBuilder class and using Insert() to insert one character at a time. But my problem is I can't use \0 as a character value. How do I make it so that identifier names are still extremely easy to read then? would it be better to have an array of strings or string builder objects in this case?

+6  A: 

I would recommend using List<string>, and adding items to the list one at a time.

This gives you all of the usability of an array of strings, but it will grow dynamically. The C# code to parse your file should be much, much simpler and easier to understand than the C code, if you use this approach.

Reed Copsey