Пример #1
0
        /// <summary>
        /// Simplify {x "str".f( y} into {$temp0 = "str"; x $temp0.f ( y}
        /// </summary>
        static void simplify_string_method_call(LinkedListNode <TokenList> node, int pattern_start_index)
        {
            var old_code        = node.Value;
            int string_location = pattern_start_index;

            // new code:
            // $temp0 = str
            var temp_var = Token.create_system_var(TempVar.get_var_name(old_code.indentation));

            var str_assignment = new TokenList(old_code.line_number, old_code.indentation);

            str_assignment.add_token(temp_var);
            str_assignment.add_token(Token.Constants.Assign);
            str_assignment.add_token(old_code[string_location]);

            // x $temp0.f (
            // The new method_call, "$temp0.f", is a single token.
            string method_call = temp_var.value.ToString() + '.' + old_code[string_location + 2].value.ToString();

            var new_code = new TokenList(old_code.line_number, old_code.indentation);

            new_code.add_token_list(old_code, 0, string_location - 1);
            new_code.add_token(Token.create_system_var(method_call));
            new_code.add_token_list(old_code, string_location + 3);

            // insert new code into the LinkedList
            node.List.AddBefore(node, str_assignment);
            node.Value = new_code;
        }
Пример #2
0
        /// <summary>
        /// Simplify "for x in y:" into "$temp0 = y", and "for x in $temp0:".
        /// This applies only if y is two or more tokens.
        /// </summary>
        static void simplify_for_loop_iterator(LinkedListNode <TokenList> node, int pattern_start_index)
        {
            var old_code = node.Value;

            // find the extent of y, the iterator variable
            int iterator_start = old_code.find_token(TokenType.In, 0) + 1;
            int iterator_end   = old_code.find_token_outside_of_paren(
                TokenType.Colon, iterator_start + 1, old_code.Count - 1) - 1;

            // new code:
            // $temp0 = y
            var iterator_assign = new TokenList(old_code.line_number, old_code.indentation);
            var temp_var        = Token.create_system_var(TempVar.get_var_name(old_code.indentation));

            iterator_assign.add_token(temp_var);
            iterator_assign.add_token(Token.Constants.Assign);
            iterator_assign.add_token_list(old_code, iterator_start, iterator_end);

            // for x in $temp0:
            var new_code = new TokenList(old_code.line_number, old_code.indentation);

            new_code.add_token_list(old_code, 0, iterator_start - 1);
            new_code.add_token(temp_var);
            new_code.add_token_list(old_code, iterator_end + 1);

            // insert new code into the LinkedList
            var iterator_assign_node = node.List.AddBefore(node, iterator_assign);

            node.Value = new_code;

            // The caller, which is check(...), will repeat its check on the current
            // node (which is new_code). This function needs to call check(...)
            // on any other newly generated node.
            check(iterator_assign_node);
        }
Пример #3
0
        /// <summary>
        /// Simplify {x s[...] . f( y} into {$temp0 = s[...]; x $temp0.f ( y}
        /// </summary>
        static void simplify_slice_method_call(LinkedListNode <TokenList> node, int pattern_start_index)
        {
            var old_code    = node.Value;
            int slice_start = pattern_start_index;
            int slice_end   = old_code.find_ending_token(slice_start + 1);

            // keep searching for more '[', so to handle "x[:][0:3]" situation
            while (slice_end < old_code.Count - 1 &&
                   old_code[slice_end + 1].type == TokenType.Left_Bracket)
            {
                slice_end = old_code.find_ending_token(slice_end + 1);
            }

            // new code:
            // $temp0 = s[...]
            var temp_var = Token.create_system_var(TempVar.get_var_name(old_code.indentation));

            var slice_assignment = new TokenList(old_code.line_number, old_code.indentation);

            slice_assignment.add_token(temp_var);
            slice_assignment.add_token(Token.Constants.Assign);
            for (int i = slice_start; i <= slice_end; i++)
            {
                slice_assignment.add_token(old_code[i]);
            }

            // x $temp0.f (
            // The new method_call, "$temp0.f", is a single token.
            string method_call = temp_var.value.ToString() + '.' + old_code[slice_end + 2].value.ToString();

            var new_code = new TokenList(old_code.line_number, old_code.indentation);

            new_code.add_token_list(old_code, 0, slice_start - 1);
            new_code.add_token(Token.create_system_var(method_call));
            new_code.add_token_list(old_code, slice_end + 3);

            // insert new code into the LinkedList
            var slice_assignment_node = node.List.AddBefore(node, slice_assignment);

            node.Value = new_code;

            // The caller, which is check(...), will repeat its check on the current
            // node (which is new_code). This function needs to call check(...)
            // on any other newly generated node.
            check(slice_assignment_node);
        }
Пример #4
0
        /// <summary>
        /// Simplify "x f() y" into "f()", "$temp0 = $return", and "x $temp0 y"
        /// </summary>
        static void simplify_function(LinkedListNode <TokenList> node, int pattern_start_index)
        {
            var old_code       = node.Value;
            int function_start = pattern_start_index + 1;

            // new code:
            // f()
            var f_call = new TokenList(old_code.line_number, old_code.indentation);

            int function_end = old_code.find_ending_token(function_start + 1);

            if (function_end < function_start)
            {
                throw new Exception("Source code error on line # "
                                    + (old_code.line_number + 1) + ". The closing ')' cannot be found.");
            }

            f_call.add_token_list(old_code, function_start, function_end);

            // $temp0 = $return
            var return_assignment = new TokenList(old_code.line_number, old_code.indentation);
            var temp_var          = Token.create_system_var(TempVar.get_var_name(old_code.indentation));

            return_assignment.add_token(temp_var);
            return_assignment.add_token(Token.Constants.Assign);
            return_assignment.add_token(Token.create_system_var("$return"));

            // x $temp0 y
            var new_code = new TokenList(old_code.line_number, old_code.indentation);

            new_code.add_token_list(old_code, 0, function_start - 1);
            new_code.add_token(temp_var);
            new_code.add_token_list(old_code, function_end + 1);

            // insert new code into the LinkedList
            var f_call_node = node.List.AddBefore(node, f_call);

            node.List.AddBefore(node, return_assignment);
            node.Value = new_code;

            // The caller, which is check(...), will repeat its check on the current
            // node (which is new_code). This function needs to call check(...)
            // on any other newly generated node.
            check(f_call_node);
        }