diff --git a/Convention/[Symbolization]/Detail/Exception.cs b/Convention/[Symbolization]/Detail/Exception.cs new file mode 100644 index 0000000..465b0d2 --- /dev/null +++ b/Convention/[Symbolization]/Detail/Exception.cs @@ -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) { } + } +} diff --git a/Convention/[Symbolization]/Internal/Functional.cs b/Convention/[Symbolization]/Detail/Functional.cs similarity index 100% rename from Convention/[Symbolization]/Internal/Functional.cs rename to Convention/[Symbolization]/Detail/Functional.cs diff --git a/Convention/[Symbolization]/Detail/Keyword.cs b/Convention/[Symbolization]/Detail/Keyword.cs new file mode 100644 index 0000000..55c99ac --- /dev/null +++ b/Convention/[Symbolization]/Detail/Keyword.cs @@ -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 Keywords = new(); + + public abstract SymbolizationContext Execute(SymbolizationContext context, Variable[] leftParameters, Variable[] rightParameters); + + public override string ToString() + { + return SymbolInfo.SymbolName; + } + } + + public abstract class Keyword : Keyword where T:Keyword,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 + { + 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 + { + 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 + { + 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; + } + } +} diff --git a/Convention/[Symbolization]/Internal/Modification.cs b/Convention/[Symbolization]/Detail/Modification.cs similarity index 100% rename from Convention/[Symbolization]/Internal/Modification.cs rename to Convention/[Symbolization]/Detail/Modification.cs diff --git a/Convention/[Symbolization]/Internal/Namespace.cs b/Convention/[Symbolization]/Detail/Namespace.cs similarity index 99% rename from Convention/[Symbolization]/Internal/Namespace.cs rename to Convention/[Symbolization]/Detail/Namespace.cs index b5a66d5..ac16fb3 100644 --- a/Convention/[Symbolization]/Internal/Namespace.cs +++ b/Convention/[Symbolization]/Detail/Namespace.cs @@ -45,7 +45,7 @@ namespace Convention.Symbolization.Internal Structures.Add(structure.SymbolInfo.SymbolName, structure); } - public void EndAndApplyUpdate() + public void EndAndTApplyUpdate() { Updateable--; if (Updateable == 0) diff --git a/Convention/[Symbolization]/Detail/PrimitiveType.cs b/Convention/[Symbolization]/Detail/PrimitiveType.cs new file mode 100644 index 0000000..90a5a3f --- /dev/null +++ b/Convention/[Symbolization]/Detail/PrimitiveType.cs @@ -0,0 +1,53 @@ +using System; + +namespace Convention.Symbolization.Primitive +{ + public class PrimitiveType : Internal.Variable + { + public PrimitiveType() : base(new(typeof(T).Name, typeof(T))) + { + } + + public override bool Equals(Internal.Variable other) + { + return other is PrimitiveType; + } + + 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 : Internal.CloneableVariable> + { + private readonly PrimitiveType MyPrimitiveType = new(); + public T Value; + + public PrimitiveInstance(string symbolName, T value, PrimitiveType primitiveType) : base(symbolName) + { + this.Value = value; + this.MyPrimitiveType = primitiveType; + } + + public override PrimitiveInstance CloneVariable(string targetSymbolName) + { + return new(targetSymbolName, MyPrimitiveType.CloneValue(this.Value), this.MyPrimitiveType); + } + + public override bool Equals(PrimitiveInstance other) + { + return this.Value.Equals(other.Value); + } + } +} diff --git a/Convention/[Symbolization]/Detail/Reader.cs b/Convention/[Symbolization]/Detail/Reader.cs new file mode 100644 index 0000000..1b022e1 --- /dev/null +++ b/Convention/[Symbolization]/Detail/Reader.cs @@ -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 + { + + } +} diff --git a/Convention/[Symbolization]/Internal/Structure.cs b/Convention/[Symbolization]/Detail/Structure.cs similarity index 100% rename from Convention/[Symbolization]/Internal/Structure.cs rename to Convention/[Symbolization]/Detail/Structure.cs diff --git a/Convention/[Symbolization]/Internal/Variable.cs b/Convention/[Symbolization]/Detail/Variable.cs similarity index 100% rename from Convention/[Symbolization]/Internal/Variable.cs rename to Convention/[Symbolization]/Detail/Variable.cs diff --git a/Convention/[Symbolization]/Symbolization.cs b/Convention/[Symbolization]/Symbolization.cs index 03b27f4..44c6f16 100644 --- a/Convention/[Symbolization]/Symbolization.cs +++ b/Convention/[Symbolization]/Symbolization.cs @@ -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 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; + } + } }