Beispiel #1
0
 public Assignment(Symbol id, Expression val)
 {
     this.id = id; this.val = val;
 }
Beispiel #2
0
 public Primapp(IPrim prim, Expression[] rands)
 {
     this.prim = prim; this.rands = rands;
 }
Beispiel #3
0
        public Symbol[] ids; // if null, gets all args as a list

        #endregion Fields

        #region Constructors

        public Proc(Symbol[] ids, Expression body, bool all_in_one)
        {
            this.ids = ids; this.body = body; this.all_in_one = all_in_one;
        }
Beispiel #4
0
 public App(Expression rator, Expression[] rands)
 {
     this.rator = rator; this.rands = rands;
 }
Beispiel #5
0
 public If(Expression test_exp, Expression true_exp, Expression false_exp)
 {
     this.test_exp = test_exp; this.true_exp = true_exp; this.false_exp = false_exp;
 }
Beispiel #6
0
        // 		internal void Mark(object obj)
        // 		{
        // 			if (obj is Pair)
        // 				marker = ((Pair) obj).marker;
        // 		}
        public static Object[] Eval_Rands(Expression[] rands, Env env)
        {
            if (rands == null)
                return null;

            Object[] dest = new Object[rands.Length];

            for (int i=0; i<rands.Length; i++)
                dest[i] = rands[i].Eval(env);

            return dest;
        }
Beispiel #7
0
        public static Expression Parse(object a)
        {
            if (a is Symbol)
            {
                if (((Symbol) a).val.IndexOf(".") == -1)
                    return new Var(a as Symbol);
                else
                {
                    string aString = ((Symbol) a).val;
                    int posLastDot = aString.LastIndexOf(".");

                    Expression[] rands = new Expression[2];
                    rands[0] = Expression.Parse(Symbol.Create(aString.Substring(0, posLastDot)));
                    rands[1] = new Lit(Symbol.Create(aString.Substring(posLastDot + 1, aString.Length - posLastDot - 1)));

                    return new App(Expression.Parse(Symbol.Create("get-property")), rands);

                }
            }
            if (a is Pair)
            {
                Pair pair = a as Pair;
                object car = pair.car;
                switch (car.ToString())
                {
                    case "begin":
                        Expression[] exps = new Expression[pair.cdr.Count];
                        int pos = 0;
                        foreach (object obj in pair.cdr)
                        {
                            exps[pos] = Parse(obj);
            // 							exps[pos].Mark(obj);
                            pos++;
                        }
                        Begin beginExps = new Begin(exps);
            // 						beginExps.Mark(pair);
                        return beginExps;
                    case "if":
                        Pair curr = pair.cdr;
                        Expression test_exp = Parse(curr.car);
            // 						test_exp.Mark(curr.car);
                        curr = curr.cdr;
                        Expression true_exp = Parse(curr.car);
            // 						true_exp.Mark(curr.car);
                        curr = curr.cdr;
                        Expression false_exp = Parse(curr.car);
            // 						false_exp.Mark(curr.car);
                        return new If(test_exp, true_exp, false_exp);
                    case "quote":
                        return new Lit(pair.cdr.car);
                    case "set!":
                    {
                        Symbol var = pair.cdr.car as Symbol;
                        if (var == null)
                            throw new Exception("set! error -> variable must be a symbol: " + Util.Dump(pair));

                        Expression exp = Parse(pair.cdr.cdr.car) as Expression;
            // 						exp.Mark(pair.cdr.cdr.car);
                        if (var.val.IndexOf('.') == -1)
                        {
                            Assignment assignment = new Assignment(var, exp);
            // 							assignment.Mark(pair);
                            return assignment;
                        }
                        else
                        {
                            string aString = var.val;
                            int posLastDot = aString.LastIndexOf(".");
                            Expression[] rands = new Expression[3];
                            rands[0] = Expression.Parse(Symbol.Create(aString.Substring(0, posLastDot)));
                            rands[1] = new Lit(Symbol.Create(aString.Substring(posLastDot + 1, aString.Length - posLastDot - 1)));
                            rands[2] = exp;

                            App app = new App(Expression.Parse(Symbol.Create("set-property")), rands);
            // 							app.Mark(pair);
                            return app;
                        }
                    }
                    case "lambda":
                        // Debug.WriteLine("sparsing lambda " + pair.ToString());
                        curr = pair.cdr  as Pair;
                        Symbol[] ids = null;
                        bool all_in_one = false;
                        if (curr.car != null)
                        {
                            if (curr.car is Pair)
                            {
                                Object[] ids_as_obj = (curr.car as Pair).ToArray();
                                ids = new Symbol[ids_as_obj.Length];
                                for (int i=0; i<ids_as_obj.Length; i++)
                                {
                                    ids[i] = ids_as_obj[i] as Symbol;
                                    if (ids[i] == null)
                                        throw new Exception("lambda error -> params must be symbols: " + Util.Dump(pair));
                                }
                            }
                            else
                            {
                                all_in_one = true;
                                ids = new Symbol[1];
                                ids[0] = curr.car as Symbol;
                                if (ids[0] == null)
                                    throw new Exception("lambda error -> params must be symbols: " + Util.Dump(pair));
                            }
                        }
                        curr = curr.cdr  as Pair;
                        // insert implied begin if neccessary
                        Expression body;
                        if (curr.cdr == null)
                        {
                            body = Parse(curr.car);
            // 							if (curr.car is Pair)
            // 								body.Mark(curr.car);
            // 							else
            // 								body.Mark(curr);
                        }
                        else
                        {
                            Expression[] begin = new Expression[curr.Count];
                            pos = 0;
                            foreach (object obj in curr)
                            {
                                begin[pos] = Parse(obj);
            // 								begin[pos].Mark(obj);
                                pos++;
                            }
                            body = new Begin(begin);
                        }
                        return new Proc(ids, body, all_in_one);
                    default:  // app
                        if (pair.hasMember)
                        {
                            Expression[] rands = new Expression[2];
                            if (pair.member.IndexOf('.') != -1)
                            {
                                string currentMember = pair.member;
                                int posLastDot = currentMember.LastIndexOf(".");

                                pair.member = currentMember.Substring(0, posLastDot);
                                rands[0] = Expression.Parse(pair);
                                pair.member = currentMember;

                                rands[1] = new Lit(Symbol.Create(currentMember.Substring(posLastDot + 1, currentMember.Length - posLastDot - 1)));
                            }
                            else
                            {
                                pair.hasMember = false;
                                rands[0] = Expression.Parse(pair);
                                pair.hasMember = true;

                                rands[1] = new Lit(Symbol.Create(pair.member));
                            }
                            return new App(Expression.Parse(Symbol.Create("get-property")), rands);
                        }
                        else
                        {
                            Expression[] rands = null;
                            if (pair.cdr != null)
                            {
                                rands = new Expression[pair.cdr.Count];
                                pos = 0;
                                foreach (object obj in pair.cdr)
                                {
                                    rands[pos] = Expression.Parse(obj);
            // 									rands[pos].Mark(obj);
                                    pos++;
                                }
                            }

                            IPrim prim = Primitives.getPrim(pair.car.ToString());
                            if (prim != null)
                            {
                                Primapp primapp = new Primapp(prim, rands);
            // 								primapp.Mark(pair);
                                return primapp;
                            }
                            else
                            {
                                App app = new App(Expression.Parse(pair.car), rands);
            // 								app.Mark(pair);
                                return app;
                            }
                        }
                }
            }
            else
                return new Lit(a);
        }
