Skip to content

Commit

Permalink
Refactor ProcessingUnit trait for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Kota Mizushima committed Dec 27, 2023
1 parent 5e759be commit bb18c3f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/onion/compiler/Generating.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package onion.compiler

class Generating(config: CompilerConfig) extends AnyRef with ProcessingUnit[Seq[IRT.ClassDefinition], Seq[CompiledClass]] {
class Generating(config: CompilerConfig) extends AnyRef with Processor[Seq[IRT.ClassDefinition], Seq[CompiledClass]] {
class CodeGeneratingEnvironment
type Environment = CodeGeneratingEnvironment
def newEnvironment(source: Seq[IRT.ClassDefinition]): CodeGeneratingEnvironment = {
new CodeGeneratingEnvironment
}

private val generator = new CodeGeneration(config)
def doProcess(source: Seq[IRT.ClassDefinition], environment: CodeGeneratingEnvironment): Seq[CompiledClass] = {
def processBody(source: Seq[IRT.ClassDefinition], environment: CodeGeneratingEnvironment): Seq[CompiledClass] = {
generator.process(source)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/onion/compiler/Parsing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import _root_.onion.compiler.exceptions.CompilationException
import _root_.onion.compiler.parser.{JJOnionParser, ParseException}

class Parsing(config: CompilerConfig) extends AnyRef
with ProcessingUnit[Seq[InputSource], Seq[AST.CompilationUnit]] {
with Processor[Seq[InputSource], Seq[AST.CompilationUnit]] {
type Environment = Null
def newEnvironment(source: Seq[InputSource]): Null = null
def doProcess(source: Seq[InputSource], environment: Null): Seq[AST.CompilationUnit] = {
def processBody(source: Seq[InputSource], environment: Null): Seq[AST.CompilationUnit] = {
def parse(reader: Reader, fileName: String): AST.CompilationUnit = {
new JJOnionParser(reader).unit().copy(sourceFile = fileName)
}
Expand Down
22 changes: 0 additions & 22 deletions src/main/scala/onion/compiler/ProcessingUnit.scala

This file was deleted.

22 changes: 22 additions & 0 deletions src/main/scala/onion/compiler/Processor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package onion.compiler

trait Processor[A, B] {self =>
type Environment
def newEnvironment(source: A): Environment
protected def processBody(source: A, environment: Environment): B
protected def preprocess(source: A): Unit = {}
protected def postprocess(source: A, result: B): Unit = {}

final def process(source: A): B = {
preprocess(source)
val result = processBody(source, newEnvironment(source))
postprocess(source, result)
result
}

def andThen[C](nextUnit: Processor[B, C]): Processor[A, C] = new Processor[A, C] {
type Environment = Null
def newEnvironment(source: A): Environment = null
def processBody(source: A, environment: Null): C = nextUnit.process(self.process(source))
}
}
4 changes: 2 additions & 2 deletions src/main/scala/onion/compiler/Rewriting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import onion.compiler.AST.{ClassDeclaration, InterfaceDeclaration, RecordDeclara
import _root_.scala.collection.JavaConverters._
import scala.collection.mutable.{Buffer, Map, Set => MutableSet}

class Rewriting(config: CompilerConfig) extends AnyRef with ProcessingUnit[Seq[AST.CompilationUnit], Seq[AST.CompilationUnit]] {
class Rewriting(config: CompilerConfig) extends AnyRef with Processor[Seq[AST.CompilationUnit], Seq[AST.CompilationUnit]] {

class TypingEnvironment

type Environment = TypingEnvironment

def newEnvironment(source: Seq[AST.CompilationUnit]) = new TypingEnvironment

def doProcess(source: Seq[AST.CompilationUnit], environment: TypingEnvironment): Seq[AST.CompilationUnit] = {
def processBody(source: Seq[AST.CompilationUnit], environment: TypingEnvironment): Seq[AST.CompilationUnit] = {
val rewritten = Buffer.empty[AST.CompilationUnit]
for (unit <- source) {
rewritten += rewrite(unit)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/onion/compiler/Typing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import _root_.onion.compiler.SemanticError._
import collection.mutable.{Stack, Buffer, Map, HashMap, Set => MutableSet}
import java.util.{Arrays, TreeSet => JTreeSet}

class Typing(config: CompilerConfig) extends AnyRef with ProcessingUnit[Seq[AST.CompilationUnit], Seq[ClassDefinition]] {
class Typing(config: CompilerConfig) extends AnyRef with Processor[Seq[AST.CompilationUnit], Seq[ClassDefinition]] {
class TypingEnvironment
type Continuable = Boolean
type Environment = TypingEnvironment
Expand Down Expand Up @@ -66,7 +66,7 @@ class Typing(config: CompilerConfig) extends AnyRef with ProcessingUnit[Seq[AST.
private var unit_ : AST.CompilationUnit = _
private val reporter_ : SemanticErrorReporter = new SemanticErrorReporter(config.maxErrorReports)
def newEnvironment(source: Seq[AST.CompilationUnit]) = new TypingEnvironment
def doProcess(source: Seq[AST.CompilationUnit], environment: TypingEnvironment): Seq[ClassDefinition] = {
def processBody(source: Seq[AST.CompilationUnit], environment: TypingEnvironment): Seq[ClassDefinition] = {
for(unit <- source) processHeader(unit)
for(unit <- source) processOutline(unit)
for(unit <- source) processTyping(unit)
Expand Down

0 comments on commit bb18c3f

Please sign in to comment.