Beispiel #1
0
        private static void setMethods(LpObject obj)
        {
            // TODO: new
            // TODO: []
            // TODO: each
            // TODO: map
            // TODO: keys
            // TODO: values
            // TODO: len
            //obj.methods["update"] = new LpMethod( new BinMethod(update) );
            //obj.methods["len"] = new LpMethod( new BinMethod(len));
            // TODO: to_a

            /*
             * obj.methods["=="] = new BinMethod(equal);
             * obj.methods["==="] = new BinMethod(eq);
             *
             * obj.methods["to_s"] = new BinMethod(to_s);
             * obj.methods["display"] = new BinMethod(display);
             * obj.methods["inspect"] = new BinMethod(inspect);
             */
            obj.methods["[]"]      = new LpMethod(new BinMethod(fetch), 1);
            obj.methods["to_s"]    = new LpMethod(new BinMethod(to_s), 0);
            obj.methods["display"] = new LpMethod(new BinMethod(display), 0);
        }
Beispiel #2
0
        private static LpObject open(LpObject self, LpObject[] args, LpObject block = null)
        {
            var filename = args[0].stringValue;
            var mode     = args[1].stringValue;
            var obj      = self.Clone();

            obj.stringValue = mode;
            switch (mode)
            {
            case "r":
            case "r+":
                obj.streamReader = new System.IO.StreamReader(filename, System.Text.Encoding.GetEncoding("UTF-8"));
                break;

            case "w":
            case "w+":
                obj.streamWriter = new System.IO.StreamWriter(filename);
                break;

            default:
                throw new Error.LpArgumentError();
            }

            return(obj);
        }
Beispiel #3
0
        private static LpObject init()
        {
            LpObject obj = createClassTemplate();

            obj.hashValues = new Dictionary <LpObject, LpObject>();
            return(obj);
        }
Beispiel #4
0
        private static LpObject at(LpObject self, LpObject[] args, LpObject block = null)
        {
            var arg = args[0];
            var v   = self.arrayValues.ElementAt((int)arg.doubleValue);

            return(v);
        }
Beispiel #5
0
        public static LpObject initialize(AstNode stmts, AstNode args)
        {
            LpObject obj = init();

            obj.statements = stmts;
            return(obj);
        }
Beispiel #6
0
        private static void setMethods(LpObject obj)
        {
            // TODO: new
            // TODO: cdr
            // TODO: each
            // TODO: map
            // TODO: join
            obj.methods["+"]      = new LpMethod(new  BinMethod(concat), 1);
            obj.methods["concat"] = new LpMethod(new BinMethod(concat), 1);
            obj.methods["last"]   = new LpMethod(new BinMethod(last), 0);
            obj.methods["push"]   = new LpMethod(new BinMethod(push), 1);
            obj.methods["<<"]     = new LpMethod(new BinMethod(push), 0);
            obj.methods["at"]     = new LpMethod(new BinMethod(at), 1);
            obj.methods["[]"]     = new LpMethod(new BinMethod(at), 1);
            obj.methods["car"]    = new LpMethod(new BinMethod(first), 0);
            obj.methods["first"]  = new LpMethod(new BinMethod(first), 0);
            obj.methods["size"]   = new LpMethod(new BinMethod(len), 0);
            obj.methods["len"]    = new LpMethod(new BinMethod(len), 0);
            obj.methods["map"]    = new LpMethod(new BinMethod(map), 0);
            //obj.methods["=="] = new LpMethod( new BinMethod(equal), 1);
            //obj.methods["==="] = new LpMethod( new BinMethod(eq), 1 );

            obj.methods["display"] = new LpMethod(new BinMethod(display), 0);
            obj.methods["inspect"] = new LpMethod(new BinMethod(inspect), 0);
            obj.methods["to_s"]    = new LpMethod(new BinMethod(to_s), 0);
        }
Beispiel #7
0
        static LpObject concat(LpObject self, LpObject[] args, LpObject block = null)
        {
            var v = args[0];

            self.arrayValues.AddRange(v.arrayValues);
            return(self);
        }
