Example #1
0
        public node math_random_int(MathRandomIntBlock block)
        {
            // Random integer between [X] and [Y].
            var argument0 = valueToCode(block, "FROM");

            if (argument0 == null)
            {
                argument0 = new int_node(this, 0);
            }
            var argument1 = valueToCode(block, "TO");

            if (argument1 == null)
            {
                argument1 = new int_node(this, 0);
            }
            var code = new dot2_node(this, argument0, argument1);

            return(new fcall_node(this, intern("rand"), new JsArray <node>()
            {
                code
            }, null));
        }
Example #2
0
        public node text_getSubstring(TextGetSubstringBlock block)
        {
            // Get substring.
            var text = valueToCode(block, "STRING");

            if (text == null)
            {
                text = new str_node(this, "");
            }
            var where1 = block.getFieldValue("WHERE1");
            var where2 = block.getFieldValue("WHERE2");
            var at1    = valueToCode(block, "AT1");

            if (at1 == null)
            {
                at1 = new int_node(this, 1);
            }
            var at2 = valueToCode(block, "AT2");

            if (at2 == null)
            {
                at2 = new int_node(this, 1);
            }
            if (where1 == "FIRST" || (where1 == "FROM_START" && at1 is int_node && ((int_node)at1).to_i() == 1))
            {
                at1 = new int_node(this, 0);
            }
            else if (where1 == "FROM_START")
            {
                // Blockly uses one-based indicies.
                if (at1 is int_node)
                {
                    // If the index is a naked number, decrement it right now.
                    at1 = new int_node(this, (int)(((int_node)at1).to_i() - 1));
                }
                else
                {
                    // If the index is dynamic, decrement it in code.
                    at1 = new call_node(this, at1, intern("to_i"));
                    at1 = new call_node(this, at1, intern("-"), new int_node(this, 1));
                }
            }
            else if (where1 == "FROM_END")
            {
                if (at1 is int_node)
                {
                    at1 = new int_node(this, (int)(-((int_node)at1).to_i()));
                }
                else
                {
                    at1 = new call_node(this, at1, intern("-@"), (node)null);
                    at1 = new call_node(this, at1, intern("to_i"));
                }
            }
            if (where2 == "LAST" || (where2 == "FROM_END" && at2 is int_node && ((int_node)at2).to_i() == 1))
            {
                at2 = new int_node(this, -1);
            }
            else if (where2 == "FROM_START")
            {
                if (at2 is int_node)
                {
                    at2 = new int_node(this, (int)(((int_node)at2).to_i() - 1));
                }
                else
                {
                    at2 = new call_node(this, at2, intern("to_i"));
                    at2 = new call_node(this, at2, intern("-"), new int_node(this, 1));
                }
            }
            else if (where2 == "FROM_END")
            {
                if (at2 is int_node)
                {
                    at2 = new int_node(this, (int)(-((int_node)at2).to_i()));
                }
                else
                {
                    at2 = new call_node(this, at2, intern("-@"), (node)null);
                    at2 = new call_node(this, at2, intern("to_i"));
                }
            }
            var code = new dot2_node(this, at1, at2);

            return(new call_node(this, text, intern("[]"), new JsArray <node>()
            {
                code
            }, null));
        }