EP 0.1.1 语句分界线尚未完成

This commit is contained in:
ninemine
2025-07-03 21:50:26 +08:00
parent fff7b274f5
commit 6806d90829
12 changed files with 175 additions and 123 deletions

View File

@@ -45,7 +45,7 @@ namespace Convention.EasySave.Types
{
if(!reader.StartReadCollectionItem())
break;
stack.Push(reader.Read<T>(elementType));
stack.CompleteLine(reader.Read<T>(elementType));
if(reader.EndReadCollectionItem())
break;
}

View File

@@ -126,7 +126,7 @@ namespace Convention.EasySave.Types
}
else if (type == typeof(EasySaveStackType))
{
var method = baseType.type.GetMethod("Push");
var method = baseType.type.GetMethod("CompleteLine");
foreach (var item in loaded)
method.Invoke(obj, new object[] { item });
}

View File

@@ -11,7 +11,6 @@ namespace Convention
{
private string FullPath;
private FileSystemInfo OriginInfo;
public FileStream OriginControlStream { get; private set; }
public ToolFile(string path)
{
FullPath = path;
@@ -126,21 +125,12 @@ namespace Convention
EasySave.EasySave.Save(key, data,FullPath);
}
private void SaveAsText(string data)
{
if (OriginControlStream != null && OriginControlStream.CanWrite)
{
using var sw = new StreamWriter(OriginControlStream);
sw.Write(data);
sw.Flush();
}
else
{
using var fs = new FileStream(FullPath, FileMode.CreateNew, FileAccess.Write);
using var sw = new StreamWriter(fs);
sw.Write(data);
sw.Flush();
}
}
public static void SaveDataAsBinary(string path, byte[] outdata, FileStream Stream = null)
{
if (Stream != null && Stream.CanWrite)
@@ -158,7 +148,7 @@ namespace Convention
public void SaveAsBinary(byte[] data)
{
SaveDataAsBinary(FullPath, data, OriginControlStream);
SaveDataAsBinary(FullPath, data, (OriginInfo as FileInfo).OpenWrite());
}
#endregion
@@ -201,15 +191,8 @@ namespace Convention
Refresh();
return this;
}
public ToolFile Open(FileMode mode)
{
this.Close();
OriginControlStream = new FileStream(this.FullPath, mode);
return this;
}
public ToolFile Close()
{
OriginControlStream?.Close();
return this;
}
public ToolFile Create()

View File

@@ -16,7 +16,6 @@ namespace Convention
public static void GenerateEmptyConfigJson(ToolFile file)
{
file.Open(System.IO.FileMode.CreateNew);
file.SaveAsRawJson<Dictionary<string, object>>(new()
{
{ "properties",new Dictionary<string, object>() }
@@ -71,13 +70,10 @@ namespace Convention
var file = DataDir | path;
if (file.Exists())
{
try
{
file.Open(System.IO.FileMode.Create);
file.Delete();
file.Create();
return true;
}
catch (Exception) { }
}
return false;
}
public bool RemoveFile(string path)

View File

@@ -43,7 +43,7 @@ namespace Convention.Symbolization.Internal
namespace Convention.Symbolization.Keyword
{
/// <summary>
/// <b><see langword="import"/> namespace-expression</b>
/// <b><see langword="import"/> file</b>
/// </summary>
public sealed class Import : Internal.Keyword<Import>
{

View File

@@ -7,6 +7,15 @@ using System.Threading.Tasks;
namespace Convention.Symbolization.Internal
{
public abstract class Modification : Variable
{
protected Modification(string modificationalName, Type type) : base(new(modificationalName, type))
{
}
}
public abstract class Modification<T> : Modification where T : Modification<T>
{
protected Modification(string modificationalName) : base(modificationalName, typeof(T))
{
}
}
}

View File

@@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Convention.Symbolization.Internal
{
public class SymbolizationReader
{
}
}

View File

@@ -6,7 +6,7 @@
public ScriptWordVariable(string word) : base("")
{
this.word = word;
this.word = (string)word.Clone();
}
public override ScriptWordVariable CloneVariable(string targetSymbolName)
@@ -19,20 +19,9 @@
return other is not null && word.Equals(other.word);
}
public Keyword GetKeyword()
public override string ToString()
{
if(Keyword.Keywords.TryGetValue(word, out var keyword))
return keyword;
return null;
}
public Variable GetVariable(SymbolizationContext context)
{
if (context.Variables.TryGetValue(word, out var variable))
{
return variable;
}
return null;
return word;
}
}
}

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

View 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();
}
}
}

View File

@@ -1,56 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
}
public readonly SymbolizationContext ParentContext;
public readonly Dictionary<int, List<Internal.Variable>> ScriptWords = new();
public readonly Dictionary<int, List<Internal.Variable>> ScriptCommands = new();
public readonly Dictionary<string, Internal.Variable> Variables = new();
public readonly Internal.Namespace CurrentNamespace;
public SymbolizationContext Compile()
{
return this;
}
}
public class SymbolizationRunner
{
private readonly Internal.Namespace GlobalNamespace;
private readonly SymbolizationContext Context;
private SymbolizationContext Context;
public SymbolizationRunner(SymbolizationContext context)
public void Compile(string path)
{
GlobalNamespace = Internal.Namespace.CreateRootNamespace();
Context = context;
}
public SymbolizationRunner() :this(new()){ }
public Exception Compile()
{
try
{
}
catch (Exception ex)
{
return ex;
}
return null;
Context = new();
Context.Compile(new ToolFile(path));
}
}

View File

@@ -4,32 +4,25 @@ using System.IO;
using System.Threading;
using Convention;
using Convention.EasySave;
using Convention.Symbolization;
public class Program
{
class Vector2
{
public double x, y;
}
class Vector2X2
{
public double x, y;
public Vector2 next;
}
static void Main(string[] args)
{
Directory.CreateDirectory(PlatformIndicator.PersistentDataPath);
Console.WriteLine(PlatformIndicator.PersistentDataPath);
EasySave.Save("key", new Vector2X2()
// Create a new SymbolizationContext
var context = new SymbolizationContext();
// Compile a script from a file
var toolFile = new ToolFile("example_script.txt");
try
{
x = 10,
y = 20,
next = new()
context.Compile(toolFile);
Console.WriteLine("Script compiled successfully.");
}
catch (FileNotFoundException ex)
{
x = 30,
y = 40
}
}, "./test.json");
Console.WriteLine($"Error: {ex.Message}");
toolFile.Create();
}
}
}