diff --git a/app/src/main/kotlin/printscript/v1/app/PrintscriptFunction.kt b/app/src/main/kotlin/printscript/v1/app/PrintscriptFunction.kt index 60a52bc..e4426d9 100644 --- a/app/src/main/kotlin/printscript/v1/app/PrintscriptFunction.kt +++ b/app/src/main/kotlin/printscript/v1/app/PrintscriptFunction.kt @@ -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) -} - -class ExecuteFunction : PrintscriptFunction { - private val parser = Parser() - private val interpreter = Interpreter(Scope(mutableMapOf(), mutableMapOf(), EmptyScope)) - override fun execute(tokenLine: List) = interpreter.interpret(parser.parse(tokenLine)) -} - -class LinterFunction(configFileName: String) : PrintscriptFunction { - private val parser = Parser() - private val linter = Linter(configFileName) - override fun execute(tokenLine: List) { - 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) { - ftw.writeLine(formatter.format(parser.parse(tokenLine))) - } -} interface PrintscriptStreamedFunction { fun execute() @@ -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() { diff --git a/app/src/main/kotlin/printscript/v1/cli/CLI.kt b/app/src/main/kotlin/printscript/v1/cli/CLI.kt index a2762b0..49f5baf 100644 --- a/app/src/main/kotlin/printscript/v1/cli/CLI.kt +++ b/app/src/main/kotlin/printscript/v1/cli/CLI.kt @@ -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 @@ -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() } } @@ -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() - 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() } } diff --git a/interpreter/src/main/kotlin/interpreter/input/Inputter.kt b/common/src/main/kotlin/common/io/Inputter.kt similarity index 74% rename from interpreter/src/main/kotlin/interpreter/input/Inputter.kt rename to common/src/main/kotlin/common/io/Inputter.kt index 0898815..3f24ad7 100644 --- a/interpreter/src/main/kotlin/interpreter/input/Inputter.kt +++ b/common/src/main/kotlin/common/io/Inputter.kt @@ -1,4 +1,4 @@ -package interpreter.input +package common.io import java.util.* diff --git a/interpreter/src/main/kotlin/interpreter/output/Outputter.kt b/common/src/main/kotlin/common/io/Outputter.kt similarity index 66% rename from interpreter/src/main/kotlin/interpreter/output/Outputter.kt rename to common/src/main/kotlin/common/io/Outputter.kt index dc6c8c4..12cabfa 100644 --- a/interpreter/src/main/kotlin/interpreter/output/Outputter.kt +++ b/common/src/main/kotlin/common/io/Outputter.kt @@ -1,4 +1,4 @@ -package interpreter.output +package common.io interface Outputter { fun output(text: String) diff --git a/formatter/src/main/kotlin/formatter/implementations/FormattedTextWriter.kt b/formatter/src/main/kotlin/formatter/implementations/FormattedTextWriter.kt index 7bffa29..904d17c 100644 --- a/formatter/src/main/kotlin/formatter/implementations/FormattedTextWriter.kt +++ b/formatter/src/main/kotlin/formatter/implementations/FormattedTextWriter.kt @@ -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) } } } diff --git a/formatter/src/main/kotlin/formatter/implementations/Formatter.kt b/formatter/src/main/kotlin/formatter/implementations/Formatter.kt index 33818c4..c05edba 100644 --- a/formatter/src/main/kotlin/formatter/implementations/Formatter.kt +++ b/formatter/src/main/kotlin/formatter/implementations/Formatter.kt @@ -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 diff --git a/formatter/src/main/kotlin/formatter/implementations/StreamedFormatter.kt b/formatter/src/main/kotlin/formatter/implementations/StreamedFormatter.kt index 782b66f..8c4bf87 100644 --- a/formatter/src/main/kotlin/formatter/implementations/StreamedFormatter.kt +++ b/formatter/src/main/kotlin/formatter/implementations/StreamedFormatter.kt @@ -1,10 +1,11 @@ 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) @@ -12,7 +13,7 @@ class StreamedFormatter( fun format() { val astProviderResult = astProvider.getAST() if (astProviderResult.isPresent) { - ftw.writeLine(formatter.format(astProviderResult.get())) + outputter.output(formatter.format(astProviderResult.get())) } format() } diff --git a/interpreter/src/main/kotlin/interpreter/implementation/AssignationInterpreter.kt b/interpreter/src/main/kotlin/interpreter/implementation/AssignationInterpreter.kt index 0eb1b05..aeb66ea 100644 --- a/interpreter/src/main/kotlin/interpreter/implementation/AssignationInterpreter.kt +++ b/interpreter/src/main/kotlin/interpreter/implementation/AssignationInterpreter.kt @@ -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 diff --git a/interpreter/src/main/kotlin/interpreter/implementation/BlockInterpreter.kt b/interpreter/src/main/kotlin/interpreter/implementation/BlockInterpreter.kt index 2af1e9e..726508e 100644 --- a/interpreter/src/main/kotlin/interpreter/implementation/BlockInterpreter.kt +++ b/interpreter/src/main/kotlin/interpreter/implementation/BlockInterpreter.kt @@ -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) { diff --git a/interpreter/src/main/kotlin/interpreter/implementation/ConditionalInterpreter.kt b/interpreter/src/main/kotlin/interpreter/implementation/ConditionalInterpreter.kt index 5b9b0c7..e90a5fd 100644 --- a/interpreter/src/main/kotlin/interpreter/implementation/ConditionalInterpreter.kt +++ b/interpreter/src/main/kotlin/interpreter/implementation/ConditionalInterpreter.kt @@ -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( diff --git a/interpreter/src/main/kotlin/interpreter/implementation/FunctionInterpreter.kt b/interpreter/src/main/kotlin/interpreter/implementation/FunctionInterpreter.kt index 58dfdd8..3b06e58 100644 --- a/interpreter/src/main/kotlin/interpreter/implementation/FunctionInterpreter.kt +++ b/interpreter/src/main/kotlin/interpreter/implementation/FunctionInterpreter.kt @@ -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, diff --git a/interpreter/src/main/kotlin/interpreter/implementation/Interpreter.kt b/interpreter/src/main/kotlin/interpreter/implementation/Interpreter.kt index 1af4161..d66fe98 100644 --- a/interpreter/src/main/kotlin/interpreter/implementation/Interpreter.kt +++ b/interpreter/src/main/kotlin/interpreter/implementation/Interpreter.kt @@ -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, diff --git a/interpreter/src/main/kotlin/interpreter/implementation/StreamInterpreter.kt b/interpreter/src/main/kotlin/interpreter/implementation/StreamInterpreter.kt index d38c9d0..08f556c 100644 --- a/interpreter/src/main/kotlin/interpreter/implementation/StreamInterpreter.kt +++ b/interpreter/src/main/kotlin/interpreter/implementation/StreamInterpreter.kt @@ -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) diff --git a/interpreter/src/main/kotlin/interpreter/input/ConsoleInputter.kt b/interpreter/src/main/kotlin/interpreter/input/ConsoleInputter.kt index b49ba58..3082bff 100644 --- a/interpreter/src/main/kotlin/interpreter/input/ConsoleInputter.kt +++ b/interpreter/src/main/kotlin/interpreter/input/ConsoleInputter.kt @@ -1,5 +1,7 @@ package interpreter.input +import common.io.Inputter + class ConsoleInputter : Inputter { override fun getInputLine(): String? { return readlnOrNull() diff --git a/interpreter/src/main/kotlin/interpreter/output/ConsolePrintOutputter.kt b/interpreter/src/main/kotlin/interpreter/output/ConsolePrintOutputter.kt index 67e3e70..7f66a29 100644 --- a/interpreter/src/main/kotlin/interpreter/output/ConsolePrintOutputter.kt +++ b/interpreter/src/main/kotlin/interpreter/output/ConsolePrintOutputter.kt @@ -1,5 +1,7 @@ package interpreter.output +import common.io.Outputter + class ConsolePrintOutputter : Outputter { override fun output(text: String) { diff --git a/linter/src/main/kotlin/linter/implementations/StreamedLinter.kt b/linter/src/main/kotlin/linter/implementations/StreamedLinter.kt index 889bc3b..45d54fe 100644 --- a/linter/src/main/kotlin/linter/implementations/StreamedLinter.kt +++ b/linter/src/main/kotlin/linter/implementations/StreamedLinter.kt @@ -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)