Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys,

i am currently making a new programming language. i know what you are thinking, i make a lot of programming languages, and i do. i have a couple reasons for that. i need my practice and every single one of the programming languages that i make are different. this one is am running more like a virtual machine, it will have memory and all of the commands are stored in it. example, if i were to type in the code:

push string 'Hello World'


if would put in the command: push string
and then it would put in after it: Hello World

when i run this, it will look at the instruction and see that it is push string, and then it will look at the next instruction and push it. back on topic.

i am using this system to make variables and have them like classes. so i can type in something like:

var tom,person
tom::age = 6
tom::height = 179


this means that i can store lots of values within the one variable. but if you can see, i have to have the '::'. that is basically because it looks for the characters before the first ':' and the characters after the last ':'. I have tried everything i can. here is the code i have at the moment:

C#
else if (cmd.StartsWith("set "))
{
    if (cmd.Contains("::"))
    {
        cmd = cmd.Substring(cmd.LastIndexOf(' ') + 1);
        string Class = cmd.Substring(0, cmd.IndexOf(':'));
        string Name = cmd.Substring(cmd.LastIndexOf(':') + 1);
        currentClass.Emit(Opcodes.Set);
        currentClass.Emit(Class);
        currentClass.Emit(Name);
    }
    else
    {
        cmd = cmd.Substring(cmd.LastIndexOf(' ') + 1);
        string Class = cmd;
        string Name = "variable";
        currentClass.Emit(Opcodes.Set);
        currentClass.Emit(Class);
        currentClass.Emit(Name);
    }
}


please help!
Posted
Comments
OriginalGriff 18-Nov-12 2:52am    
And what is the problem? "Please help" with what?
Member 8378691 18-Nov-12 3:55am    
Didn't I say? sorry, i want that code to go from 'tom::age = 6' to just one character to look like 'tom.age = 6'

1 solution

Have you considered using a Regex? It looks like your code is getting quite complex, and subject to errors. For example:
hello:this::is:wrong
would process as a class of "hello" and a name of "wrong" It also wouldn't fail if you gave it
Hello::::::::there

A regex to handle it is pretty simple:
C#
Regex classAndName = new Regex(@"(?<Class>[A-Za-z][\w_]*)::(?<Name>[A-Za-z][\w_]*)");
Match m = classAndName.Match("Class::Name");
if (m.Success)
    {
    string vName = m.Groups["Name"].Value;
    string vClass = m.Groups["Class"].Value;
    }
 
Share this answer
 
Comments
Member 8378691 18-Nov-12 4:26am    
i know all the bugs and errors of my code, i have written, tested, written and tested some more. This is the best thing that i could come up with. i don't really want to use regex that much anyway. this programming language isn't meant to be a serious programming language anyway, i upload videos on youtube and make tutorials, this programming language is meant to be easy to use and understand yet effective. if i start using regex, people who are just starting to learn how to do this kind of stuff could get really confused.
OriginalGriff 18-Nov-12 4:38am    
The problem is that what you are trying to do is complicated - and the code to do what the regex does in C# is equally complex and confusing. It's your choice - but I would use the regex!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900