Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Nitra typing on Nitra.
Browse files Browse the repository at this point in the history
  • Loading branch information
VladD2 committed Nov 6, 2015
1 parent 441badb commit 46de5aa
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 26 deletions.
Expand Up @@ -15,7 +15,7 @@ namespace CSharp
{
symbol
{
out TypeParametersCount : int = GetTypeParametersCount(this.Declarations);
out TypeParametersCount : int = GetTypeParametersCount(this.FirstDeclarationOrDefault);
out TypeParameters : SCG.IList[TypeParameterSymbol] = CreateTypeParameters(TypeParametersCount);
}

Expand Down
4 changes: 2 additions & 2 deletions Grammars/CSharp/CSharp.Grammar/Utils.n
Expand Up @@ -104,9 +104,9 @@ namespace CSharp
}
}

public GetTypeParametersCount(declarations : Seq[GenericEntity]) : int
public GetTypeParametersCount(declaration : Declaration) : int
{
declarations.FirstOrDefault()?.TypeParameterAliases?.Count
(declaration :> GenericEntity).TypeParameterAliases?.Count
}

public GetTypeParameter(alias : TypeParameterAlias, typeParameterSymbols : IList[TypeParameterSymbol], IndexIn : int) : TypeParameterSymbol
Expand Down
13 changes: 8 additions & 5 deletions Nitra/Boot2/Nitra.Runtime/Declarations/Declaration.n
Expand Up @@ -11,11 +11,14 @@ namespace Nitra.Runtime.Binding

public partial interface DeclarationSymbol : IDependentPropertyContainer, ISerializable
{
Id : int { get; }
Name : string { get; }
IsNameValid : bool { get; }
Owner : TableScope { get; }

Id : int { get; }
Name : string { get; }
IsNameValid : bool { get; }
Owner : TableScope { get; }
FirstDeclarationOrDefault : Declaration { get; }
DeclarationsCount : int { get; }
HasDeclarations : bool { get; }

GetDeclarationsUntyped() : Seq[Declaration];
AddDeclaration(newDeclaration : Declaration) : void; // TODO: remove from public interface
RemoveDeclarations(shouldRemove : Predicate[Declaration]) : int; // TODO: remove from public interface
Expand Down
87 changes: 82 additions & 5 deletions Nitra/Nitra.Grammar/AST/DotNet/CompilationUnit.n
Expand Up @@ -105,7 +105,52 @@ namespace DotNet
def span = NSpan(0);
def nodeToSymbolMap = Hashtable();
def alaises = List();


def createTxTypeParameters(count : int) : array[TypeParameterSymbol]
{
if (count == 0)
Utils.NoTypeParameters
else
{
def result = array(count);
for (mutable i = 0; i < result.Length; ++i)
{
def tps = TypeParameterSymbol();
def name = NRB.Name(file, span, if (count <= 1) "T" else "T" + i);
def decl = TypeParameterStubDeclaration(name, tps);
tps.AddDeclaration(decl);
result[i] = tps;
}
result
}
}
def createTypeParameters(tycon : TypeInfo) : array[TypeParameterSymbol]
{
def count = tycon.TyparmsCount;
if (count == 0)
Utils.NoTypeParameters
else
{
def result = array(count);
mutable typarms = tycon.Typarms;
for (mutable i = 0; i < result.Length; ++i)
{
def tp = typarms.Head;
def tps = TypeParameterSymbol();
def name = NRB.Name(file, span, tp.Name);
def decl = TypeParameterDeclaration(name, tps, tp);
tps.AddDeclaration(decl);
result[i] = tps;
typarms = typarms.Tail;
}
result
}
}
def mekeTypeParameters(symbol : GenericEntitySymbol, tycon : TypeInfo) : void
{
symbol.TypeParametersCount = tycon.TyparmsCount;
symbol.TypeParameters = createTypeParameters(tycon);
}
def loadNsMemebers(node : NamespaceTree.Node, parent : DotNet.NamespaceSymbol) : void
{
def addTopType(node : NamespaceTree.Node, tycon : TypeInfo) : void
Expand All @@ -121,20 +166,20 @@ namespace DotNet
else if (tycon.IsInterface) TopInterfaceSymbol()
else if (tycon.IsValueType) TopStructSymbol()
else TopClassSymbol();
mekeTypeParameters(symbol, tycon);
symbol.Parent = parent;
nodeToSymbolMap[tycon] = symbol;
symbol.TypeParametersCount = tycon.TyparmsCount;
symbol.TypeParameters = Utils.CreateTypeParameters(tycon.TyparmsCount);
def decl = ExternalTypeDeclaration(name, symbol, tycon);
_ = parent.MemberTable.Define(decl, context, null);
symbol.Parent = parent;
symbol.EvalProperties(context);

| Alias =>
def symbol = TypeAliasSymbol();
def decl = ExternalTypeDeclaration(name, symbol, tycon);
_ = parent.MemberTable.Define(decl, context, null);
mekeTypeParameters(symbol, tycon);
symbol.Parent = parent;
alaises.Add(symbol);
_ = parent.MemberTable.Define(decl, context, null);
nodeToSymbolMap[tycon] = symbol;
symbol.EvalProperties(context);

Expand Down Expand Up @@ -235,6 +280,22 @@ namespace DotNet

makeTuples();

def makeOptionList() : void
{
def parent = rootNamespace;
def name = NRB.Name(file, span, "#OptionList");
def symbol = TypeAliasSymbol();
def decl = OptionListDeclaration(name, symbol);
symbol.TypeParametersCount = 1;
symbol.TypeParameters = createTxTypeParameters(1);
symbol.Parent = parent;
alaises.Add(symbol);
_ = parent.MemberTable.Define(decl, context, null);
symbol.EvalProperties(context);
}

makeOptionList();

def res1 = bind(rootNamespace.MemberTable, "Nemerle");

when (res1.IsSymbolEvaluated)
Expand All @@ -248,6 +309,22 @@ namespace DotNet
}
}

