<commit_before>//
// OrcCompiler.scala -- Scala class OrcCompiler
// Project OrcScala
//
// $Id$
//
// Created by jthywiss on May 26, 2010.
//
// Copyright (c) 2010 The University of Texas at Austin. All rights reserved.
//
// Use and redistribution of this file is governed by the license terms in
// the LICENSE file found in the project's top-level directory and also found at
// URL: http://orc.csres.utexas.edu/license.shtml .
//

package orc.compile

import java.io.InputStreamReader
import java.io.PrintWriter

import scala.util.parsing.input.Reader
import scala.util.parsing.input.StreamReader
import scala.collection.JavaConversions._

import orc.error.compiletime.CompilationException
import orc.error.compiletime.CompileLogger
import orc.error.compiletime.CompileLogger.Severity
import orc.error.compiletime.ParsingException
import orc.error.compiletime.PrintWriterCompileLogger
import orc.OrcCompilerAPI
import orc.CompilerEnvironmentIfc
import orc.OrcOptions
import orc.compile.parse.OrcParser
import orc.compile.parse.OrcReader

/**
 * Represents one phase in a compiler.  It is defined as a function from
 * compiler options to a function from a phase's input to its output.
 * CompilerPhases can be composed with the >>> operator.
 *
 * @author jthywiss
 */
trait CompilerPhase[O, A, B] extends (O => A => B) { self =>
  val phaseName: String
  def >>>[C](that: CompilerPhase[O, B, C]) = new CompilerPhase[O, A, C] { 
    val phaseName = self.phaseName+" >>> "+that.phaseName
    override def apply(o: O) = { a: A => that(o)(self.apply(o)(a)) }
  }
}

/**
 * An instance of OrcCompiler is a particular Orc compiler configuration, 
 * which is a particular Orc compiler implementation, in a JVM instance.
 * Note, however, that an OrcCompiler instance is not specialized for
 * a single Orc program; in fact, multiple compilations of different programs,
 * with different options set, may be in progress concurrently within a
 * single OrcCompiler instance.  
 *
 * @author jthywiss
 */
class OrcCompiler extends OrcCompilerAPI with CompilerEnvironmentIfc {
  //TODO: Skip remaining phases when compileLogger.getMaxSeverity >= FATAL 

  val parse = new CompilerPhase[OrcOptions, Reader[Char], orc.compile.ext.Expression] {
    val phaseName = "parse"
    override def apply(options: OrcOptions) = { source =>
      var includeFileNames = options.additionalIncludes
      val parser = new OrcParser(options)
      if (options.usePrelude) {
        includeFileNames = "prelude.inc" :: (includeFileNames).toList
      }<commit_msg>Don't apply compiler phases to null returns from upstream phases<commit_after>//
// OrcCompiler.scala -- Scala class OrcCompiler
// Project OrcScala
//
// $Id$
//
// Created by jthywiss on May 26, 2010.
//
// Copyright (c) 2010 The University of Texas at Austin. All rights reserved.
//
// Use and redistribution of this file is governed by the license terms in
// the LICENSE file found in the project's top-level directory and also found at
// URL: http://orc.csres.utexas.edu/license.shtml .
//

package orc.compile

import java.io.InputStreamReader
import java.io.PrintWriter

import scala.util.parsing.input.Reader
import scala.util.parsing.input.StreamReader
import scala.collection.JavaConversions._

import orc.error.compiletime.CompilationException
import orc.error.compiletime.CompileLogger
import orc.error.compiletime.CompileLogger.Severity
import orc.error.compiletime.ParsingException
import orc.error.compiletime.PrintWriterCompileLogger
import orc.OrcCompilerAPI
import orc.CompilerEnvironmentIfc
import orc.OrcOptions
import orc.compile.parse.OrcParser
import orc.compile.parse.OrcReader

/**
 * Represents one phase in a compiler.  It is defined as a function from
 * compiler options to a function from a phase's input to its output.
 * CompilerPhases can be composed with the >>> operator.
 *
 * @author jthywiss
 */
trait CompilerPhase[O, A, B] extends (O => A => B) { self =>
  val phaseName: String
  def >>>[C >: Null](that: CompilerPhase[O, B, C]) = new CompilerPhase[O, A, C] { 
    val phaseName = self.phaseName+" >>> "+that.phaseName
    override def apply(o: O) = { a: A => 
      if (a == null) null else {
        val b = self.apply(o)(a)
        if (b == null) null else {
          that(o)(b)
        }
      }
    }
  }
}

/**
 * An instance of OrcCompiler is a particular Orc compiler configuration, 
 * which is a particular Orc compiler implementation, in a JVM instance.
 * Note, however, that an OrcCompiler instance is not specialized for
 * a single Orc program; in fact, multiple compilations of different programs,
 * with different options set, may be in progress concurrently within a
 * single OrcCompiler instance.  
 *
 * @author jthywiss
 */
class OrcCompiler extends OrcCompilerAPI with CompilerEnvironmentIfc {
  //TODO: Skip remaining phases when compileLogger.getMaxSeverity >= FATAL 

  val parse = new CompilerPhase[OrcOptions, Reader[Char], orc.compile.ext.Expression] {
    val phaseName = "parse"
    override def apply(options: OrcOptions) = { source =>
      var includeFileNames = options.additionalIncludes
      val parser = new OrcParser(options)
      if (options.usePrelude) {
        includeFileNames = "prelude.inc" :: (includeFileNames).toList
      }
{'fraction_non_alphanumeric': 0.09650455927051672, 'fraction_numerical': 0.003799392097264438, 'mean_word_length': 3.84360625574977, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7546310', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1547, 'n_words': 523}