Author: dda
Date: 2008-01-14 07:18:01 -0800 (Mon, 14 Jan 2008)
New Revision: 7820

Modified:
   openlaszlo/branches/devildog/WEB-INF/lps/lfc/build.xml
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CodeGenerator.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/SWF9ParseTreePrinter.java
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/TranslationUnit.java
   
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/Translator.java
Log:
Change 20080114-dda-j by [EMAIL PROTECTED] on 2008-01-14 10:08:50 EST
    in /Users/dda/laszlo/src/svn/openlaszlo/branches/devildog
    for http://svn.openlaszlo.org/openlaszlo/branches/devildog

Summary: SWF9: put top level executable statements into app constructor.

New Features:

Bugs Fixed: LPP-5234

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

Documentation:

Release Notes:

Details:

This change triggered some various needed cleanup.

SWF9Generator.java, SWF9ParseTreePrinter.java
   Removed old generation of main app/library class.

   A 'main' app class is generated automatically as a preprocessing step.

   When the main class is seen during parsing, a constructor is inserted
   with a marker for where the 'default' stuff should land.

Compiler.java: Unified calling of compiler phases as much as possible.

Compiler.java, JavascriptGenerator.java, CodeGenerator.java, SWF9Generator.java:
   added new preprocess phase to allow some special handling in SWF9.

ParseTreePrinter, SWF9ParseTreePrinter.java, JavascriptGenerator.java, 
SWF9Generator.java:
   removed 'Compiler.Options' as flag to makeTranslationUnits,
   individual options are now all passed into construtor.

SWF9Generator.java:
   nomenclature change -- fully separated the idea of 'main' class
   (LzApplication or LFCApplication) from 'default' class.  The 'default'
   class is a holder for the stuff that appears outside of any class
   during parsing.  It's true that this all eventually gets put into a
   constructor (if app) or at the top level (if shared lib) in the
   main class, but calling both classes the 'default' was confusing.

TranslationUnit.java 
   nomenclature - change variable names etc. so that is clear that
   'text' is the unprocessed string being assembled, and there is
   an explicit 'getContents()' call to get the final (processed) result.

build.xml: Cleanup old TODO


Tests:
  Built LFC, built tiny app without errors.
  Verified that top level statements are put into the right place for both.

  smokecheck SWF8, DHTML.



Modified: openlaszlo/branches/devildog/WEB-INF/lps/lfc/build.xml
===================================================================
--- openlaszlo/branches/devildog/WEB-INF/lps/lfc/build.xml      2008-01-14 
15:17:38 UTC (rev 7819)
+++ openlaszlo/branches/devildog/WEB-INF/lps/lfc/build.xml      2008-01-14 
15:18:01 UTC (rev 7820)
@@ -263,10 +263,6 @@
 
   <target name="clean" depends="init,doc.clean">
     <delete failonerror="false">
-      <!-- TODO: [2006-11-30 ptw] Remove next 3 lines after Legals Beta 1 -->
-      <fileset dir="." includes="*.lzl" />
-      <fileset dir="." includes="*.swc" />
-      <fileset dir="." includes="LFC*.js" />
       <fileset dir="${LFCdir}" includes="*" excludes="defaultpreloader.swf" />
     </delete>
   </target>

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
    2008-01-14 15:17:38 UTC (rev 7819)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/CodeGenerator.java
    2008-01-14 15:18:01 UTC (rev 7820)
@@ -91,6 +91,10 @@
     return program;
   }
 
+  public String preProcess(String source) {
+    return source;
+  }
+
   // TODO: [2007-11-20 dda] Consider supporting TranslationUnits
   // within this runtime code generator, so we can have a unified
   // way of calling all generators.

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
 2008-01-14 15:17:38 UTC (rev 7819)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/Compiler.java
 2008-01-14 15:18:01 UTC (rev 7820)