[Record]
class OptionListDeclaration : ExternalDeclaration
{
}

[Record]
class TypeParameterDeclaration : ExternalDeclaration
{
public NemerleTypeParam : StaticTypeVar;
}

[Record]
class TypeParameterStubDeclaration : ExternalDeclaration
{
}

[Record]
class ExternalTypeDeclaration : ExternalDeclaration
{
Expand Down
4 changes: 1 addition & 3 deletions Nitra/Nitra.Grammar/AST/DotNet/Type/GenericType.nitra
Expand Up @@ -18,10 +18,8 @@ namespace DotNet
{
symbol
{
out TypeParametersCount : int = 42;
out TypeParametersCount : int = GetTypeParametersCount(this.FirstDeclarationOrDefault);
out TypeParameters : SCG.IList[TypeParameterSymbol] = CreateTypeParameters(TypeParametersCount);

//Symbol.TypeParametersCount = GetTypeParametersCount(this.Declarations);
}


Expand Down
10 changes: 6 additions & 4 deletions Nitra/Nitra.Grammar/AST/DotNet/Type/Utils.n
Expand Up @@ -3,6 +3,8 @@ using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using Nitra.Runtime.Binding;

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,9 +13,9 @@ namespace DotNet
{
module Utils
{
public GetTypeParametersCount(declarations : Seq[GenericEntity]) : int
public GetTypeParametersCount(declaration : Declaration) : int
{
declarations.FirstOrDefault()?.TypeParameterAliases?.Count
(declaration :> GenericEntity).TypeParameterAliases?.Count
}

public GetTypeParameter(alias : TypeParameterAlias, typeParameterSymbols : IList[TypeParameterSymbol], IndexIn : int) : TypeParameterSymbol
Expand All @@ -23,12 +25,12 @@ namespace DotNet
typeParameterSymbol
}

private _noTypeParameters : array[TypeParameterSymbol] = array(0);
public NoTypeParameters : array[TypeParameterSymbol] = array(0);

public CreateTypeParameters(count : int) : array[TypeParameterSymbol]
{
if (count == 0)
_noTypeParameters
NoTypeParameters
else
{
def result = array(count);
Expand Down
2 changes: 1 addition & 1 deletion Nitra/Nitra.Grammar/AST/SyntaxModule.nitra
Expand Up @@ -12,7 +12,7 @@ namespace Nitra.Ast
symbol
{
Kind = "syntax module";
SpanClass = DotNetLangLanguage.TypeSpanClass; // NitraLangLanguage.SyntaxModuleSpanClass;
SpanClass = NitraLangLanguage.ModuleSpanClass; // NitraLangLanguage.SyntaxModuleSpanClass;
Scope = MemberTable;

in Literals : Map[string, string];
Expand Down
1 change: 1 addition & 0 deletions Nitra/Nitra.Grammar/NitraLanguage.nitra
Expand Up @@ -16,6 +16,7 @@ language NitraLang
{
span class Ast { ForegroundColor=Sienna; }
span class Language { ForegroundColor=DarkCyan; }
span class Module { ForegroundColor=DarkCyan; }
span class SyntaxModule { ForegroundColor=DarkCyan; }
span class Rule { ForegroundColor=SteelBlue; }
span class RegexRule { ForegroundColor=Olive; }
Expand Down
13 changes: 8 additions & 5 deletions Nitra/Nitra.Runtime/Declarations/Declaration.n
Expand Up @@ -11,11 +11,14 @@ namespace Nitra.Runtime.Binding

public partial interface DeclarationSymbol : IDependentPropertyContainer, ISerializable
{
Id : int { get; }
Name : string { get; }
IsNameValid : bool { get; }
Owner : TableScope { get; }

Id : int { get; }
Name : string { get; }
IsNameValid : bool { get; }
Owner : TableScope { get; }
FirstDeclarationOrDefault : Declaration { get; }
DeclarationsCount : int { get; }
HasDeclarations : bool { get; }

GetDeclarationsUntyped() : Seq[Declaration];
AddDeclaration(newDeclaration : Declaration) : void; // TODO: remove from public interface
RemoveDeclarations(shouldRemove : Predicate[Declaration]) : int; // TODO: remove from public interface
Expand Down

0 comments on commit 46de5aa

Please sign in to comment.