void NewScopeFrame() { var scope = new StaticScope { Parent = ScopeStack.Peek(), }; ScopeStack.Push(scope); }
public void closing_a_non_opened_scope_is_a_programming_error() { StaticScope s = new StaticScope(); s.OpenScope(); s.CloseScope(); Assert.Throws<InvalidOperationException>( () => s.CloseScope() ); StaticScope sWithGlobal = new StaticScope( true ); sWithGlobal.OpenScope(); sWithGlobal.CloseScope(); Assert.Throws<InvalidOperationException>( () => s.CloseScope() ); }
public void declaring_in_subordinated_scopes_masks_declarations_from_upper_scope() { StaticScope s = new StaticScope(); s.OpenScope(); s.Declare("V1", new AccessorLetExpr(SourceLocation.Empty, "V1 from scope n°1")); s.Declare("V2", new AccessorLetExpr(SourceLocation.Empty, "V2 from scope n°1")); s.Declare("V3", new AccessorLetExpr(SourceLocation.Empty, "V3 from scope n°1")); CheckDeclVarName(s, "V1", "V1 from scope n°1"); CheckDeclVarName(s, "V2", "V2 from scope n°1"); CheckDeclVarName(s, "V3", "V3 from scope n°1"); s.OpenScope(); { s.Declare("V1", new AccessorLetExpr(SourceLocation.Empty, "V1 from scope n°2")); CheckDeclVarName(s, "V1", "V1 from scope n°2"); CheckDeclVarName(s, "V2", "V2 from scope n°1"); CheckDeclVarName(s, "V3", "V3 from scope n°1"); s.OpenScope(); { s.Declare("V1", new AccessorLetExpr(SourceLocation.Empty, "V1 from scope n°3")); s.Declare("V2", new AccessorLetExpr(SourceLocation.Empty, "V2 from scope n°3")); CheckDeclVarName(s, "V1", "V1 from scope n°3"); CheckDeclVarName(s, "V2", "V2 from scope n°3"); CheckDeclVarName(s, "V3", "V3 from scope n°1"); s.OpenScope(); { s.Declare("V3", new AccessorLetExpr(SourceLocation.Empty, "V3 from scope n°4")); CheckDeclVarName(s, "V1", "V1 from scope n°3"); CheckDeclVarName(s, "V2", "V2 from scope n°3"); CheckDeclVarName(s, "V3", "V3 from scope n°4"); CheckClose(s.CloseScope(), "V3 from scope n°4"); } CheckDeclVarName(s, "V1", "V1 from scope n°3"); CheckDeclVarName(s, "V2", "V2 from scope n°3"); CheckDeclVarName(s, "V3", "V3 from scope n°1"); s.Declare("V4", new AccessorLetExpr(SourceLocation.Empty, "V4")); s.Declare("V5", new AccessorLetExpr(SourceLocation.Empty, "V5")); CheckClose(s.CloseScope(), "V1 from scope n°3", "V2 from scope n°3", "V4", "V5"); } Assert.IsNull(s.Find("V4")); Assert.IsNull(s.Find("V5")); CheckClose(s.CloseScope(), "V1 from scope n°2"); } CheckClose(s.CloseScope(), "V1 from scope n°1", "V2 from scope n°1", "V3 from scope n°1"); }
public void closing_a_non_opened_scope_is_a_programming_error() { StaticScope s = new StaticScope(); s.OpenScope(); s.CloseScope(); Assert.Throws <InvalidOperationException>(() => s.CloseScope()); StaticScope sWithGlobal = new StaticScope(true); sWithGlobal.OpenScope(); sWithGlobal.CloseScope(); Assert.Throws <InvalidOperationException>(() => s.CloseScope()); }
public void declaring_in_subordinated_scopes_masks_declarations_from_upper_scope() { StaticScope s = new StaticScope(); s.OpenScope(); var v1From1 = (AccessorLetExpr)s.Declare(new AccessorLetExpr(SourceLocation.Empty, "V1")); var v2From1 = (AccessorLetExpr)s.Declare(new AccessorLetExpr(SourceLocation.Empty, "V2")); var v3From1 = (AccessorLetExpr)s.Declare(new AccessorLetExpr(SourceLocation.Empty, "V3")); s.Find("V1").Should().BeSameAs(v1From1); s.Find("V2").Should().BeSameAs(v2From1); s.Find("V3").Should().BeSameAs(v3From1); s.OpenScope(); { var v1From2 = (AccessorLetExpr)s.Declare(new AccessorLetExpr(SourceLocation.Empty, "V1")); s.Find("V1").Should().BeSameAs(v1From2); s.Find("V2").Should().BeSameAs(v2From1); s.Find("V3").Should().BeSameAs(v3From1); s.OpenScope(); { var v1From3 = (AccessorLetExpr)s.Declare(new AccessorLetExpr(SourceLocation.Empty, "V1")); var v2From3 = (AccessorLetExpr)s.Declare(new AccessorLetExpr(SourceLocation.Empty, "V2")); s.Find("V1").Should().BeSameAs(v1From3); s.Find("V2").Should().BeSameAs(v2From3); s.Find("V3").Should().BeSameAs(v3From1); s.OpenScope(); { var v3From4 = (AccessorLetExpr)s.Declare(new AccessorLetExpr(SourceLocation.Empty, "V3")); s.Find("V1").Should().BeSameAs(v1From3); s.Find("V2").Should().BeSameAs(v2From3); s.Find("V3").Should().BeSameAs(v3From4); CheckClose(s.CloseScope(), v3From4); } s.Find("V1").Should().BeSameAs(v1From3); s.Find("V2").Should().BeSameAs(v2From3); s.Find("V3").Should().BeSameAs(v3From1); var v4From3 = (AccessorLetExpr)s.Declare(new AccessorLetExpr(SourceLocation.Empty, "V4")); var v5From3 = (AccessorLetExpr)s.Declare(new AccessorLetExpr(SourceLocation.Empty, "V5")); CheckClose(s.CloseScope(), v1From3, v2From3, v4From3, v5From3); } s.Find("V4").Should().BeNull(); s.Find("V5").Should().BeNull(); CheckClose(s.CloseScope(), v1From2); } CheckClose(s.CloseScope(), v1From1, v2From1, v3From1); }
public void declaring_in_subordinated_scopes_masks_declarations_from_upper_scope() { StaticScope s = new StaticScope(); s.OpenScope(); s.Declare( "V1", new AccessorLetExpr( SourceLocation.Empty, "V1 from scope n°1" ) ); s.Declare( "V2", new AccessorLetExpr( SourceLocation.Empty, "V2 from scope n°1" ) ); s.Declare( "V3", new AccessorLetExpr( SourceLocation.Empty, "V3 from scope n°1" ) ); CheckDeclVarName( s, "V1", "V1 from scope n°1" ); CheckDeclVarName( s, "V2", "V2 from scope n°1" ); CheckDeclVarName( s, "V3", "V3 from scope n°1" ); s.OpenScope(); { s.Declare( "V1", new AccessorLetExpr( SourceLocation.Empty, "V1 from scope n°2" ) ); CheckDeclVarName( s, "V1", "V1 from scope n°2" ); CheckDeclVarName( s, "V2", "V2 from scope n°1" ); CheckDeclVarName( s, "V3", "V3 from scope n°1" ); s.OpenScope(); { s.Declare( "V1", new AccessorLetExpr( SourceLocation.Empty, "V1 from scope n°3" ) ); s.Declare( "V2", new AccessorLetExpr( SourceLocation.Empty, "V2 from scope n°3" ) ); CheckDeclVarName( s, "V1", "V1 from scope n°3" ); CheckDeclVarName( s, "V2", "V2 from scope n°3" ); CheckDeclVarName( s, "V3", "V3 from scope n°1" ); s.OpenScope(); { s.Declare( "V3", new AccessorLetExpr( SourceLocation.Empty, "V3 from scope n°4" ) ); CheckDeclVarName( s, "V1", "V1 from scope n°3" ); CheckDeclVarName( s, "V2", "V2 from scope n°3" ); CheckDeclVarName( s, "V3", "V3 from scope n°4" ); CheckClose( s.CloseScope(), "V3 from scope n°4" ); } CheckDeclVarName( s, "V1", "V1 from scope n°3" ); CheckDeclVarName( s, "V2", "V2 from scope n°3" ); CheckDeclVarName( s, "V3", "V3 from scope n°1" ); s.Declare( "V4", new AccessorLetExpr( SourceLocation.Empty, "V4" ) ); s.Declare( "V5", new AccessorLetExpr( SourceLocation.Empty, "V5" ) ); CheckClose( s.CloseScope(), "V1 from scope n°3", "V2 from scope n°3", "V4", "V5" ); } Assert.IsNull( s.Find( "V4" ) ); Assert.IsNull( s.Find( "V5" ) ); CheckClose( s.CloseScope(), "V1 from scope n°2" ); } CheckClose( s.CloseScope(), "V1 from scope n°1", "V2 from scope n°1", "V3 from scope n°1" ); }
public void declaring_while_no_scope_is_opened_is_a_syntax_error() { StaticScope s = new StaticScope(); var v = new AccessorLetExpr(SourceLocation.Empty, "V"); Assert.IsInstanceOf <SyntaxErrorExpr>(s.Declare("toto", v)); s.OpenScope(); Assert.AreSame(s.Declare("toto", v), v); Assert.AreSame(s.Find("toto"), v); CheckClose(s.CloseScope(), "V"); Assert.IsInstanceOf <SyntaxErrorExpr>(s.Declare("toto", v)); }
public void declaring_while_no_scope_is_opened_is_a_syntax_error() { StaticScope s = new StaticScope(); var v = new AccessorLetExpr(SourceLocation.Empty, "V"); s.Declare(v).Should().BeOfType <SyntaxErrorExpr>(); s.OpenScope(); s.Declare(v).Should().BeSameAs(v); s.Find("V").Should().BeSameAs(v); CheckClose(s.CloseScope(), v); s.Declare(v).Should().BeOfType <SyntaxErrorExpr>(); }
public void closing_a_non_opened_scope_is_a_programming_error() { StaticScope s = new StaticScope(); s.OpenScope(); s.CloseScope(); Action act = () => s.CloseScope(); act.Should().Throw <InvalidOperationException>(); StaticScope sWithGlobal = new StaticScope(true); sWithGlobal.OpenScope(); sWithGlobal.CloseScope(); act.Should().Throw <InvalidOperationException>(); }
public void redefinition_in_the_same_scope_is_not_allowed_by_default() { StaticScope s = new StaticScope(); var v = new AccessorLetExpr(SourceLocation.Empty, "V"); var v1 = new AccessorLetExpr(SourceLocation.Empty, "V (redefined)"); var v2 = new AccessorLetExpr(SourceLocation.Empty, "V (redefined again)"); var vFailed = new AccessorLetExpr(SourceLocation.Empty, "V (failed)"); s.OpenScope(); Assert.AreSame(s.Declare("V", v), v); Assert.IsInstanceOf <SyntaxErrorExpr>(s.Declare("V", vFailed)); s.AllowLocalRedefinition = true; Assert.AreSame(s.Declare("V", v1), v1); Assert.AreSame(s.Declare("V", v2), v2); s.AllowLocalRedefinition = false; Assert.IsInstanceOf <SyntaxErrorExpr>(s.Declare("V", vFailed)); CheckClose(s.CloseScope(), "V", "V (redefined)", "V (redefined again)"); }
public void redefinition_in_the_same_scope_is_not_allowed_by_default() { StaticScope s = new StaticScope(); var v = new AccessorLetExpr(SourceLocation.Empty, "V"); var v1 = new AccessorLetExpr(SourceLocation.Empty, "V"); var v2 = new AccessorLetExpr(SourceLocation.Empty, "V"); var vFailed = new AccessorLetExpr(SourceLocation.Empty, "V"); s.OpenScope(); s.Declare(v).Should().BeSameAs(v); s.Declare(vFailed).Should().BeOfType <SyntaxErrorExpr>(); s.AllowLocalRedefinition = true; s.Declare(v1).Should().BeSameAs(v1); s.Declare(v2).Should().BeSameAs(v2); s.AllowLocalRedefinition = false; s.Declare(vFailed).Should().BeOfType <SyntaxErrorExpr>(); CheckClose(s.CloseScope(), v, v1, v2); }
static void CheckDeclVarName(StaticScope s, string varName, string name) { Assert.That(((AccessorLetExpr)s.Find(varName)).Name == name); }
public ExprAnalyser( Config configuration = null ) { if( configuration == null ) configuration = _emptyConfig; _scope = new StaticScope( configuration.GlobalScope, configuration.AllowMasking, configuration.AllowLocalRedefinition ); }
static void CheckDeclVarName( StaticScope s, string varName, string name ) { Assert.That( ((AccessorLetExpr)s.Find( varName )).Name == name ); }
public void redefinition_in_the_same_scope_is_not_allowed_by_default() { StaticScope s = new StaticScope(); var v = new AccessorLetExpr( SourceLocation.Empty, "V" ); var v1 = new AccessorLetExpr( SourceLocation.Empty, "V (redefined)" ); var v2 = new AccessorLetExpr( SourceLocation.Empty, "V (redefined again)" ); var vFailed = new AccessorLetExpr( SourceLocation.Empty, "V (failed)" ); s.OpenScope(); Assert.AreSame( s.Declare( "V", v ), v ); Assert.IsInstanceOf<SyntaxErrorExpr>( s.Declare( "V", vFailed ) ); s.AllowLocalRedefinition = true; Assert.AreSame( s.Declare( "V", v1 ), v1 ); Assert.AreSame( s.Declare( "V", v2 ), v2 ); s.AllowLocalRedefinition = false; Assert.IsInstanceOf<SyntaxErrorExpr>( s.Declare( "V", vFailed ) ); CheckClose( s.CloseScope(), "V", "V (redefined)", "V (redefined again)" ); }
public void declaring_while_no_scope_is_opened_is_a_syntax_error() { StaticScope s = new StaticScope(); var v = new AccessorLetExpr( SourceLocation.Empty, "V" ); Assert.IsInstanceOf<SyntaxErrorExpr>( s.Declare( "toto", v ) ); s.OpenScope(); Assert.AreSame( s.Declare( "toto", v ), v ); Assert.AreSame( s.Find( "toto" ), v ); CheckClose( s.CloseScope(), "V" ); Assert.IsInstanceOf<SyntaxErrorExpr>( s.Declare( "toto", v ) ); }