Skip to content

Commit

Permalink
Refactored formatter to have an outputter
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardolopezb committed Jun 21, 2023
1 parent ca8a4fd commit df36072
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 107 deletions.
50 changes: 8 additions & 42 deletions app/src/main/kotlin/printscript/v1/app/PrintscriptFunction.kt
Original file line number Diff line number Diff line change
@@ -1,50 +1,16 @@
package printscript.v1.app

import common.token.Token
import formatter.implementations.FormattedTextWriter
import formatter.implementations.Formatter
import formatter.implementations.StreamedFormatter
import interpreter.EmptyScope
import interpreter.Scope
import interpreter.implementation.Interpreter
import interpreter.implementation.StreamInterpreter
import interpreter.input.Inputter
import interpreter.output.Outputter
import common.io.Inputter
import common.io.Outputter
import lexer.provider.FileTokenProvider
import linter.implementations.Linter
import linter.implementations.StreamedLinter
import parser.implementation.Parser
import parser.provider.ASTProvider
import java.io.File
import java.io.FileInputStream
import java.io.InputStream

interface PrintscriptFunction {
fun execute(tokenLine: List<Token>)
}

class ExecuteFunction : PrintscriptFunction {
private val parser = Parser()
private val interpreter = Interpreter(Scope(mutableMapOf(), mutableMapOf(), EmptyScope))
override fun execute(tokenLine: List<Token>) = interpreter.interpret(parser.parse(tokenLine))
}

class LinterFunction(configFileName: String) : PrintscriptFunction {
private val parser = Parser()
private val linter = Linter(configFileName)
override fun execute(tokenLine: List<Token>) {
linter.lint(parser.check(tokenLine))
}
}

class FormatFunction(fileToWrite: String, configFileName: String) : PrintscriptFunction {
private val parser = Parser()
private val formatter = Formatter(configFileName)
private val ftw = FormattedTextWriter(File(fileToWrite))
override fun execute(tokenLine: List<Token>) {
ftw.writeLine(formatter.format(parser.parse(tokenLine)))
}
}

interface PrintscriptStreamedFunction {
fun execute()
Expand All @@ -60,18 +26,18 @@ class StreamedExecution(sourceFileStream: InputStream, version: String, inputter
}
}

