Inheritance: IScheminType
Example #1
0
 public void RemoveBinding(ScheminAtom symbol)
 {
     if (bindings.ContainsKey(symbol.Name))
     {
         bindings.Remove(symbol.Name);
     }
 }
Example #2
0
 public void AddBinding(ScheminAtom symbol, IScheminType type)
 {
     if (bindings.ContainsKey(symbol.Name))
     {
         bindings[symbol.Name] = type;
     }
     else
     {
         bindings.Add(symbol.Name, type);
     }
 }
Example #3
0
        public IScheminType GetValue(ScheminAtom symbol)
        {
            Environment parent = this;
            while (parent != null)
            {
                IScheminType value;
                parent.bindings.TryGetValue(symbol.Name, out value);

                if (value != null)
                {
                    return parent.bindings[symbol.Name];
                }

                parent = parent.parent;
            }

            return null;
        }
Example #4
0
 private IScheminType ConvertAtom(ScheminAtom atom, Token token)
 {
     switch (atom.Name)
     {
         // these are special forms that can't be bound to symbols, they're always primitives
         case "lambda":
             return new ScheminPrimitive("lambda", token);
         case "define":
             return new ScheminPrimitive("define", token);
         case "define-rewriter":
             return new ScheminPrimitive("define-rewriter", token);
         case "quote":
             return new ScheminPrimitive("quote", token);
         case "unquote":
             return new ScheminPrimitive("unquote", token);
         case "unquote-splicing":
             return new ScheminPrimitive("unquote-splicing", token);
         case "quasiquote":
             return new ScheminPrimitive("quasiquote", token);
         case "begin":
             return new ScheminPrimitive("begin", token);
         case "if":
             return new ScheminPrimitive("if", token);
         case "or":
             return new ScheminPrimitive("or", token);
         case "and":
             return new ScheminPrimitive("and", token);
         case "cond":
             return new ScheminPrimitive("cond", token);
         case "let":
             return new ScheminPrimitive("let", token);
         case "letrec":
             return new ScheminPrimitive("letrec", token);
         case "let*":
             return new ScheminPrimitive("let*", token);
         case "set!":
             return new ScheminPrimitive("set!", token);
         case "call/cc":
             return new ScheminPrimitive("call/cc", token);
         default:
             return atom;
     }
 }
Example #5
0
        private int AdjustLevelEnter(ScheminAtom first, int currentLevel)
        {
            if (first != null)
            {
                if (first.Name == "quasiquote")
                {
                    currentLevel++;
                }
                else if (first.Name == "unquote")
                {
                    currentLevel--;
                }
                else if (first.Name == "unquote-splicing")
                {
                    currentLevel--;
                }
            }

            return currentLevel;
        }
Example #6
0
        private void DefinePrimitives(Environment env)
        {
            foreach (KeyValuePair<string, Primitive> kvp in PrimitiveFactory.Primitives)
            {
                ScheminAtom symbol = new ScheminAtom(kvp.Key);
                ScheminPrimitive prim = new ScheminPrimitive(kvp.Key);

                env.AddBinding(symbol, prim);
            }

            var prebound_schemin = new List<string>();
            prebound_schemin.Add(ScheminPrimitives.Map);
            prebound_schemin.Add(ScheminPrimitives.Filter);
            prebound_schemin.Add(ScheminPrimitives.Foldl);
            prebound_schemin.Add(ScheminPrimitives.Foldr);
            prebound_schemin.Add(ScheminPrimitives.Not);
            prebound_schemin.Add(ScheminPrimitives.Id);
            prebound_schemin.Add(ScheminPrimitives.Flip);
            prebound_schemin.Add(ScheminPrimitives.Fold);
            prebound_schemin.Add(ScheminPrimitives.Unfold);
            prebound_schemin.Add(ScheminPrimitives.Reverse);
            prebound_schemin.Add(ScheminPrimitives.Curry);
            prebound_schemin.Add(ScheminPrimitives.Compose);
            prebound_schemin.Add(ScheminPrimitives.Zero);
            prebound_schemin.Add(ScheminPrimitives.Positive);
            prebound_schemin.Add(ScheminPrimitives.Negative);
            prebound_schemin.Add(ScheminPrimitives.Odd);
            prebound_schemin.Add(ScheminPrimitives.Even);
            prebound_schemin.Add(ScheminPrimitives.CallWithCC);
            prebound_schemin.Add(ScheminPrimitives.Error);
            prebound_schemin.Add(ScheminPrimitives.Sum);
            prebound_schemin.Add(ScheminPrimitives.Product);
            prebound_schemin.Add(ScheminPrimitives.Max);
            prebound_schemin.Add(ScheminPrimitives.Min);
            prebound_schemin.Add(ScheminPrimitives.Caddr);
            prebound_schemin.Add(ScheminPrimitives.DefineMacro);

            Tokenize.Tokenizer t = new Tokenize.Tokenizer();
            Schemin.Parse.Parser p = new Parse.Parser();

            foreach (string primitive in prebound_schemin)
            {
                var tokens = t.Tokenize(primitive);
                var ast = p.Parse(tokens, false);
                Evaluate(ast);
            }
        }