From 05232b0fc6410afb958f3907b71c28df9d0dfa84 Mon Sep 17 00:00:00 2001 From: NathanFallet Date: Sat, 13 May 2023 21:37:46 +0200 Subject: [PATCH] Javadoc --- .../me/nathanfallet/makth/actions/IfAction.kt | 12 +++++ .../nathanfallet/makth/actions/PrintAction.kt | 9 ++++ .../nathanfallet/makth/actions/SetAction.kt | 10 ++++ .../nathanfallet/makth/actions/WhileAction.kt | 10 ++++ .../makth/extensions/BooleanExtension.kt | 4 ++ .../makth/extensions/StringExtension.kt | 4 ++ .../nathanfallet/makth/interfaces/Action.kt | 30 +++++++++++ .../nathanfallet/makth/interfaces/Output.kt | 3 ++ .../me/nathanfallet/makth/interfaces/Value.kt | 29 +++++++++++ .../makth/lexers/AlgorithmLexer.kt | 51 +++++++++++++++++++ .../me/nathanfallet/makth/lexers/MathLexer.kt | 28 ++++++++-- .../me/nathanfallet/makth/numbers/Integer.kt | 16 +++++- .../me/nathanfallet/makth/numbers/Natural.kt | 10 +++- .../me/nathanfallet/makth/numbers/Rational.kt | 32 +++++++++++- .../me/nathanfallet/makth/numbers/Real.kt | 19 +++++++ .../makth/operations/Divisible.kt | 13 +++++ .../nathanfallet/makth/operations/Equality.kt | 17 +++++++ .../makth/operations/Exponentiable.kt | 8 +++ .../makth/operations/Exponentiation.kt | 5 ++ .../makth/operations/Multipliable.kt | 8 +++ .../makth/operations/Operation.kt | 24 +++++++-- .../nathanfallet/makth/operations/Product.kt | 5 ++ .../nathanfallet/makth/operations/Quotient.kt | 5 ++ .../makth/operations/Remainder.kt | 5 ++ .../me/nathanfallet/makth/operations/Sum.kt | 5 ++ .../nathanfallet/makth/operations/Summable.kt | 8 +++ .../nathanfallet/makth/resolvables/Context.kt | 15 ++++++ .../makth/resolvables/Variable.kt | 8 +++ .../makth/lexers/MathLexerTest.kt | 2 +- .../makth/operations/OperationsTest.kt | 2 +- 30 files changed, 383 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/me/nathanfallet/makth/actions/IfAction.kt b/src/main/kotlin/me/nathanfallet/makth/actions/IfAction.kt index 364227f..ad19110 100644 --- a/src/main/kotlin/me/nathanfallet/makth/actions/IfAction.kt +++ b/src/main/kotlin/me/nathanfallet/makth/actions/IfAction.kt @@ -7,6 +7,13 @@ import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException import me.nathanfallet.makth.resolvables.Context +/** + * Action that executes a list of actions if a condition is true, + * or another list of actions otherwise. + * @param condition Condition to check + * @param actions Actions to execute if condition is true + * @param elseActions Actions to execute if condition is false + */ data class IfAction @JvmOverloads constructor( val condition: Value, val actions: List, @@ -15,6 +22,11 @@ data class IfAction @JvmOverloads constructor( companion object { + /** + * Handler for if action + * @param args List of arguments + * @return Action created from arguments + */ @JvmStatic fun handler(args: List): Action { if (args.count() != 1) { diff --git a/src/main/kotlin/me/nathanfallet/makth/actions/PrintAction.kt b/src/main/kotlin/me/nathanfallet/makth/actions/PrintAction.kt index e66ca7c..100c668 100644 --- a/src/main/kotlin/me/nathanfallet/makth/actions/PrintAction.kt +++ b/src/main/kotlin/me/nathanfallet/makth/actions/PrintAction.kt @@ -5,10 +5,19 @@ import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context +/** + * Action that prints values + * @param values Values to print + */ data class PrintAction(val values: List) : Action { companion object { + /** + * Handler for print action + * @param args Arguments of the action + * @return Action created from the arguments + */ @JvmStatic fun handler(args: List): Action { return PrintAction(args.toList()) diff --git a/src/main/kotlin/me/nathanfallet/makth/actions/SetAction.kt b/src/main/kotlin/me/nathanfallet/makth/actions/SetAction.kt index 07fafbd..360e0c2 100644 --- a/src/main/kotlin/me/nathanfallet/makth/actions/SetAction.kt +++ b/src/main/kotlin/me/nathanfallet/makth/actions/SetAction.kt @@ -8,10 +8,20 @@ import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentTypeExceptio import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Action that sets a variable + * @param identifier Identifier of the variable to set + * @param value Value to set + */ data class SetAction(val identifier: String, val value: Value) : Action { companion object { + /** + * Handler for set action + * @param args Arguments of the action + * @return Action created from the arguments + */ @JvmStatic fun handler(args: List): Action { if (args.count() != 2) { diff --git a/src/main/kotlin/me/nathanfallet/makth/actions/WhileAction.kt b/src/main/kotlin/me/nathanfallet/makth/actions/WhileAction.kt index 85ef8d8..1c01ea6 100644 --- a/src/main/kotlin/me/nathanfallet/makth/actions/WhileAction.kt +++ b/src/main/kotlin/me/nathanfallet/makth/actions/WhileAction.kt @@ -7,10 +7,20 @@ import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException import me.nathanfallet.makth.resolvables.Context +/** + * Action that executes a list of actions while a condition is true. + * @param condition Condition to check + * @param actions Actions to execute while condition is true + */ data class WhileAction(val condition: Value, val actions: List) : Action { companion object { + /** + * Handler for while action + * @param args List of arguments + * @return Action created from arguments + */ @JvmStatic fun handler(args: List): Action { if (args.count() != 1) { diff --git a/src/main/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt b/src/main/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt index 749b27e..42165ee 100644 --- a/src/main/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt +++ b/src/main/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt @@ -4,6 +4,10 @@ import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Boolean value + * @param value Boolean value + */ data class BooleanValue(val value: Boolean) : Value { override fun compute(context: Context): Value { diff --git a/src/main/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt b/src/main/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt index a6ba51f..a536e10 100644 --- a/src/main/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt +++ b/src/main/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt @@ -4,6 +4,10 @@ import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * String value + * @param value String value + */ data class StringValue(val value: String, val latex: Boolean = false) : Value { override fun compute(context: Context): Value { diff --git a/src/main/kotlin/me/nathanfallet/makth/interfaces/Action.kt b/src/main/kotlin/me/nathanfallet/makth/interfaces/Action.kt index ba9b3ee..44911c0 100644 --- a/src/main/kotlin/me/nathanfallet/makth/interfaces/Action.kt +++ b/src/main/kotlin/me/nathanfallet/makth/interfaces/Action.kt @@ -3,16 +3,31 @@ package me.nathanfallet.makth.interfaces import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Interface for all actions that can be executed + */ interface Action { // Errors + /** + * Base class for all execution errors + * @param action Action that failed + * @param context Context of the action + * @param message Error message + */ open class ExecutionException( val action: Action, val context: Context, message: String ) : Exception(message) + /** + * Exception thrown when one (or more) variable is not found + * @param action Action that failed + * @param context Context of the action + * @param variable Variables that were not found + */ open class UnknownVariablesException( action: Action, context: Context, @@ -22,6 +37,12 @@ interface Action { "Unknown variable(s): ${variables.joinToString(", ") { it.name }}" ) + /** + * Exception thrown when a variable is not a boolean + * @param action Action that failed + * @param context Context of the action + * @param value Value that is not a boolean + */ open class NotABooleanException( action: Action, context: Context, @@ -33,9 +54,18 @@ interface Action { // Interface + /** + * Execute the action in the given context + * @param context Context of the action + * @return New context after the action was executed + */ @Throws(ExecutionException::class) fun execute(context: Context): Context + /** + * Convert the action to a string that can be parsed by the algorithm lexer + * @return String representation of the action + */ fun toAlgorithmString(): String } \ No newline at end of file diff --git a/src/main/kotlin/me/nathanfallet/makth/interfaces/Output.kt b/src/main/kotlin/me/nathanfallet/makth/interfaces/Output.kt index 6003281..030e37a 100644 --- a/src/main/kotlin/me/nathanfallet/makth/interfaces/Output.kt +++ b/src/main/kotlin/me/nathanfallet/makth/interfaces/Output.kt @@ -1,3 +1,6 @@ package me.nathanfallet.makth.interfaces +/** + * Interface for all outputs of actions + */ interface Output \ No newline at end of file diff --git a/src/main/kotlin/me/nathanfallet/makth/interfaces/Value.kt b/src/main/kotlin/me/nathanfallet/makth/interfaces/Value.kt index 7b295df..f67dcdd 100644 --- a/src/main/kotlin/me/nathanfallet/makth/interfaces/Value.kt +++ b/src/main/kotlin/me/nathanfallet/makth/interfaces/Value.kt @@ -3,19 +3,48 @@ package me.nathanfallet.makth.interfaces import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Interface for all values that can be computed + */ interface Value: Output { + /** + * Convert the value to a string that can be parsed by the algorithm lexer + * @return Algorithm string + */ fun toAlgorithmString(): String { return toRawString() } + /** + * Compute the value in the given context + * @param context Context of the value + * @return Computed value + */ fun compute(context: Context): Value + /** + * Convert the value to a raw string + * @return Raw string + */ fun toRawString(): String + + /** + * Convert the value to a LaTeX string + * @return LaTeX string + */ fun toLaTeXString(): String + /** + * Extract all variables from the value + * @return Set of variables + */ fun extractVariables(): Set + /** + * Get the precedence of the value, used for braces + * @return Precedence + */ fun getMainPrecedence(): Int { return Int.MAX_VALUE } diff --git a/src/main/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexer.kt b/src/main/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexer.kt index ba05c5b..c6ac212 100644 --- a/src/main/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexer.kt +++ b/src/main/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexer.kt @@ -9,30 +9,66 @@ import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context +/** + * A lexer for algorithms + * @param content Content of the algorithm + */ class AlgorithmLexer(private var content: String) { // Errors + /** + * Base class for all syntax errors + * @param message Error message + */ open class SyntaxException(message: String) : Exception(message) + /** + * Exception thrown when an unknown keyword is found + * @param keyword Unknown keyword + */ open class UnknownKeywordException(val keyword: String) : SyntaxException("Unknown keyword: $keyword") + /** + * Exception thrown when an unexpected keyword is found + * @param keyword Unexpected keyword + */ open class UnexpectedKeywordException(val keyword: String) : SyntaxException("Unexpected keyword: $keyword") + /** + * Exception thrown when an unexpected brace is found + * @param character Unexpected brace + */ open class UnexpectedBraceException(val character: String) : SyntaxException("Unexpected brace: $character") + /** + * Exception thrown when an unexpected slash is found + * @param character Unexpected slash + */ open class UnexpectedSlashException(val character: String) : SyntaxException("Unexpected slash: $character") + /** + * Exception thrown when an incorrect argument count is found + * @param keyword Keyword + * @param count Argument count + * @param expected Expected argument count + */ open class IncorrectArgumentCountException( val keyword: String, val count: Int, val expected: Int ) : SyntaxException("Incorrect argument count for $keyword, got $count, expected $expected") + /** + * Exception thrown when an incorrect argument type is found + * @param keyword Keyword + * @param value Argument value + * @param expected Expected argument type + */ open class IncorrectArgumentTypeException( val keyword: String, val value: Value, @@ -65,11 +101,22 @@ class AlgorithmLexer(private var content: String) { "while" to WhileAction::handler ) + /** + * Register a keyword handler + * @param keyword Keyword to register + * @param handler Handler to register + * @return This lexer, with the new handler registered + */ fun registerKeyword(keyword: String, handler: (List) -> Action): AlgorithmLexer { keywordHandlers += mapOf(keyword to handler) return this } + /** + * Register multiple keyword handlers + * @param keywords Keywords to register + * @return This lexer, with the new handlers registered + */ fun registerKeywords(keywords: Map) -> Action>): AlgorithmLexer { keywordHandlers += keywords return this @@ -77,6 +124,10 @@ class AlgorithmLexer(private var content: String) { // Parse an algorithm + /** + * Parse an algorithm, and return a list of actions + * @return List of actions + */ @Throws(SyntaxException::class) fun execute(): List { // For each character of the string diff --git a/src/main/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt b/src/main/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt index 20877c3..92b5fdf 100644 --- a/src/main/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt +++ b/src/main/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt @@ -3,16 +3,31 @@ package me.nathanfallet.makth.lexers import me.nathanfallet.makth.extensions.StringValue import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.interfaces.Value +import me.nathanfallet.makth.lexers.AlgorithmLexer.SyntaxException import me.nathanfallet.makth.numbers.Integer import me.nathanfallet.makth.operations.Operation import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Lexer for math expressions + * @param content Content to parse + */ class MathLexer(private var content: String) { // Errors - class SyntaxException : Exception() + /** + * Exception thrown when a value is expected but not found + */ + open class NoValueFoundException : SyntaxException("No value found") + + /** + * Exception thrown when an unknown operator is found + * @param operator Unknown operator + */ + open class UnknownOperatorException(val operator: String) : + SyntaxException("Unknown operator: $operator") // Constants @@ -30,6 +45,11 @@ class MathLexer(private var content: String) { // Parse an expression + /** + * Parse an expression + * @param content Content to parse + * @return Parsed value + */ @Throws(SyntaxException::class) fun execute(context: Context): Value { // For each character of the string @@ -64,7 +84,7 @@ class MathLexer(private var content: String) { return values[0].compute(context) } - throw SyntaxException() + throw NoValueFoundException() } // Parse something @@ -150,7 +170,7 @@ class MathLexer(private var content: String) { i-- } - fun parseVariable() { + private fun parseVariable() { // Check name var name = StringBuilder() name.append(content[i]) @@ -224,7 +244,7 @@ class MathLexer(private var content: String) { if (values.isNotEmpty()) { return values.removeAt(0) } - throw SyntaxException() + throw NoValueFoundException() } private fun getFirstOperationAndRemove(): String? { diff --git a/src/main/kotlin/me/nathanfallet/makth/numbers/Integer.kt b/src/main/kotlin/me/nathanfallet/makth/numbers/Integer.kt index f1c8e14..ce1d978 100644 --- a/src/main/kotlin/me/nathanfallet/makth/numbers/Integer.kt +++ b/src/main/kotlin/me/nathanfallet/makth/numbers/Integer.kt @@ -5,12 +5,20 @@ import kotlin.math.pow import me.nathanfallet.makth.extensions.pow import me.nathanfallet.makth.extensions.nthRoot +/** + * Integer representation + */ interface Integer : Rational { // Instantiate companion object { + /** + * Instantiate an integer from a long value + * @param value Long value + * @return Integer + */ @JvmStatic fun instantiate(value: Long): Integer { return if (value < 0) IntegerImpl(value) else Natural.instantiate(value) @@ -20,9 +28,13 @@ interface Integer : Rational { // Integer interface + /** + * Get long value + * @return Long value + */ fun getLongValue(): Long - // AbstractRational + // Rational override fun getNumerator(): Integer { return this @@ -32,7 +44,7 @@ interface Integer : Rational { return Natural.instantiate(1) } - // AbstractReal + // Real override fun getDoubleValue(): Double { return getLongValue().toDouble() diff --git a/src/main/kotlin/me/nathanfallet/makth/numbers/Natural.kt b/src/main/kotlin/me/nathanfallet/makth/numbers/Natural.kt index 3b602ef..27d0d51 100644 --- a/src/main/kotlin/me/nathanfallet/makth/numbers/Natural.kt +++ b/src/main/kotlin/me/nathanfallet/makth/numbers/Natural.kt @@ -1,11 +1,19 @@ package me.nathanfallet.makth.numbers +/** + * Natural representation + */ interface Natural : Integer { // Instantiate companion object { + /** + * Instantiate a natural from a long value + * @param value Long value + * @return Natural + */ @JvmStatic fun instantiate(value: Long): Natural { return NaturalImpl(value) @@ -13,7 +21,7 @@ interface Natural : Integer { } - // AbstractReal + // Real override fun absoluteValue(): Natural { return this diff --git a/src/main/kotlin/me/nathanfallet/makth/numbers/Rational.kt b/src/main/kotlin/me/nathanfallet/makth/numbers/Rational.kt index 6a5094c..48e8a29 100644 --- a/src/main/kotlin/me/nathanfallet/makth/numbers/Rational.kt +++ b/src/main/kotlin/me/nathanfallet/makth/numbers/Rational.kt @@ -2,12 +2,21 @@ package me.nathanfallet.makth.numbers import me.nathanfallet.makth.extensions.gcd +/** + * Rational representation + */ interface Rational : Real { // Instantiate companion object { + /** + * Instantiate a rational from a numerator and a denominator + * @param numerator Numerator + * @param denominator Denominator + * @return Rational + */ @JvmStatic fun instantiate( numerator: Integer, @@ -31,6 +40,12 @@ interface Rational : Real { } } + /** + * Instantiate a rational from a numerator and a denominator + * @param numerator Numerator + * @param denominator Denominator + * @return Rational + */ @JvmStatic fun instantiate( numerator: Integer, @@ -46,6 +61,12 @@ interface Rational : Real { } } + /** + * Instantiate a rational from a numerator and a denominator + * @param numerator Numerator + * @param denominator Denominator + * @return Rational + */ @JvmStatic fun instantiate( numerator: Long, @@ -58,10 +79,19 @@ interface Rational : Real { // Rational interface + /** + * Get numerator + * @return Numerator + */ fun getNumerator(): Integer + + /** + * Get denominator + * @return Denominator + */ fun getDenominator(): Natural - // AbstractReal + // Real override fun getDoubleValue(): Double { return getNumerator().getDoubleValue() / getDenominator().getDoubleValue() diff --git a/src/main/kotlin/me/nathanfallet/makth/numbers/Real.kt b/src/main/kotlin/me/nathanfallet/makth/numbers/Real.kt index 8358711..1a3645d 100644 --- a/src/main/kotlin/me/nathanfallet/makth/numbers/Real.kt +++ b/src/main/kotlin/me/nathanfallet/makth/numbers/Real.kt @@ -11,6 +11,9 @@ import me.nathanfallet.makth.operations.Summable import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Real representation + */ interface Real : Value, Summable, @@ -22,9 +25,17 @@ interface Real : companion object { + /** + * Pi constant + */ @JvmStatic val pi: Real = RealImplPi() + /** + * Instantiate a real from a double value + * @param value Double value + * @return Real + */ @JvmStatic fun instantiate(value: Double): Real { // Check if value is an integer @@ -48,8 +59,16 @@ interface Real : // Real interface + /** + * Get double value + * @return Double value + */ fun getDoubleValue(): Double + /** + * Get the absolute value of this real + * @return Real + */ fun absoluteValue(): Real { return instantiate(abs(getDoubleValue())) } diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Divisible.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Divisible.kt index 887bc5c..9b1f617 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Divisible.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Divisible.kt @@ -2,10 +2,23 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.interfaces.Value +/** + * Divisible interface + */ interface Divisible where T : Value, U : Value { + /** + * Divide by another value + * @param right Value to divide by + * @return Result of the division + */ fun divide(right: T): U + /** + * Get the remainder of the division by another value + * @param right Value to divide by + * @return Remainder of the division + */ fun remainder(right: T): U } \ No newline at end of file diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Equality.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Equality.kt index 315a0b6..a78f74c 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Equality.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Equality.kt @@ -6,16 +6,29 @@ import me.nathanfallet.makth.numbers.Real import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Equality operation. + * @param left Left value + * @param right Right value + * @param operator Operator, default to Equals + */ data class Equality( val left: Value, val right: Value, val operator: Operator = Operator.Equals ) : Operation { + /** + * Available operators for equalities + */ enum class Operator { Equals, NotEquals, LessThan, GreaterThan, LessThanOrEquals, GreaterThanOrEquals; + /** + * Get the raw string for this operator + * @return Raw string + */ fun toRawString(): String { return when (this) { Equals -> "=" @@ -27,6 +40,10 @@ data class Equality( } } + /** + * Get the LaTeX string for this operator + * @return LaTeX string + */ fun toLaTeXString(): String { return when (this) { Equals -> "\\eq" diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Exponentiable.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Exponentiable.kt index 5d76364..6d6f729 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Exponentiable.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Exponentiable.kt @@ -2,8 +2,16 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.interfaces.Value +/** + * Exponentiable interface + */ interface Exponentiable where T : Value, U : Value { + /** + * Raise left value to the power of right value + * @param right Right value + * @return Result + */ fun raise(right: T): U } \ No newline at end of file diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Exponentiation.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Exponentiation.kt index c0e21c6..6945c16 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Exponentiation.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Exponentiation.kt @@ -5,6 +5,11 @@ import me.nathanfallet.makth.numbers.Real import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Exponentiation operation. + * @param left Left value + * @param right Right value + */ data class Exponentiation( val left: Value, val right: Value diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Multipliable.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Multipliable.kt index 3825868..ec73622 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Multipliable.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Multipliable.kt @@ -2,8 +2,16 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.interfaces.Value +/** + * Multipliable interface + */ interface Multipliable where T : Value, U : Value { + /** + * Multiply left value by right value + * @param right Right value + * @return Result + */ fun multiply(right: T): U } \ No newline at end of file diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Operation.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Operation.kt index 3bda183..6796ad0 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Operation.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Operation.kt @@ -2,15 +2,26 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.lexers.MathLexer +import me.nathanfallet.makth.lexers.AlgorithmLexer.SyntaxException import me.nathanfallet.makth.numbers.Integer +/** + * Interface for all operations between values + */ interface Operation : Value { object Utils { - @Throws(MathLexer.SyntaxException::class) - fun initialize(operation: String, left: Value, right: Value): Value { - return when (operation) { + /** + * Initialize an operation from an operator and two values + * @param operator Operator + * @param left Left value + * @param right Right value + * @return Operation + */ + @Throws(SyntaxException::class) + fun initialize(operator: String, left: Value, right: Value): Value { + return when (operator) { "+" -> Sum(left, right) "-" -> Sum(left, Product(Integer.instantiate(-1), right)) "*" -> Product(left, right) @@ -23,10 +34,15 @@ interface Operation : Value { ">" -> Equality(left, right, Equality.Operator.GreaterThan) "<=" -> Equality(left, right, Equality.Operator.LessThanOrEquals) ">=" -> Equality(left, right, Equality.Operator.GreaterThanOrEquals) - else -> throw MathLexer.SyntaxException() + else -> throw MathLexer.UnknownOperatorException(operator) } } + /** + * Get the precedence of an operation + * @param operation Operation + * @return Precedence + */ fun getPrecedence(operation: String): Int { return when (operation) { "^" -> 3 diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Product.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Product.kt index 5016919..bc0b890 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Product.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Product.kt @@ -5,6 +5,11 @@ import me.nathanfallet.makth.numbers.Real import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Product operation. + * @param left Left value + * @param right Right value + */ data class Product( val left: Value, val right: Value diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Quotient.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Quotient.kt index 4a9b496..d131832 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Quotient.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Quotient.kt @@ -5,6 +5,11 @@ import me.nathanfallet.makth.numbers.Real import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Quotient operation. + * @param left Left value + * @param right Right value + */ data class Quotient( val left: Value, val right: Value diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Remainder.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Remainder.kt index d3299ae..ab0adbd 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Remainder.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Remainder.kt @@ -5,6 +5,11 @@ import me.nathanfallet.makth.numbers.Real import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Remainder operation. + * @param left Left value + * @param right Right value + */ data class Remainder( val left: Value, val right: Value diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Sum.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Sum.kt index 3ffff9d..3eb9211 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Sum.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Sum.kt @@ -5,6 +5,11 @@ import me.nathanfallet.makth.numbers.Real import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable +/** + * Sum operation. + * @param left Left value + * @param right Right value + */ data class Sum( val left: Value, val right: Value diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Summable.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Summable.kt index 5352aa5..be04484 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Summable.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Summable.kt @@ -2,8 +2,16 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.interfaces.Value +/** + * Summable interface + */ interface Summable where T : Value, U : Value { + /** + * Sum left value with right value + * @param right Right value + * @return Result + */ fun sum(right: T): U } \ No newline at end of file diff --git a/src/main/kotlin/me/nathanfallet/makth/resolvables/Context.kt b/src/main/kotlin/me/nathanfallet/makth/resolvables/Context.kt index a9b7b1e..8d52bff 100644 --- a/src/main/kotlin/me/nathanfallet/makth/resolvables/Context.kt +++ b/src/main/kotlin/me/nathanfallet/makth/resolvables/Context.kt @@ -4,16 +4,31 @@ import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.interfaces.Output +/** + * Context class + * @param data Variables in current memory + * @param outputs Outputs list + */ data class Context @JvmOverloads constructor( val data: Map = mapOf(), val outputs: List = listOf() ) { + /** + * Execute an action in this context + * @param action Action to execute + * @return New context after execution + */ @Throws(Action.ExecutionException::class) fun execute(action: Action): Context { return action.execute(this) } + /** + * Execute a list of actions in this context + * @param actions Actions to execute + * @return New context after execution + */ @Throws(Action.ExecutionException::class) fun execute(actions: List): Context { var context = this diff --git a/src/main/kotlin/me/nathanfallet/makth/resolvables/Variable.kt b/src/main/kotlin/me/nathanfallet/makth/resolvables/Variable.kt index 061863a..978ac58 100644 --- a/src/main/kotlin/me/nathanfallet/makth/resolvables/Variable.kt +++ b/src/main/kotlin/me/nathanfallet/makth/resolvables/Variable.kt @@ -4,10 +4,18 @@ import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.numbers.Real import me.nathanfallet.makth.extensions.BooleanValue +/** + * Variable representation + */ data class Variable private constructor(val name: String) : Value { companion object { + /** + * Instantiate a variable from a name + * @param name Variable name + * @return Variable instance + */ @JvmStatic fun instantiate(name: String): Value { // Check for booleans diff --git a/src/test/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt b/src/test/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt index 66a7a68..66f0bba 100644 --- a/src/test/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt +++ b/src/test/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt @@ -2,7 +2,7 @@ package me.nathanfallet.makth.lexers import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.extensions.StringValue -import me.nathanfallet.makth.lexers.MathLexer.SyntaxException +import me.nathanfallet.makth.lexers.AlgorithmLexer.SyntaxException import me.nathanfallet.makth.numbers.Integer import me.nathanfallet.makth.numbers.Rational import me.nathanfallet.makth.numbers.Real diff --git a/src/test/kotlin/me/nathanfallet/makth/operations/OperationsTest.kt b/src/test/kotlin/me/nathanfallet/makth/operations/OperationsTest.kt index 90a0366..4962fcd 100644 --- a/src/test/kotlin/me/nathanfallet/makth/operations/OperationsTest.kt +++ b/src/test/kotlin/me/nathanfallet/makth/operations/OperationsTest.kt @@ -1,6 +1,6 @@ package me.nathanfallet.makth.operations; -import me.nathanfallet.makth.lexers.MathLexer.SyntaxException +import me.nathanfallet.makth.lexers.AlgorithmLexer.SyntaxException import me.nathanfallet.makth.numbers.Integer import org.junit.Assert.assertEquals import org.junit.Assert.assertThrows