diff --git a/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/InvalidTermTypeException.kt b/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/InvalidTermTypeException.kt index 0537bcbfa..2382ee89a 100644 --- a/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/InvalidTermTypeException.kt +++ b/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/InvalidTermTypeException.kt @@ -15,6 +15,6 @@ class InvalidTermTypeException( offendingSymbol, line, column, - "Expecting ${expected.simpleName}, got `$offendingSymbol` instead" , + "Expecting ${expected.simpleName}, got `$offendingSymbol` instead", cause ) diff --git a/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/ParseException.kt b/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/ParseException.kt index f89b603c0..edcb15d1a 100644 --- a/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/ParseException.kt +++ b/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/ParseException.kt @@ -17,11 +17,10 @@ open class ParseException( override fun toString(): String { return "ParseException{" + - "message='" + message!!.replace("\\n", "\\\\n") + '\'' + - ", line=" + line + - ", column=" + column + - ", offendingSymbol='" + offendingSymbol + '\'' + - '}' + "message='" + message!!.replace("\\n", "\\\\n") + '\'' + + ", line=" + line + + ", column=" + column + + ", offendingSymbol='" + offendingSymbol + '\'' + + '}' } - } diff --git a/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/TermParser.kt b/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/TermParser.kt index 5e4f37cf6..25d454b16 100644 --- a/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/TermParser.kt +++ b/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/TermParser.kt @@ -1,6 +1,14 @@ package it.unibo.tuprolog.core.parsing -import it.unibo.tuprolog.core.* +import it.unibo.tuprolog.core.Atom +import it.unibo.tuprolog.core.Clause +import it.unibo.tuprolog.core.Constant +import it.unibo.tuprolog.core.Integer +import it.unibo.tuprolog.core.Numeric +import it.unibo.tuprolog.core.Real +import it.unibo.tuprolog.core.Struct +import it.unibo.tuprolog.core.Term +import it.unibo.tuprolog.core.Var import it.unibo.tuprolog.core.operators.Operator import it.unibo.tuprolog.core.operators.OperatorSet import kotlin.js.JsName @@ -18,7 +26,6 @@ interface TermParser { fun parseTerm(input: String): Term = parseTerm(input, defaultOperatorSet) - @JsName("parseStructWithOperators") fun parseStruct(input: String, operators: OperatorSet): Struct = parseTerm(input, operators) as Struct @@ -27,7 +34,6 @@ interface TermParser { fun parseStruct(input: String): Struct = parseStruct(input, defaultOperatorSet) - @JsName("parseConstantWithOperators") fun parseConstant(input: String, operators: OperatorSet): Constant = parseTerm(input, operators) as Constant @@ -36,7 +42,6 @@ interface TermParser { fun parseConstant(input: String): Constant = parseConstant(input, defaultOperatorSet) - @JsName("parseVarWithOperators") fun parseVar(input: String, operators: OperatorSet): Var = parseTerm(input, operators) as Var @@ -45,7 +50,6 @@ interface TermParser { fun parseVar(input: String): Var = parseVar(input, defaultOperatorSet) - @JsName("parseAtomWithOperators") fun parseAtom(input: String, operators: OperatorSet): Atom = parseTerm(input, operators) as Atom @@ -54,7 +58,6 @@ interface TermParser { fun parseAtom(input: String): Atom = parseAtom(input, defaultOperatorSet) - @JsName("parseNumericWithOperators") fun parseNumeric(input: String, operators: OperatorSet): Numeric = parseTerm(input, operators) as Numeric @@ -63,7 +66,6 @@ interface TermParser { fun parseNumeric(input: String): Numeric = parseNumeric(input, defaultOperatorSet) - @JsName("parseIntegerWithOperators") fun parseInteger(input: String, operators: OperatorSet): Integer = parseTerm(input, operators) as Integer @@ -72,7 +74,6 @@ interface TermParser { fun parseInteger(input: String): Integer = parseInteger(input, defaultOperatorSet) - @JsName("parseRealWithOperators") fun parseReal(input: String, operators: OperatorSet): Real = parseTerm(input, operators) as Real @@ -81,12 +82,11 @@ interface TermParser { fun parseReal(input: String): Real = parseReal(input, defaultOperatorSet) - @JsName("parseClauseWithOperators") fun parseClause(input: String, operators: OperatorSet): Clause { require(operators.any { it.functor == Clause.FUNCTOR }) { "Error while parsing string as Clause: the provided operator set has no " + - "operator for '${Clause.FUNCTOR}'/1 or '${Clause.FUNCTOR}'/1" + "operator for '${Clause.FUNCTOR}'/1 or '${Clause.FUNCTOR}'/1" } return parseTerm(input, operators).toClause() } @@ -116,5 +116,4 @@ interface TermParser { @JsName("withOperators") fun withOperators(vararg operators: Operator) = withOperators(OperatorSet(*operators)) } - -} \ No newline at end of file +} diff --git a/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt b/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt index cfea64e4f..521c79f33 100644 --- a/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt +++ b/parser-core/src/commonMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt @@ -2,7 +2,16 @@ package it.unibo.tuprolog.core.parsing -import it.unibo.tuprolog.core.* +import it.unibo.tuprolog.core.Atom +import it.unibo.tuprolog.core.Clause +import it.unibo.tuprolog.core.Constant +import it.unibo.tuprolog.core.Fact +import it.unibo.tuprolog.core.Integer +import it.unibo.tuprolog.core.Numeric +import it.unibo.tuprolog.core.Real +import it.unibo.tuprolog.core.Struct +import it.unibo.tuprolog.core.Term +import it.unibo.tuprolog.core.Var import it.unibo.tuprolog.core.operators.OperatorSet import kotlin.js.JsName import kotlin.jvm.JvmName @@ -13,7 +22,7 @@ private val defaultParser = TermParser.withDefaultOperators @JsName("termToClause") fun Term.toClause(source: Any? = null, line: Int = 0, column: Int = 0): Clause = - when(this) { + when (this) { is Clause -> this is Struct -> Fact.of(this) else -> throw InvalidTermTypeException(source, toString(), Clause::class, line, column) @@ -99,7 +108,6 @@ fun String.parseAsAtom(operators: OperatorSet): Atom = fun String.parseAsAtom(): Atom = defaultParser.parseAtom(this) - @JsName("parseNumericWithOperators") fun Numeric.Companion.parse(input: String, operators: OperatorSet): Numeric = defaultParser.parseNumeric(input, operators) @@ -116,7 +124,6 @@ fun String.parseAsNumeric(operators: OperatorSet): Numeric = fun String.parseAsNumeric(): Numeric = defaultParser.parseNumeric(this) - @JsName("parseIntegerWithOperators") fun Integer.Companion.parse(input: String, operators: OperatorSet): Integer = defaultParser.parseInteger(input, operators) @@ -133,7 +140,6 @@ fun String.parseAsInteger(operators: OperatorSet): Integer = fun String.parseAsInteger(): Integer = defaultParser.parseInteger(this) - @JsName("parseRealWithOperators") fun Real.Companion.parse(input: String, operators: OperatorSet): Real = defaultParser.parseReal(input, operators) @@ -164,4 +170,4 @@ fun String.parseAsClause(operators: OperatorSet): Clause = @JsName("parseStringAsClause") fun String.parseAsClause(): Clause = - defaultParser.parseClause(this) \ No newline at end of file + defaultParser.parseClause(this) diff --git a/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/AssertionUtils.kt b/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/AssertionUtils.kt index 6c5aef7a3..c246f7a62 100644 --- a/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/AssertionUtils.kt +++ b/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/AssertionUtils.kt @@ -13,7 +13,10 @@ fun assertTermsAreEqual(expected: Term, actual: Term) { assertEquals(expected.isGround, actual.isGround) if (expected.isGround) { assertEquals( - expected, actual, message = """Comparing: + expected, + actual, + message = + """Comparing: | actual: $actual | type: ${actual::class} | expected: $expected @@ -61,4 +64,4 @@ fun TermParser.assertTermIsCorrectlyParsed(stringToBeParsed: String, expected: T assertTermsAreEqual(actual, expected) if (loggingOn) println("".padEnd(80, '-')) -} \ No newline at end of file +} diff --git a/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/DoubleChecksOnEqualities.kt b/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/DoubleChecksOnEqualities.kt index 337dd6bff..3e4a3444a 100644 --- a/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/DoubleChecksOnEqualities.kt +++ b/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/DoubleChecksOnEqualities.kt @@ -13,83 +13,119 @@ class DoubleChecksOnEqualities { @Test fun testNumsAreEquals1() { - assertEquals(Integer.of(1), prolog { - 1.toTerm() - }) + assertEquals( + Integer.of(1), + prolog { + 1.toTerm() + } + ) } @Test fun testRealsInitialization() { - assertEquals(Real.of(3.1), Real.of("3.1")) + assertEquals( + Real.of(3.1), + Real.of("3" + ".1") + ) } @Test fun testNumsAreEquals2() { - assertEquals(Integer.of(1), prolog { - numOf(1) - }) + assertEquals( + Integer.of(1), + prolog { + numOf(1) + } + ) } @Test fun testNumsAreEquals3() { - assertEquals(Integer.of(1), prolog { - numOf(1L) - }) + assertEquals( + Integer.of(1), + prolog { + numOf(1L) + } + ) } @Test fun testNumsAreEquals4() { - assertEquals(Integer.of(1), prolog { - numOf(BigInteger.ONE) - }) + assertEquals( + Integer.of(1), + prolog { + numOf(BigInteger.ONE) + } + ) } @Test fun testNumsAreEquals5() { - assertEquals(Integer.of(1), prolog { - numOf(BigInteger.TEN / BigInteger.TEN) - }) + assertEquals( + Integer.of(1), + prolog { + numOf(BigInteger.TEN / BigInteger.TEN) + } + ) } @Test fun testListsAreEquals1() { - assertEquals(Cons.singleton(Integer.of(1)), prolog { - listOf(1) - }) + assertEquals( + Cons.singleton(Integer.of(1)), + prolog { + listOf(1) + } + ) } @Test fun testListsAreEquals2() { - assertEquals(Cons.singleton(Integer.of(1)), prolog { - listOf(numOf(1)) - }) + assertEquals( + Cons.singleton(Integer.of(1)), + prolog { + listOf(numOf(1)) + } + ) } @Test fun testListsAreEquals3() { - assertEquals(Cons.singleton(Integer.of(1)), prolog { - consOf(1, emptyList) - }) + assertEquals( + Cons.singleton(Integer.of(1)), + prolog { + consOf(1, emptyList) + } + ) } @Test fun testListsAreEquals4() { - assertEquals(Cons.singleton(Integer.of(1)), prolog { - consOf(numOf(1), emptyList) - }) + assertEquals( + Cons.singleton(Integer.of(1)), + prolog { + consOf(numOf(1), emptyList) + } + ) } @Test fun testListsAreEquals5() { - assertEquals(Cons.singleton(Integer.of(1)), prolog { - consOf(Integer.of(1), emptyList) - }) + assertEquals( + Cons.singleton(Integer.of(1)), + prolog { + consOf(Integer.of(1), emptyList) + } + ) } @Test fun testStructsAreEquals() { - assertEquals(Struct.of("+", Integer.of(1), Integer.of(2)), prolog { - 1.toTerm() + 2 - }) + assertEquals( + Struct.of("+", Integer.of(1), Integer.of(2)), + prolog { + 1.toTerm() + 2 + } + ) } -} \ No newline at end of file +} diff --git a/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/ParsingExamples.kt b/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/ParsingExamples.kt index 66dacb53d..f417da4c2 100644 --- a/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/ParsingExamples.kt +++ b/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/ParsingExamples.kt @@ -79,135 +79,148 @@ object ParsingExamples { 1.toTerm() + 2 }, "3 + 4 * 5 - 1" to prolog { - (3.toTerm() + ( 4.toTerm() * 5)).toTerm() - 1 + (3.toTerm() + (4.toTerm() * 5)).toTerm() - 1 }, "A; _" to prolog { "A" or `_` }, - "a; B :- 1" to prolog{ + "a; B :- 1" to prolog { "a" or "B" impliedBy 1 }, - "a :- 1; '2'" to prolog{ + "a :- 1; '2'" to prolog { "a" impliedBy (1 or "2") }, "a; B :- 1, 3.1; '2'" to prolog { "a" or "B" impliedBy - (1 and 3.1 or "2") + (1 and 3.1 or "2") }, "a, c(D); B :- 1, 3.1; '2'" to prolog { "a" and "c"("D") or "B" impliedBy - (1 and 3.1 or "2") + (1 and 3.1 or "2") }, "a; B, c(D) :- 1, \"4\"; '2', 3.1" to prolog { "a" or ("B" and "c"("D")) impliedBy - (1 and "4" or ("2" and 3.1)) + (1 and "4" or ("2" and 3.1)) }, "a, c(D); B, e(_f, [g]) :- 1; '2', 3.1" to prolog { - ("a" and "c"("D") or ("B" and "e"( "_f", listOf("g")))) impliedBy - (1 or ("2" and 3.1)) + ("a" and "c"("D") or ("B" and "e"("_f", listOf("g")))) impliedBy + (1 or ("2" and 3.1)) }, "a, 3 -> 5; 5.3, 1 -> 6 :- a; b, c" to prolog { - (("a" and 3) then 5 ) or (( 5.3 and 1) then 6) impliedBy - ("a" or ( "b" and "c" )) + (("a" and 3) then 5) or ((5.3 and 1) then 6) impliedBy + ("a" or ("b" and "c")) }, "first_step(X, [X])" to prolog { "first_step"("X", listOf("X")) }, - "sec_step(X,[_|L]) :- first_step(X,L)" to prolog{ + "sec_step(X,[_|L]) :- first_step(X,L)" to prolog { "sec_step"("X", consOf(`_`, "L")) impliedBy - ("first_step"("X", "L")) + ("first_step"("X", "L")) }, "last_but_one(X,[X,_])" to prolog { - "last_but_one"("X",listOf("X",`_`)) + "last_but_one"("X", listOf("X", `_`)) }, "last_but_one(X,[_,Y|Ys]) :- last_but_one(X,[Y|Ys])" to prolog { - "last_but_one"("X",consOf( `_`, consOf("Y","Ys"))) impliedBy - "last_but_one"("X",consOf("Y","Ys")) + "last_but_one"("X", consOf(`_`, consOf("Y", "Ys"))) impliedBy + "last_but_one"("X", consOf("Y", "Ys")) }, "element_at(X,[_|L],K) :- K > 1, K1 is K - 1, element_at(X,L,K1)" to prolog { - "element_at"("X",consOf(`_`,"L"),"K") impliedBy - (("K" greaterThan 1) and (("K1" `is` ("K" - 1)) and ("element_at"("X","L","K1")))) + "element_at"("X", consOf(`_`, "L"), "K") impliedBy + (("K" greaterThan 1) and (("K1" `is` ("K" - 1)) and ("element_at"("X", "L", "K1")))) }, "my_length([],0)" to prolog { - "my_length"(emptyList,0) + "my_length"(emptyList, 0) }, "my_length([_|L],N) :- my_length(L,N1), N is N1 + 1" to prolog { - "my_length"(consOf(`_`,"L"),"N") impliedBy - ( "my_length"("L", "N1") and (( "N" `is` ("N1".toTerm() + 1)))) + "my_length"(consOf(`_`, "L"), "N") impliedBy + ("my_length"("L", "N1") and (("N" `is` ("N1".toTerm() + 1)))) }, "my_reverse(L1,L2) :- my_rev(L1,L2,[])" to prolog { - "my_reverse"("L1","L2") impliedBy - "my_rev"("L1", "L2", emptyList) + "my_reverse"("L1", "L2") impliedBy + "my_rev"("L1", "L2", emptyList) }, "my_rev([],L2,L2) :- !" to prolog { - "my_rev"(emptyList,"L2","L2") impliedBy "!" + "my_rev"(emptyList, "L2", "L2") impliedBy "!" }, "my_rev([X|Xs],L2,Acc) :- my_rev(Xs,L2,[X|Acc])" to prolog { - "my_rev"(consOf("X","Xs"), "L2","Acc") impliedBy - "my_rev"("Xs","L2",consOf("X","Acc")) + "my_rev"(consOf("X", "Xs"), "L2", "Acc") impliedBy + "my_rev"("Xs", "L2", consOf("X", "Acc")) }, "is_palindrome(L) :- reverse(L,L)" to prolog { "is_palindrome"("L") impliedBy - "reverse"("L","L") + "reverse"("L", "L") }, "my_flatten(X,[X]) :- \\+ is_list(X)" to prolog { - "my_flatten"("X",listOf("X")) impliedBy - "\\+"("is_list"("X")) + "my_flatten"("X", listOf("X")) impliedBy + "\\+"("is_list"("X")) }, "my_flatten([],[])" to prolog { "my_flatten"(emptyList, emptyList) }, "my_flatten([X|Xs],Zs) :- my_flatten(X,Y), my_flatten(Xs,Ys), append(Y,Ys,Zs)" to prolog { - "my_flatten"(consOf("X","Xs"),"Zs") impliedBy - ( "my_flatten"("X","Y") and ( "my_flatten"("Xs","Ys") and "append"("Y","Ys","Zs"))) + "my_flatten"(consOf("X", "Xs"), "Zs") impliedBy + ("my_flatten"("X", "Y") and ("my_flatten"("Xs", "Ys") and "append"("Y", "Ys", "Zs"))) }, "compress([X,Y|Ys],[X|Zs]) :- X \\= Y, compress([Y|Ys],Zs)" to prolog { - "compress"(consOf("X",consOf("Y","Ys")),consOf("X","Zs")) impliedBy - ("\\="("X","Y") and "compress"(consOf("Y","Ys"),"Zs")) + "compress"(consOf("X", consOf("Y", "Ys")), consOf("X", "Zs")) impliedBy + ("\\="("X", "Y") and "compress"(consOf("Y", "Ys"), "Zs")) }, "count(X,[],[],N,[N,X]) :- N > 1" to prolog { - "count"("X", emptyList, emptyList,"N",consOf("N","X")) impliedBy - ("N" greaterThan 1) + "count"("X", emptyList, emptyList, "N", consOf("N", "X")) impliedBy + ("N" greaterThan 1) }, "count(X,[Y|Ys],[Y|Ys],1,X) :- X \\= Y" to prolog { - "count"("X",consOf("Y","Ys"),consOf("Y","Ys"),1,"X") impliedBy - ( "\\="("X","Y")) + "count"("X", consOf("Y", "Ys"), consOf("Y", "Ys"), 1, "X") impliedBy + ("\\="("X", "Y")) }, "count(X,[Y|Ys],[Y|Ys],N,[N,X]) :- N > 1, X \\= Y" to prolog { - "count"("X", consOf("Y","Ys"),consOf("Y","Ys"),"N",consOf("N","X")) impliedBy - (("N" greaterThan 1) and ( "\\="("X","Y"))) + "count"("X", consOf("Y", "Ys"), consOf("Y", "Ys"), "N", consOf("N", "X")) impliedBy + (("N" greaterThan 1) and ("\\="("X", "Y"))) }, "count(X,[X|Xs],Ys,K,T) :- K1 is K + 1, count(X,Xs,Ys,K1,T)" to prolog { - "count"("X",consOf("X","Xs"),"Ys","K","T") impliedBy - (("K1" `is` ("K".toTerm() + 1)) and ( "count"("X","Xs","Ys","K1","T"))) + "count"("X", consOf("X", "Xs"), "Ys", "K", "T") impliedBy + (("K1" `is` ("K".toTerm() + 1)) and ("count"("X", "Xs", "Ys", "K1", "T"))) }, "map_upper_bound(XMax, YMax) :- map_size(XSize, YSize), XMax is XSize - 1, YMax is YSize - 1" to prolog { - "map_upper_bound"("XMax","YMax") impliedBy - ( "map_size"("XSize","YSize") and (("XMax" `is` ("XSize" - 1)) and ("YMax" `is` ("YSize" - 1)))) + "map_upper_bound"("XMax", "YMax") impliedBy + ("map_size"("XSize", "YSize") and (("XMax" `is` ("XSize" - 1)) and ("YMax" `is` ("YSize" - 1)))) }, "in_map(X, Y) :- X >= 0, Y >= 0, map_size(XSize, YSize), X < XSize, Y < YSize" to prolog { - "in_map"("X","Y") impliedBy - (("X" greaterThanOrEqualsTo 0) and (("Y" greaterThanOrEqualsTo 0) and ( "map_size"("XSize","YSize") and (("X" lowerThan ("XSize")) and ("Y" lowerThan "YSize"))))) + "in_map"("X", "Y") impliedBy + (("X" greaterThanOrEqualsTo 0) and (("Y" greaterThanOrEqualsTo 0) and ("map_size"("XSize", "YSize") and (("X" lowerThan ("XSize")) and ("Y" lowerThan "YSize"))))) }, "tile(wall, X, Y) :- \\+ in_map(X, Y)" to prolog { - "tile"("wall","X","Y") impliedBy - ( "\\+"("in_map"("X","Y"))) + "tile"("wall", "X", "Y") impliedBy + ("\\+"("in_map"("X", "Y"))) }, "draw_char(X, Y) :- tty_size(_, XSize), X >= XSize, NY is Y + 1, draw_char(0, NY)" to prolog { - "draw_char"("X","Y") impliedBy - ( "tty_size"(`_`,"XSize") and (("X" greaterThanOrEqualsTo "XSize") and (("NY" `is` ("Y".toTerm() + 1)) and ("draw_char"(0,"NY"))))) + "draw_char"("X", "Y") impliedBy + ("tty_size"(`_`, "XSize") and (("X" greaterThanOrEqualsTo "XSize") and (("NY" `is` ("Y".toTerm() + 1)) and ("draw_char"(0, "NY"))))) }, "Y < YMsgs -> write(' ') ; display_offset(XOff, YOff), XMap is X + XOff, YMap is Y + YOff, get_character(XMap, YMap, C), format('~s', [C])" to prolog { (("Y" lowerThan "YMsgs") then "write"(" ")) or - ( "display_offset"("XOff","YOff") and (("XMap" `is` ("X".toTerm() + "XOff")) and (("YMap" `is` ("Y".toTerm() + "YOff")) and - ( "get_character"("XMap","YMap","C") and ( "format"("~s",listOf("C"))))))) + ( + "display_offset"("XOff", "YOff") and ( + ("XMap" `is` ("X".toTerm() + "XOff")) and ( + ("YMap" `is` ("Y".toTerm() + "YOff")) and + ("get_character"("XMap", "YMap", "C") and ("format"("~s", listOf("C")))) + ) + ) + ) }, "display_offset(X, Y) :- player(XPos, YPos), tty_size(YSize, XSize), message_lines(YMsgs), X is XPos - floor(XSize / 2), Y is YPos - floor((YSize - YMsgs) / 2)" to prolog { - "display_offset"("X","Y") impliedBy - ( "player"("XPos","YPos") and ( "tty_size"("YSize","XSize")and ( "message_lines"("YMsgs") and ( - ( "X" `is` ("XPos" - "floor"("XSize" / 2))) and (("Y" `is` ("YPos" - "floor"(("YSize" - "YMsgs") / 2)))))))) + "display_offset"("X", "Y") impliedBy + ( + "player"("XPos", "YPos") and ( + "tty_size"("YSize", "XSize")and ( + "message_lines"("YMsgs") and ( + ("X" `is` ("XPos" - "floor"("XSize" / 2))) and (("Y" `is` ("YPos" - "floor"(("YSize" - "YMsgs") / 2)))) + ) + ) + ) + ) } ) val all = canonicalTerms + expressions -} \ No newline at end of file +} diff --git a/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/TermParserTest.kt b/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/TermParserTest.kt index c11019e69..9f726b539 100644 --- a/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/TermParserTest.kt +++ b/parser-core/src/commonTest/kotlin/it/unibo/tuprolog/core/parsing/test/TermParserTest.kt @@ -22,5 +22,4 @@ class TermParserTest { parser.assertTermIsCorrectlyParsed(it.first, it.second) } } - -} \ No newline at end of file +} diff --git a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/Conversions.kt b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/Conversions.kt index f2bbee745..eddfccee9 100644 --- a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/Conversions.kt +++ b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/Conversions.kt @@ -6,4 +6,4 @@ fun String.toSpecifier(): Specifier = Specifier.valueOf(toUpperCase()) fun Specifier.toAssociativity(): String = - toString() \ No newline at end of file + toString() diff --git a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/DynamicOpListener.kt b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/DynamicOpListener.kt index cb127674c..3dab80a74 100644 --- a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/DynamicOpListener.kt +++ b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/DynamicOpListener.kt @@ -60,5 +60,4 @@ class DynamicOpListener private constructor( return DynamicOpListener(parser, operatorDefinedCallback) } } - -} \ No newline at end of file +} diff --git a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/PrologParserFactory.kt b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/PrologParserFactory.kt index 98b07b58a..bc1282fb2 100644 --- a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/PrologParserFactory.kt +++ b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/PrologParserFactory.kt @@ -1,7 +1,21 @@ package it.unibo.tuprolog.core.parsing import it.unibo.tuprolog.core.operators.OperatorSet -import it.unibo.tuprolog.parser.* +import it.unibo.tuprolog.parser.BailErrorStrategy +import it.unibo.tuprolog.parser.ClauseContext +import it.unibo.tuprolog.parser.CommonTokenStream +import it.unibo.tuprolog.parser.ErrorListener +import it.unibo.tuprolog.parser.ErrorStrategy +import it.unibo.tuprolog.parser.InputStream +import it.unibo.tuprolog.parser.OptClauseContext +import it.unibo.tuprolog.parser.PredictionMode +import it.unibo.tuprolog.parser.PrologLexer +import it.unibo.tuprolog.parser.PrologParser +import it.unibo.tuprolog.parser.RecognitionException +import it.unibo.tuprolog.parser.SingletonExpressionContext +import it.unibo.tuprolog.parser.Token +import it.unibo.tuprolog.parser.isParseCancellationException +import it.unibo.tuprolog.parser.isRecognitionException object PrologParserFactory { @@ -75,7 +89,6 @@ object PrologParserFactory { } } - fun parseExpressionWithStandardOperators(string: String): SingletonExpressionContext = parseExpression(string, OperatorSet.DEFAULT) @@ -84,7 +97,6 @@ object PrologParserFactory { return parseClauses(parser, source) } - fun parseClauses(source: String): Sequence = parseClauses(source, OperatorSet.EMPTY) @@ -156,5 +168,4 @@ object PrologParserFactory { }.takeWhile { !it.isOver } .map { it.clause() } } - -} \ No newline at end of file +} diff --git a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/PrologVisitor.kt b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/PrologVisitor.kt index c85559678..f7fa13ed3 100644 --- a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/PrologVisitor.kt +++ b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/PrologVisitor.kt @@ -1,7 +1,9 @@ package it.unibo.tuprolog.core.parsing -import it.unibo.tuprolog.core.* -import it.unibo.tuprolog.parser.* +import it.unibo.tuprolog.core.Integer +import it.unibo.tuprolog.core.Real +import it.unibo.tuprolog.core.Scope +import it.unibo.tuprolog.core.Term import it.unibo.tuprolog.parser.Associativity.FX import it.unibo.tuprolog.parser.Associativity.FY import it.unibo.tuprolog.parser.Associativity.INFIX @@ -12,8 +14,21 @@ import it.unibo.tuprolog.parser.Associativity.XFX import it.unibo.tuprolog.parser.Associativity.XFY import it.unibo.tuprolog.parser.Associativity.YF import it.unibo.tuprolog.parser.Associativity.YFX +import it.unibo.tuprolog.parser.ClauseContext +import it.unibo.tuprolog.parser.ExpressionContext +import it.unibo.tuprolog.parser.IntegerContext +import it.unibo.tuprolog.parser.ListContext +import it.unibo.tuprolog.parser.NumberContext +import it.unibo.tuprolog.parser.OuterContext +import it.unibo.tuprolog.parser.PrologParserVisitor +import it.unibo.tuprolog.parser.RealContext +import it.unibo.tuprolog.parser.SetContext +import it.unibo.tuprolog.parser.SingletonExpressionContext +import it.unibo.tuprolog.parser.SingletonTermContext +import it.unibo.tuprolog.parser.StructureContext +import it.unibo.tuprolog.parser.TermContext +import it.unibo.tuprolog.parser.VariableContext import org.gciatto.kt.math.BigInteger -import kotlin.collections.List class PrologVisitor : PrologParserVisitor() { private val scope: Scope = Scope.empty() @@ -34,8 +49,9 @@ class PrologVisitor : PrologParserVisitor() { INFIX.contains(ctx.associativity) -> visitInfixExpression(ctx) POSTFIX.contains(ctx.associativity) -> visitPostfixExpression(ctx) PREFIX.contains(ctx.associativity) -> visitPrefixExpression(ctx) - else -> throw IllegalArgumentException("Associativity unknown: ${ctx.associativity} INFIX=${INFIX} PREFIX=${PREFIX} POSTFIX=${POSTFIX}") - }, flatten(ctx.outers.asList()) + else -> throw IllegalArgumentException("Associativity unknown: ${ctx.associativity} INFIX=$INFIX PREFIX=$PREFIX POSTFIX=$POSTFIX") + }, + flatten(ctx.outers.asList()) ) } @@ -46,7 +62,6 @@ class PrologVisitor : PrologParserVisitor() { ctx.children[0].accept(this) as Term } - override fun visitInteger(ctx: IntegerContext): Term { val value = parseInteger(ctx) return Integer.of(value) @@ -79,10 +94,11 @@ class PrologVisitor : PrologParserVisitor() { } override fun visitStructure(ctx: StructureContext): Term { - if (ctx.isList) + if (ctx.isList) { return scope.listOf() - else if (ctx.isSet) + } else if (ctx.isSet) { return scope.setOf() + } return if (ctx.arity == 0) { scope.atomOf(ctx.functor.text) } else { @@ -103,10 +119,11 @@ class PrologVisitor : PrologParserVisitor() { } override fun visitSet(ctx: SetContext): Term { - return if (ctx.length == 1) + return if (ctx.length == 1) { scope.setOf(ctx.items[0].accept(this)) - else + } else { scope.setOf(ctx.items.map(this::visitExpression)) + } } private fun parseInteger(ctx: IntegerContext): BigInteger { @@ -154,9 +171,12 @@ class PrologVisitor : PrologParserVisitor() { } private fun visitPostfixExpression(ctx: ExpressionContext): Term = - postfix(ctx.left?.accept(this)!!, ctx.operators.map { - it.symbol.text - }) + postfix( + ctx.left?.accept(this)!!, + ctx.operators.map { + it.symbol.text + } + ) private fun postfix(term: Term, ops: List): Term { val operator = ops.iterator() @@ -168,9 +188,12 @@ class PrologVisitor : PrologParserVisitor() { } private fun visitPrefixExpression(ctx: ExpressionContext): Term = - prefix(ctx.right[0].accept(this), ctx.operators.map { - it.symbol.text - }) + prefix( + ctx.right[0].accept(this), + ctx.operators.map { + it.symbol.text + } + ) private fun prefix(term: Term, ops: List): Term { var i = ops.size - 1 @@ -218,7 +241,7 @@ class PrologVisitor : PrologParserVisitor() { return result } - //Prova refactoring List Kotlin + // Prova refactoring List Kotlin private fun infixRight(terms: List, ops: List): Term { var i = terms.size - 1 var j = ops.size - 1 @@ -248,7 +271,6 @@ class PrologVisitor : PrologParserVisitor() { private fun listOfOperators(ctx: ExpressionContext): List = ctx.operators.map { it.symbol.text } - private fun visitInfixRightAssociativeExpression(ctx: ExpressionContext): Term = infixRight(listOfOperands(ctx), listOfOperators(ctx)) @@ -262,4 +284,4 @@ class PrologVisitor : PrologParserVisitor() { private fun flatten(outers: List): List = flatten(outers.asSequence()).toList() -} \ No newline at end of file +} diff --git a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt index 7f5677585..2a9c9741d 100644 --- a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt +++ b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt @@ -3,4 +3,4 @@ package it.unibo.tuprolog.core.parsing import it.unibo.tuprolog.core.operators.OperatorSet actual fun termParserWithOperators(operators: OperatorSet): TermParser = - TermParserImpl(operators) \ No newline at end of file + TermParserImpl(operators) diff --git a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserImpl.kt b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserImpl.kt index f226d6af6..eab2e517a 100644 --- a/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserImpl.kt +++ b/parser-core/src/jsMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserImpl.kt @@ -7,4 +7,4 @@ class TermParserImpl(override val defaultOperatorSet: OperatorSet) : TermParser override fun parseTerm(input: String, operators: OperatorSet): Term { return PrologParserFactory.parseExpression(input, operators).accept(PrologVisitor()) } -} \ No newline at end of file +} diff --git a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/Conversions.kt b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/Conversions.kt index 0b1262e76..7a38a2200 100644 --- a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/Conversions.kt +++ b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/Conversions.kt @@ -23,4 +23,4 @@ fun Specifier.toAssociativity(): Associativity = Specifier.XFX -> Associativity.XFX Specifier.YFX -> Associativity.YFX Specifier.XFY -> Associativity.XFY - } \ No newline at end of file + } diff --git a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/DynamicOpListener.kt b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/DynamicOpListener.kt index 16ae9c384..b6bc15870 100644 --- a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/DynamicOpListener.kt +++ b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/DynamicOpListener.kt @@ -68,5 +68,4 @@ class DynamicOpListener private constructor( return DynamicOpListener(parser, operatorDefinedCallback) } } - -} \ No newline at end of file +} diff --git a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/Echo.kt b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/Echo.kt index e651f6eff..9f983030e 100644 --- a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/Echo.kt +++ b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/Echo.kt @@ -1,4 +1,5 @@ @file:JvmName("Echo") + package it.unibo.tuprolog.core.parsing import it.unibo.tuprolog.core.TermFormatter @@ -13,4 +14,4 @@ fun main() { val term = parser.parseTerm(line!!) println(term.format(TermFormatter.prettyExpressions())) } -} \ No newline at end of file +} diff --git a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/PrologExpressionVisitor.kt b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/PrologExpressionVisitor.kt index 8c27aafce..c61c3fa95 100644 --- a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/PrologExpressionVisitor.kt +++ b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/PrologExpressionVisitor.kt @@ -5,10 +5,16 @@ import it.unibo.tuprolog.core.Scope import it.unibo.tuprolog.core.Term import it.unibo.tuprolog.parser.PrologParser import it.unibo.tuprolog.parser.PrologParserBaseVisitor -import it.unibo.tuprolog.parser.dynamic.Associativity.* import it.unibo.tuprolog.parser.dynamic.Associativity.Companion.INFIX import it.unibo.tuprolog.parser.dynamic.Associativity.Companion.POSTFIX import it.unibo.tuprolog.parser.dynamic.Associativity.Companion.PREFIX +import it.unibo.tuprolog.parser.dynamic.Associativity.FX +import it.unibo.tuprolog.parser.dynamic.Associativity.FY +import it.unibo.tuprolog.parser.dynamic.Associativity.XF +import it.unibo.tuprolog.parser.dynamic.Associativity.XFX +import it.unibo.tuprolog.parser.dynamic.Associativity.XFY +import it.unibo.tuprolog.parser.dynamic.Associativity.YF +import it.unibo.tuprolog.parser.dynamic.Associativity.YFX import org.gciatto.kt.math.BigInteger class PrologExpressionVisitor : PrologParserBaseVisitor() { @@ -33,14 +39,14 @@ class PrologExpressionVisitor : PrologParserBaseVisitor() { PREFIX.contains(ctx.associativity) -> visitPrefixExpression(ctx) ctx.exception != null -> throw ctx.exception else -> throw IllegalArgumentException() // use kotlin's IllegalArgumentException - }, flatten(ctx.outers) + }, + flatten(ctx.outers) ) } override fun visitTerm(ctx: PrologParser.TermContext): Term = if (ctx.isExpr) visitExpression(ctx.expression()) else ctx.children[0].accept(this) - override fun visitInteger(ctx: PrologParser.IntegerContext): Term { val value = parseInteger(ctx) return scope.numOf(value) @@ -66,10 +72,11 @@ class PrologExpressionVisitor : PrologParserBaseVisitor() { } override fun visitStructure(ctx: PrologParser.StructureContext): Term { - if (ctx.isList) + if (ctx.isList) { return scope.listOf() - else if (ctx.isSet) + } else if (ctx.isSet) { return scope.setOf() + } return if (ctx.arity == 0) { scope.atomOf(ctx.functor.text) } else { @@ -90,10 +97,11 @@ class PrologExpressionVisitor : PrologParserBaseVisitor() { } override fun visitSet(ctx: PrologParser.SetContext): Term { - return if (ctx.length == 1) + return if (ctx.length == 1) { scope.setOf(ctx.items[0].accept(this)) - else + } else { scope.setOf(ctx.items.map(this::visitExpression)) + } } @Suppress("NullableBooleanElvis", "UNNECESSARY_SAFE_CALL") @@ -146,9 +154,12 @@ class PrologExpressionVisitor : PrologParserBaseVisitor() { } private fun visitPostfixExpression(ctx: PrologParser.ExpressionContext): Term = - postfix(ctx.left.accept(this), ctx.operators.map { - it.symbol.text - }) + postfix( + ctx.left.accept(this), + ctx.operators.map { + it.symbol.text + } + ) private fun postfix(term: Term, ops: List): Term { val operator = ops.iterator() @@ -160,9 +171,12 @@ class PrologExpressionVisitor : PrologParserBaseVisitor() { } private fun visitPrefixExpression(ctx: PrologParser.ExpressionContext): Term = - prefix(ctx.right[0].accept(this), ctx.operators.map { - it.symbol.text - }) + prefix( + ctx.right[0].accept(this), + ctx.operators.map { + it.symbol.text + } + ) private fun prefix(term: Term, ops: List): Term { var i = ops.size - 1 @@ -238,7 +252,6 @@ class PrologExpressionVisitor : PrologParserBaseVisitor() { private fun listOfOperators(ctx: PrologParser.ExpressionContext): List = ctx.operators.map { it.symbol.text } - private fun visitInfixRightAssociativeExpression(ctx: PrologParser.ExpressionContext): Term = infixRight(listOfOperands(ctx), listOfOperators(ctx)) @@ -252,4 +265,4 @@ class PrologExpressionVisitor : PrologParserBaseVisitor() { private fun flatten(outers: List): List = flatten(outers.asSequence()).toList() -} \ No newline at end of file +} diff --git a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/PrologParserFactory.kt b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/PrologParserFactory.kt index 11b4b43ff..842fa9fe1 100644 --- a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/PrologParserFactory.kt +++ b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/PrologParserFactory.kt @@ -3,7 +3,17 @@ package it.unibo.tuprolog.core.parsing import it.unibo.tuprolog.core.operators.OperatorSet import it.unibo.tuprolog.parser.PrologLexer import it.unibo.tuprolog.parser.PrologParser -import org.antlr.v4.runtime.* +import org.antlr.v4.runtime.ANTLRErrorListener +import org.antlr.v4.runtime.BailErrorStrategy +import org.antlr.v4.runtime.BaseErrorListener +import org.antlr.v4.runtime.BufferedTokenStream +import org.antlr.v4.runtime.CharStream +import org.antlr.v4.runtime.CharStreams +import org.antlr.v4.runtime.DefaultErrorStrategy +import org.antlr.v4.runtime.RecognitionException +import org.antlr.v4.runtime.Recognizer +import org.antlr.v4.runtime.Token +import org.antlr.v4.runtime.TokenStream import org.antlr.v4.runtime.atn.PredictionMode import org.antlr.v4.runtime.misc.ParseCancellationException import java.io.InputStream @@ -268,5 +278,4 @@ object PrologParserFactory { }.takeWhile { !it.isOver } .map { it.clause() } } - -} \ No newline at end of file +} diff --git a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt index 7f5677585..2a9c9741d 100644 --- a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt +++ b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserExtensions.kt @@ -3,4 +3,4 @@ package it.unibo.tuprolog.core.parsing import it.unibo.tuprolog.core.operators.OperatorSet actual fun termParserWithOperators(operators: OperatorSet): TermParser = - TermParserImpl(operators) \ No newline at end of file + TermParserImpl(operators) diff --git a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserImpl.kt b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserImpl.kt index 6b38829d6..200219845 100644 --- a/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserImpl.kt +++ b/parser-core/src/jvmMain/kotlin/it/unibo/tuprolog/core/parsing/TermParserImpl.kt @@ -8,4 +8,4 @@ class TermParserImpl(override val defaultOperatorSet: OperatorSet) : TermParser override fun parseTerm(input: String, operators: OperatorSet): Term { return PrologParserFactory.parseExpression(input, operators).accept(PrologExpressionVisitor()) } -} \ No newline at end of file +}