Beispiel #8
0
        static LpObject inspect(LpObject self, LpObject[] args, LpObject block = null)
        {
            var vs = self.arrayValues.Select <LpObject, string>((a, b) => a.funcall("inspect", null).stringValue.ToString()).ToArray();
            var s  = string.Join(", ", vs);

            return(LpString.initialize("[" + s + "]"));
        }
Beispiel #9
0
        static long runNode(string[] argv)
        {
            string fileName = argv[0];
            string code     = readFile(fileName);

            var       parser   = new Parser.LpGrammer();
            var       language = new LanguageData(parser);
            ScriptApp app      = new ScriptApp(language);
            var       tree     = app.Parser.Parse(code);

            Object.LpObject result = null;
            try
            {
                result = (Object.LpObject)app.Evaluate(tree);
                if (result == null)
                {
                    Debug.WriteLine("null");
                }
                else
                {
                    Console.WriteLine(result);
                    Console.WriteLine("result: {0}", result);
                    result.funcall("display", new Object.LpObject[] { }, null);
                }
            }
            catch (Error.LpError e)
            {
                Console.WriteLine(e.ToString());
            }
            Debug.WriteLine("Finish");

            return(0);
        }
Beispiel #10
0
        static LpObject to_class(LpObject self, LpObject[] args, LpObject block = null)
        {
            var name = args[0].stringValue;

            //return LpClass.initialize( name, self.statements.ToList() );
            return(null);
        }
Beispiel #11
0
        public LpObject funcall(string name, LpObject self, LpObject[] args, LpObject block)
        {
            LpObject ret = execMethod(name, self, args, block);

            if (ret != null)
            {
                return(ret);
            }

            LpMethod m = null;

            // method_missing
            m = methods["method_missing"] as LpMethod;
            if (null != m)
            {
                return(m.funcall(self, args, block));
            }

            // superclass
            if (null != superclass)
            {
                return(superclass.funcall(name, self, args, block));
            }

            throw new Error.LpNoMethodError();
        }
Beispiel #12
0
        private static LpObject break_(LpObject self, LpObject[] args, LpObject block = null)
        {
            var ret = LpNl.initialize();

            ret.controlStatus = ControlCode.BREAK;
            return(ret);
        }
Beispiel #13
0
        private static LpObject init(AstNode node)
        {
            LpObject obj = createClassTemplate();

            obj.statements = node;
            return(obj);
        }
Beispiel #14
0
        private static LpObject sleep(LpObject self, LpObject[] args, LpObject block = null)
        {
            var time = args[0].doubleValue;

            System.Threading.Thread.Sleep((int)time);
            return(LpNl.initialize());
        }
Beispiel #15
0
        protected static LpObject send(LpObject self, LpObject[] args, LpObject block = null)
        {
            var methodName = args[0].stringValue;
            var sendedArgs = args.Skip(1).Take(args.Length - 1).ToArray();

            return(self.funcall(methodName, sendedArgs, block));
        }
Beispiel #16
0
        protected static LpObject add(LpObject self, LpObject[] args, LpObject block = null)
        {
            var v = args[0];

            self.stringValue += v.stringValue;
            return(self);
        }
Beispiel #17
0
        protected static LpObject display(LpObject self, LpObject[] args, LpObject block = null)
        {
            var so = to_s(self, args);

            Console.WriteLine(so.stringValue);
            return(null);
        }
Beispiel #18
0
        protected static LpObject define_method(LpObject self, LpObject[] args, LpObject block = null)
        {
            var name = args[0].stringValue;

            //self.methods[name] = LpMethod.initialize( block.arguments, block.statements.ToList() );
            return(LpSymbol.initialize(name));
        }
Beispiel #19
0
        public LpObject doMethod(System.Object method, LpObject self, LpObject[] args, LpObject block)
        {
            LpMethod m = null;

            if (null != (m = method as LpMethod))
            {
                return(m.funcall(self, args, block));
            }
            else
            {
                var klass = (method as LpObject);
                switch (klass.class_name)
                {
                case "Macro":
                    return(LpMacro.call((LpObject)method, args, block));

                case "Lambda":
                case "Block":
                    return(LpLambda.call((LpObject)method, args, block));

                default:
                    return(null);
                }
            }
        }
