示例#1
0
        public static Symbol GetSymbol(this Cobject This)
        {
            if (!TryGetSymbol(This, out var symbol))
            {
                throw new BinderException($"`{This}´ can't be a symbol");
            }

            return(symbol);
        }
示例#2
0
 public static Special Define(Symbol symbol, Cobject value)
 {
     return(new Special(
                (scope, stack) =>
     {
         value.Eval(scope, stack);
         value = stack.Pop();
         scope.Define(symbol, value);
     }));
 }
示例#3
0
        public static bool TryGetSymbol(this Cobject This, out Symbol symbol)
        {
            if (!(This is Block block) || !block.TryGetQuotedSymbol(out symbol))
            {
                symbol = null;
                return(false);
            }

            return(true);
        }
示例#4
0
        public void Update(Symbol symbol, Cobject value)
        {
            var binding = Find(symbol);

            if (binding.IsSealed.Value)
            {
                throw new BinderException($"`{symbol}´ is marked as sealed and can't be updated");
            }

            binding.Value = value;
        }
示例#5
0
        public void Define(Symbol symbol, Cobject value, bool isSealed = false, bool isOpaque = false)
        {
            if (TryFind(symbol, out var binding))
            {
                throw new BinderException($"`{symbol}´ already defined in current scope");
            }

            binding = new Binding(this, value, Bool.From(isSealed), Bool.From(isOpaque));
            this.bindings.Add(symbol, binding);
            this.symbols.Add(symbol);
        }
示例#6
0
 public static void Eval(this Cobject This, IScope scope, IStack stack)
 {
     if (This is Cobject cvalue)
     {
         cvalue.Eval(scope, stack);
     }
     else
     {
         stack.Push(This);
     }
 }
示例#7
0
        public static IEnumerable <Cobject> Enumerate(this Cobject This)
        {
            /*// ReSharper disable once UsePatternMatching*/
            var enumerable = This as IEnumerable <Cobject>;

            // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
            if (enumerable == null)
            {
                enumerable = Enumerable.Repeat(This, 1);
            }

            return(enumerable);
        }
示例#8
0
        public void Define(Symbol symbol, Cobject value, bool isSealed = false, bool isOpaque = false)
        {
            if (TryFind(symbol, out var binding))
            {
                if (Equals(binding.Binder, this.Binder))
                {
                    throw new BinderException($"`{symbol}´ already defined in current scope");
                }

                if (binding.IsOpaque.Value)
                {
                    throw new BinderException($"`{symbol}´ is marked as opaque and can't be redefined");
                }
            }

            this.Binder.Define(symbol, value, isSealed, isOpaque);
        }
示例#9
0
 public static Special SingleDefine(Cobject symbol)
 {
     return(MultiDefine(Enumerable.Repeat(symbol, 1)));
 }
示例#10
0
 public void GetValue(Symbol symbol, out Cobject value)
 {
     value = Find(symbol).Value;
 }