/**
 * Copyright (c) 2009 Michael Haupt, michael.haupt@hpi.uni-potsdam.de
 * Software Architecture Group, Hasso Plattner Institute, Potsdam, Germany
 * http://www.hpi.uni-potsdam.de/swa/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package som.compiler;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;

import som.compiler.Parser.ParseError;
import som.vm.Universe;
import som.vmobjects.SClass;
import som.vmobjects.SSymbol;

import com.oracle.truffle.api.CompilerDirectives.SlowPath;
import com.oracle.truffle.api.source.Source;

public final class SourcecodeCompiler {

  private Parser parser;

  @SlowPath
  public static SClass compileClass(final String path, final String file,
      final SClass systemClass, final Universe universe)
      throws IOException {
    return new SourcecodeCompiler().compile(path, file, systemClass, universe);
  }

  @SlowPath
  public static SClass compileClass(final String stmt,
      final SClass systemClass, final Universe universe) {
    return new SourcecodeCompiler().compileClassString(stmt, systemClass,
        universe);
  }

  @SlowPath
  private SClass compile(final String path, final String file,
      final SClass systemClass, final Universe universe)
      throws IOException {
    SClass result = systemClass;

    String fname = path + File.separator + file + ".som";
    FileReader stream = new FileReader(fname);

    Source source = Source.fromFileName(fname);
    parser = new Parser(stream, new File(fname).length(), source, universe);

    result = compile(systemClass, universe);

    SSymbol cname = result.getName();
    String cnameC = cname.getString();

    if (file != cnameC) {
      throw new IllegalStateException("File name " + file
          + " does not match class name " + cnameC);
    }

    return result;
  }

  private SClass compileClassString(final String stream,
      final SClass systemClass, final Universe universe) {
    parser = new Parser(new StringReader(stream), stream.length(), null, universe);

    SClass result = compile(systemClass, universe);
    return result;
  }

  private SClass compile(final SClass systemClass,
      final Universe universe) {
    ClassGenerationContext cgc = new ClassGenerationContext(universe);

    SClass result = systemClass;
    try {
      parser.classdef(cgc);
    } catch (ParseError pe) {
      Universe.errorExit(pe.toString());
    }

    if (systemClass == null) {
      result = cgc.assemble();
    } else {
      cgc.assembleSystemClass(result);
    }

    return result;
  }

}

@@ -1662,34 +1662,8 @@
  %7B%0A%0A
-  private Parser parser;%0A%0A
   @S
@@ -1832,510 +1832,8 @@
 n %7B%0A
-    return new SourcecodeCompiler().compile(path, file, systemClass, universe);%0A  %7D%0A%0A  @SlowPath%0A  public static SClass compileClass(final String stmt,%0A      final SClass systemClass, final Universe universe) %7B%0A    return new SourcecodeCompiler().compileClassString(stmt, systemClass,%0A        universe);%0A  %7D%0A%0A  @SlowPath%0A  private SClass compile(final String path, final String file,%0A      final SClass systemClass, final Universe universe)%0A      throws IOException %7B%0A    SClass result = systemClass;%0A%0A
     
@@ -1982,24 +1982,31 @@
 fname);%0A    
+Parser 
 parser = new
@@ -2067,24 +2067,31 @@
 erse);%0A%0A    
+SClass 
 result = com
@@ -2087,32 +2087,40 @@
 esult = compile(
+parser, 
 systemClass, uni
@@ -2368,39 +2368,57 @@
  result;%0A  %7D%0A%0A  
-private
+@SlowPath%0A  public static
  SClass compileC
@@ -2421,22 +2421,16 @@
 ileClass
-String
 (final S
@@ -2441,19 +2441,11 @@
 g st
-ream,%0A     
+mt,
  fin
@@ -2458,32 +2458,38 @@
 ass systemClass,
+%0A     
  final Universe 
@@ -2500,24 +2500,31 @@
 erse) %7B%0A    
+Parser 
 parser = new
@@ -2554,21 +2554,17 @@
 r(st
-rea
 m
+t
 ), st
-rea
 m
+t
 .len
@@ -2616,16 +2616,24 @@
 compile(
+parser, 
 systemCl
@@ -2682,16 +2682,23 @@
 private 
+static 
 SClass c
@@ -2706,24 +2706,45 @@
 mpile(final 
+Parser parser, final 
 SClass syste
@@ -3149,11 +3149,10 @@
 lt;%0A  %7D%0A
-%0A
 %7D%0A

Remove instantiation of SourcecodeCompiler as simplification