Author: dda
Date: 2007-12-20 23:43:35 -0800 (Thu, 20 Dec 2007)
New Revision: 7653

Added:
   openlaszlo/branches/devildog/test/optargs.lzx
Modified:
   
openlaszlo/branches/devildog/WEB-INF/lps/server/sc/src/org/openlaszlo/sc/Parser.jjt
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CodeGenerator.java
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CommonGenerator.java
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/Compiler.java
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/ParseTreePrinter.java
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/SWF9Generator.java
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/VariableAnalyzer.java
Log:
Change 20071221-dda-4 by [EMAIL PROTECTED] on 2007-12-21 02:15:48 EST
    in /Users/dda/laszlo/src/svn/openlaszlo/branches/devildog
    for http://svn.openlaszlo.org/openlaszlo/branches/devildog

Summary: Added support for initializers in SWF9

New Features:

Bugs Fixed: LPP-5308

Technical Reviewer: ptw (pending)
QA Reviewer: hminsky (pending)
Doc Reviewer: (pending)

Documentation:

Release Notes:

Details:
 - Added grammar to recognize formal parameter initializers,
   param = expression.

 - initializers are checked to see that they are null (a current
   restriction) if not, a warning is issued.

 - For all runtimes but SWF9, the initializers are removed.
   Eventually that behavior could change for all runtimes
   (no removal of initializers) and with more work, we could
   fully support them.

 - changed the unparser to support printing the initializers
   (not as pretty as it should be).

 - removed some unused code in CodeGenerator, JavascriptGenerator.

Tests:
 - smokecheck SWF8/DHMTL
 - added test/optargs.lzx to test all cases, this is
   now accepted by SWF8/DHTML.  Gives reasonable results
   in DHTML.  I believe more work may be needed in SWF8
   to support this.
 - temporarily copied the functions from optargs.lzx to
   LzNode to demonstrate it working for SWF9.
   



Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/sc/src/org/openlaszlo/sc/Parser.jjt
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/sc/src/org/openlaszlo/sc/Parser.jjt
 2007-12-21 07:28:12 UTC (rev 7652)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/sc/src/org/openlaszlo/sc/Parser.jjt
 2007-12-21 07:43:35 UTC (rev 7653)
@@ -878,8 +878,14 @@
 
 void FormalParameters() : {}
 {
-    FormalParameter() (LOOKAHEAD(2) "," FormalParameter())* ["," 
FormalRestParameter()]
-|   FormalRestParameter()
+    FormalRestParameter()
+|   LOOKAHEAD(FormalParameter() ("," | ")")) FormalParameter()
+      (LOOKAHEAD("," FormalParameter() ("," | ")")) "," FormalParameter())*
+      (LOOKAHEAD("," FormalInitParameter()) "," FormalInitParameter())*
+      ["," FormalRestParameter()]
+|   FormalInitParameter()
+      (LOOKAHEAD("," FormalInitParameter()) "," FormalInitParameter())*
+      ["," FormalRestParameter()]
 }
 
 void FormalRestParameter() #void : {ASTIdentifier id;}
@@ -892,6 +898,16 @@
     id = FormalParameterIdentifier()  { id.setEllipsis(false); }
 }
 
+void FormalInitParameter() #void : {}
+{
+    FormalParameter() FormalInitializer()
+}
+
+void FormalInitializer() #FormalInitializer : {}
+{
+     "=" AssignmentExpression()
+}
+
 ASTIdentifier FormalParameterIdentifier() #void : {ASTIdentifier id; 
ASTIdentifier.Type type;}
 {
     (id = Identifier() [":" type = TypeIdentifier() { id.setType(type); }])

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CodeGenerator.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CodeGenerator.java
    2007-12-21 07:28:12 UTC (rev 7652)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CodeGenerator.java
    2007-12-21 07:43:35 UTC (rev 7653)
@@ -1758,11 +1758,6 @@
     // global name (this allows us to name closures more
     // mnemonically at runtime
     String meterFunctionName = functionName;
-    Set pnames = new LinkedHashSet();
-    SimpleNode[] paramIds = params.getChildren();
-    for (int i = 0, len = paramIds.length; i < len; i++) {
-      pnames.add(((ASTIdentifier)paramIds[i]).getName());
-    }
     // Pull all the pragmas from the beginning of the
     // statement list: process them, and remove them
     assert stmts instanceof ASTStatementList;
@@ -1900,6 +1895,7 @@
                            " unused in " + filename + "(" + lineno + ")");
       }
     }
+    translateFormalParameters(params);
     // auto-declared locals
     Set auto = new LinkedHashSet(Instructions.Register.AUTO_REG);
     auto.retainAll(used.keySet());

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CommonGenerator.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CommonGenerator.java
  2007-12-21 07:28:12 UTC (rev 7652)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CommonGenerator.java
  2007-12-21 07:43:35 UTC (rev 7653)
