前后读点 [英] Reading Before and After Point

查看:52
本文介绍了前后读点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我目前正在制作一种新的编程语言.我知道您在想什么,我会使用很多编程语言,而且我会做.我有两个原因.我需要我的实践,并且我制作的每种编程语言都不尽相同.这个程序运行起来更像一台虚拟机,它将具有内存并且所有命令都存储在其中.例如,如果我要输入代码:

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''



如果在命令中输入: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:

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!

推荐答案

您是否考虑过使用正则表达式?看起来您的代码变得非常复杂,并且容易出错.例如:
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

会被当作"hello"类和"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:

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;
    }


这篇关于前后读点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