我正在尝试制作解析器和解释器或编译器。现在,当我尝试执行测试代码时,它显示的都是空白。我没有解析它还是有什么干扰或什么?有人可以看看并告诉我什么不起作用吗?
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
namespace Mikebite
{
class Program
{
static void Main(string[] args)
{
try
{
string code = "";
compile("function Main {", code);
compile("x = Hello world!!", code);
compile("print x", code);
compile("input x", code);
compile("} ;", code);
Console.WriteLine(code);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
static void compile(string line, string code)
{
string[] tokens = line.Split(' ');
for (int i = 0; i < tokens.Length; i++)
{
if (tokens[i].Contains("function"))
{
code += ":" + tokens[i+1];
i++;
}
else if (tokens[i].Contains("="))
{
code += "PUSH " + tokens[i-1] + "\n";
code += "PUSH " + tokens[i+1] + "\n";
code += "SET\n";
i++;
}
else if (tokens[i].Contains("exec"))
{
code += "GOTO " + tokens[i+1] + "\n";
i++;
}
else if (tokens[i].Contains("}"))
{
code += "RTN\n";
}
else if (tokens[i].Contains("input"))
{
code += "PUSH " + tokens[i+1] + "\nPUSH NULL\nINPUT\n";
}
else if (tokens[i].Contains("print"))
{
code += "PUSH " + tokens[i+1] + "\nPUSH NULL\nPRINT\n";
}
}
}
}
}