Exemplo n.º 1
0
        void when_opening_and_closing_actions_are_supplied()
        {
            it["should trigger both"] = () => {
                int i = 0;
                using (
                    Enclosing.GetEnclosure(
                        onOpen: (context) => { i++; },
                        onClose: (context) => { i++; }))
                {
                    i.should_be(1);
                }
                i.should_be(2);
            };

            it["should trigger both within custom subclass"] = () => {
                int myNumberCopied = 0;
                using (
                    var enclosing = Enclosing.GetEnclosure <CustomEnclosing>(
                        onOpen: (context) => { context.MyNumber++; },
                        onClose: (context) => { context.MyNumber++; myNumberCopied = context.MyNumber; }))
                {
                    enclosing.MyNumber.should_be(1);
                }
                myNumberCopied.should_be(2);
            };
        }
Exemplo n.º 2
0
 public void Assign(Token name, object value)
 {
     if (!Values.ContainsKey(name.Lexeme))
     {
         if (Enclosing != null)
         {
             Enclosing.Assign(name, value);
             return;
         }
         throw new RuntimeException(name, $"Undefined variable '{name.Lexeme}'.");
     }
     Values[name.Lexeme] = value;
 }
Exemplo n.º 3
0
 void with_actions_taking_no_parameters_provided()
 {
     it["should accept and trigger"] = () => {
         int i = 0;
         using (
             Enclosing.GetEnclosure(
                 onOpen: () => { i++; },
                 onClose: () => { i++; }))
         {
             i.should_be(1);
         }
         i.should_be(2);
     };
 }
Exemplo n.º 4
0
        public object Get(Token name)
        {
            if (Values.ContainsKey(name.Lexeme))
            {
                return(Values[name.Lexeme]);
            }

            if (Enclosing != null)
            {
                return(Enclosing.Get(name));
            }

            throw new RuntimeError(name, $"Undefined variable '{name.Lexeme}'.");
        }
Exemplo n.º 5
0
        private void WriteTo(StringBuilder builder)
        {
            Enclosing?.WriteTo(builder);

            foreach (var field in Values)
            {
                if (char.IsDigit(field.Key[0]))
                {
                    builder.Append("$");
                }

                builder.Append(field.Key).Append(" = ").AppendLine(Interpreter.Stringify(field.Value));
            }
        }
Exemplo n.º 6
0
        public object Get(Token name)
        {
            if (Values.TryGetValue(name.Lexeme, out var value))
            {
                return(value);
            }

            if (Enclosing != null)
            {
                return(Enclosing.Get(name));
            }

            throw new RuntimeError(name, $"Undefined variable '{name.Lexeme}'.");
        }
Exemplo n.º 7
0
        public void Assign(string name, object value)
        {
            if (Values.ContainsKey(name))
            {
                Values[name] = value;
            }

            else if (Enclosing != null)
            {
                Enclosing.Assign(name, value);
            }
            else
            {
                throw new RuntimeException($"Variable '{name}' is not defined!");
            }
        }
Exemplo n.º 8
0
        void when_opening_action_is_supplied()
        {
            it["should trigger onOpen action when opening"] = () => {
                int i = 0;
                using (
                    Enclosing.GetEnclosure(
                        onOpen: (context) => { i++; },
                        onClose: (context) => {}))
                {
                    i.should_be(1);
                }
            };

            it["should trigger onOpen action when opening within custom subclass"] = () => {
                using (
                    var enclosing = Enclosing.GetEnclosure <CustomEnclosing>(
                        onOpen: (context) => { context.MyNumber++; },
                        onClose: (context) => {}))
                {
                    enclosing.MyNumber.should_be(1);
                }
            };
        }
Exemplo n.º 9
0
 public object Get(Token name)
 => Values.TryGetValue(name.Lexeme, out var v)
         ? v
         : Enclosing != null
             ? Enclosing.Get(name)
             : throw new RuntimeException(name, $"Undefined variable {name.Lexeme}.");
Exemplo n.º 10
0
        private MethodMemberBuilder(NamedTypeSymbol container, Binder enclosing, MemberDeclarationSyntax syntax, DiagnosticBag diagnostics)
            : base(enclosing.Location(syntax) as SourceLocation, container, enclosing)
        {
            Debug.Assert(syntax != null);
            this.syntax = syntax;

            // Make a binder context in which each type parameter binds to a corresponding numbered type parameter
            Binder parametersContext = Enclosing;

            if (syntax.Kind == SyntaxKind.MethodDeclaration)
            {
                var methodSyntax = syntax as MethodDeclarationSyntax;
                int arity        = methodSyntax.Arity;
                if (arity != 0)
                {
                    var typeParamMap = new MultiDictionary <string, TypeParameterSymbol>();
                    var typeParams   = methodSyntax.TypeParameterListOpt.Parameters;

                    for (int iParam = 0; iParam < typeParams.Count; iParam++)
                    {
                        var arg    = typeParams[iParam];
                        var symbol = IndexedTypeParameterSymbol.GetTypeParameter(iParam);
                        typeParamMap.Add(arg.Identifier.ValueText, symbol);
                    }

                    parametersContext = new WithDummyTypeParametersBinder(typeParamMap, Enclosing);
                }

                if (methodSyntax.ExplicitInterfaceSpecifierOpt != null)
                {
                    this.explicitInterfaceType = enclosing.BindType(methodSyntax.ExplicitInterfaceSpecifierOpt.Name, diagnostics);
                }
            }

            // TODOngafter 1: recast this code using ReadOnlyArray.
            IEnumerable <ParameterSyntax> parameters = SyntaxParameters.HasValue ? SyntaxParameters.Value : SpecializedCollections.EmptyEnumerable <ParameterSyntax>();

            declaredParameterTypes = parameters.Select(p =>
            {
                if (p.TypeOpt == null)
                {
                    return(new CSErrorTypeSymbol(enclosing.Compilation.GlobalNamespace, "ErrorType", 0, diagnostics.Add(ErrorCode.ERR_NotYetImplementedInRoslyn, new SourceLocation(Tree, p))));
                }
                return(parametersContext.BindType(p.TypeOpt, diagnostics));
            }).ToList();

            var parameterRefs = parameters.Select(p => p.Modifiers.GetRefKind()).ToList();

            switch (syntax.Kind)
            {
            case SyntaxKind.ConstructorDeclaration:
                Binder original = parametersContext;     // TODOngafter 1: worry about diagnostic reporting and suppression here.
                declaredReturnType = Enclosing.GetSpecialType(SpecialType.System_Void, diagnostics, syntax);
                break;

            default:
                declaredReturnType = parametersContext.BindType(SyntaxReturnType, diagnostics);
                break;
            }

            TypeSymbol explType   = null;
            var        explSyntax = ExplicitInterface;

            if (explSyntax != null)
            {
                explType = parametersContext.BindType(explSyntax, diagnostics);
            }

            // TODOngafter 3: map dynamic->object for the signature
            this.signature = new MethodSignature(Name, SyntaxArity, declaredParameterTypes, parameterRefs, explType);
        }