[vala/wip/transform: 81/97] Use %? in the parser rather than stringifying expressions



commit fb06fdfe55c9cf6023cffa71316ae58f156c3e4f
Author: Luca Bruno <luca bruno immobiliare it>
Date:   Mon Feb 3 14:30:02 2014 +0100

    Use %? in the parser rather than stringifying expressions

 codegen/valaccodetransformer.vala    | 26 ++++++++++++--------------
 codegen/valagvarianttransformer.vala |  4 ++--
 vala/valacodebuilder.vala            |  8 ++++----
 vala/valacodetransformer.vala        |  8 ++++----
 vala/valaparser.vala                 | 23 ++++++++++++++++++++---
 5 files changed, 42 insertions(+), 27 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index 18f64e8e7..d1f3e095e 100644
--- a/codegen/valaccodetransformer.vala
+++ b/codegen/valaccodetransformer.vala
@@ -174,7 +174,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                if (!always_false (stmt.condition)) {
                        b.open_loop ();
                        if (!always_true (stmt.condition)) {
-                               var cond = expression (@"!$(stmt.condition)");
+                               var cond = expression ("!%?", {stmt.condition});
                                b.open_if (cond);
                                b.add_break ();
                                b.close ();
@@ -231,7 +231,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                        b.close ();
 
                        if (stmt.condition != null && !always_true (stmt.condition)) {
-                               statements (@"if (!$(stmt.condition)) break;");
+                               statements ("if (!%?) break;", {stmt.condition});
                        }
                        b.add_statement (stmt.body);
 
@@ -378,11 +378,11 @@ public class Vala.CCodeTransformer : CodeTransformer {
                begin_replace_expression (expr);
 
                var result = b.add_temp_declaration (expr.value_type);
-               statements (@"if ($(expr.condition)) {
-                                       $result = $(expr.true_expression);
+               statements (@"if (%?) {
+                                       $result = %?;
                                        } else {
-                                       $result = $(expr.false_expression);
-                                       }");
+                                       $result = %?;
+                                       }", {expr.condition, expr.true_expression, expr.false_expression});
 
                replacement = return_temp_access (result, expr.value_type, target_type, formal_target_type);
                end_replace_expression (replacement);
@@ -407,11 +407,11 @@ public class Vala.CCodeTransformer : CodeTransformer {
                        if (is_and) {
                                b.add_assignment (expression (result), expr.right);
                        } else {
-                               b.add_expression (expression (@"$result = true"));
+                               statements (@"$result = true;");
                        }
                        b.add_else ();
                        if (is_and) {
-                               b.add_expression (expression (@"$result = false"));
+                               statements (@"$result = false;");
                        } else {
                                b.add_assignment (expression (result), expr.right);
                        }
@@ -420,9 +420,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                } else if (expr.operator == BinaryOperator.COALESCE) {
                        var result = b.add_temp_declaration (copy_type (expr.value_type), expr.left);
 
-                       b.open_if (expression (@"$result == null"));
-                       b.add_assignment (expression (result), expr.right);
-                       b.close ();
+                       statements (@"if ($result == null) { $result = %?; }", {expr.right});
 
                        replacement = return_temp_access (result, expr.value_type, target_type);
                } else if (expr.operator == BinaryOperator.IN && !(expr.left.value_type.compatible 
(context.analyzer.int_type) && expr.right.value_type.compatible (context.analyzer.int_type)) && 
!(expr.right.value_type is ArrayType)) {
@@ -494,7 +492,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                if (expr.value_type.data_type != null && expr.value_type.data_type.is_subtype_of 
(context.analyzer.string_type.data_type)) {
                        return expr;
                } else {
-                       return expression (@"$expr.to_string ()");
+                       return expression (@"%?.to_string ()", {expr});
                }
        }
 
@@ -509,7 +507,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                } else {
                        replacement = stringify (expression_list[0]);
                        if (expression_list.size > 1) {
-                               var concat = (MethodCall) expression (@"$replacement.concat()");
+                               var concat = (MethodCall) expression ("%?.concat()", {replacement});
                                for (int i = 1; i < expression_list.size; i++) {
                                        concat.add_argument (stringify (expression_list[i]));
                                }
@@ -526,7 +524,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
 
                var result = b.add_temp_declaration (copy_type (expr.value_type), expr.inner);
                var op = expr.increment ? "+ 1" : "- 1";
-               b.add_expression (expression (@"$(expr.inner) = $result $op"));
+               statements (@"$(expr.inner) = $result $op;");
 
                var replacement = return_temp_access (result, expr.value_type, expr.target_type);
 
diff --git a/codegen/valagvarianttransformer.vala b/codegen/valagvarianttransformer.vala
index 6a77ea22d..30fe2be7d 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -367,7 +367,7 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                        m.add_parameter (new Parameter ("variant", data_type ("GLib.Variant", false), 
b.source_reference));
                        push_builder (new CodeBuilder.for_subroutine (m));
 
-                       var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter"), expression 
(@"variant.iterator ()"));
+                       var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter"), expression 
("variant.iterator ()"));
 
                        var result = b.add_temp_declaration (type, expression ("{}"));
 
@@ -397,7 +397,7 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                        m.add_parameter (new Parameter ("variant", data_type ("GLib.Variant", false), 
b.source_reference));
                        push_builder (new CodeBuilder.for_subroutine (m));
 
-                       var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter"), expression 
(@"variant.iterator ()"));
+                       var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter"), expression 
("variant.iterator ()"));
 
                        var type_args = type.get_type_arguments ();
                        assert (type_args.size == 2);
