コード例 #1
0
ファイル: PDefine.cs プロジェクト: Entkenntnis/kernel-sharp
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 2);
            KObject definand = First(args), expr = Second(args);
            var     cc = new Continuation <KObject>((e) => {
                try {
                    Evaluator.BindFormalTree(definand, e, env);
                } catch (Exception ex) {
                    return(CPS.Error <KObject>(ex.Message, cont));
                }
                return(CPS.Return <KObject>(new KInert(), cont));
            }, cont, "define");

            return(CPS.PassTo(() => Evaluator.rceval(expr, env, cc)));
        }
コード例 #2
0
ファイル: PSubtract.cs プロジェクト: Entkenntnis/kernel-sharp
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            int length = KPair.Length(args);

            if (length == -1)
            {
                throw new RuntimeException("improper list");
            }
            if (length == 0)
            {
                throw new RuntimeException("at least one arg expected");
            }
            else if (length == 1)
            {
                KObject a = NumbersModule.Check(First(args));
                if (a is KFraction)
                {
                    return(new KFraction((a as KFraction).Numerator * -1, (a as KFraction).Denominator));
                }
                else
                {
                    return(new KDouble((a as KDouble).Value * -1));
                }
            }
            else
            {
                KObject dif  = NumbersModule.Check(First(args));
                KObject head = (args as KPair).Cdr;
                while (head is KPair)
                {
                    KObject nextNumber = NumbersModule.Check(First(head));
                    if (dif is KFraction && nextNumber is KFraction)
                    {
                        dif = (dif as KFraction).Subtract(nextNumber as KFraction);
                    }
                    else
                    {
                        KDouble a = NumbersModule.ToInexact(dif);
                        KDouble b = NumbersModule.ToInexact(nextNumber);
                        dif = new KDouble(a.Value - b.Value);
                    }
                    head = (head as KPair).Cdr;
                }
                return(dif);
            }
        }
コード例 #3
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 1);
            KString path = First(args) as KString;

            Check(path);
            try
            {
                List <KObject> tokens = Parser.ParseAll(File.ReadAllText(path.Value));
                foreach (var token in tokens)
                {
                    Evaluator.Eval(token, env);
                }
                return(new KInert());
            }
            catch (Exception e)
            {
                throw new RuntimeException("Failed to load file: " + e.Message);
            }
        }
コード例 #4
0
 private Continuation <KObject> fixStructure(Continuation <KObject> c)
 {
     if (c.Parent == null)
     {
         return(c); // base continuation without state
     }
     else
     {
         var thatsnew = new Continuation <KObject>(c.NextStep, fixStructure(c.Parent), c.Context);
         thatsnew.isError = c.isError;
         if (c._Placeholder != null)
         {
             var remaing = new LinkedList <KObject>(c._RemainingObjs);
             var pars    = new LinkedList <KObject>(c._Pairs);
             thatsnew._Pairs         = pars;
             thatsnew._RemainingObjs = remaing;
             thatsnew._Placeholder   = c._Placeholder;
         }
         return(thatsnew);
     }
 }
コード例 #5
0
ファイル: PEncapP.cs プロジェクト: Entkenntnis/kernel-sharp
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            int len = KPair.Length(args);

            if (len == -1)
            {
                throw new RuntimeException("parameter is not a list");
            }
            else
            {
                bool result = true;
                KPair.Foreach(x =>
                {
                    if (!(x is KEncapsulation) || (x as KEncapsulation).Id != id)
                    {
                        result = false;
                    }
                }, args);
                return(result);
            }
        }
コード例 #6
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            int length = KPair.Length(args);

            if (length == -1)
            {
                throw new RuntimeException("improper list");
            }
            if (length == 0)
            {
                return(new KFraction(BigInteger.One, BigInteger.One));
            }
            else
            {
                // we have a list of at least one elements
                // we must fold it to something useful
                KObject product = NumbersModule.Check(First(args));
                KObject head    = (args as KPair).Cdr;
                while (head is KPair)
                {
                    KObject nextNumber = NumbersModule.Check(First(head));
                    if (product is KFraction && nextNumber is KFraction)
                    {
                        product = (product as KFraction).Multiply(nextNumber as KFraction);
                    }
                    else
                    {
                        KDouble a = NumbersModule.ToInexact(product);
                        KDouble b = NumbersModule.ToInexact(nextNumber);
                        product = new KDouble(a.Value * b.Value);
                    }
                    head = (head as KPair).Cdr;
                }
                return(product);
            }
        }
コード例 #7
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 3);
            KObject pred = First(args), tr = Second(args), fl = Third(args);
            var     cc = new Continuation <KObject>((p) => {
                if (!(p is KBoolean))
                {
                    return(CPS.Error <KObject>("$if: predicate not boolean", cont));
                }
                else
                {
                    if (((KBoolean)p).Value)
                    {
                        return(CPS.PassTo <KObject>(() => Evaluator.rceval(tr, env, cont)));
                    }
                    else
                    {
                        return(CPS.PassTo <KObject>(() => Evaluator.rceval(fl, env, cont)));
                    }
                }
            }, cont, "if");

            return(CPS.PassTo(() => Evaluator.rceval(pred, env, cc)));
        }
コード例 #8
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            int len = KPair.Length(args);

            if (len < 2 || len > 3)
            {
                return(CPS.Error("extend-continuation: argument mismatch", cont));
            }
            KContinuation argC = First(args) as KContinuation;
            KApplicative  argA = Second(args) as KApplicative;
            KEnvironment  argE = len == 3 ? Third(args) as KEnvironment : new KEnvironment();

            if (argC == null || argA == null || argE == null)
            {
                return(CPS.Error("extend-continuation: mismatching arguments", cont));
            }

            var nc = new Continuation <KObject>((val, ctxt) =>
            {
                return(CPS.Next(() => Evaluator.rceval(new KPair(argA.Combiner, val, true), argE, argC.Value), argC.Value));
            }, argC.Value, argA);

            return(Return(new KContinuation(nc), cont));
        }
コード例 #9
0
ファイル: Numbers.cs プロジェクト: Entkenntnis/kernel-sharp
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "number?");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            return(ReturnBool(First(args) is KDouble || First(args) is KInteger, cont));
        }
コード例 #10
0
ファイル: Numbers.cs プロジェクト: Entkenntnis/kernel-sharp
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 2, "-");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject a = First(args), b = Second(args);

            if (KNumber.IsNumber(b) && KNumber.GetDouble(b) == 0.0)
            {
                return(CPS.Error("Division by zero", cont));
            }
            var result = KNumber.DoMath(a, b, (x, y) => x / y, (x, y) => x / y);

            if (result == null)
            {
                return(CPS.Error("/: wrong types", cont));
            }
            return(Return(result, cont));
        }
コード例 #11
0
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     CPara(args, 1);
     Check(First(args), "message not a string");
     return(CPS.Error <KObject>((First(args) as KString).Value, cont));
 }
コード例 #12
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "wrap");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject op = First(args);

            if (!(op is KOperative || op is KApplicative))
            {
                return(CPS.Error("wrap: not a operative", cont));
            }
            else
            {
                return(Return(new KApplicative(op as KCombiner), cont));
            }
        }
コード例 #13
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "decapsle");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject datum = First(args);

            if (!(datum is KEncapsulation))
            {
                return(CPS.Error("decaplse: not an encap", cont));
            }
            KEncapsulation enc = datum as KEncapsulation;

            if (enc.Id != id)
            {
                return(CPS.Error("decaplse: wrong", cont));
            }
            return(Return(enc.Value, cont));
        }
コード例 #14
0
ファイル: Numbers.cs プロジェクト: Entkenntnis/kernel-sharp
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 2, "*");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject a = First(args), b = Second(args);
            var     result = KNumber.DoMath(a, b, (x, y) => x * y, (x, y) => x * y);

            if (result == null)
            {
                return(CPS.Error("*: wrong types", cont));
            }
            return(Return(result, cont));
        }
コード例 #15
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "encapsulate");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject datum = First(args);

            return(Return(new KEncapsulation(id, datum), cont));
        }
コード例 #16
0
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     CPara(args, 1);
     Console.Write(First(args).Display());
     return(new KInert());
 }
コード例 #17
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            // first of all I wanna check for exit guards
            //Continuation<KObject> loop = cont;
            Continuation <KObject> result = myc.Value;

            /*var conts = new LinkedList<Continuation<KObject>>();
             * while (loop != null)
             * {
             *  conts.AddFirst(loop);
             *  loop = loop.Parent;
             * }
             * loop = cont;
             * Continuation<KObject> innerloop = myc.Value;
             * while(true)
             * {
             *  if (innerloop == null)
             *      break;
             *  if (continuationEqual(innerloop, cont))
             *  {
             *      loop = null;
             *      break;
             *  }
             *  innerloop = innerloop.Parent;
             * }
             * while (loop != null)
             * {
             *  if (loop.ExitGuard != null && loop.ExitGuard.Count > 0)
             *  {
             *      // ok, found something, now check
             *      foreach (KGuard guard in loop.ExitGuard)
             *      {
             *          foreach (Continuation<KObject> c in conts)
             *          {
             *              // from the last to the first
             *              if (continuationEqual(c, guard.Selector.Value))
             *              {
             *                  var child = new Continuation<KObject>((x, ctxt) =>
             *                      {
             *                          return Evaluator.rceval(new KPair(guard.Interceptor, new KPair(x, new KPair(new KNil(), new KNil(), true), true), true), KEnvironment.GetGroundEnv(), ctxt._RemainingGuards);
             *                      }, result, new KString("guard"));
             *                  child._RemainingGuards = result;
             *                  result = child;
             *              }
             *          }
             *      }
             *  }
             *  loop = loop.Parent;
             * }*/

            if (result.isError)
            {
                Continuation <KObject> cc = new Continuation <KObject>(null, cont, null);
                cc.isError = true;
                return(CPS.Return(args, cc));
            }
            if (result.Context == null)
            {
                //well, top level reached hm hm
                return(CPS.Return(args, result));
            }
            return(result.NextStep(args, result));
        }
