コード例 #1
0
ファイル: Applier.cs プロジェクト: TommyPersson/Yalla
        public object ApplySet(IList<object> arguments, Environment environment)
        {
            /*if (arguments.Count != 2)
            {
                throw new ArgumentException("Wrong number of arguments given to set!! Expected: " + 2);
            }*/

            var symbol = arguments.ElementAt(0) as SymbolNode;
            if (symbol == null)
            {
                throw new ArgumentException("First argument to set! not a symbol!");
            }

            // (set! .Name cs-obj "Hello")
            if (symbol.Name.First() == '.')
            {
                var propName = symbol.Name.Substring(1);

                var propValue = evaluator.Evaluate(arguments.ElementAt(2), environment);
                var target = evaluator.Evaluate(arguments.ElementAt(1), environment);

                var otype = target.GetType();

                var oproperty = otype.GetProperty(propName);
                if (oproperty != null)
                {
                    oproperty.SetValue(target, propValue, null);
                    return AstNode.MakeNode(propValue);
                }

                var ofield = otype.GetField(propName);
                if (ofield != null)
                {
                    ofield.SetValue(target, propValue);
                    return AstNode.MakeNode(propValue);
                }

                throw new ArgumentException(propName + " is not a valid property or field of " + otype);
            }

            var value = evaluator.Evaluate(arguments.ElementAt(1), environment);

            if (!environment.SetSymbolValue(symbol, value))
            {
                throw new ArgumentException("Symbol '" + symbol.Name + "' not defined!");
            }

            return value;
        }