diff --git a/vala/valacodebuilder.vala b/vala/valacodebuilder.vala
index 036303cb9..a79abd53f 100644
--- a/vala/valacodebuilder.vala
+++ b/vala/valacodebuilder.vala
@@ -268,12 +268,12 @@ public class Vala.CodeBuilder {
 
        /* Utilities for building the code */
 
-       public Expression expression (string str) {
-               return new Parser().parse_expression_string (str, source_reference);
+       public Expression expression (string str, owned Expression[]? replacements = null) {
+               return new Parser().parse_expression_string (str, (owned) replacements, source_reference);
        }
 
-       public void statements (string str) {
-               new Parser().parse_statements_string (str, current_block, source_reference);
+       public void statements (string str, owned Expression[]? replacements = null) {
+               new Parser().parse_statements_string (str, current_block, (owned) replacements, 
source_reference);
        }
 
        // only qualified types, will slightly simplify the work of SymbolResolver
diff --git a/vala/valacodetransformer.vala b/vala/valacodetransformer.vala
index f6444b8e9..0f5333dcf 100644
--- a/vala/valacodetransformer.vala
+++ b/vala/valacodetransformer.vala
@@ -180,12 +180,12 @@ public class Vala.CodeTransformer : CodeVisitor {
                return CodeBuilder.data_type (s, value_owned, nullable);
        }
 
-       public Expression expression (string str) {
-               return b.expression (str);
+       public Expression expression (string str, Expression[]? replacements = null) {
+               return b.expression (str, replacements);
        }
 
-       public void statements (string str) {
-               b.statements (str);
+       public void statements (string str, owned Expression[]? replacements = null) {
+               b.statements (str, (owned) replacements);
        }
 
        public void check (CodeNode node) {
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 5718201dc..a32961a62 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -31,6 +31,8 @@ public class Vala.Parser : CodeVisitor {
        CodeContext context;
        bool compiler_code = false;
        SourceReference? from_string_reference = null;
+       Expression[]? replacements = null;
+       int replacement_index = 0;
 
        // token buffer
        TokenInfo[] tokens;
@@ -337,10 +339,12 @@ public class Vala.Parser : CodeVisitor {
                }
        }
 
-       public Expression? parse_expression_string (string str, SourceReference source_reference) {
+       public Expression? parse_expression_string (string str, owned Expression[]? replacements, 
SourceReference source_reference) {
                compiler_code = true;
                context = source_reference.file.context;
                from_string_reference = source_reference;
+               this.replacements = (owned) replacements;
+               replacement_index = 0;
 
                scanner = new Scanner.from_string (str, source_reference.file);
                index = -1;
@@ -358,10 +362,12 @@ public class Vala.Parser : CodeVisitor {
                return null;
        }
 
-       public void parse_statements_string (string str, Block block, SourceReference source_reference) {
+       public void parse_statements_string (string str, Block block, owned Expression[]? replacements, 
SourceReference source_reference) {
                compiler_code = true;
                context = source_reference.file.context;
                from_string_reference = source_reference;
+               this.replacements = (owned) replacements;
+               replacement_index = 0;
 
                scanner = new Scanner.from_string (str, source_reference.file);
                index = -1;
@@ -704,7 +710,18 @@ public class Vala.Parser : CodeVisitor {
                        expr = parse_typeof_expression ();
                        break;
                default:
-                       expr = parse_simple_name ();
+                       if (compiler_code && current () == TokenType.PERCENT) {
+                               var begin2 = get_location ();
+                               next ();
+                               if (accept (TokenType.INTERR)) {
+                                       expr = replacements[replacement_index++];
+                               } else {
+                                       rollback (begin2);
+                                       expr = parse_simple_name ();
+                               }
+                       } else {
+                               expr = parse_simple_name ();
+                       }
                        break;
                }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]