Inheritance: IScheminType
Beispiel #1
0
        private void DefinePrimitives()
        {
            foreach (KeyValuePair<string, Primitive> kvp in PrimitiveFactory.Primitives)
            {
                ScheminAtom symbol = AtomFactory.GetAtom(kvp.Key);
                ScheminPrimitive prim = new ScheminPrimitive(kvp.Key);

                this.GlobalEnv.AddBinding(symbol, prim);
            }

            string filename = "ScheminLib\\\\ScheminLib.ss";
            string baseDir = AppDomain.CurrentDomain.BaseDirectory;
            FileStream fs = File.OpenRead(baseDir + Path.DirectorySeparatorChar + filename);
            StreamReader sr = new StreamReader(fs);
            string file = sr.ReadToEnd();

            var tokens = this.tokenizer.Tokenize(file);
            ScheminPair ast = this.parser.Parse(tokens, true).Cons(new ScheminPrimitive("begin"));

            this.evaluator.EvaluateInternal(ast);
        }
Beispiel #2
0
        public bool EvaluateNextArg(ScheminPrimitive currentPrimitive, int currentArg, ScheminPair args)
        {
            if (currentPrimitive != null)
            {
                switch (currentPrimitive.Name)
                {
                    case "define":
                        if ((args.Car as ScheminPair) != null)
                        {
                            return false;
                        }
                        else
                        {
                            if (currentArg == 1)
                                return false;
                        }
                        break;
                    case "define-rewriter":
                        if (currentArg == 1)
                            return false;
                        break;
                    case "lambda":
                        return false;
                    case "quote":
                        if (currentArg == 1)
                            return false;
                        break;
                    case "quasiquote":
                        if (currentArg == 1)
                            return false;
                        break;
                    case "let":
                        return false;
                    case "letrec":
                        return false;
                    case "let*":
                        return false;
                    case "if":
                        if (currentArg == 2)
                            return false;
                        if (currentArg == 3)
                            return false;
                        break;
                    case "cond":
                        return false;
                    case "and":
                        if (currentArg != 1)
                            return false;
                        break;
                    case "or":
                        if (currentArg != 1)
                            return false;
                        break;
                    case "set!":
                        if (currentArg == 1)
                            return false;
                        break;
                    case "case":
                        if (currentArg != 1)
                            return false;
                        break;
                }
            }

            return true;
        }
Beispiel #3
0
 public IScheminType EvaluatePrimitive(ScheminPrimitive functionPosition, ScheminPair args, Environment env)
 {
     try
     {
         return functionPosition.Evaluate(args, env, this);
     }
     catch (BadArgumentsException ba)
     {
         Token sourceToken = functionPosition.SourceToken;
         string line = String.Empty;
         if (sourceToken != null)
         {
             line = " line: " + sourceToken.LineNumber.ToString() + " col: " + sourceToken.ColNumber.ToString();
         }
         throw new BadArgumentsException(functionPosition.ToString() + " " + ba.Message + line);
     }
 }
Beispiel #4
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);
            }
        }