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

Commit

Permalink
Play with C# typing. Work on usings.
Browse files Browse the repository at this point in the history
  • Loading branch information
VladD2 committed May 22, 2015
1 parent 0b34e39 commit 6c1e177
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 116 deletions.
3 changes: 3 additions & 0 deletions Grammars/CSharp/CSharp.Grammar/CSharp.Grammar.nproj
Expand Up @@ -72,6 +72,9 @@
<Compile Include="CSharp\IntrinsicUtils.n">
<SubType>Code</SubType>
</Compile>
<Compile Include="CSharp\Symbols\AliasSymbol.n">
<SubType>Code</SubType>
</Compile>
<Compile Include="CSharp\Symbols\ClassSymbol.n">
<SubType>Code</SubType>
</Compile>
Expand Down
Expand Up @@ -13,7 +13,7 @@ namespace CSharp
Members.Parent = RootNamespace;
ExternAlias.ScopeIn = RootScope;
UsingDirectives.ScopeBuilderIn = UsingsScopeBuilder(ExternAlias.ScopeOut);
Members.Scope = UsingDirectives.ScopeBuilderOut.MakeResultScop();
Members.Scope = UsingDirectives.ScopeBuilderOut.ResultScop;

ExternAlias : ExternAliasDirective*;
UsingDirectives : UsingDirective*;
Expand Down
Expand Up @@ -3,36 +3,36 @@ using Nitra.Runtime.Binding;

