Skip to content

Instantly share code, notes, and snippets.

@NN---
Last active December 10, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NN---/4445011 to your computer and use it in GitHub Desktop.
Save NN---/4445011 to your computer and use it in GitHub Desktop.
/// <summary>
/// Transforms system type identifier with generic arguments to the parse-tree expression.
/// Number of elemnts in args must match number of generic parameters in the systemType.
/// </summary>
/// <example>
/// "NS.A`2+B`1", ["X", "Y", "Z"] ==> <[ NS.A.[X, Y].B.[Z] ]>
/// </example>
public static FromSystemType(manager : ManagerClass, systemType : string, args : list[string]) : PExpr
{
if (string.IsNullOrEmpty (systemType)) <[]>
else
{
// Split according to generic arguments number
def parts = _genericArgs.Split(systemType.Replace("+", "."));
if (parts.Length == 1)
// No generic, convert as usual
FromQualifiedIdentifier(manager, systemType)
else
{
def name(n)
{
<[ $(Name(n, manager.MacroColors.UseColor, manager.MacroColors.UseContext) : name) ]>
}
def createExpr(parts, pos, args, posArg, expr)
{
if (pos >= 0)
{
def part = parts[pos];
if (!part.IsEmpty())
{
// even - Member
// odd - GenericSpecifier
if (pos % 2 == 0)
{
def split = part.Split('.');
// Create expression
mutable ex = createExpr(parts, pos - 1, args, posArg, expr);
// Add first member if can and need
ex =
if (ex is <[]>)
name(split[0])
else
if (!split[0].IsEmpty())
<[ $ex.$(name(split[0])) ]>
else
ex;
// Add other members if exist
for (mutable i = 1; i < split.Length; i++)
ex = <[ $ex.$(name(split[i])) ]>;
ex
}
else
{
def argsNum = int.Parse(parts[pos]);
// Create expression
mutable ex = createExpr(parts, pos - 1, args, posArg - argsNum, expr);
// Create generic args
mutable argsExpr = [];
for (mutable i = 0; i < argsNum; i++)
argsExpr ::= name(args[posArg - argsNum + i]);
<[ $ex.[..$(argsExpr.Rev())] ]>
}
}
else
// Skip empty sequence
createExpr(parts, pos - 1, args, posArg, expr)
}
else
expr
}
def argsList = SCG.List(args);
def ret = createExpr(parts, parts.Length - 1, argsList, argsList.Count, <[]>);
ret
}
}
}
private static _genericArgs : Regex = Regex(@"`(\d)");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment