Ejemplo n.º 1
0
        public KOperative(KObject formals, KObject eformal, KObject expr, KEnvironment env)
        {
            Formals = formals;
            EFormal = eformal;
            Expr    = expr;
            if (!(eformal is KIgnore || eformal is KSymbol))
            {
                throw new RuntimeException("Can't construct operative, type mismatch of eformal");
            }
            var lst = CheckFormalTree(Formals);

            if (eformal is KSymbol)
            {
                KSymbol s = eformal as KSymbol;
                if (lst.Contains(s.Value))
                {
                    throw new RuntimeException("Distinct eformal needed");
                }
            }
            if (!(env is KEnvironment))
            {
                throw new RuntimeException("Operative: not an environment!");
            }
            else
            {
                staticenv = env;
            }
        }
Ejemplo n.º 2
0
 public static void BindFormalTree(KObject formal, KObject vals, KEnvironment env, KObject ctxt = null)
 {
     if (ctxt == null)
     {
         ctxt = formal;
     }
     if (formal is KSymbol)
     {
         env.Bind(((KSymbol)formal).Value, vals);
     }
     else if (formal is KIgnore)
     {
         return;
     }
     else if (formal is KNil && vals is KNil)
     {
         return;
     }
     else if (formal is KPair && vals is KPair)
     {
         KPair f = formal as KPair;
         KPair v = vals as KPair;
         BindFormalTree(f.Car, v.Car, env, ctxt);
         BindFormalTree(f.Cdr, v.Cdr, env, ctxt);
     }
     else
     {
         throw new RuntimeException("Can't bind formal tree of " + ctxt.Write());
     }
 }
Ejemplo n.º 3
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));
            }
        }
Ejemplo n.º 4
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));
            }
        }
Ejemplo n.º 5
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 3);
            KObject formalt = First(args), eformal = Second(args), expr = Third(args);

            return(new KOperative(formalt, eformal, expr, env));
        }
Ejemplo n.º 6
0
        public virtual RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            object result = null;

            try {
                result = Do(args, env, cont);
            } catch (RuntimeException e) {
                return(CPS.Error <KObject>(getName() + ": " + e.Message, cont));
            }
            if (result is KObject)
            {
                return(CPS.Return(result as KObject, cont));
            }
            else if (result is bool)
            {
                return(CPS.Return(new KBoolean((bool)result), cont));
            }
            else if (result is RecursionResult <KObject> )
            {
                return(result as RecursionResult <KObject>);
            }
            else
            {
                return(CPS.Return(new KInert(), cont));
            }
        }
Ejemplo n.º 7
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 1);
            KFraction input = First(args) as KFraction;

            Check(input);
            return(new KDouble(input.ToDouble()));
        }
Ejemplo n.º 8
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 1);
            var inex = First(args) as KDouble;

            Check(inex);
            return(KFraction.fromDouble(inex.Value));
        }
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 1);
            KContinuation c = First(args) as KContinuation;

            Check(c, "expected continuation");
            return(new KApplicative(new PPassCont(c)));
        }
Ejemplo n.º 10
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 1);
            KCombiner comb = First(args) as KCombiner;

            Check(comb, "not a combiner");
            return(Evaluator.rceval(new KPair(comb, new KPair(new KContinuation(cont), new KNil())), env, cont));
        }
Ejemplo n.º 11
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 1);
            KCombiner op = First(args) as KCombiner;

            Check(op, "not a combiner");
            return(new KApplicative(op));
        }
Ejemplo n.º 12
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 2);
            KObject a = First(args);
            KObject b = Second(args);

            return(CompareEqual(a, b));
        }
Ejemplo n.º 13
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 2);
            KObject      expr  = First(args);
            KEnvironment envir = Second(args) as KEnvironment;

            Check(envir, "not an enviroment");
            return(CPS.PassTo <KObject>(() => Evaluator.rceval(expr, envir, cont)));
        }
Ejemplo n.º 14
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "copy-es-immutable");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            return(Return(KPair.CopyEs(First(args)), cont));
        }
Ejemplo n.º 15
0
        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));
        }
Ejemplo n.º 16
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 1);
            Continuation <KObject> result = myc.Value;

            if (result.Context == null)
            {
                return(CPS.Return(First(args), CPS.RootContinuation <KObject>()));
            }
            return(result.Call(First(args)));
        }
Ejemplo n.º 17
0
 private static void init()
 {
     if (initialized)
     {
         return;
     }
     GroundEnv     = new KEnvironment(null);
     env           = new KEnvironment(GroundEnv);
     loadedModules = new List <string>();
     initialized   = true;
 }
Ejemplo n.º 18
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 0);
            KObject datum = null;

            while (null == datum)
            {
                datum = Interpreter.readDatum();
            }
            return(datum);
        }
Ejemplo n.º 19
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));
        }
Ejemplo n.º 20
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 1);
            KEncapsulation datum = First(args) as KEncapsulation;

            Check(datum, "not an encap");
            if (datum.Id != id)
            {
                throw new RuntimeException("wrong id");
            }
            return(datum.Value);
        }
Ejemplo n.º 21
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 2);
            KPair p = args as KPair;
            Continuation <KObject> c = null;

            c = new Continuation <KObject>((x) => {
                return(CPS.PassTo(() => Evaluator.rceval(
                                      new KPair(p.Car, new KPair(new KString(c.Context.ToString()), new KNil())), env, cont)));
            }, cont, "error-handler");
            c.isHandler = true;
            return(CPS.PassTo(() => Evaluator.rceval(Second(p), env, c)));
        }
Ejemplo n.º 22
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            CPara(args, 0);
            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());
            KPair        p2 = new KPair(p, p3);
            KPair        p1 = new KPair(e, p2);

            return(p1);
        }
Ejemplo n.º 23
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));
        }
Ejemplo n.º 24
0
        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)));
        }
Ejemplo n.º 25
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));
        }
Ejemplo n.º 26
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)
            {
                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);
            }
        }
Ejemplo n.º 27
0
        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));
        }
Ejemplo n.º 28
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));
        }
Ejemplo n.º 29
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 1, "unwrap");

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

            if (!(ap is KApplicative))
            {
                return(CPS.Error("unwrap: not an applicative", cont));
            }
            else
            {
                return(Return((ap as KApplicative).Combiner, cont));
            }
        }
Ejemplo n.º 30
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));
            }
        }