namespace CSharp
{

asts UsingDirective
abstract ast UsingDirective
{
inout ScopeBuilder : UsingsScopeBuilder;
}

| Alias
{
NamespaceOrTypeName.Scope = ScopeBuilderIn.Current;
ScopeBuilderOut = ScopeBuilderIn.Alias(Name, NamespaceOrTypeName.Symbol, NamespaceOrTypeName);
declaration UsingAliasDirective : UsingDirective
{
NamespaceOrTypeName.Scope = ScopeBuilderIn.Current;
ScopeBuilderOut = ScopeBuilderIn.Alias(NamespaceOrTypeName.Symbol, this);

Name : Reference;
NamespaceOrTypeName : QualifiedReference;
}
| Open
{
NamespaceOrTypeName.Scope = ScopeBuilderIn.Current;
ScopeBuilderOut = ScopeBuilderIn.Open(NamespaceOrTypeName.Symbol, NamespaceOrTypeName);
NamespaceOrTypeName : QualifiedReference;
}

NamespaceOrTypeName : QualifiedReference;
}
ast UsingOpenDirective : UsingDirective
{
NamespaceOrTypeName.Scope = ScopeBuilderIn.Current;
ScopeBuilderOut = ScopeBuilderIn.Open(NamespaceOrTypeName.Symbol, NamespaceOrTypeName);

NamespaceOrTypeName : QualifiedReference;
}

map syntax TopDeclarations.UsingDirective -> UsingDirective
{
| Alias
| Alias -> UsingAliasDirective
{
Name -> Name;
QualifiedName -> NamespaceOrTypeName;
}
| Open

| Open -> UsingOpenDirective
{
QualifiedName -> NamespaceOrTypeName;
}
Expand Down
Expand Up @@ -16,43 +16,50 @@ namespace CSharp
{
public class UsingsScopeBuilder
{
public Current : Scope;
private mutable _opens : list[Scope] = [];
public Current : Scope { get; }
private mutable _opens : list[Scope] = [];
private mutable _aliases : Scope.Table;

public this(current : Scope) { Current = current; }

public Open(symbol : Symbol2, ast : IAst) : UsingsScopeBuilder
public Open(symbol : Symbol2, namespaceOrType : QualifiedReference) : UsingsScopeBuilder
{
if (symbol is NamespaceSymbol as ns)
_opens ::= ns.Scope;
else when (symbol.IsResolved)
AstContext.CompilerMessages.Error(ast, <#Using directive can open only namespace.#>);
AstContext.CompilerMessages.Error(namespaceOrType, <#Using directive can open only namespace.#>);

this
}

public Alias(alias : IReference, symbol : Symbol2, ast : IAst) : UsingsScopeBuilder
public Alias(_symbol : Symbol2, usingAliasDirective : UsingAliasDirective) : UsingsScopeBuilder
{
//AliasSymbol(alias)
def alias = usingAliasDirective.Name;
def sym = AliasSymbol(alias, usingAliasDirective.NamespaceOrTypeName);
sym.Declarations ::= usingAliasDirective;
alias.Symbol = sym;
Aliases.DefineSymbol(sym);
this
}

public MakeResultScop() : Scope
public ResultScop : Scope
{
def main =
if (_aliases == null)
Current
else
Scope.Union([Current, _aliases]);
get
{
def main =
if (_aliases == null)
Current
else
Scope.Union([Current, _aliases]);

def result =
if (_opens.IsEmpty)
main
else
Scope.Hide(main, Scope.Union(_opens));
def result =
if (_opens.IsEmpty)
main
else
Scope.Hide(main, Scope.Union(_opens));

result
result
}
}

private Aliases : Scope.Table { get { when (_aliases == null) _aliases = Scope.Table(); _aliases } }
Expand Down
Expand Up @@ -8,12 +8,14 @@ namespace CSharp
{
out Symbol : NamespaceSymbol;

Name.Symbol = Symbol;
Path.Symbol = Symbol;
Symbol = EnterNamespace(Parent :> NamespaceSymbol, Path, this);
Members.Parent = Symbol;

ExternAlias.ScopeIn = Scope;
UsingDirectives.ScopeBuilderIn = UsingsScopeBuilder(ExternAlias.ScopeOut);
Members.Scope = UsingDirectives.ScopeBuilderOut.MakeResultScop();
Members.Scope = UsingDirectives.ScopeBuilderOut.ResultScop;

Path : Reference*;
ExternAlias : ExternAliasDirective*;
Expand Down
23 changes: 23 additions & 0 deletions Grammars/CSharp/CSharp.Grammar/CSharp/Symbols/AliasSymbol.n
@@ -0,0 +1,23 @@
using Nitra;
using Nitra.Runtime.Binding;
using Nitra.Declarations;

using Nemerle;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharp.Symbols
{
[Record]
public class AliasSymbol : Symbol2
{
public override FullName : string { get { Name.Text } }
public override Kind : string { get { "alias" } }
public NamespaceOrType : QualifiedReference { get; }
}
}
6 changes: 0 additions & 6 deletions Grammars/CSharp/CSharp.Grammar/Utils.n
Expand Up @@ -37,11 +37,5 @@ namespace CSharp

ns
}

public ExitNamespace(nsIn : NamespaceSymbol, nsOut : NamespaceSymbol) : NamespaceSymbol
{
_ = nsOut;
nsIn
}
}
}
11 changes: 4 additions & 7 deletions Nitra.Visualizer/MainWindow.xaml.Declarations.cs
Expand Up @@ -23,9 +23,6 @@ public partial class MainWindow
{
public TreeViewItem ObjectToItem(PropertyInfo prop, object obj)
{
if (obj is IReference)
{
}
string name = prop == null ? "" : prop.Name;
var tvi = new TreeViewItem { Tag = obj, FontWeight = FontWeights.Normal };
tvi.MouseDoubleClick += TviOnMouseDoubleClick;
Expand Down Expand Up @@ -56,7 +53,7 @@ public TreeViewItem ObjectToItem(PropertyInfo prop, object obj)
}

var declaration = obj as IAst;
if (declaration != null && !(obj is IReference))
if (declaration != null /*&& !(obj is IReference)*/)
{
var xaml = RenderXamlForDeclaration(name, declaration);
tvi.Header = XamlReader.Parse(xaml);
Expand Down Expand Up @@ -85,7 +82,7 @@ public TreeViewItem ObjectToItem(PropertyInfo prop, object obj)
var xaml = RenderXamlForValue(prop, obj);
tvi.Header = XamlReader.Parse(xaml);

if (obj == null || obj is IReference)
if (obj == null /*|| obj is IReference*/)
return tvi;

var t = obj.GetType();
Expand Down Expand Up @@ -117,7 +114,7 @@ private void TviOnExpanded(object sender, RoutedEventArgs routedEventArgs)
return;

var declaration = obj as IAst;
if (declaration != null && !(obj is IReference))
if (declaration != null /*&& !(obj is IReference)*/)
{
var t = obj.GetType();
var props = t.GetProperties();
Expand Down Expand Up @@ -150,7 +147,7 @@ private void TviOnExpanded(object sender, RoutedEventArgs routedEventArgs)
{
var t = obj.GetType();

if (obj is string || t.IsPrimitive || obj is IReference)
if (obj is string || t.IsPrimitive /*|| obj is IReference*/)
return;

var props = t.GetProperties();
Expand Down
Expand Up @@ -69,7 +69,7 @@ namespace Nitra.Compiler
def cycledNodes = Node.FindCycling(graphNodes);
foreach (cycledNode in cycledNodes)
Message.Error(cycledNode.Location, "Is cycled.");
otherwise assert3(false, "Cycled nodes not found");
otherwise assert(false, "Cycled nodes not found");
}
else
{
Expand All @@ -79,7 +79,7 @@ namespace Nitra.Compiler
if (tb.LookupMember(s.Name).Find(m => m is IProperty) is Some(m))
props.Add(m :> IProperty);
else
assert3(false, $"IProperty for symbol $s not found");
assert(false, $"IProperty for symbol $s not found");
}
def graphNodes2 = AddDependencyOnAstProperties(graphNodes, props);
//CheckOutputPropertiesAssignment(symbol, newGraphNodes);
Expand Down Expand Up @@ -207,7 +207,7 @@ namespace Nitra.Compiler

| TExpr.Delayed =>
// мы не можем производить преобразование, если у нас остались объкты отложенной типизации
assert3(false);
assert(false);

| _ => ()
}
Expand Down Expand Up @@ -238,7 +238,7 @@ namespace Nitra.Compiler
else
Message.FatalError(e.Location, "You can assign only a dependent property.");

| TExpr.Delayed => assert3(false);
| TExpr.Delayed => assert(false);
| _ => Message.FatalError(assign.target.Location, "You can assign only a dependent property.");
}
}
Expand All @@ -263,15 +263,15 @@ namespace Nitra.Compiler
else
Message.FatalError(e.Location, "You can assign only a dependent property.");

| TExpr.Delayed => assert3(false);
| TExpr.Delayed => assert(false);
| _ => Message.FatalError(assign.target.Location, "You can assign only a dependent property.");
}
}
def addAssigns(expr : TExpr, debug : TExpr = null) : void
{
match (expr)
{
| TExpr.Delayed => assert3(false);
| TExpr.Delayed => assert(false);
| TExpr.DebugInfo as e => addAssigns(e.expr, e)
| TExpr.Sequence as e => addAssigns(e.e1); addAssigns(e.e2);
| TExpr.Assign as e => addKey(e, debug);
Expand All @@ -280,7 +280,7 @@ namespace Nitra.Compiler
}
def calcDependents(expr : TExpr) : void
{
| TExpr.Delayed => assert3(false);
| TExpr.Delayed => assert(false);
| TExpr.DebugInfo as e => calcDependents(e.expr)
| TExpr.Sequence as e => calcDependents(e.e1); calcDependents(e.e2);
| TExpr.Assign as e => processAssignProp(e);
Expand Down
2 changes: 1 addition & 1 deletion Nitra/Nitra.Compiler/Generation/Ast/AstEmitter.n
Expand Up @@ -153,7 +153,7 @@ namespace Nitra.Compiler
protected static MakeAccessAssertion(propertyRef : DependentAstPropertyRef) : PExpr
{
def assertMessage = $"Property '$(propertyRef.Symbol.Name)' is not set";
<[ assert3(false, $(assertMessage : string)); ]>
<[ assert(false, $(assertMessage : string)); ]>
}

protected static MakeDependentPropertyAttribute(propertySymbol : DependentAstPropertySymbol, index : int) : PExpr
Expand Down

0 comments on commit 6c1e177

Please sign in to comment.