Beispiel #20
0
        private static LpObject eq(LpObject self, LpObject[] args, LpObject block = null)
        {
            var o   = args[0];
            var sym = symbols[o.stringValue];

            return(LpBool.initialize(self == sym));
        }
Beispiel #21
0
        //`(?recv).times(?*args)
        // `?7 // '7
        // `?a // a
        //  `(a+?b)
        private static LpObject init(List <string> nodes)
        {
            LpObject obj = init();

            //obj.statements = //nodes.Select((node) => castAndExpand(node) ).ToList();
            return(obj);
        }
Beispiel #22
0
        private static LpObject loop(LpObject self, LpObject[] args, LpObject block = null)
        {
            LpObject ret = LpNl.initialize();

            if (block == null)
            {
                return(ret);
            }

            while (true)
            {
                ret = block.funcall("call", args, null);
                // break文
                if (ret.controlStatus == ControlCode.BREAK)
                {
                    ret.controlStatus = ControlCode.NONE;
                    break;
                }
                // next文
                if (ret.controlStatus == ControlCode.NEXT)
                {
                    ret.controlStatus = ControlCode.NONE;
                    break;
                }
            }
            return(ret);
        }
Beispiel #23
0
        protected static LpObject to_s(LpObject self, LpObject[] args = null, LpObject block = null)
        {
            var hashCode = self.GetHashCode();
            var str      = string.Format("<obj {0}>", hashCode.ToString("x4"));

            return(LpString.initialize(str));
        }
Beispiel #24
0
        static LpObject display(LpObject self, LpObject[] args, LpObject block = null)
        {
            var so = to_s(self, args);

            Console.WriteLine(so.stringValue);
            return(LpNl.initialize());
        }
Beispiel #25
0
        /*public static LpObject initialize(Ast.LpAstNode stmt)
         * {
         *  LpObject obj = init();
         *  //obj.statements.Add(stmt);
         *  return obj;
         * }
         *
         * public static LpObject initialize(List<Ast.LpAstNode> stmts)
         * {
         *  LpObject obj = init();
         *  //obj.statements = stmts;
         *  return obj;
         * }
         *
         * public static LpObject initialize(List<Ast.LpAstNode> stmts, string[] args, bool argLoose = false )
         * {
         *  LpObject obj = init();
         *  //obj.statements = stmts;
         *  obj.arguments = new Util.LpArguments(args, argLoose);
         *  obj.superclass = LpObject.initialize();
         *  return obj;
         * }*/

        static LpObject init()
        {
            LpObject obj = createClassTemplate();

            //obj.statements = new List<Ast.LpAstNode>();
            return(obj);
        }
Beispiel #26
0
        private static LpObject create_mold_class(string class_name)
        {
            LpObject kls = initialize().Clone();

            kls.class_name = class_name;
            //kls.methods = (Hashtable)kls.methods.Clone();
            return(kls);
        }
Beispiel #27
0
        protected static LpObject alias(LpObject self, LpObject[] args, LpObject block = null)
        {
            string src = args[0].stringValue;
            string dst = args[1].stringValue;

            self.methods[dst] = (LpMethod)self.methods[src];
            return(LpSymbol.initialize(dst));
        }
Beispiel #28
0
        static LpObject bind(LpObject self, LpObject[] args, LpObject block = null)
        {
            var name = args[0].stringValue;

            //var ctx =Util.LpIndexer.getLatestClass();
            //ctx.methods[name] = self;
            return(self);
        }
Beispiel #29
0
        private static LpObject setOp(LpObject self, LpObject[] args, LpObject block = null)
        {
            var v = args[0];

            //var env = Util.LpIndexer.last();
            //var o = env.setVariable(self.stringValue, v);
            return(v);
        }
Beispiel #30
0
        private static LpObject load(LpObject self, LpObject[] args, LpObject block = null)
        {
            // TODO: load path から使えるものを順番に捜す
            var code = readFile(args[0].stringValue);

            //LpParser.execute(code);
            return(LpNl.initialize());
        }