コード例 #1
0
ファイル: Lists.cs プロジェクト: h7ga40/BlockFactory
        public node lists_setIndex(ListsSetIndexBlock block)
        {
            // Set element at index.
            var list = valueToCode(block, "LIST");

            if (list == null)
            {
                list = new array_node(this, new JsArray <node>());
            }
            var mode = block.getFieldValue("MODE");

            if (String.IsNullOrEmpty(mode))
            {
                mode = "GET";
            }
            var where = block.getFieldValue("WHERE");
            if (String.IsNullOrEmpty(where))
            {
                where = "FROM_START";
            }
            var at = valueToCode(block, "AT");

            if (at == null)
            {
                at = new int_node(this, 1);
            }
            var value = valueToCode(block, "TO");

            if (value == null)
            {
                value = new nil_node(this);
            }

            if (where == "FIRST")
            {
                if (mode == "SET")
                {
                    return(new asgn_node(this, new call_node(this, list, intern("[]"), new JsArray <node>()
                    {
                        new int_node(this, 0)
                    }, null), value));
                }
                else if (mode == "INSERT")
                {
                    return(new call_node(this, list, intern("unshift"), new JsArray <node>()
                    {
                        value
                    }, null));
                }
            }
            else if (where == "LAST")
            {
                if (mode == "SET")
                {
                    return(new asgn_node(this, new call_node(this, list, intern("[]"), new JsArray <node>()
                    {
                        new int_node(this, -1)
                    }, null), value));
                }
                else if (mode == "INSERT")
                {
                    return(new call_node(this, list, intern("push"), new JsArray <node>()
                    {
                        value
                    }, null));
                }
            }
            else if (where == "FROM_START")
            {
                // Blockly uses one-based indicies.
                if (at is int_node)
                {
                    // If the index is a naked number, decrement it right now.
                    at = new int_node(this, (int)(((int_node)at).to_i() - 1));
                }
                else
                {
                    // If the index is dynamic, decrement it in code.
                    at = new begin_node(this, new call_node(this, at, intern("-"), new int_node(this, 1)), true);
                    at = new call_node(this, at, intern("to_i"));
                }
                if (mode == "SET")
                {
                    return(new asgn_node(this, new call_node(this, list, intern("[]"), new JsArray <node>()
                    {
                        at
                    }, null), value));
                }
                else if (mode == "INSERT")
                {
                    return(new call_node(this, list, intern("insert"), new JsArray <node>()
                    {
                        at, value
                    }, null));
                }
            }
            else if (where == "FROM_END")
            {
                if (mode == "SET")
                {
                    // Blockly uses one-based indicies.
                    if (at is int_node)
                    {
                        // If the index is a naked number, decrement it right now.
                    }
                    else
                    {
                        // If the index is dynamic, decrement it in code.
                        at = new call_node(this, at, intern("to_i"));
                    }
                    return(new asgn_node(this, new call_node(this, list, intern("[]"), new JsArray <node>()
                    {
                        at
                    }, null), value));
                }
                else if (mode == "INSERT")
                {
                    // Blockly uses one-based indicies.
                    if (at is int_node)
                    {
                        // If the index is a naked number, decrement it right now.
                        at = new int_node(this, (int)(((int_node)at).to_i() + 1));
                    }
                    else
                    {
                        // If the index is dynamic, decrement it in code.
                        at = new begin_node(this, new call_node(this, at, intern("+"), new int_node(this, 1)), true);
                        at = new call_node(this, at, intern("to_i"));
                    }

                    at = new call_node(this, at, intern("-@"), (node)null);
                    return(new call_node(this, list, intern("insert"), new JsArray <node>()
                    {
                        at, value
                    }, null));
                }
            }
            else if (where == "RANDOM")
            {
                if (mode == "SET")
                {
                    return(new fcall_node(this, intern("lists_set_random_item"), new JsArray <node>()
                    {
                        list, value
                    }, null));
                }
                else if (mode == "INSERT")
                {
                    return(new fcall_node(this, intern("lists_insert_random_item"), new JsArray <node>()
                    {
                        list, value
                    }, null));
                }
            }
            throw new Exception("Unhandled combination (lists_setIndex).");
        }
