예제 #1
0
파일: NETRuby.cs 프로젝트: emtees/old-code
/*        
        internal object Eval(object self, RNode n)
        {
#if EVAL_DEBUG        
            System.Console.WriteLine("Entering Eval");
#endif        
            object o = null;
            for (RNode node = n; node != null; )
            {
#if EVAL_DEBUG
                System.Console.WriteLine("Node:" + node.GetType());
                RNode prev = node;
#endif

                node = node.Eval(this, self, out o);

#if EVAL_DEBUG        
                System.Console.WriteLine("Leaving Eval(" + prev.ToString() + "), result=" + ((o == null) ? "null" : o.ToString()));
#endif
            }
            return o;
        }
        internal object Eval(object self, string src, RBasic scope, string file, int line)
        {
            RThread th = GetCurrentContext();

            object result = null;
            string filesave = th.file;
            int linesave = th.line;
            ITER itr = th.frame.iter;
            Frame frm = null;
            Scope old_scope = null;
            Block old_block = null;
            RVarmap old_dyna_vars = null;
            Scope.ScopeMode old_vmode = th.ScopeMode;
            RNCRef old_cref = null;
            RModule old_wrapper = null;
            Block data = null;
            if (scope != null)
            {
                if (scope is RProc == false)
                    throw new eTypeError(String.Format("wrong argument type {0} (expected Proc/Binding)",
                                                       ClassOf(scope).Name));
                data = ((RProc)scope).block;
                frm = data.frame;
                frm.tmp = th.frame;
                th.frame = frm;
                old_scope = th.scope;
                old_block = th.block;
                th.block = data.prev;
                old_dyna_vars = th.dyna_vars;
                th.dyna_vars = data.dyna_vars;
                old_vmode = th.ScopeMode;
                old_cref = th.cRef;
                th.cRef = frm.cBase;
                old_wrapper = th.wrapper;
                th.wrapper = data.wrapper;
                if ((file == null || file == String.Empty || (line == 1 && file == "(eval)"))
                    && data.body != null && data.body.File != null)
                {
                    file = data.body.File;
                    line = data.body.Line;
                }
                self = data.self;
                th.frame.iter = data.iter;
            }
            else
            {
                if (th.frame.prev != null)
                {
                    th.frame.iter = th.frame.prev.iter;
                }

            }
            if (file == null)
            {
                file = th.file;
                line = th.line;
            }

            RMetaObject cls = th.rClass;
            th.rClass = th.cBase;

            th.EnterEval();
            if (th.rClass is RIncClass)
                th.rClass = th.rClass.klass;
            Tag.TAG state = Tag.TAG.EMPTY;
            th.PushTag(Tag.TAG.PROT_NONE);
            try
            {
                RNode node = CompileFile(file, new StringReader(src), line, th);
                if (th.CompileFailed)
                {
                    compileError(null);
                }

                result = EvalNode(self, node, th);
            }
            catch (eTagJump tj)
            {
#if _DEBUG
                System.Console.WriteLine(tj.Message);
                System.Console.WriteLine(tj.StackTrace);
#endif
                state = tj.state;
            }
            catch (Exception e)
            {
#if _DEBUG
                System.Console.WriteLine(e.Message);
                System.Console.WriteLine(e.StackTrace);
#endif
                state = Tag.TAG.RAISE;
                th.errInfo = new RException(this, e);
            }
            th.PopTag(true);
            th.rClass = cls;
            th.LeaveEval();
        
            if (th.scope != null)
            {
                th.wrapper = old_wrapper;
                th.cRef = old_cref;
                th.frame = frm.tmp;
                th.scope = old_scope;
                th.block = old_block;
                th.dyna_vars = old_dyna_vars;
                th.ScopeSet(old_vmode);
            }
            else
            {
                th.frame.iter = itr;
            }
            th.file = filesave;
            th.line = linesave;

            if (state != Tag.TAG.EMPTY)
            {
                if (state == Tag.TAG.RAISE)
                {
                    RArray errat = null;
                    string msg = th.errInfo.ToString();
                    string err = msg;
                    if (file == "(eval)")
                    {
                        if (th.line > 1)
                        {
                            errat = th.errInfo.Backtrace;
                            if (errat != null)
                            {
                                err = String.Format("{0}: {1}", errat[0], msg);
                            }
                        }
                        ruby_raise((RException)RException.exc_new(ClassOf(th.errInfo), err));
                    }
                    ruby_raise(th.errInfo);
                }
                th.TagJump(state);
            }
            return result;
        }
        
        public object EvalString(string str)
        {
            bool newThread;
            RThread th = GetCurrentContext(out newThread);

            string oldsrc = th.file;
            th.file = "(eval)";
            object o = Eval(topSelf, str, null, null, 0);
            th.file = oldsrc;
            if (newThread)
            {
                ClearContext(th);
            }
            if (th.errInfo != null)
            {
#if _DEBUG
                if (th.errInfo.InnerException != null)
                    System.Console.Error.WriteLine(th.errInfo.InnerException.StackTrace);
#endif
                throw new NetRubyException(th.errInfo);
            }
            return o;
        }
*/        
        void Start(string filename, bool print)
        {
            StreamReader sr = new StreamReader(File.OpenRead(filename));
            
            RNode n = CompileFile(filename, sr, 0, GetCurrentContext());
           
            if(print) {
                BlockPrinter p = new BlockPrinter(this, Console.Out);
                p.Write(n);
                Console.WriteLine("");
            } else {
                RThread th = GetCurrentContext();
                EmitContext ec = new EmitContext(this, "ruby_program", "ruby.out.dll");
                RCMethod m = ec.Compile(n);
                th.PushClassScope(topSelf.klass);
                m.Call(th, topSelf, new RBasic[] {}, null);
            }
            // Flush the console
            Console.WriteLine("");
        }