@@ -327,33 +327,36 @@
   public byte[] compile(String source) {
     try {
       Profiler profiler = new Profiler();
-      profiler.enter("parse");
-      SimpleNode program = new Parser().parse(source);
-      profiler.phase("generate");
-      Translator cg;
       byte[] bytes;
       String runtime = (String)options.get(RUNTIME);
       boolean compress = (! options.getBoolean(NAME_FUNCTIONS));
       boolean obfuscate = options.getBoolean(OBFUSCATE);
-      if (org.openlaszlo.compiler.Compiler.SCRIPT_RUNTIMES.contains(runtime)) {
+      boolean isScript = 
org.openlaszlo.compiler.Compiler.SCRIPT_RUNTIMES.contains(runtime);
+      Translator cg;
+      if (runtime.equals("swf9")) {
+        cg = new SWF9Generator();
+      }
+      else if (isScript) {
+        cg = new JavascriptGenerator();
+      }
+      else {
+        cg = new CodeGenerator();
+      }
+      cg.setOptions(options);
+      cg.setOriginalSource(source);
 
-        if ((runtime).equals("swf9")) {
-          cg = new SWF9Generator();
-        }
-        else {
-          cg = new JavascriptGenerator();
-        }
-        cg.setOptions(options);
-        cg.setOriginalSource(source);
-        SimpleNode translated = cg.translate(program);
+      profiler.enter("parse");
+      source = cg.preProcess(source);
+      SimpleNode program = new Parser().parse(source);
+      profiler.phase("generate");
+      SimpleNode translated = cg.translate(program);
+
+      if (isScript) {
+
         List tunits = cg.makeTranslationUnits(translated, compress, obfuscate);
         bytes = cg.postProcess(tunits);
 
       } else {
-        cg = new CodeGenerator();
-        cg.setOptions(options);
-        cg.setOriginalSource(source);
-        cg.translate(program);
         if (options.getBoolean(PROGRESS)) {
           System.err.println("Assembling...");
         }

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
      2008-01-14 15:17:38 UTC (rev 7819)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
      2008-01-14 15:18:01 UTC (rev 7820)
@@ -188,9 +188,13 @@
     // No implementation to collect stats for Javascript
   }
 
+  public String preProcess(String source) {
+    return source;
+  }
+
   public List makeTranslationUnits(SimpleNode translatedNode, boolean 
compress, boolean obfuscate)
   {
-    return (new ParseTreePrinter(compress, 
obfuscate)).makeTranslationUnits(translatedNode, options);
+    return (new ParseTreePrinter(compress, 
obfuscate)).makeTranslationUnits(translatedNode);
   }
 
   public byte[] postProcess(List tunits) {

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
 2008-01-14 15:17:38 UTC (rev 7819)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/ParseTreePrinter.java
 2008-01-14 15:18:01 UTC (rev 7820)
@@ -828,8 +828,8 @@
     return "throw" + delimit(children[0]);
   }
   
-  public List makeTranslationUnits(SimpleNode node, Compiler.OptionMap 
options) {
-    return makeTranslationUnits(visit(node), options);
+  public List makeTranslationUnits(SimpleNode node) {
+    return makeTranslationUnits(visit(node));
   }
 
   public String text(SimpleNode node) {
@@ -990,7 +990,7 @@
     return ap.process(annotated);
   }
 
-  public List makeTranslationUnits(String annotated, Compiler.OptionMap 
options) {
+  public List makeTranslationUnits(String annotated) {
     final ArrayList tunits = new ArrayList();
     final TranslationUnit defaulttu = new TranslationUnit(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
    2008-01-14 15:17:38 UTC (rev 7819)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/SWF9Generator.java
    2008-01-14 15:18:01 UTC (rev 7820)
@@ -47,10 +47,10 @@
   public static final boolean USE_COMPILER_DEBUG_FLAG = true;
 
   /** The user 'main' class, which extends LFCApplication */
-  public final static String DEFAULT_APP_CLASSNAME = "LzApplication";
+  public final static String MAIN_APP_CLASSNAME = "LzApplication";
 
   /** The LFC 'main' class, which extends nothing */
-  public final static String DEFAULT_LIB_CLASSNAME = "LFCApplication";
+  public final static String MAIN_LIB_CLASSNAME = "LFCApplication";
 
   /**
    * Saved program node, to show during debugging
@@ -70,7 +70,7 @@
   /**
    * The 'default' class is implicit and encompasses everything
    * not declared within an explicit 'class' definition.  This variable
-   * is set when we are outside of exlicit classes.
+   * is set when we are outside of explicit classes.
    *
    * Use of this variable to save state during 'visit' calls means that
    * visit methods are not reentrant.  This is not really
@@ -225,6 +225,19 @@
     return super.translate(program);
   }
 
+  public String preProcess(String source) {
+
+    // TODO: [2007-1-14 dda] maybe tag compiler should emit app main class?
+    // We need a 'main' application class to inherit from Sprite and do some
+    // initialization, and currently there is no default one.
+    // We'll add one here, doing it later adds too many special cases.
+    if (!options.getBoolean(Compiler.BUILD_SHARED_LIBRARY)) {
+      source += "public class " + SWF9Generator.MAIN_APP_CLASSNAME +
+        " extends " +  SWF9Generator.MAIN_LIB_CLASSNAME + " {}\n";
+    }
+    return source;
+  }
+
   /**
    * Intercept JavascriptGenerator version, just to
    * track that we are within a function.
@@ -749,7 +762,11 @@
 
   public List makeTranslationUnits(SimpleNode translatedNode, boolean 
compress, boolean obfuscate)
   {
-    return (new SWF9ParseTreePrinter(compress, 
obfuscate)).makeTranslationUnits(translatedNode, options);
+    boolean buildSharedLibrary = 
options.getBoolean(Compiler.BUILD_SHARED_LIBRARY);
+    String mainClass = buildSharedLibrary ?
+      SWF9Generator.MAIN_LIB_CLASSNAME : SWF9Generator.MAIN_APP_CLASSNAME;
+
+    return (new SWF9ParseTreePrinter(compress, obfuscate, mainClass, 
buildSharedLibrary)).makeTranslationUnits(translatedNode);
   }
 
   public void writeOutputFile(TranslationUnit tunit, String pre, String post) {

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/SWF9ParseTreePrinter.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/SWF9ParseTreePrinter.java
     2008-01-14 15:17:38 UTC (rev 7819)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/SWF9ParseTreePrinter.java
     2008-01-14 15:18:01 UTC (rev 7820)
@@ -40,16 +40,21 @@
   public final static int TOP_LEVEL_STREAM = 1;
 
   /** We collect certain parts of translation units
-   * to appear just inside the class declaration.
+   * into the class level, appearing inside a class.
    */
-  public final static int CLASS_BEGIN_STREAM = 2;
+  public final static int CLASS_LEVEL_STREAM = 2;
 
   /** We collect certain parts of translation units
-   * to appear at the end of the class declaration.
+   * to appear in the constructor for the main class only.
    */
-  public final static int CLASS_END_STREAM = 3;
+  public final static int MAIN_CONSTRUCTOR_STREAM = 3;
 
+  /** Main class gets default constructor inserted with initialization */
+  private String mainClassName = null;
 
+  /** True if we are creating a shared library */
+  private boolean islib;
+
   // Adjust the known operator names we output to include
   // ones that we know about.
   static {
@@ -58,20 +63,24 @@
   }
   
   public SWF9ParseTreePrinter() {
-      // never compress
-      this(false, false);
+    this(false, false, null, false);
   }
   
   public SWF9ParseTreePrinter(boolean compress) {
-      // never compress
-      super(false, false);
+    this(compress, false, null, false);
   }
   
   public SWF9ParseTreePrinter(boolean compress, boolean obfuscate) {
-      // never compress
-      super(false, false);
+    this(compress, obfuscate, null, false);
   }
 
+  public SWF9ParseTreePrinter(boolean compress, boolean obfuscate, String 
mainClassName, boolean sharedLibrary) {
+    // never compress or obfuscate
+    super(false, false);
+    this.mainClassName = mainClassName;
+    this.islib = sharedLibrary;
+  }
+
   // Override parent class.
   //
   // We need a translation unit (class) to put all the executable
@@ -80,13 +89,10 @@
   // application or the LFC, we have a 'main' class that must
   // be present to accept these statements.
   //
-  public List makeTranslationUnits(String annotated, Compiler.OptionMap 
options) {
-    List result = super.makeTranslationUnits(annotated, options);
+  public List makeTranslationUnits(String annotated) {
+    List result = super.makeTranslationUnits(annotated);
     TranslationUnit defaultTunit = null;
     TranslationUnit mainTunit = null;
-    boolean sharedLibrary = options.getBoolean(Compiler.BUILD_SHARED_LIBRARY);
-    String mainClassName = sharedLibrary ?
-      SWF9Generator.DEFAULT_LIB_CLASSNAME : 
SWF9Generator.DEFAULT_APP_CLASSNAME;
     for (Iterator iter = result.iterator(); iter.hasNext(); ) {
       TranslationUnit tunit = (TranslationUnit)iter.next();
       if (tunit.isDefaultTranslationUnit()) {
@@ -103,21 +109,17 @@
     // If the main class is not present, we'll need to revisit
     // our assumptions about where we put top level statements.
     if (mainTunit != null) {
-      mainTunit.addStreamText(CLASS_END_STREAM, defaultTunit.getContents());
+      String topstmts = defaultTunit.getContents();
+      if (islib) {
+        mainTunit.addStreamText(CLASS_LEVEL_STREAM, topstmts);
+      }
+      else {
+        mainTunit.addStreamText(MAIN_CONSTRUCTOR_STREAM, topstmts);
+      }
       mainTunit.setMainTranslationUnit(true);
     }
     else {
-      // TODO: [2008-1-11 dda] throw an error for this case, rather than fudge
-      //throw new CompilerError("Could not find application main or library 
main class");
-      System.err.println("Warning: could not find main class: " + 
mainClassName + ", creating one");
-      defaultTunit.setMainTranslationUnit(true);
-      defaultTunit.setName(mainClassName);
-      defaultTunit.addText("class " + mainClassName);
-      if (!sharedLibrary) {
-        defaultTunit.addText(" extends " + 
SWF9Generator.DEFAULT_LIB_CLASSNAME);
-      }
-      defaultTunit.addText(" { }\n");
-      result.add(defaultTunit);
+      throw new CompilerError("Could not find application main or library main 
class");
     }
 
     return result;
@@ -206,11 +208,15 @@
     // if (unannotate(children[3]).length() > 0) ....
 
     sb.append("{\n");
-    sb.append(annotateInsertStream(CLASS_BEGIN_STREAM));
+    sb.append(annotateInsertStream(CLASS_LEVEL_STREAM));
+    if (classnm.equals(mainClassName) && !islib) {
+      sb.append("public function " + mainClassName + "() {\n");
+      sb.append(annotateInsertStream(MAIN_CONSTRUCTOR_STREAM));
+      sb.append("}\n");
+    }
     for (int i=4; i<children.length; i++) {
       sb.append(children[i]);
     }
-    sb.append(annotateInsertStream(CLASS_END_STREAM));
     sb.append("}\n");
 
     return annotateClass(classnm, sb.toString());

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/TranslationUnit.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/TranslationUnit.java
  2008-01-14 15:17:38 UTC (rev 7819)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/TranslationUnit.java
  2008-01-14 15:18:01 UTC (rev 7820)
@@ -19,7 +19,7 @@
 public class TranslationUnit
 {
   private String name;                  // name of class
-  private StringBuffer contents = new StringBuffer();
+  private StringBuffer text = new StringBuffer();
   private SortedMap lnums = new TreeMap();
   private int linenum = 1;
   private String srcFilename;   // name of associated source file, if 
applicable
@@ -57,8 +57,9 @@
     this.srcFilename = srcname;
   }
 
+  /** Get text with any insertions resolved */
   public String getContents() {
-    String result = contents.toString();
+    String result = text.toString();
     for (int i=1; i<=maxInserts; i++) {
       String mark = INSERT_STREAM_MARK + i + INSERT_END_MARK;
       int markPos = result.indexOf(mark);
@@ -83,12 +84,12 @@
   }
 
   public void addText(String s) {
-    contents.append(s);
+    text.append(s);
     linenum += countOccurence(s, '\n');
   }
 
   public void addInsertStreamMarker(int streamNum) {
-    contents.append(INSERT_STREAM_MARK + streamNum + INSERT_END_MARK);
+    text.append(INSERT_STREAM_MARK + streamNum + INSERT_END_MARK);
     if (streamNum > maxInserts)
       maxInserts = streamNum;
   }
@@ -129,17 +130,17 @@
   }
 
   public String toString() {
-    String shortContents = contents.toString();
-    if (shortContents.length() > 10) {
-      shortContents = shortContents.substring(0, 10) + "...";
+    String shortText = text.toString();
+    if (shortText.length() > 10) {
+      shortText = shortText.substring(0, 10) + "...";
     }
     return "TranslationUnit[" + name + ", line " +
-      linenum + "] = \"" + shortContents + "\"";
+      linenum + "] = \"" + shortText + "\"";
   }
 
   public void dump() {
     System.out.println("TranslationUnit[" + name + ", line " + linenum + "]");
-    System.out.println("  contents=" + contents);
+    System.out.println("  text=" + text);
     System.out.println("  linemap=");
     for (Iterator iter=lnums.keySet().iterator(); iter.hasNext(); ) {
       Object key = iter.next();

Modified: 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/Translator.java
===================================================================
--- 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/Translator.java
       2008-01-14 15:17:38 UTC (rev 7819)
+++ 
openlaszlo/branches/devildog/WEB-INF/lps/server/src/org/openlaszlo/sc/Translator.java
       2008-01-14 15:18:01 UTC (rev 7820)
@@ -5,7 +5,7 @@
  */
 
 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
-* Copyright 2001-2007 Laszlo Systems, Inc.  All Rights Reserved.              *
+* Copyright 2001-2008 Laszlo Systems, Inc.  All Rights Reserved.              *
 * Use is subject to license terms.                                            *
 * J_LZ_COPYRIGHT_END *********************************************************/
 
@@ -24,6 +24,8 @@
 
   public void setOriginalSource(String source);
 
+  public String preProcess(String source);
+
   public SimpleNode translate(SimpleNode program);
 
   public InstructionCollector getCollector();


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

Reply via email to