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



commit cbf8c7a1fd9dc5ebabd682dfdfdd239b4fc13a97
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                 |   25 +++++++++++++++++++++----
 5 files changed, 43 insertions(+), 28 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index cc9d5bb..a0f21d6 100644
--- a/codegen/valaccodetransformer.vala
+++ b/codegen/valaccodetransformer.vala
@@ -170,7 +170,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 ();
@@ -227,7 +227,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);
 
@@ -374,11 +374,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);
@@ -403,11 +403,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);
                        }
@@ -416,9 +416,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)) {
@@ -490,7 +488,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});
                }
        }
 
@@ -505,7 +503,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]));
                                }
@@ -522,7 +520,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 7cc974b..b3af954 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -365,7 +365,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 ("{}"));
 
@@ -395,7 +395,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 d7c63b8..3b81f28 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 f0c3197..9819ea5 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, Expression[]? replacements = null) {
+               b.statements (str, replacements);
        }
 
        public void check (CodeNode node) {
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 80db849..a640281 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -31,7 +31,9 @@ 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;
        // index of current token in buffer
@@ -336,10 +338,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;
@@ -357,10 +361,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;
@@ -697,7 +703,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]