コード例 #2
0
ファイル: Lists.cs プロジェクト: h7ga40/BlockFactory
        public node lists_getIndex(ListsGetIndexBlock block)
        {
            // Get element at index.
            var mode = block.getFieldValue("MODE");

            if (String.IsNullOrEmpty(mode))
            {
                mode = "GET";
            }
            var where = block.getFieldValue("WHERE");
            if (String.IsNullOrEmpty(where))
            {
                where = "FROM_START";
            }
            var at = valueToCode(block, "AT");

            if (at == null)
            {
                at = new int_node(this, 1);
            }
            var list = valueToCode(block, "VALUE");

            if (list == null)
            {
                list = new array_node(this, new JsArray <node>());
            }

            if (where == "FIRST")
            {
                if (mode == "GET")
                {
                    return(new call_node(this, list, intern("first")));
                }
                else
                {
                    if (mode == "GET_REMOVE")
                    {
                        return(new call_node(this, list, intern("shift")));
                    }
                    else if (mode == "REMOVE")
                    {
                        return(new call_node(this, list, intern("shift")));
                    }
                }
            }
            else if (where == "LAST")
            {
                if (mode == "GET")
                {
                    return(new call_node(this, list, intern("last")));
                }
                else
                {
                    var code = list + ".pop";
                    if (mode == "GET_REMOVE")
                    {
                        return(new call_node(this, list, intern("pop")));
                    }
                    else if (mode == "REMOVE")
                    {
                        return(new call_node(this, list, intern("pop")));
                    }
                }
            }
            else if (where == "FROM_START")
            {
                // Blockly uses one-based indicies.
                if (at is int_node)
                {
                    // If the index is a naked number, decrement it right now.
                    at = new int_node(this, (int)(((int_node)at).to_i() - 1));
                }
                else
                {
                    // If the index is dynamic, decrement it in code.
                    at = new begin_node(this, new call_node(this, at, intern("-"), new int_node(this, 1)), true);
                    at = new call_node(this, at, intern("to_i"));
                }
                if (mode == "GET")
                {
                    return(new call_node(this, list, intern("[]"), new JsArray <node>()
                    {
                        at
                    }, null));
                }
                else if (mode == "GET_REMOVE")
                {
                    return(new call_node(this, list, intern("delete_at"), new JsArray <node>()
                    {
                        at
                    }, null));
                }
                else if (mode == "REMOVE")
                {
                    return(new call_node(this, list, intern("delete_at"), new JsArray <node>()
                    {
                        at
                    }, null));
                }
            }
            else if (where == "FROM_END")
            {
                at = new call_node(this, at, intern("-@"), (node)null);
                if (mode == "GET")
                {
                    return(new call_node(this, list, intern("[]"), new JsArray <node>()
                    {
                        at
                    }, null));
                }
                else if (mode == "GET_REMOVE")
                {
                    return(new call_node(this, list, intern("delete_at"), new JsArray <node>()
                    {
                        at
                    }, null));
                }
                else if (mode == "REMOVE")
                {
                    return(new call_node(this, list, intern("delete_at"), new JsArray <node>()
                    {
                        at
                    }, null));
                }
            }
            else if (where == "RANDOM")
            {
                if (mode == "GET")
                {
                    return(new fcall_node(this, intern("lists_random_item"), new JsArray <node>()
                    {
                        list
                    }, null));
                }
                else
                {
                    if (mode == "GET_REMOVE")
                    {
                        return(new fcall_node(this, intern("lists_remove_random_item"), new JsArray <node>()
                        {
                            list
                        }, null));
                    }
                    else if (mode == "REMOVE")
                    {
                        return(new fcall_node(this, intern("lists_remove_random_item"), new JsArray <node>()
                        {
                            list
                        }, null));
                    }
                }
            }
            throw new Exception("Unhandled combination (lists_getIndex).");
        }