Hey All,

I'm an avid c++ programmer, fairly new to C#. I understand most the basics and I can get simple tasks complete in C#.

I'm looking for any links to tutorials on writting a scripting language in C# if anyone has/knows any.

In relation to this question, I seem to be having a couple of problems finding a good tutorial in reading text from a file.
I know how to read an entire file and display it, but I'm having problems looking for certain words, reading one word at a time etc.

If I can figure that part out, im sure creating the script language will be that much easier.

Thanks,
-Mario

Alright so Im looking at every example of parsing text files that I can find, and really none of them are of any help. They are all showing what I already know, and none of them explain the things I don't know, they only show them.

So, I'll give it a shot asking here. I'm basically just looking for a simple way to parse an entire text file, and when it comes across a line that begins with a ';' (without the single quotes), that the following text in that line is a comment.

I have the following:

StreamReader sr = new StreamReader(@"File.txt", true); 
String str = sr.ReadToEnd(); 
Regex reg = new Regex(";");

So as you can see, this obviously does nothing. I only saw some examples of the Regex class but nothing that showed me how to use it, so I'm probably using it wrong in the above example. There must be a simpler way to just look at each line and see if it begins with a ';' .

Thanks

StreamReader sr = new StreamReader(@"File.txt", true); 
String str = sr.ReadToEnd(); 
Regex reg = new Regex(";");

Could also do something like this:

StreamReader sr = new StreamReader(@"File.txt", true);
char[] dlim = { ' ', '=', '\t' };
string line = sr.ReadLine();

string[] lines = line.Split(dlim);
            if (lines[0].ToUpper().Contains(" matched case here ") )
                // do something with line 0

what that does is reads the file line by line, and breaking it apart in to a string array for each line. The dlim tells it how to break the string apart. In the above example it will ignore all white spaces, tabs in '=' signs.

Example:

File.txt contains 3 lines

; Welcome to my Program
;it does super cool stuff
a=b

it will give you 3 line arrays:

first: [0] = ";" [1] = "Welcome" [2] = "to" [3] = "my" [4] = "Program"
second [1] = ";it" [1] = "does" etc etc
third [1] = "a" [2] = "b"

the contains function checks the entire string to see if there is a match for that pattern anywhere within the string. So you could say

if (line[0].Contains(";"))

and it would return true on the first and second lines.

There is a much easier way. I write programming languages in C# and have much more complex ways of making a tokenizer, but this is the simplist that I can think of and looks like it answers your question.

StreamReader sr = new StreamReader(@"File.txt", true);
string code = sr.ReadToEnd();
code = code.Replace(((char)13).Tostring(), "");
//don't worry what the replace does, just remember that you have to do it
//everytime otherwise the code will stuff up.

foreach (string a in code.Split('\n'))
{
    if (a.StartsWith("print "))
    {
        Console.Write(a.Substring(6));
    }
    else if (a == "read")
    {
        Console.Read();
    }
}

that is a very basic programming language but it is a good place to start. Here is what is happening:

  1. string code is equal to all the code in the 'File.txt' file
  2. the foreach statement says that a is now equal to code being split by every new line.
  3. the rest is just executing commands

if you need any more help, contact me on my Youtube accout 'Hamilation'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.