Beispiel #8
0
 public Closure(Symbol[] ids, Expression body, bool all_in_one, Env env)
 {
     this.ids = ids; this.body = body; this.all_in_one = all_in_one; this.env = env;
 }
Beispiel #9
0
 public Begin(Expression[] expressions)
 {
     this.expressions = expressions;
 }
Beispiel #10
0
 public override object Eval(Expression exp)
 {
     return exp.Eval(initEnv);
 }
Beispiel #11
0
 public abstract object Eval(Expression exp);
Beispiel #12
0
        internal static void EvalExpression(Expression expression)
        {
            if (!InitCalled)
                return;

            if ((!RunHidden) && (expression.marker != null))
            {
                MarkExpression(expression.marker);
                Wait(expression);
            }
            else if ((expression.marker != null) && (command != Command.EvalImmediate) && LineHasBreakPoint(expression.marker))
            {
                Pause();
                MarkExpression(expression.marker);
                Wait(expression);
            }
        }
Beispiel #13
0
        public static Command Wait(Expression currentExpression)
        {
            if (!InitCalled)
                return Command.Run;

            ShowCalls();

            do
            {
                debugWaitEventIsWaiting = true;
                DebugInfo.SetEnableButtons();
                debugWaitEvent.WaitOne();
                debugWaitEventIsWaiting = false;
                DebugInfo.SetEnableButtons();

                if (command != Command.Run)
                    debugWaitEvent.Reset();

                switch (command)
                {
                    case Command.Abort :
                        Init();
                        throw new Exception("Abort");

                    case Command.EvalImmediate :
                        EvalImmediate();
                        break;

                    case Command.ShowNextExpression :
                        MarkExpression(currentExpression.marker);
                        currentExpression.marker.document.Activate();

                        break;
                }

            } while ((command != Command.Run) && (command != Command.Step) && (command != Command.StepOver));

            return command;
        }
Beispiel #14
0
        public static void Push(Closure proc, Expression rator, Object[] args, Marker marker)
        {
            if (!InitCalled)
                return;

            if (command == Command.EvalImmediate)
                return;

            procs.Add(proc);

            if (marker == null)
                return;

            if ((command == Command.StepOver) && (stepOverProcsCount == -1))
            {
                RunHidden = true;
                stepOverProcsCount = procs.Count;
            }

            procIndexOfItemInCallStack.Add(procs.Count - 1);
            string ratorname;
            if (rator is Var)
                ratorname = ((Tachy.Var) rator).id.ToString();
            else
                ratorname = "...";

            string argsstr = "";
            foreach (object arg in args)
                argsstr = argsstr + " " + arg.ToString();

            //ListViewItem lvi = callstackListView.Items.Add("");
            //lvi.SubItems.Add("(" + ratorname + argsstr + ")");
            calls.Add("(" + ratorname + argsstr + ")");
        }