EP 0.1.1
This commit is contained in:
16
Convention/[Symbolization]/Detail/Exception.cs
Normal file
16
Convention/[Symbolization]/Detail/Exception.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Convention.Symbolization
|
||||
{
|
||||
|
||||
[Serializable]
|
||||
public class InvalidGrammarException : Exception
|
||||
{
|
||||
public InvalidGrammarException() { }
|
||||
public InvalidGrammarException(string message) : base(message) { }
|
||||
public InvalidGrammarException(string message, Exception inner) : base(message, inner) { }
|
||||
protected InvalidGrammarException(
|
||||
System.Runtime.Serialization.SerializationInfo info,
|
||||
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
101
Convention/[Symbolization]/Detail/Keyword.cs
Normal file
101
Convention/[Symbolization]/Detail/Keyword.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public abstract class Keyword : Variable
|
||||
{
|
||||
protected Keyword(string keyword, Type realType) : base(new(keyword, realType))
|
||||
{
|
||||
Keywords.Add(keyword, this);
|
||||
}
|
||||
|
||||
public static readonly Dictionary<string, Keyword> Keywords = new();
|
||||
|
||||
public abstract SymbolizationContext Execute(SymbolizationContext context, Variable[] leftParameters, Variable[] rightParameters);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return SymbolInfo.SymbolName;
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
}
|
||||
|
||||
public override bool Equals(Variable other)
|
||||
{
|
||||
return MyInstance == other;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace Convention.Symbolization.Keyword
|
||||
{
|
||||
public sealed class ExitScope : Internal.Keyword<ExitScope>
|
||||
{
|
||||
public ExitScope() : base("__exit_scope__")
|
||||
{
|
||||
}
|
||||
|
||||
public override SymbolizationContext Execute(SymbolizationContext context, Internal.Variable[] leftParameters, Internal.Variable[] rightParameters)
|
||||
{
|
||||
context.CurrentNamespace.EndAndTApplyUpdate();
|
||||
return context.ParentContext;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Namespace : Internal.Keyword<Namespace>
|
||||
{
|
||||
public Namespace() : base("namespace")
|
||||
{
|
||||
}
|
||||
public override SymbolizationContext Execute(SymbolizationContext context, Internal.Variable[] leftParameters, Internal.Variable[] rightParameters)
|
||||
{
|
||||
if (leftParameters.Length != 0)
|
||||
throw new InvalidGrammarException($"{this}: has invalid subject");
|
||||
if (rightParameters.Length != 1)
|
||||
throw new InvalidGrammarException($"{this}: needs to have one and only one object");
|
||||
Internal.Namespace subNamespace = context.CurrentNamespace.CreateOrGetSubNamespace(rightParameters[0].ToString());
|
||||
subNamespace.BeginUpdate();
|
||||
SymbolizationContext subContext = new(context, subNamespace);
|
||||
return subContext;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Structure : Internal.Keyword<Structure>
|
||||
{
|
||||
public Structure() : base("struct")
|
||||
{
|
||||
}
|
||||
|
||||
public override SymbolizationContext Execute(SymbolizationContext context, Internal.Variable[] leftParameters, Internal.Variable[] rightParameters)
|
||||
{
|
||||
if (leftParameters.Length != 0)
|
||||
throw new InvalidGrammarException($"{this}: has invalid subject");
|
||||
if (rightParameters.Length != 1)
|
||||
throw new InvalidGrammarException($"{this}: needs to have one and only one object");
|
||||
Internal.Namespace subNamespace = context.CurrentNamespace.CreateOrGetSubNamespace(rightParameters[0].ToString());
|
||||
subNamespace.BeginUpdate();
|
||||
SymbolizationContext subContext = new(context, subNamespace);
|
||||
return subContext;
|
||||
}
|
||||
}
|
||||
}
|
@@ -45,7 +45,7 @@ namespace Convention.Symbolization.Internal
|
||||
Structures.Add(structure.SymbolInfo.SymbolName, structure);
|
||||
}
|
||||
|
||||
public void EndAndApplyUpdate()
|
||||
public void EndAndTApplyUpdate()
|
||||
{
|
||||
Updateable--;
|
||||
if (Updateable == 0)
|
53
Convention/[Symbolization]/Detail/PrimitiveType.cs
Normal file
53
Convention/[Symbolization]/Detail/PrimitiveType.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
|
||||
namespace Convention.Symbolization.Primitive
|
||||
{
|
||||
public class PrimitiveType<T> : Internal.Variable
|
||||
{
|
||||
public PrimitiveType() : base(new(typeof(T).Name, typeof(T)))
|
||||
{
|
||||
}
|
||||
|
||||
public override bool Equals(Internal.Variable other)
|
||||
{
|
||||
return other is PrimitiveType<T>;
|
||||
}
|
||||
|
||||
public virtual T CloneValue(T value)
|
||||
{
|
||||
if (Utility.IsNumber(typeof(T)))
|
||||
return value;
|
||||
else if (Utility.IsString(typeof(T)))
|
||||
return (T)(object)new string((string)(object)value);
|
||||
else if (value is ICloneable cloneable)
|
||||
return (T)cloneable.Clone();
|
||||
else if (value is Internal.Variable)
|
||||
return value;
|
||||
return value;
|
||||
}
|
||||
|
||||
public virtual T DefaultValue() => default;
|
||||
}
|
||||
|
||||
public class PrimitiveInstance<T> : Internal.CloneableVariable<PrimitiveInstance<T>>
|
||||
{
|
||||
private readonly PrimitiveType<T> MyPrimitiveType = new();
|
||||
public T Value;
|
||||
|
||||
public PrimitiveInstance(string symbolName, T value, PrimitiveType<T> primitiveType) : base(symbolName)
|
||||
{
|
||||
this.Value = value;
|
||||
this.MyPrimitiveType = primitiveType;
|
||||
}
|
||||
|
||||
public override PrimitiveInstance<T> CloneVariable(string targetSymbolName)
|
||||
{
|
||||
return new(targetSymbolName, MyPrimitiveType.CloneValue(this.Value), this.MyPrimitiveType);
|
||||
}
|
||||
|
||||
public override bool Equals(PrimitiveInstance<T> other)
|
||||
{
|
||||
return this.Value.Equals(other.Value);
|
||||
}
|
||||
}
|
||||
}
|
13
Convention/[Symbolization]/Detail/Reader.cs
Normal file
13
Convention/[Symbolization]/Detail/Reader.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.Symbolization.Internal
|
||||
{
|
||||
public class SymbolizationReader
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@@ -8,7 +8,17 @@ 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<string, Internal.Variable> Variables = new();
|
||||
public readonly Internal.Namespace CurrentNamespace;
|
||||
}
|
||||
|
||||
public class SymbolizationRunner
|
||||
@@ -23,5 +33,18 @@ namespace Convention.Symbolization
|
||||
}
|
||||
public SymbolizationRunner() :this(new()){ }
|
||||
|
||||
public Exception Compile()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user