This commit is contained in:
ninemine
2025-07-02 18:46:09 +08:00
parent 9345700d8a
commit 15878d7e3b
5 changed files with 93 additions and 23 deletions

View File

@@ -16,11 +16,6 @@ namespace Convention.Symbolization.Internal
public static readonly Dictionary<string, Keyword> Keywords = new(); public static readonly Dictionary<string, Keyword> Keywords = new();
public abstract SymbolizationContext Execute(SymbolizationContext context, Variable[] leftParameters, Variable[] rightParameters); 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() public abstract class Keyword<T> : Keyword where T:Keyword<T>,new()
@@ -49,19 +44,6 @@ namespace Convention.Symbolization.Internal
namespace Convention.Symbolization.Keyword 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 sealed class Namespace : Internal.Keyword<Namespace>
{ {
public Namespace() : base("namespace") public Namespace() : base("namespace")
@@ -80,6 +62,69 @@ namespace Convention.Symbolization.Keyword
} }
} }
public sealed class Return : Internal.Keyword<Return>
{
public Return() : base("return")
{
}
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}: has invalid object");
// Inject return command to context
return context;
}
}
public sealed class While : Internal.Keyword<While>
{
public While() : base("while")
{
}
public override SymbolizationContext Execute(SymbolizationContext context, Internal.Variable[] leftParameters, Internal.Variable[] rightParameters)
{
if (leftParameters.Length != 0)
throw new InvalidGrammarException($"{this}: needs to have one and only one subject");
if (rightParameters.Length != 1)
throw new InvalidGrammarException($"{this}: has invalid object");
SymbolizationContext subContext
return context;
}
}
public sealed class Break : Internal.Keyword<Break>
{
public Break() : base("break")
{
}
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 != 0)
throw new InvalidGrammarException($"{this}: has invalid object");
return context;
}
}
public sealed class Continue : Internal.Keyword<Continue>
{
public Continue() : base("continue")
{
}
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 != 0)
throw new InvalidGrammarException($"{this}: has invalid object");
return context;
}
}
public sealed class Structure : Internal.Keyword<Structure> public sealed class Structure : Internal.Keyword<Structure>
{ {
public Structure() : base("struct") public Structure() : base("struct")
@@ -92,10 +137,21 @@ namespace Convention.Symbolization.Keyword
throw new InvalidGrammarException($"{this}: has invalid subject"); throw new InvalidGrammarException($"{this}: has invalid subject");
if (rightParameters.Length != 1) if (rightParameters.Length != 1)
throw new InvalidGrammarException($"{this}: needs to have one and only one object"); throw new InvalidGrammarException($"{this}: needs to have one and only one object");
Internal.Namespace subNamespace = context.CurrentNamespace.CreateOrGetSubNamespace(rightParameters[0].ToString()); return context;
subNamespace.BeginUpdate(); }
SymbolizationContext subContext = new(context, subNamespace);
return subContext; public sealed class Function : Internal.Keyword<Function>
{
public Function() : base("def")
{
}
public override SymbolizationContext Execute(SymbolizationContext context, Internal.Variable[] leftParameters, Internal.Variable[] rightParameters)
{
if (leftParameters.Length != 0)
throw new InvalidGrammarException($"{this}: has invalid subject");
}
} }
} }
} }

View File

@@ -45,11 +45,12 @@ namespace Convention.Symbolization.Internal
Structures.Add(structure.SymbolInfo.SymbolName, structure); Structures.Add(structure.SymbolInfo.SymbolName, structure);
} }
public void EndAndTApplyUpdate() public bool EndAndTApplyUpdate()
{ {
Updateable--; Updateable--;
if (Updateable == 0) if (Updateable == 0)
Refresh(); Refresh();
return Updateable == 0;
} }
private readonly Dictionary<string, int> SymbolCounter = new(); private readonly Dictionary<string, int> SymbolCounter = new();

View File

@@ -49,5 +49,9 @@ namespace Convention.Symbolization.Primitive
{ {
return this.Value.Equals(other.Value); return this.Value.Equals(other.Value);
} }
public override string ToString()
{
return Value.ToString();
}
} }
} }

View File

@@ -22,6 +22,10 @@ namespace Convention.Symbolization.Internal
{ {
return base.GetHashCode(); return base.GetHashCode();
} }
public override string ToString()
{
return SymbolInfo.SymbolName;
}
} }

View File

@@ -19,6 +19,11 @@ namespace Convention.Symbolization
public readonly SymbolizationContext ParentContext; public readonly SymbolizationContext ParentContext;
public readonly Dictionary<string, Internal.Variable> Variables = new(); public readonly Dictionary<string, Internal.Variable> Variables = new();
public readonly Internal.Namespace CurrentNamespace; public readonly Internal.Namespace CurrentNamespace;
public SymbolizationContext Compile()
{
return this;
}
} }
public class SymbolizationRunner public class SymbolizationRunner