-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8049b54
commit 6663bab
Showing
13 changed files
with
1,427 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.