@@ -89,6 +89,10 @@
   boolean debugVisit = false;
   InstructionCollector collector = null;
 
+  public CommonGenerator() {
+    setGeneratorOptions();
+  }
+
   public Compiler.OptionMap getOptions() {
     return options;
   }
@@ -103,8 +107,14 @@
     this.options = options;
     this.runtime = ((String)options.get(Compiler.RUNTIME)).intern();
     setRuntime(this.runtime);
+    setGeneratorOptions();
   }
 
+  // does nothing here - may be overridden to set options
+  public void setGeneratorOptions() {
+  }
+
+
   // Give the generators an option to save the original
   // input source for debugging.
 
@@ -186,6 +196,14 @@
     }
   }
 
+  //skip any passthrough nodes
+  SimpleNode passThrough(SimpleNode node)
+  {
+    if (node instanceof Compiler.PassThroughNode)
+      node = ((Compiler.PassThroughNode)node).realNode;
+    return node;
+  }
+
   Boolean evaluateCompileTimeConditional(SimpleNode node) {
     Object value = null;
     if (node instanceof ASTIdentifier) {
@@ -461,6 +479,40 @@
     }
   }
 
+  public void translateFormalParameters(SimpleNode params)
+  {
+    // Warn about any formal param initializers that are not '= null'
+    int paramCount = 0;
+    for (int i = 0, len = params.size(); i < len; i++) {
+      SimpleNode param = passThrough(params.get(i));
+      SimpleNode child;
+      if (param instanceof ASTIdentifier) {
+        paramCount++;
+      }
+      else if (!(param instanceof ASTFormalInitializer) ||
+               param.size() != 1 ||
+               !((child = param.getChildren()[0]) instanceof ASTLiteral) ||
+               ((ASTLiteral)child).getValue() != null) {
+        System.err.println("Warning: parameter initializer must be null" +
+                           " in " + param.filename + 
+                           " (" + param.beginLine + ")");
+      }
+    }
+
+    if (!options.getBoolean(Compiler.PASSTHROUGH_FORMAL_INITIALIZERS)) {
+      // Remove any initializers seen.
+      SimpleNode[] newParams = new SimpleNode[paramCount];
+      paramCount = 0;
+      for (int i = 0, len = params.size(); i < len; i++) {
+        SimpleNode param = passThrough(params.get(i));
+        if (param instanceof ASTIdentifier) {
+          newParams[paramCount++] = param;
+        }
+      }
+      params.setChildren(newParams);
+    }
+  }
+
   //
   // Statements
   //

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/Compiler.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/Compiler.java
 2007-12-21 07:28:12 UTC (rev 7652)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/Compiler.java
 2007-12-21 07:43:35 UTC (rev 7653)
@@ -418,6 +418,7 @@
   public static String METHOD_NAME = "methodName";
   public static String NAME_FUNCTIONS = "nameFunctions";
   public static String OBFUSCATE = "obfuscate";
+  public static String PASSTHROUGH_FORMAL_INITIALIZERS = 
"passthroughFormalInitializers";
   public static String PROFILE = "profile";
   public static String PROFILE_COMPILER = "profileCompiler";
   public static String PROGRESS = "progress";

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
      2007-12-21 07:28:12 UTC (rev 7652)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
      2007-12-21 07:43:35 UTC (rev 7653)
@@ -1134,11 +1134,7 @@
     // global name (this allows us to name closures more
     // mnemonically at runtime
     String meterFunctionName = useName ? functionName : null;
-    Set pnames = new LinkedHashSet();
     SimpleNode[] paramIds = params.getChildren();
-    for (int i = 0, len = paramIds.length; i < len; i++) {
-      pnames.add(((ASTIdentifier)paramIds[i]).getName());
-    }
     // Pull all the pragmas from the beginning of the
     // statement list: process them, and remove them
     assert stmts instanceof ASTStatementList;
@@ -1336,12 +1332,14 @@
 
     // Replace params
     for (int i = 0, len = paramIds.length; i < len; i++) {
-      ASTIdentifier oldParam = (ASTIdentifier)paramIds[i];
-      SimpleNode newParam = translateReference(oldParam).declare();
-      params.set(i, newParam);
+      if (paramIds[i] instanceof ASTIdentifier) {
+        ASTIdentifier oldParam = (ASTIdentifier)paramIds[i];
+        SimpleNode newParam = translateReference(oldParam).declare();
+        params.set(i, newParam);
+      }
     }
+    translateFormalParameters(params);
 
