diff options
Diffstat (limited to 'src/main/scala/firrtl')
| -rw-r--r-- | src/main/scala/firrtl/Driver.scala | 9 | ||||
| -rw-r--r-- | src/main/scala/firrtl/Parser.scala | 34 | ||||
| -rw-r--r-- | src/main/scala/firrtl/Translator.scala | 25 |
3 files changed, 22 insertions, 46 deletions
diff --git a/src/main/scala/firrtl/Driver.scala b/src/main/scala/firrtl/Driver.scala index c748f92e..ce8d2b1d 100644 --- a/src/main/scala/firrtl/Driver.scala +++ b/src/main/scala/firrtl/Driver.scala @@ -3,6 +3,7 @@ package firrtl import java.io._ import scala.sys.process._ import java.nio.file.{Paths, Files} +import scala.io.Source import Utils._ import DebugUtils._ import Passes._ @@ -31,7 +32,7 @@ object Driver // Parse input file and print to output private def firrtl(input: String, output: String)(implicit logger: Logger) { - val ast = Parser.parse(input) + val ast = Parser.parse(input, Source.fromFile(input).getLines) val writer = new PrintWriter(new File(output)) writer.write(ast.serialize()) writer.close() @@ -59,7 +60,7 @@ object Driver //// Don't lower //val temp1 = genTempFilename(input) - //val ast = Parser.parse(input) + //val ast = Parser.parse(Source.fromFile(input).getLines) //val writer = new PrintWriter(new File(temp1)) //val ast2 = fame1Transform(ast) //writer.write(ast2.serialize()) @@ -72,7 +73,7 @@ object Driver preCmd.! // Read in and execute infer-types - val ast = Parser.parse(temp1) + val ast = Parser.parse(input, Source.fromFile(temp1).getLines) val ast2 = inferTypes(ast)(logger) // FAME-1 Transformation @@ -125,7 +126,7 @@ object Driver // if( scalaPass.isEmpty ) { // scala2Stanza = stanza2Scala // } else { - // var ast = Parser.parse(stanza2Scala) + // var ast = Parser.parse(input, stanza2Scala) // //scalaPass.foreach( f => (ast = f(ast)) ) // Does this work? // for ( f <- scalaPass ) yield { ast = mapString2Pass(f)(ast) } diff --git a/src/main/scala/firrtl/Parser.scala b/src/main/scala/firrtl/Parser.scala index 40956ab7..00cd110e 100644 --- a/src/main/scala/firrtl/Parser.scala +++ b/src/main/scala/firrtl/Parser.scala @@ -11,38 +11,12 @@ import antlr._ object Parser { - def parseModule(string: String): Module = { - val fixedInput = Translator.addBrackets(Iterator(string)) - val antlrStream = new ANTLRInputStream(fixedInput.result) - val lexer = new FIRRTLLexer(antlrStream) - val tokens = new CommonTokenStream(lexer) - val parser = new FIRRTLParser(tokens) - - // FIXME Dangerous - parser.getInterpreter.setPredictionMode(PredictionMode.SLL) - - // Concrete Syntax Tree - val cst = parser.module - - val visitor = new Visitor("none") - //val ast = visitor.visitCircuit(cst) match { - val ast = visitor.visit(cst) match { - case m: Module => m - case x => throw new ClassCastException("Error! AST not rooted with Module node!") - } - - ast - - } - - /** Takes a firrtl filename, returns AST (root node is Circuit) + /** Takes Iterator over lines of FIRRTL, returns AST (root node is Circuit) * - * Currently must be standard FIRRTL file * Parser performs conversion to machine firrtl */ - def parse(filename: String): Circuit = { - //val antlrStream = new ANTLRInputStream(input.reader) - val fixedInput = Translator.addBrackets(Source.fromFile(filename).getLines) + def parse(filename: String, lines: Iterator[String]): Circuit = { + val fixedInput = Translator.addBrackets(lines) val antlrStream = new ANTLRInputStream(fixedInput.result) val lexer = new FIRRTLLexer(antlrStream) val tokens = new CommonTokenStream(lexer) @@ -64,4 +38,6 @@ object Parser ast } + def parse(lines: Seq[String]): Circuit = parse("<None>", lines.iterator) + } diff --git a/src/main/scala/firrtl/Translator.scala b/src/main/scala/firrtl/Translator.scala index e7bd6821..9fe40af8 100644 --- a/src/main/scala/firrtl/Translator.scala +++ b/src/main/scala/firrtl/Translator.scala @@ -31,21 +31,20 @@ object Translator if( !it.hasNext ) throw new Exception("Empty file!") - //// Find circuit before starting scope checks - //var line = it.next - //while ( it.hasNext && !line._1.contains("circuit") ) { - // ret ++= line._1 + "\n" - // line = it.next - //} - //ret ++= line._1 + " { \n" - //if( !it.hasNext ) throw new Exception("No circuit in file!") + // Find circuit before starting scope checks + var line = it.next + while ( it.hasNext && !line._1.contains("circuit") ) { + ret ++= line._1 + "\n" + line = it.next + } + ret ++= line._1 + " { \n" + if( !it.hasNext ) throw new Exception("No circuit in file!") val scope = Stack[Int]() - scope.push(0) - var newScope = false - //scope.push(countSpaces(line._1)) - //var newScope = true // indicates if increasing scope spacing is legal on next line + val lowestScope = countSpaces(line._1) + scope.push(lowestScope) + var newScope = true // indicates if increasing scope spacing is legal on next line while( it.hasNext ) { it.next match { case (lineText, lineNum) => @@ -94,7 +93,7 @@ object Translator } // while( it.hasNext ) // Print any closing braces - while( scope.top > 0 ) { + while( scope.top > lowestScope ) { scope.pop() ret.deleteCharAt(ret.lastIndexOf("\n")) // Put on previous line ret ++= " }\n" |
