using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ChInterpreter
{
class Interpreter
{
private Dictionary<string, object> _variables;
public Interpreter() { _variables = new Dictionary<string, object>(); }
public void Clear() { _variables.Clear(); }
public void Execute(string[] cmds)
{
var jmp = new Dictionary<int, int>();
try
{
for (int i = 0; i < cmds.Length; i++)
{
string line = cmds[i];
int it = 0;
while (line.Contains("\""))
{
string value = line.Substring(line.IndexOf("\"") + 1);
value = value.Substring(0, value.IndexOf("\""));
_variables.Add("@" + i.ToString() + it.ToString(), value);
line = line.Replace("\"" + value + "\"", "@" + i.ToString() + it.ToString());
it++;
}
line = line.Trim();
if (line != "" && line[0] == ':') jmp.Add(int.Parse(line.Substring(1)), i);
cmds[i] = line;
}
for (int i = 0; i < cmds.Length; i++)
{
string[] spCmds = cmds[i].Split();
switch (spCmds[0])
{
case "つ":
for (int j = 1; j < spCmds.Length; j++) _variables.Add(spCmds[j], null);
break;
case "オワタ":
return;
case ">>":
i = jmp[int.Parse(spCmds[1])];
break;
case "":
case ":":
break;
case "カキコ":
Console.WriteLine(_variables[spCmds[1]]);
break;
case "うp":
System.IO.File.WriteAllText((string)_variables[spCmds[1]], (string)_variables[spCmds[2]]);
break;
case "ろだ":
_variables[spCmds[1]] = System.IO.File.ReadAllText((string)_variables[spCmds[2]]);
break;
}
}
}
catch (Exception ex) { Console.WriteLine("-ERR : " + ex.Message); }
}
}
}