Skip to content

Commit

Permalink
Project directory restructure.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvontamar committed Jul 2, 2023
1 parent 8049b54 commit 6663bab
Show file tree
Hide file tree
Showing 13 changed files with 1,427 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Tamar.Clausewitz/Constructs/Binding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Tamar.Clausewitz.Constructs;

/// <summary>
/// Any statement which includes the assignment operator '=' in Clausewitz.
/// Including most commands, conditions
/// and triggers which come in a single line.
/// </summary>
public class Binding : Construct
{
/// <summary>Left side.</summary>
public string Name;

/// <summary>Right side.</summary>
public string Value;

/// <summary>Primary constructor.</summary>
/// <param name="parent">Parent scope.</param>
/// <param name="name">Left side.</param>
/// <param name="value">Right side.</param>
internal Binding(Scope parent, string name, string value) : base(parent)
{
Name = name;
Value = value;
}
}
72 changes: 72 additions & 0 deletions Tamar.Clausewitz/Constructs/Construct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Tamar.Clausewitz.Constructs;

/// <summary>Basic Clausewitz language construct.</summary>
public abstract class Construct
{
/// <summary>Associated comments.</summary>
public readonly List<string> Comments = new();

/// <summary>Construct type & parent must be defined when created.</summary>
/// <param name="parent">Parent scope.</param>
protected Construct(Scope parent)
{
Parent = parent;
}

/// <summary>Scope's depth level within the containing file.</summary>
public int Level
{
get
{
// This recursive function retrieves the count of all parents up to the root.
var parentScopes = 0;
var currentScope = Parent;
while (true)
{
if (currentScope == null)
return parentScopes;
parentScopes++;
currentScope = currentScope.Parent;
}
}
}

/// <summary>The parent scope.</summary>
public Scope Parent { get; internal set; }

/// <summary>
/// Extracts pragmas from associated comments within brackets, which are separated
/// by commas, and their keywords
/// which are separated by spaces.
/// </summary>
public IEnumerable<Pragma> Pragmas
{
get
{
var allComments = new List<string>();
var @return = new HashSet<Pragma>();
if (Comments != null)
allComments.AddRange(Comments);
if (this is Scope scope)
if (scope.EndComments != null)
allComments.AddRange(scope.EndComments);
if (allComments.Count == 0)
return @return;
foreach (var comment in allComments)
{
if (!(comment.Contains('[') && comment.Contains(']')))
continue;

// All pragmas are guaranteed to be lower case and trimmed.
var pragmas = Regex.Replace(comment.Split('[', ']')[1], @"\s+", " ").ToLower().Split(',');
foreach (var pragma in pragmas)
@return.Add(new Pragma(pragma.Split(' ')));
}

return @return;
}
}
}
45 changes: 45 additions & 0 deletions Tamar.Clausewitz/Constructs/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;

namespace Tamar.Clausewitz.Constructs;

/// <summary>
/// Extension class for all language constructs.
/// </summary>
public static class Extensions
{
/// <summary>
/// Checks if a collection of pragmas has the requested pragma with the specified
/// keywords.
/// </summary>
/// <param name="pragmas">Extended.</param>
/// <param name="keywords">Keywords (all keywords).</param>
/// <returns>Returns true if a pragma with the said keywords was found.</returns>
public static bool Contains(this IEnumerable<Pragma> pragmas, params string[] keywords)
{
foreach (var pragma in pragmas)
if (pragma.Contains(keywords))
return true;
return false;
}

/// <summary>
/// Formats a keyword to lower case and trims it.
/// </summary>
/// <param name="keyword">Extended.</param>
/// <returns>Formatted</returns>
internal static string FormatKeyword(this string keyword)
{
return keyword.Trim().ToLower();
}

/// <summary>
/// Formats all keywords with lazy evaluation.
/// </summary>
/// <param name="keywords">Extended.</param>
/// <returns>Formatted.</returns>
internal static IEnumerable<string> FormatKeywords(this IEnumerable<string> keywords)
{
foreach (var keyword in keywords)
yield return keyword.FormatKeyword();
}
}
47 changes: 47 additions & 0 deletions Tamar.Clausewitz/Constructs/Pragma.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;

namespace Tamar.Clausewitz.Constructs;

/// <summary>
/// Every Clausewitz construct may have pragmas within the associated comments.
/// Each pragma includes a set of
/// keywords.
/// </summary>
public struct Pragma
{
/// <summary>User-friendly constructor.</summary>
/// <param name="keywords">Keywords.</param>
public Pragma(params string[] keywords)
{
Keywords = new HashSet<string>(keywords);
}

/// <summary>Primary constructor.</summary>
/// <param name="keywords">Keywords.</param>
public Pragma(HashSet<string> keywords)
{
Keywords = keywords;
}

/// <summary>Checks if a pragma has all of the specified keywords.</summary>
/// <param name="keywords">Keywords.</param>
/// <returns>Boolean.</returns>
public bool Contains(IEnumerable<string> keywords)
{
return Keywords.IsSupersetOf(keywords.FormatKeywords());
}

/// <summary>Checks if a pragma has all of the specified keywords.</summary>
/// <param name="keywords">Keywords.</param>
/// <returns>Boolean.</returns>
public bool Contains(params string[] keywords)
{
return Keywords.IsSupersetOf(keywords.FormatKeywords());
}

/// <summary>
/// Keywords are separated by spaces within each pragma, and their order does not
/// matter.
/// </summary>
public readonly HashSet<string> Keywords;
}
Loading

0 comments on commit 6663bab

Please sign in to comment.