Skip to content

Commit

Permalink
Add extended enum syntax, allowing assignment of enum values upon dec…
Browse files Browse the repository at this point in the history
…laration

Signed-off-by: QiuFeng54321 <[email protected]>
  • Loading branch information
WilliamQiufeng committed Aug 16, 2023
1 parent ec603ec commit d316800
Show file tree
Hide file tree
Showing 12 changed files with 761 additions and 645 deletions.
3 changes: 2 additions & 1 deletion PseudoCode.Core/Parsing/PseudoCode.g4
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ argumentDeclaration: (Byval | Byref)? Identifier Colon dataType;
tuple: expression (Comma expression)*;

enumDefinition: Type name=Identifier Equal OpenParen enumBody CloseParen;
enumBody: Identifier (Comma Identifier)*;
enumBody: enumItem (Comma enumItem)*;
enumItem: Identifier (Equal integer)?;
typeAliasDefinition: Type Identifier Equal dataType;
typeDefinition: Type Identifier typeBody Endtype;
typeBody: INDENT typeChild+ DEDENT;
Expand Down
3 changes: 2 additions & 1 deletion PseudoCode.Core/Parsing/PseudoCode.interp

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions PseudoCode.Core/Parsing/PseudoCodeBaseListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@ public virtual void EnterEnumBody([NotNull] PseudoCodeParser.EnumBodyContext con
/// <param name="context">The parse tree.</param>
public virtual void ExitEnumBody([NotNull] PseudoCodeParser.EnumBodyContext context) { }
/// <summary>
/// Enter a parse tree produced by <see cref="PseudoCodeParser.enumItem"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void EnterEnumItem([NotNull] PseudoCodeParser.EnumItemContext context) { }
/// <summary>
/// Exit a parse tree produced by <see cref="PseudoCodeParser.enumItem"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void ExitEnumItem([NotNull] PseudoCodeParser.EnumItemContext context) { }
/// <summary>
/// Enter a parse tree produced by <see cref="PseudoCodeParser.typeAliasDefinition"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
Expand Down
23 changes: 22 additions & 1 deletion PseudoCode.Core/Parsing/PseudoCodeCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -738,10 +738,31 @@ public override void ExitTypeAliasDefinition(PseudoCodeParser.TypeAliasDefinitio
public override void ExitEnumDefinition(PseudoCodeParser.EnumDefinitionContext context)
{
base.ExitEnumDefinition(context);

var autoVal = -1;
Dictionary<string, int> enumItems = new();
Dictionary<int, string> names = new();
foreach (var enumItemContext in context.enumBody().enumItem())
{
var name = enumItemContext.Identifier().GetText();
var valueContext = enumItemContext.integer();
var value = valueContext == null ? ++autoVal : autoVal = int.Parse(valueContext.GetText());
if (!enumItems.TryAdd(name, value))
Program.AnalyserFeedbacks.Add(new Feedback
{
Message = $"Duplicate enum name {name}",
Severity = Feedback.SeverityType.Error,
SourceRange = SourceLocationHelper.SourceRange(enumItemContext)
});

names[value] = name;
}

CurrentScope.AddOperation(new MakeEnumOperation(CurrentScope, Program)
{
Name = context.name.Text,
Names = context.enumBody().Identifier().Select(i => i.GetText()).ToList(),
Values = enumItems,
Names = names,
PoiLocation = SourceLocationHelper.SourceLocation(context),
SourceRange = SourceLocationHelper.SourceRange(context)
});
Expand Down
10 changes: 10 additions & 0 deletions PseudoCode.Core/Parsing/PseudoCodeListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ public interface IPseudoCodeListener : IParseTreeListener {
/// <param name="context">The parse tree.</param>
void ExitEnumBody([NotNull] PseudoCodeParser.EnumBodyContext context);
/// <summary>
/// Enter a parse tree produced by <see cref="PseudoCodeParser.enumItem"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterEnumItem([NotNull] PseudoCodeParser.EnumItemContext context);
/// <summary>
/// Exit a parse tree produced by <see cref="PseudoCodeParser.enumItem"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitEnumItem([NotNull] PseudoCodeParser.EnumItemContext context);
/// <summary>
/// Enter a parse tree produced by <see cref="PseudoCodeParser.typeAliasDefinition"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
Expand Down
Loading

0 comments on commit d316800

Please sign in to comment.