From 60db9f3b9520d14180ede2a175b6dc38722389cc Mon Sep 17 00:00:00 2001 From: sergeych Date: Mon, 19 May 2025 11:26:00 +0400 Subject: [PATCH] string concatenation --- .../kotlin/net/sergeych/ling/Compiler.kt | 2 ++ .../kotlin/net/sergeych/ling/statements.kt | 2 +- library/src/commonTest/kotlin/ScriptTest.kt | 30 ++++++++++++------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/library/src/commonMain/kotlin/net/sergeych/ling/Compiler.kt b/library/src/commonMain/kotlin/net/sergeych/ling/Compiler.kt index a41ebcf..ba891b7 100644 --- a/library/src/commonMain/kotlin/net/sergeych/ling/Compiler.kt +++ b/library/src/commonMain/kotlin/net/sergeych/ling/Compiler.kt @@ -148,6 +148,8 @@ class Compiler { } } + Token.Type.STRING -> statement(t.pos, true) { ObjString(t.value) } + Token.Type.LPAREN -> { // ( subexpr ) parseExpression(tokens)?.also { diff --git a/library/src/commonMain/kotlin/net/sergeych/ling/statements.kt b/library/src/commonMain/kotlin/net/sergeych/ling/statements.kt index 36a4f15..f14616f 100644 --- a/library/src/commonMain/kotlin/net/sergeych/ling/statements.kt +++ b/library/src/commonMain/kotlin/net/sergeych/ling/statements.kt @@ -75,7 +75,7 @@ class PlusStatement( val l = left.execute(context) if (l is ObjString) - return ObjString(l.toString() + right.execute(context).toString()) + return ObjString(l.toString() + right.execute(context).asStr) if (l !is Numeric) raise("left operand is not number: $l") diff --git a/library/src/commonTest/kotlin/ScriptTest.kt b/library/src/commonTest/kotlin/ScriptTest.kt index da8415b..35e1eee 100644 --- a/library/src/commonTest/kotlin/ScriptTest.kt +++ b/library/src/commonTest/kotlin/ScriptTest.kt @@ -8,7 +8,7 @@ import kotlin.test.* class ScriptTest { @Test - fun parseNumbers() { + fun parseNumbersTest() { fun check(expected: String, type: Token.Type, row: Int, col: Int, src: String, offset: Int = 0) { val source = src.toSource() assertEquals( @@ -48,7 +48,7 @@ class ScriptTest { } @Test - fun parse0() { + fun parse0Test() { val src = """ println("Hello") println( "world" ) @@ -68,7 +68,7 @@ class ScriptTest { } @Test - fun parse1() { + fun parse1Test() { val src = "2 + 7".toSource() val p = parseLing(src).listIterator() @@ -79,7 +79,7 @@ class ScriptTest { } @Test - fun compileNumbers() = runTest { + fun compileNumbersTest() = runTest { assertEquals(ObjInt(17), eval("17")) assertEquals(ObjInt(17), eval("+17")) assertEquals(ObjInt(-17), eval("-17")) @@ -95,7 +95,7 @@ class ScriptTest { } @Test - fun compileBuiltinCalls() = runTest { + fun compileBuiltinCallsTest() = runTest { // println(eval("π")) val pi = eval("Math.PI") assertIs(pi) @@ -107,7 +107,7 @@ class ScriptTest { } @Test - fun varsAndConsts() = runTest { + fun varsAndConstsTest() = runTest { val context = Context() assertEquals(ObjVoid,context.eval(""" val a = 17 @@ -171,7 +171,7 @@ class ScriptTest { } @Test - fun testArithmeticOperators() = runTest { + fun arithmeticOperatorsTest() = runTest { assertEquals(2, eval("5/2").toInt()) assertEquals(2.5, eval("5.0/2").toDouble()) assertEquals(2.5, eval("5/2.0").toDouble()) @@ -190,7 +190,7 @@ class ScriptTest { } @Test - fun testArithmeticParenthesis() = runTest { + fun arithmeticParenthesisTest() = runTest { assertEquals(17, eval("2 + 3 * 5").toInt()) assertEquals(17, eval("2 + (3 * 5)").toInt()) assertEquals(25, eval("(2 + 3) * 5").toInt()) @@ -198,7 +198,13 @@ class ScriptTest { } @Test - fun testEqNeq() = runTest { + fun stringOpTest() = runTest { + assertEquals("foobar", eval(""" "foo" + "bar" """).toString()) + assertEquals("foo17", eval(""" "foo" + 17 """).toString()) + } + + @Test + fun eqNeqTest() = runTest { assertEquals(ObjBool(true), eval("val x = 2; x == 2")) assertEquals(ObjBool(false), eval("val x = 3; x == 2")) assertEquals(ObjBool(true), eval("val x = 3; x != 2")) @@ -214,7 +220,7 @@ class ScriptTest { } @Test - fun testGtLt() = runTest { + fun gtLtTest() = runTest { assertTrue { eval("3 > 2").toBool() } assertFalse { eval("3 > 3").toBool() } assertTrue { eval("3 >= 2").toBool() } @@ -228,5 +234,9 @@ class ScriptTest { assertFalse { eval("4 <= 3").toBool()} } + @Test + fun ifTest() = runTest { + + } } \ No newline at end of file