我目前正在使用C#开发自己的编程语言,我在尝试做某事时遇到了问题。我有一部分想出了我需要读取代码并将其存储在字符串中的位置。但是,我的问题是,我试图在搜索关键字的地方使用它,并在没有关键字的情况下显示该行。 例如,我想这样工作:
我在使用C#工作时遇到了麻烦。 我现有的代码:
public void Run()
{
string uriPath;
uriPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string path = new Uri(uriPath).LocalPath;
NBasicInfo info = new NBasicInfo();
Console.Clear();
Console.WriteLine("Run Project");
Console.WriteLine("=======================================");
Console.WriteLine("Files to open: ");
string[] filePaths = Directory.GetFiles(path + @"\NDOS\Programs\NBasic");
for (int i = 0; i < filePaths.Length; ++i)
{
string _path = filePaths[i];
Console.WriteLine(System.IO.Path.GetFileName(_path));
}
Console.WriteLine();
Console.Write("Project Name (Name before .nbasic) (Do not include .nbasic): ");
string fileName = Console.ReadLine();
try
{
StreamReader contentCode = new StreamReader(path + @"\NDOS\Programs\NBasic\" + fileName + ".nbasic");
string codeContent = contentCode.ReadToEnd();
contentCode.Close();
XmlSerializer xs = new XmlSerializer(typeof(NBasicInfo));
FileStream read = new FileStream(path + @"\NDOS\Programs\NBasic\" + fileName + ".xml", FileMode.Open, FileAccess.Read, FileShare.Read);
NBasicInfo info2 = (NBasicInfo)xs.Deserialize(read);
info2.pname = info2.ProjectName;
info2.aname = info2.ApplicationName;
info2.dname = info2.DeveloperName;
read.Close();
Console.Clear();
Console.WriteLine("Running " + info2.aname + "...");
Console.WriteLine("===================================================================");
Thread.Sleep(200);
Console.ReadKey();
}
catch
{
Console.WriteLine("Error running code. Please check code and correct any mistakes.");
}
}
我需要的是搜索关键字的代码(在这种情况下&#39; print&#39;)并显示之后的字词。
答案 0 :(得分:0)
我可以给你一个简单的答案
if(command.StartsWith("Print"))
{
Console.WriteLine(command.Substring(6));
}
但那不是真正的世界级语言翻译
答案 1 :(得分:0)
你说你正在开发一种新的编程语言......这里有一个超级超基本示例(为了更好地理解这种任务所需的约束),使其更易于维护并保存所有如果@Keith Nicholas提到的话,那就是嵌套:
class Program
{
static void Main(string[] args)
{
PrintKeyWords.CheckForKeyWord("print hello world!!!");
PrintKeyWords.CheckForKeyWord("specialPrint hello world!!!");
Console.ReadLine();
}
}
delegate void ExecuteMethod();
public static class PrintKeyWords
{
private static string Command { get; set; }
public static void CheckForKeyWord(string cmd)
{
foreach (KeyValuePair<string, ExecuteMethod> keyword in KeyWordsDict)
{
if (cmd.StartsWith(keyword.Key))
{
Command = cmd;
KeyWordsDict[keyword.Key].Invoke();
return;
}
}
Console.WriteLine("your command is not recognized as an internal or exter
}
private static Dictionary<string, ExecuteMethod> _KeyWordsDict;
private static Dictionary<string, ExecuteMethod> KeyWordsDict
{
get
{
if (_KeyWordsDict == null)
{
_KeyWordsDict = new Dictionary<string, ExecuteMethod>();
_KeyWordsDict.Add("print", Print);
_KeyWordsDict.Add("specialPrint", SpecialPrint);
return _KeyWordsDict;
}
else
{
return _KeyWordsDict;
}
}
}
private static void Print()
{
Console.WriteLine(Command.Replace("print", ""));
}
private static void SpecialPrint()
{
Console.WriteLine(Command.Replace("specialPrint", "") + "*****************");
}
}