private static Dictionary <string, string> WalkTypes( LangType actualType, LangType genericType, Dictionary <string, string> replacements) { if (genericType.IsTypeParameter) { replacements.Add(genericType.GetName(), actualType.GetName()); } else { for (var i = 0; i < genericType.SubTypes.Count; i++) { var genericSubtype = genericType.SubTypes[i]; var actualSubtype = actualType.SubTypes[i]; WalkTypes(actualSubtype, genericSubtype, replacements); } } return(replacements); }
public LangType CheckGenericTypes(LangType langType) { if (langType.Name != ParserContext.NativeTypes.LangVoid.Name) { var genericType = this.Templates.Where(tmpl => tmpl.LangType.Equals(langType)).FirstOrDefault(); if (genericType != null) { genericType.Realize(langType, this); return(GetLangType(langType.GetName())); } for (var i = 0; i < langType.SubTypes.Count; i++) { var subType = langType.SubTypes[i]; langType.SubTypes[i] = CheckGenericTypes(subType); } } return(langType); }
public static int GetTypeLength(LangType langType) { // If the type has a null body, its length is 1 return(langType?.Body?.Count() > 0 ? langType.Body.Count() + 3 : 1); }
/// <summary> /// Initializes a new instance of the <see cref="Template"/> class. /// </summary> /// <param name="langType">The lang type.</param> /// <param name="tokens">The tokens.</param> public Template(LangType langType, List <SyntaxToken> tokens) { Tokens = tokens ?? throw new ArgumentNullException(nameof(tokens)); LangType = langType ?? throw new ArgumentNullException(nameof(langType)); }
public LangType GetConcreteType(List <SyntaxToken> tokens, bool validateType = true) { var left = tokens.FirstOrDefault(tkn => tkn.Type == SyntaxTokenType.LessThan); // Type is geneic if (!(left is null)) { int leftIndex = tokens.IndexOf(left); var simpleTypeToken = tokens.Take(leftIndex).FirstOrDefault(); if (simpleTypeToken is null) { throw new ParseException(left.Column, tokens, null, "No type before <"); } var right = tokens.LastOrDefault(tkn => tkn.Type == SyntaxTokenType.GreaterThan); if (right is null) { throw new ParseException(left.Column, tokens, null, "No matching angle bracket"); } var subTypeList = new List <List <SyntaxToken> >() { new List <SyntaxToken>() }; var insideTokens = tokens.GetRange(leftIndex + 1, (tokens.IndexOf(right) - leftIndex) - 1); var nestingLevel = 0; foreach (var tkn in insideTokens) { if (tkn.Type == SyntaxTokenType.LessThan) { nestingLevel++; } if (tkn.Type == SyntaxTokenType.GreaterThan) { nestingLevel--; } if (tkn.Type == SyntaxTokenType.Comma && nestingLevel == 0) { subTypeList.Add(new List <SyntaxToken>()); } else { subTypeList.LastOrDefault().Add(tkn); } } var simpleType = new LangType( simpleTypeToken.Token, new List <VariableDeclaration>(), simpleTypeToken.File, simpleTypeToken.Line, simpleTypeToken.Column); simpleType.SubTypes = subTypeList.Select(t => GetConcreteType(t, validateType)).ToList(); return(simpleType); } // Type is not geneic var typeToken = tokens.FirstOrDefault(); if (typeToken is null) { throw new ParseException(0, null, null, "Unable to find type"); } var langType = this.GetLangType(typeToken.Token); if (langType is null) { if (validateType) { throw new TypeNotFoundException(typeToken.Token); } else { langType = new LangType( typeToken.Token, new List <VariableDeclaration>(), typeToken.File, typeToken.Line, typeToken.Column); } } return(langType); }
public void AddLangType(LangType langType) { LangTypes.Add(langType); }