EP 0.1.1 转换
This commit is contained in:
@@ -8,9 +8,9 @@ namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public readonly FunctionSymbol FunctionInfo;
|
||||
|
||||
protected Function(string symbolName,string functionName, Type returnType, Type[] parameterTypes, Modification[] modifiers) : base(symbolName)
|
||||
protected Function(string symbolName,string functionName, Type returnType, Type[] parameterTypes) : base(symbolName)
|
||||
{
|
||||
FunctionInfo = new(functionName, returnType, parameterTypes, modifiers);
|
||||
FunctionInfo = new(functionName, returnType, parameterTypes);
|
||||
}
|
||||
public abstract Variable Invoke(SymbolizationContext context, Variable[] parameters);
|
||||
}
|
||||
@@ -20,7 +20,7 @@ namespace Convention.Symbolization.Internal
|
||||
public readonly MethodInfo methodInfo;
|
||||
|
||||
public DelegationalFunction(string symbolName, MethodInfo methodInfo)
|
||||
: base(symbolName, methodInfo.Name, methodInfo.ReturnType, methodInfo.GetParameters().ToList().ConvertAll(x => x.ParameterType).ToArray(), Array.Empty<Modification>())
|
||||
: base(symbolName, methodInfo.Name, methodInfo.ReturnType, methodInfo.GetParameters().ToList().ConvertAll(x => x.ParameterType).ToArray())
|
||||
{
|
||||
this.methodInfo = methodInfo!;
|
||||
}
|
||||
@@ -44,22 +44,43 @@ namespace Convention.Symbolization.Internal
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ScriptFunction : Function
|
||||
{
|
||||
public ScriptFunction(string symbolName,
|
||||
string functionName, Type returnType, Type[] parameterTypes)
|
||||
: base(symbolName, functionName, returnType, parameterTypes)
|
||||
{
|
||||
}
|
||||
|
||||
public override Function CloneVariable(string targetSymbolName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool Equals(Function other)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override Variable Invoke(SymbolizationContext context, Variable[] parameters)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct FunctionSymbol
|
||||
{
|
||||
public readonly string FunctionName;
|
||||
public readonly Type ReturnType;
|
||||
public readonly Type[] ParameterTypes;
|
||||
public readonly Modification[] Modifiers;
|
||||
public readonly string FullName;
|
||||
|
||||
public FunctionSymbol(string symbolName, Type returnType, Type[] parameterTypes, Modification[] modifiers)
|
||||
public FunctionSymbol(string symbolName, Type returnType, Type[] parameterTypes)
|
||||
{
|
||||
this.FunctionName = symbolName;
|
||||
this.ReturnType = returnType;
|
||||
this.ParameterTypes = parameterTypes;
|
||||
this.Modifiers = modifiers;
|
||||
this.FullName = $"{returnType.FullName} {symbolName}({string.Join(',', parameterTypes.ToList().ConvertAll(x => x.FullName))}) {string.Join(' ', modifiers.ToList().ConvertAll(x => x.ToString()))}";
|
||||
this.FullName = $"{returnType.FullName} {symbolName}({string.Join(',', parameterTypes.ToList().ConvertAll(x => x.FullName))})";
|
||||
}
|
||||
|
||||
public bool Equals(FunctionSymbol other)
|
||||
|
@@ -1,34 +1,31 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public abstract class Keyword : Variable
|
||||
public abstract class Keyword : CloneableVariable<Keyword>
|
||||
{
|
||||
protected Keyword(string keyword, Type realType) : base(new(keyword, realType))
|
||||
protected Keyword(string keyword) : base(keyword)
|
||||
{
|
||||
Keywords.Add(keyword, this);
|
||||
Keywords.TryAdd(keyword, this);
|
||||
}
|
||||
|
||||
public static readonly Dictionary<string, Keyword> Keywords = new();
|
||||
|
||||
public override bool Equals(Keyword other)
|
||||
{
|
||||
return this.GetType() == other.GetType();
|
||||
}
|
||||
|
||||
public abstract Keyword ControlContext(SymbolizationContext context, ScriptWordVariable next);
|
||||
}
|
||||
|
||||
public abstract class Keyword<T> : Keyword where T:Keyword<T>,new()
|
||||
public abstract class Keyword<T> : Keyword where T : Keyword<T>, new()
|
||||
{
|
||||
private static T MyInstance = new();
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return MyInstance;
|
||||
}
|
||||
}
|
||||
|
||||
protected Keyword(string keyword) : base(keyword, typeof(T))
|
||||
protected Keyword(string keyword) : base(keyword)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -50,6 +47,36 @@ namespace Convention.Symbolization.Keyword
|
||||
public Import() : base("import")
|
||||
{
|
||||
}
|
||||
|
||||
public override Internal.Keyword CloneVariable(string targetSymbolName)
|
||||
{
|
||||
return new Import();
|
||||
}
|
||||
|
||||
private ToolFile ImportFile = new("./");
|
||||
private string buffer = "";
|
||||
|
||||
public override Internal.Keyword ControlContext(SymbolizationContext context, Internal.ScriptWordVariable next)
|
||||
{
|
||||
if (next.word == ";")
|
||||
{
|
||||
var importContext = new SymbolizationContext(context);
|
||||
importContext.Compile(ImportFile);
|
||||
return null;
|
||||
}
|
||||
else if(next.word==".")
|
||||
{
|
||||
ImportFile = ImportFile | buffer;
|
||||
buffer = "";
|
||||
if (ImportFile.Exists() == false)
|
||||
throw new FileNotFoundException($"File path not found: {ImportFile}", ImportFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer += next.word;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -63,7 +90,7 @@ namespace Convention.Symbolization.Keyword
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b><see langword="def"/> FunctionName(parameter-list) -> return-type { ... return return-type-instance; }</b>
|
||||
/// <b><see langword="def"/> FunctionName(parameter-list) return-type { ... return return-type-instance; }</b>
|
||||
/// </summary>
|
||||
public sealed class FunctionDef : Internal.Keyword<FunctionDef>
|
||||
{
|
||||
|
@@ -1,21 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
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))
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@@ -15,8 +15,6 @@ namespace Convention.Symbolization
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -25,9 +23,9 @@ namespace Convention.Symbolization
|
||||
Internal.SymbolizationReader reader = new();
|
||||
foreach (char ch in allText)
|
||||
reader.ReadChar(ch);
|
||||
ScriptWords = reader.ScriptWords;
|
||||
// Turn the script words into commands
|
||||
// TODO
|
||||
var ScriptWords = reader.ScriptWords;
|
||||
// Turn the script words into scope words
|
||||
reader.CompleteScopeWord(this);
|
||||
}
|
||||
|
||||
public void Compile(ToolFile file)
|
||||
|
@@ -8,10 +8,12 @@ namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public class SymbolizationReader
|
||||
{
|
||||
#region Read Script Words
|
||||
|
||||
private string buffer = "";
|
||||
private int lineContext = 0;
|
||||
private int lineContext = 1;
|
||||
private bool isOutOfStringline = true;
|
||||
public Dictionary<int, List<Variable>> ScriptWords = new() { { 0, new() } };
|
||||
public Dictionary<int, List<Variable>> ScriptWords = new() { { 1, new() } };
|
||||
|
||||
private static HashSet<char> ControllerCharSet = new()
|
||||
{
|
||||
@@ -22,6 +24,7 @@ namespace Convention.Symbolization.Internal
|
||||
'^',
|
||||
':',',','.','?',/*'\'','"',*/
|
||||
'@','#','$',
|
||||
';'
|
||||
};
|
||||
|
||||
public void ReadChar(char ch)
|
||||
@@ -30,16 +33,16 @@ namespace Convention.Symbolization.Internal
|
||||
{
|
||||
if (ControllerCharSet.Contains(ch))
|
||||
CompleteSingleSymbol(ch);
|
||||
else if (char.IsWhiteSpace(ch) || ch == '\n' || ch == '\r' || ch == '\t')
|
||||
else if (ch == '\n')
|
||||
CompleteLine();
|
||||
else if (char.IsWhiteSpace(ch) || 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");
|
||||
throw new NotImplementedException($"{lineContext + 1} line, {buffer} + <{ch}> not implemented");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -48,7 +51,7 @@ namespace Convention.Symbolization.Internal
|
||||
else if (ch == '"')
|
||||
EndString();
|
||||
else
|
||||
throw new NotImplementedException($"{lineContext+1} line, \"{buffer}\" + \'{ch}\' not implemented");
|
||||
throw new NotImplementedException($"{lineContext + 1} line, \"{buffer}\" + \'{ch}\' not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,16 +70,25 @@ namespace Convention.Symbolization.Internal
|
||||
{
|
||||
if (buffer.Length > 0)
|
||||
{
|
||||
ScriptWords[lineContext].Add(new ScriptWordVariable(buffer));
|
||||
if (ScriptWords.TryGetValue(lineContext, out var line) == false)
|
||||
{
|
||||
ScriptWords.Add(lineContext, line = new());
|
||||
}
|
||||
if (Internal.Keyword.Keywords.TryGetValue(buffer, out var keyword))
|
||||
{
|
||||
line.Add(keyword.Clone() as Internal.Variable);
|
||||
}
|
||||
else
|
||||
{
|
||||
line.Add(new ScriptWordVariable(buffer));
|
||||
}
|
||||
buffer = "";
|
||||
}
|
||||
}
|
||||
void CompleteLine()
|
||||
{
|
||||
CompleteWord();
|
||||
ScriptWords[lineContext].Add(new ScriptWordVariable(";"));
|
||||
lineContext++;
|
||||
ScriptWords.Add(lineContext, new());
|
||||
lineContext++; ;
|
||||
}
|
||||
void BeginString()
|
||||
{
|
||||
@@ -89,5 +101,39 @@ namespace Convention.Symbolization.Internal
|
||||
isOutOfStringline = true;
|
||||
CompleteWord();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Read Scope Words
|
||||
|
||||
public void CompleteScopeWord(SymbolizationContext rootContext)
|
||||
{
|
||||
int wordCounter = 1;
|
||||
Internal.Keyword currentKey = null;
|
||||
foreach(var line in ScriptWords)
|
||||
{
|
||||
foreach (var word in line.Value)
|
||||
{
|
||||
if (currentKey == null)
|
||||
{
|
||||
if(currentKey is Internal.Keyword cky)
|
||||
{
|
||||
currentKey = cky;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidGrammarException($"Line {line.Key}, word {wordCounter}: Expected a keyword, but got {word}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
currentKey = currentKey.ControlContext(rootContext, word);
|
||||
}
|
||||
wordCounter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@@ -10,19 +10,15 @@ public class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Create a new SymbolizationContext
|
||||
var context = new SymbolizationContext();
|
||||
// Compile a script from a file
|
||||
var toolFile = new ToolFile("example_script.txt");
|
||||
var context = new SymbolizationRunner();
|
||||
try
|
||||
{
|
||||
context.Compile(toolFile);
|
||||
context.Compile("example_script.txt");
|
||||
Console.WriteLine("Script compiled successfully.");
|
||||
}
|
||||
catch (FileNotFoundException ex)
|
||||
{
|
||||
Console.WriteLine($"Error: {ex.Message}");
|
||||
toolFile.Create();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user