EP 0.1.1 语句分界线尚未完成
This commit is contained in:
41
Convention/[Symbolization]/Runner/SymbolizationContext.cs
Normal file
41
Convention/[Symbolization]/Runner/SymbolizationContext.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Convention.Symbolization
|
||||
{
|
||||
public class SymbolizationContext
|
||||
{
|
||||
public SymbolizationContext() : this(null, Internal.Namespace.CreateRootNamespace()) { }
|
||||
public SymbolizationContext(SymbolizationContext parent) : this(parent, parent.CurrentNamespace) { }
|
||||
public SymbolizationContext(SymbolizationContext parent, Internal.Namespace newNamespace)
|
||||
{
|
||||
this.ParentContext = parent;
|
||||
this.CurrentNamespace = newNamespace;
|
||||
}
|
||||
|
||||
private readonly SymbolizationContext ParentContext;
|
||||
private Dictionary<int, List<Internal.Variable>> ScriptWords;
|
||||
public readonly Dictionary<int, List<Internal.Variable>> ScriptCommands = new();
|
||||
public readonly Internal.Namespace CurrentNamespace;
|
||||
|
||||
public void Compile(string allText)
|
||||
{
|
||||
// Create a new reader to parse the script words
|
||||
Internal.SymbolizationReader reader = new();
|
||||
foreach (char ch in allText)
|
||||
reader.ReadChar(ch);
|
||||
ScriptWords = reader.ScriptWords;
|
||||
// Turn the script words into commands
|
||||
// TODO
|
||||
}
|
||||
|
||||
public void Compile(ToolFile file)
|
||||
{
|
||||
if (file.Exists() == false)
|
||||
throw new FileNotFoundException($"File not found: {file}", file);
|
||||
var text = file.LoadAsText();
|
||||
this.Compile(text);
|
||||
}
|
||||
}
|
||||
}
|
93
Convention/[Symbolization]/Runner/SymbolizationReader.cs
Normal file
93
Convention/[Symbolization]/Runner/SymbolizationReader.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public class SymbolizationReader
|
||||
{
|
||||
private string buffer = "";
|
||||
private int lineContext = 0;
|
||||
private bool isOutOfStringline = true;
|
||||
public Dictionary<int, List<Variable>> ScriptWords = new() { { 0, new() } };
|
||||
|
||||
private static HashSet<char> ControllerCharSet = new()
|
||||
{
|
||||
'{','}','(',')','[',']',
|
||||
'+','-','*','/','%',
|
||||
'=',
|
||||
'!','<','>','&','|',
|
||||
'^',
|
||||
':',',','.','?',/*'\'','"',*/
|
||||
'@','#','$',
|
||||
};
|
||||
|
||||
public void ReadChar(char ch)
|
||||
{
|
||||
if (isOutOfStringline)
|
||||
{
|
||||
if (ControllerCharSet.Contains(ch))
|
||||
CompleteSingleSymbol(ch);
|
||||
else if (char.IsWhiteSpace(ch) || ch == '\n' || ch == '\r' || ch == '\t')
|
||||
CompleteWord();
|
||||
else if (buffer.Length == 0 && ch == '"')
|
||||
BeginString();
|
||||
else if (ch == ';')
|
||||
CompleteLine();
|
||||
else if (char.IsLetter(ch) || char.IsDigit(ch))
|
||||
PushChar(ch);
|
||||
else
|
||||
throw new NotImplementedException($"{lineContext+1} line, {buffer} + <{ch}> not implemented");
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((buffer.Length > 0 && buffer.Last() == '\\') || ch != '"')
|
||||
PushChar(ch);
|
||||
else if (ch == '"')
|
||||
EndString();
|
||||
else
|
||||
throw new NotImplementedException($"{lineContext+1} line, \"{buffer}\" + \'{ch}\' not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
void CompleteSingleSymbol(char ch)
|
||||
{
|
||||
CompleteWord();
|
||||
buffer = ch.ToString();
|
||||
CompleteWord();
|
||||
}
|
||||
|
||||
void PushChar(char ch)
|
||||
{
|
||||
buffer += ch;
|
||||
}
|
||||
void CompleteWord()
|
||||
{
|
||||
if (buffer.Length > 0)
|
||||
{
|
||||
ScriptWords[lineContext].Add(new ScriptWordVariable(buffer));
|
||||
buffer = "";
|
||||
}
|
||||
}
|
||||
void CompleteLine()
|
||||
{
|
||||
CompleteWord();
|
||||
ScriptWords[lineContext].Add(new ScriptWordVariable(";"));
|
||||
lineContext++;
|
||||
ScriptWords.Add(lineContext, new());
|
||||
}
|
||||
void BeginString()
|
||||
{
|
||||
if (buffer.Length > 0)
|
||||
throw new InvalidGrammarException($"String must start with nothing: {buffer}");
|
||||
isOutOfStringline = false;
|
||||
}
|
||||
void EndString()
|
||||
{
|
||||
isOutOfStringline = true;
|
||||
CompleteWord();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user