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

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
VladD2 committed May 28, 2015
1 parent d9cea52 commit 406f20f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 45 deletions.
Expand Up @@ -13,7 +13,7 @@ namespace CSharp
Members.Parent = RootNamespace;
ExternAlias.ScopeIn = RootScope;
UsingDirectives.ScopeBuilderIn = UsingsScopeBuilder(ExternAlias.ScopeOut);
Members.Scope = UsingDirectives.ScopeBuilderOut.ResultScop;
Members.Scope = UsingDirectives.ScopeBuilderOut.ResultScope;

ExternAlias : ExternAliasDirective*;
UsingDirectives : UsingDirective*;
Expand Down
Expand Up @@ -45,7 +45,7 @@ namespace CSharp
this
}

public ResultScop : Scope
public ResultScope : Scope
{
get
{
Expand Down
Expand Up @@ -15,7 +15,7 @@ namespace CSharp

ExternAlias.ScopeIn = MakeEnteredScope(Scope, Parent :> NamespaceSymbol, Symbol);
UsingDirectives.ScopeBuilderIn = UsingsScopeBuilder(ExternAlias.ScopeOut);
Members.Scope = UsingDirectives.ScopeBuilderOut.ResultScop;
Members.Scope = UsingDirectives.ScopeBuilderOut.ResultScope;

Path : Reference*;
ExternAlias : ExternAliasDirective*;
Expand Down
Expand Up @@ -11,15 +11,15 @@ namespace CSharp

| Simple
{
Name.Symbol = Scope.TryBind(Name);
Name.Symbol = Scope.Bind(Name);
Symbol = Name.Symbol;

Name : Reference;
}

| Aliased
{
Name.Symbol = Scope.TryBind(Name);
Name.Symbol = Scope.Bind(Name);
Symbol = Name.Symbol;

Alias : Reference;
Expand All @@ -29,7 +29,7 @@ namespace CSharp
| Qualified
{
Qualifier.Scope = Scope;
Name.Symbol = Qualifier.Symbol.TryBind(Name);
Name.Symbol = Qualifier.Symbol.Bind(Name);
Symbol = Name.Symbol;

Qualifier : QualifiedReference;
Expand Down
83 changes: 45 additions & 38 deletions Nitra/Nitra.Runtime/Typing2/Binding/Scope.n
Expand Up @@ -10,17 +10,20 @@ using System;
using System.Collections.Generic;
using System.Linq;

// TODO: rename Symbol2 to Symbol and remove follow line
using Symbol = Nitra.Runtime.Binding.Symbol2;

namespace Nitra.Runtime.Binding
{
public variant Scope
{
| Table
{
public NameTable : Hashtable[int, Symbol2] { get; }
public NameTable : Hashtable[int, Symbol] { get; }

public this() { NameTable = Hashtable(); }

public DefineSymbol(symbol : Symbol2) : void
public DefineSymbol(symbol : Symbol) : void
{
mutable old;
when (this.NameTable.TryGetValue(symbol.Name.Id, out old))
Expand All @@ -40,7 +43,7 @@ namespace Nitra.Runtime.Binding
this.NameTable.Add(symbol.Name.Id, symbol);
}

public GetOrDefineSymbol(symbol : Symbol2) : Symbol2
public GetOrDefineSymbol(symbol : Symbol) : Symbol
{
mutable old;
when (NameTable.TryGetValue(symbol.Name.Id, out old))
Expand All @@ -50,7 +53,7 @@ namespace Nitra.Runtime.Binding
symbol
}

public TryGetSymbol(name : IReference) : Symbol2
public TryGetSymbol(name : IReference) : Symbol
{
mutable old;
when (NameTable.TryGetValue(name.Id, out old))
Expand All @@ -62,59 +65,63 @@ namespace Nitra.Runtime.Binding

| Union { public Scopes : list[Scope] { get; } }
| Hide { public Scope : Scope { get; } public Hidden : Scope { get; } }
| Filter { public Scope : Scope { get; } public Predicate : Symbol2 -> bool { get; } }
| Filter { public Scope : Scope { get; } public Predicate : Symbol -> bool { get; } }
| Nil

public Bind(reference : IReference) : Symbol
{
def sym = TryBind(reference);
Symbol.ReportError(reference, sym);
sym
}

/// Если не может связать возвращает AmbiguousSymbol или UnresolvedSymbol.
public TryBind(reference : IReference) : Symbol2
public TryBind(reference : IReference) : Symbol
{
def result = Bind(reference);
def result = BindMany(reference);
match (result.Count)
{
| 0 =>
AstContext.CompilerMessages.Error(reference, $<#Cannot resolv symbol '$reference'#>);
UnresolvedSymbol(reference)

| 0 => UnresolvedSymbol(reference)
| 1 => result[0]
| _ => AmbiguousSymbol(reference, result.NToList())
}
}

public Bind(reference : IReference) : List[Symbol2]
public BindMany(reference : IReference) : List[Symbol]
{
def results = List(2);
Bind(reference, results);
results
}

private Bind(reference : IReference, results : List[Symbol2]) : void
{
match (this)
def bindManyImpl(thisScope : Scope, reference : IReference, results : List[Symbol]) : void
{
| Table as s =>
mutable result;
when (s.NameTable.TryGetValue(reference.Id, out result))
results.Add(result);
match (thisScope)
{
| Table as s =>
mutable result;
when (s.NameTable.TryGetValue(reference.Id, out result))
results.Add(result);

| Union as s =>
foreach (scope in s.Scopes)
scope.Bind(reference, results);
| Union as s =>
foreach (scope in s.Scopes)
bindManyImpl(scope, reference, results);

| Hide as s =>
def binded = results.Count;
s.Scope.Bind(reference, results);
when (binded == results.Count)
s.Hidden.Bind(reference, results);
| Hide as s =>
def binded = results.Count;
bindManyImpl(s.Scope, reference, results);
when (binded == results.Count)
bindManyImpl(s.Hidden, reference, results);

| Filter as s =>
def notFilteredResults = List();
s.Scope.Bind(reference, notFilteredResults);
foreach (sym in notFilteredResults)
when (s.Predicate(sym))
results.Add(sym);
| Filter as s =>
def notFilteredResults = List();
bindManyImpl(s.Scope, reference, notFilteredResults);
foreach (sym in notFilteredResults)
when (s.Predicate(sym))
results.Add(sym);

| Nil => ()
| Nil => ()
}
}

bindManyImpl(this, reference, results);
results
}

public override ToString() : string
Expand Down
29 changes: 28 additions & 1 deletion Nitra/Nitra.Runtime/Typing2/Symbols/Symbol.n
Expand Up @@ -33,9 +33,36 @@ namespace Nitra.Runtime.Binding

public virtual TryBind(reference : IReference) : Symbol
{
AstContext.CompilerMessages.Error(reference, $<#Cannot resolv symbol '$reference'#>);
UnresolvedSymbol(reference)
}

public static ReportError(reference : IReference, symbol : Symbol) : void
{
match (symbol)
{
| UnresolvedSymbol => AstContext.CompilerMessages.Error(reference, $<#Cannot resolv symbol '$reference'.#>);
| AmbiguousSymbol as sym =>
using (err = AstContext.CompilerMessages.RootError(reference, $<#The symbol '$reference' is ambiguous.#>))
foreach (a in sym.Ambiguous with i)
when (a.Declarations is head :: _)
err.Hint(head, $<#Declaration $i#>);

| AmbiguousHierarchicalSymbol as sym =>
using (err = AstContext.CompilerMessages.RootError(reference, $<#The symbol '$reference' is ambiguous.#>))
foreach (a in sym.Ambiguous with i)
when (a.Declarations is head :: _)
err.Hint(head, $<#Declaration $i#>);

| _ => ()
}
}

public Bind(reference : IReference) : Symbol
{
def sym = TryBind(reference);
ReportError(reference, sym);
sym
}

public override ToString() : string { Name?.Text + " (" + Kind + ")" }

Expand Down

0 comments on commit 406f20f

Please sign in to comment.