class StreamedFormat(sourceFile: File, configFileName: String, version: String) : PrintscriptStreamedFunction {
private val tokenProvider = FileTokenProvider(FileInputStream(sourceFile), version)
class StreamedFormat(sourceFileInputStream: InputStream, configFileName: String, version: String, outputter: Outputter) : PrintscriptStreamedFunction {
private val tokenProvider = FileTokenProvider(sourceFileInputStream, version)
private val astProvider = ASTProvider(tokenProvider)
private val ftw = FormattedTextWriter(sourceFile)
private val streamedFormatter = StreamedFormatter(astProvider, ftw, configFileName)

private val streamedFormatter = StreamedFormatter(astProvider, outputter, configFileName)
override fun execute() {
streamedFormatter.format()
}
}

class StreamedLint(sourceFile: File, configFileName: String, version: String) : PrintscriptStreamedFunction {
private val tokenProvider = FileTokenProvider(FileInputStream(sourceFile), version)
class StreamedLint(sourceFileInputStream: InputStream, configFileName: String, version: String) : PrintscriptStreamedFunction {
private val tokenProvider = FileTokenProvider(sourceFileInputStream, version)
private val astProvider = ASTProvider(tokenProvider)
private val streamedLinter = StreamedLinter(astProvider, configFileName)
override fun execute() {
Expand Down
48 changes: 5 additions & 43 deletions app/src/main/kotlin/printscript/v1/cli/CLI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package printscript.v1.cli
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.optional
import common.io.Outputter
import common.token.Token
import common.token.TokenType
import formatter.implementations.FormattedTextWriter
import interpreter.input.ConsoleInputter
import interpreter.output.ConsolePrintOutputter
import lexer.implementation.Lexer
Expand Down Expand Up @@ -38,7 +40,7 @@ class Lint : CliktCommand(help = "Lint a Printscript file") {
override fun run() {
// CLIUtils.runAppWithFunction(File(sourceFile), LinterFunction(configFile))
println(File(sourceFile))
StreamedLint(File(sourceFile), configFile, version.toString()).execute()
StreamedLint(FileInputStream(File(sourceFile)), configFile, version.toString()).execute()
}
}

Expand All @@ -50,47 +52,7 @@ class Format : CliktCommand(help = "Format a Printscript file") {

override fun run() {
// CLIUtils.runAppWithFunction(File(sourceFile), FormatFunction(sourceFile, configFile))
StreamedFormat(File(sourceFile), configFile, version.toString()).execute()
}
}

class CLIUtils {
companion object {

private fun runLexer(file: File) {
val lexer = Lexer()
lexer.extractTokensFromFile(file)
}

private fun getTokenFromStringRepresentation(input: String): Token {
val parts = input.substringAfter("(").dropLast(1).split(", ")
val orderId = parts[0].substringAfter("=").toInt()
val tokenType = TokenType.valueOf(parts[1].substringAfter("="))
val value = parts[2].substringAfter("=")
val row = parts[3].substringAfter("=").toInt()
val col = parts[4].substringAfter("=").toInt()

return Token(orderId, tokenType, value, row, col)
}

fun runAppWithFunction(file: File, function: PrintscriptFunction) {
runLexer(file)

val listOfTokensInLine = mutableListOf<Token>()
val scanner = Scanner(File("Tokens.txt"))

while (scanner.hasNextLine()) {
val token = getTokenFromStringRepresentation(scanner.nextLine())
listOfTokensInLine.add(token)

if (token.tokenType == TokenType.SEMICOLON) {
function.execute(listOfTokensInLine)
listOfTokensInLine.clear()
}
if (!scanner.hasNextLine() && token.tokenType != TokenType.SEMICOLON) {
throw java.lang.Exception("There is a semicolon missing in the last line of the file")
}
}
}
val outputter: Outputter = FormattedTextWriter(File(sourceFile))
StreamedFormat(FileInputStream(File(sourceFile)), configFile, version.toString(), outputter).execute()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package interpreter.input
package common.io

import java.util.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package interpreter.output
package common.io

interface Outputter {
fun output(text: String)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package formatter.implementations

import common.io.Outputter
import java.io.File
import kotlin.system.exitProcess

class FormattedTextWriter(private val fileToWrite: File) {
class FormattedTextWriter(private val fileToWrite: File): Outputter {

private var tempFile = File("temp.ps")

fun writeLine(line: String) {
if (line === "EOF") {
override fun output(text: String) {
if (text === "EOF") {
tempFile.copyTo(fileToWrite, overwrite = true)
tempFile.delete()
exitProcess(0)
} else {
tempFile.appendText(line)
tempFile.appendText(text)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package formatter.implementations

import common.ast.AST
import common.ast.implementations.asts.AssignationAST
import common.ast.implementations.asts.ConditionalAST
import common.ast.implementations.asts.DeclarationAST
import common.ast.implementations.asts.DeclarationAssignationAST
import common.ast.implementations.asts.FunctionAST
import common.ast.implementations.asts.ConditionalAST
import common.ast.implementations.asts.EndOfFileAST
import common.ast.implementations.asts.FunctionAST
import common.config.reader.formatter.FormatterRules
import common.token.Token
import common.token.TokenType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package formatter.implementations

import common.io.Outputter
import common.providers.ast.ASTProvider

class StreamedFormatter(
private val astProvider: ASTProvider,
private val ftw: FormattedTextWriter,
private val outputter: Outputter,
configFile: String
) {
private val formatter = Formatter(configFile)

fun format() {
val astProviderResult = astProvider.getAST()
if (astProviderResult.isPresent) {
ftw.writeLine(formatter.format(astProviderResult.get()))
outputter.output(formatter.format(astProviderResult.get()))
}
format()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import common.ast.implementations.node.ReadInputNode
import common.ast.implementations.node.TreeNode
import common.token.TokenType
import interpreter.Utils
import interpreter.input.Inputter
import common.io.Inputter
import interpreter.interfaces.Interpreter
import interpreter.interfaces.Scope
import interpreter.output.Outputter
import common.io.Outputter
import java.lang.AssertionError
import kotlin.Exception
import kotlin.collections.HashMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package interpreter.implementation

import common.ast.AST
import common.ast.implementations.asts.BlockAST
import interpreter.input.Inputter
import common.io.Inputter
import interpreter.interfaces.Interpreter
import interpreter.interfaces.Scope
import interpreter.output.Outputter
import common.io.Outputter

class BlockInterpreter(private val scope: Scope, private val inputter: Inputter, private val outputter: Outputter, private val booleanWrapper: BooleanWrapper) : Interpreter {
override fun interpret(ast: AST) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import common.ast.implementations.node.LeafNode
import common.ast.implementations.node.Node
import common.token.TokenType
import interpreter.Scope
import interpreter.input.Inputter
import common.io.Inputter
import interpreter.interfaces.Interpreter
import interpreter.output.Outputter
import common.io.Outputter
import kotlin.Exception

class ConditionalInterpreter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import interpreter.Utils
import interpreter.interfaces.Interpreter
import interpreter.interfaces.Scope
import interpreter.output.ConsolePrintOutputter
import interpreter.output.Outputter
import common.io.Outputter

class FunctionInterpreter(
private val scope: Scope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import common.ast.implementations.asts.EndOfFileAST
import common.ast.implementations.asts.FunctionAST
import interpreter.Scope
import interpreter.input.ConsoleInputter
import interpreter.input.Inputter
import common.io.Inputter
import interpreter.interfaces.Interpreter
import interpreter.output.ConsolePrintOutputter
import interpreter.output.Outputter
import common.io.Outputter

class Interpreter(
private val scope: Scope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package interpreter.implementation
import common.providers.ast.ASTProvider
import interpreter.EmptyScope
import interpreter.Scope
import interpreter.input.Inputter
import interpreter.interfaces.Interpreter
import interpreter.output.Outputter
import common.io.Inputter
import common.io.Outputter

class StreamInterpreter(private val astProvider: ASTProvider, inputter: Inputter, outputter: Outputter) {
private var isEOF = BooleanWrapper(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package interpreter.input

import common.io.Inputter

class ConsoleInputter : Inputter {
override fun getInputLine(): String? {
return readlnOrNull()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package interpreter.output

import common.io.Outputter

class ConsolePrintOutputter : Outputter {

override fun output(text: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package linter.implementations

import common.providers.ast.ASTErrorReporter
import linter.`interface`.Linter


class StreamedLinter(private val astErrorReporter: ASTErrorReporter, configFile: String) {
private val linter = Linter(configFile)
Expand Down

0 comments on commit df36072

Please sign in to comment.