-
     List newBody = new ArrayList();
 
     int activationObjectSize = 0;

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/ParseTreePrinter.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/ParseTreePrinter.java
 2007-12-21 07:28:12 UTC (rev 7652)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/ParseTreePrinter.java
 2007-12-21 07:43:35 UTC (rev 7653)
@@ -279,10 +279,12 @@
       return lnum(node, visitBinaryExpressionSequence(node, children));
     }
     if (node instanceof ASTExpressionList ||
-        node instanceof ASTFunctionCallParameters ||
-        node instanceof ASTFormalParameterList) {
+        node instanceof ASTFunctionCallParameters) {
       return lnum(node, visitExpressionList(node, children));
     }
+    if (node instanceof ASTFormalParameterList) {
+      return lnum(node, visitFormalParameterList(node, children));
+    }
     if (node instanceof ASTAndExpressionSequence) {
       return lnum(node, visitAndOrExpressionSequence(true, node, children));
     }
@@ -334,6 +336,9 @@
     if (node instanceof ASTModifiedDefinition) {
       return lnum(node, visitModifiedDefinition(node, children));
     }
+    if (node instanceof ASTFormalInitializer) {
+      return lnum(node, visitFormalInitializer(node, children));
+    }
     return lnum(node, defaultVisitor(node, children));
   }
   
@@ -633,6 +638,20 @@
     return join(COMMA, children);
   }
   
+  public String visitFormalParameterList(SimpleNode node, String[] children) {
+    int thisPrec = prec(Ops.COMMA, false);
+    // TODO: [2007-12-21 dda] FormalInitializer should be a child of identifier
+    // to help fix this oddity.
+    StringBuffer sb = new StringBuffer();
+    for (int i = 0; i < children.length; i++) {
+      if (i > 0 && !unannotate(children[i]).startsWith(ASSIGN)) {
+        sb.append(COMMA);
+      }
+      sb.append(children[i]);
+    }
+    return sb.toString();
+  }
+  
   public String visitBinaryExpressionSequence(SimpleNode node, String[] 
children) {
     int thisPrec = prec(((ASTOperator)node.get(1)).getOperator(), false);
     for (int i = 0; i < children.length; i += (i==0?2:1)) {
@@ -661,6 +680,10 @@
     return children[0];
   }
   
+  public String visitFormalInitializer(SimpleNode node, String[] children) {
+    return ASSIGN + children[0];
+  }
+  
   public String visitFunctionDeclaration(SimpleNode node, String[] children) {
     return doFunctionDeclaration(node, children, true);
   }

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/SWF9Generator.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/SWF9Generator.java
    2007-12-21 07:28:12 UTC (rev 7652)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/SWF9Generator.java
    2007-12-21 07:43:35 UTC (rev 7653)
@@ -85,6 +85,10 @@
     return false;
   }
 
+  public void setGeneratorOptions() {
+    options.putBoolean(Compiler.PASSTHROUGH_FORMAL_INITIALIZERS, true);
+  }
+
   // override superclass method.
   // For SWF9, we need to preserve
   // the class definition, rather than converting it to

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/VariableAnalyzer.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/VariableAnalyzer.java
 2007-12-21 07:28:12 UTC (rev 7652)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/VariableAnalyzer.java
 2007-12-21 07:43:35 UTC (rev 7653)
@@ -1,7 +1,7 @@
 /* -*- mode: Java; c-basic-offset: 2; -*- */
 
 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
-* Copyright 2001-2004 Laszlo Systems, Inc.  All Rights Reserved.              *
+* Copyright 2001-2007 Laszlo Systems, Inc.  All Rights Reserved.              *
 * Use is subject to license terms.                                            *
 * J_LZ_COPYRIGHT_END *********************************************************/
 
@@ -35,7 +35,13 @@
     // Parameter order is significant
     this.parameters = new LinkedHashSet();
     for (int i = 0, len = params.size(); i < len; i++) {
-      parameters.add(((ASTIdentifier)params.get(i)).getName());
+      SimpleNode param = params.get(i);
+      // might also be an initializer
+      // TODO: [2007-12-20 dda] if it is an default parameter initializer,
+      // should call visit() on the initializer expression?
+      if (param instanceof ASTIdentifier) {
+        parameters.add(((ASTIdentifier)param).getName());
+      }
     }
     this.locals = new LinkedHashSet(parameters);
     closed = new HashSet();

Added: openlaszlo/branches/devildog/test/optargs.lzx


Property changes on: openlaszlo/branches/devildog/test/optargs.lzx
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:eol-style
   + native


_______________________________________________
Laszlo-checkins mailing list
[email protected]
http://www.openlaszlo.org/mailman/listinfo/laszlo-checkins

Reply via email to