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()) if(!reader.StartReadCollectionItem())
break; break;
stack.Push(reader.Read<T>(elementType)); stack.CompleteLine(reader.Read<T>(elementType));
if(reader.EndReadCollectionItem()) if(reader.EndReadCollectionItem())
break; break;
} }

View File

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

View File

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

View File

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

View File

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

View File

@@ -8,5 +8,14 @@ namespace Convention.Symbolization.Internal
{ {
public abstract class Modification : Variable 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("") public ScriptWordVariable(string word) : base("")
{ {
this.word = word; this.word = (string)word.Clone();
} }
public override ScriptWordVariable CloneVariable(string targetSymbolName) public override ScriptWordVariable CloneVariable(string targetSymbolName)
@@ -19,20 +19,9 @@
return other is not null && word.Equals(other.word); 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 word;
return keyword;
return null;
}
public Variable GetVariable(SymbolizationContext context)
{
if (context.Variables.TryGetValue(word, out var variable))
{
return variable;
}
return null;
} }
} }
} }

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;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Convention.Symbolization 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 public class SymbolizationRunner
{ {
private readonly Internal.Namespace GlobalNamespace; private SymbolizationContext Context;
private readonly SymbolizationContext Context;
public SymbolizationRunner(SymbolizationContext context) public void Compile(string path)
{ {
GlobalNamespace = Internal.Namespace.CreateRootNamespace(); Context = new();
Context = context; Context.Compile(new ToolFile(path));
}
public SymbolizationRunner() :this(new()){ }
public Exception Compile()
{
try
{
}
catch (Exception ex)
{
return ex;
}
return null;
} }
} }

View File

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