コード例 #18
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "continuation->applicative");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject c = First(args);

            if (c is KContinuation)
            {
                return(CPS.Return(new KApplicative(new PPassCont(c as KContinuation)), cont));
            }
            return(CPS.Error("continuation->applicative: not a continuation given", cont));
        }
コード例 #19
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "call/cc");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject comb = First(args);

            try
            {
                if (comb is KCombiner)
                {
                    return((comb as KCombiner).Combine(new KPair(new KContinuation(cont), new KNil(), true), env, cont));
                }
            }
            catch (Exception e)
            {
                return(CPS.Error(e.Message, cont));
            }
            return(CPS.Error("call/cc: not a combiner", cont));
        }
コード例 #20
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 0, "make-encapsulation-type");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            this.id = counter++;
            KApplicative e  = new KApplicative(new PEncapE(id));
            KApplicative p  = new KApplicative(new PEncapP(id));
            KApplicative d  = new KApplicative(new PEncapD(id));
            KPair        p3 = new KPair(d, new KNil(), true);
            KPair        p2 = new KPair(p, p3, true);
            KPair        p1 = new KPair(e, p2, true);

            return(Return(p1, cont));
        }
コード例 #21
0
ファイル: Numbers.cs プロジェクト: Entkenntnis/kernel-sharp
 public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     return(Predicate(args, "inexcat?", typeof(KDouble), cont));
 }
コード例 #22
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "display");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject datum = First(args);

            Console.WriteLine(datum.Display());
            return(Return(new KInert(), cont));
        }
コード例 #23
0
ファイル: Numbers.cs プロジェクト: Entkenntnis/kernel-sharp
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 2, ">?");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject a = First(args), b = Second(args);

            if (!(KNumber.IsNumber(a) && KNumber.IsNumber(b)))
            {
                return(CPS.Error("wrong types", cont));
            }
            if (a is KDouble || b is KDouble)
            {
                return(ReturnBool(KNumber.GetDouble(a) > KNumber.GetDouble(b), cont));
            }
            else
            {
                return(ReturnBool((a as KInteger).Value > (b as KInteger).Value, cont));
            }
        }
コード例 #24
0
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     CPara(args, 2);
     return(new KPair(First(args), Second(args)));
 }
コード例 #25
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 3, "guard-continuation");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KContinuation c     = Second(args) as KContinuation;
            KObject       entry = First(args);
            KObject       exit  = Third(args);

            try
            {
                if (null == c)
                {
                    throw new RuntimeException("guard-continuation: not a continution");
                }
                c.Value.EntryGuard = assignGuards(entry);
                c.Value.ExitGuard  = assignGuards(exit);
                return(CPS.Return(c, cont));
            }
            catch (Exception e)
            {
                return(CPS.Error(e.Message, cont));
            }
        }
コード例 #26
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            int len = KPair.Length(args);

            if (len == -1)
            {
                return(CPS.Error("encapsulation?: parameter is not a list", cont));
            }
            else
            {
                bool result = true;
                KPair.Foreach(x =>
                {
                    if (!(x is KEncapsulation) || (x as KEncapsulation).Id != id)
                    {
                        result = false;
                    }
                }, args);
                return(ReturnBool(result, cont));
            }
        }
コード例 #27
0
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     return(NumbersModule.Numberp(args, (a, b) => {
         return a.LessThan(b);
     }));
 }
コード例 #28
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "load");


            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject val = First(args) as KString;

            if (!(val is KString))
            {
                return(CPS.Error("load: not a string", cont));
            }
            string path = (val as KString).Value;

            try
            {
                List <KObject> tokens = Parser.ParseAll(File.ReadAllText(path));
                foreach (var token in tokens)
                {
                    Evaluator.Eval(token, env);
                }
                return(CPS.Return(new KInert(), cont));
            }
            catch (Exception e)
            {
                return(CPS.Error("Failed to load file: " + e.Message, cont));
            }
        }
コード例 #29
0
ファイル: PBooleanp.cs プロジェクト: Entkenntnis/kernel-sharp
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     CPara(args, 1);
     return(First(args) is KBoolean);
 }
コード例 #30
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 2, "set-car!");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject p = First(args), v = Second(args);

            if (p is KPair)
            {
                KPair pair = p as KPair;
                if (pair.Mutable)
                {
                    pair.SetCar(v);
                    return(Return(new KInert(), cont));
                }
            }
            return(CPS.Error("set-car: pair not mutable", cont));
        }