- fixed CLI -x mode
- fixed a wrong error message when global symbol is not found
This commit is contained in:
1 parent
6e9333844e
commit
c097464750
13 files changed
+426
-77
No files matched your search
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.ScriptError
|
||||
import net.sergeych.lyng.eval
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContains
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
|
||||
class CalculusDiagnosticRegressionTest {
|
||||
|
||||
@Test
|
||||
fun undefinedHInCalculusSnippetReportsPreciseDiagnostic() = runTest {
|
||||
val ex = assertFailsWith<ScriptError> {
|
||||
eval(
|
||||
"""
|
||||
var x = 7.0
|
||||
// глубина по звуку падения
|
||||
val m = 1 // kg
|
||||
val d = 0.06 // 6 cm
|
||||
val c = 340 // скор. звука
|
||||
val g = 9.82
|
||||
var cnt = 0
|
||||
var t = x
|
||||
var message = ""
|
||||
val hinv = 1/h
|
||||
var h = c*c/g*(1 + g*t/c -sqrt(1+2*g*t/c))
|
||||
assert(h is Real)
|
||||
assert(!h.isNaN())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
assertEquals(9, ex.pos.line)
|
||||
assertEquals(13, ex.pos.column)
|
||||
assertContains(ex.errorMessage, "symbol 'h' is not defined")
|
||||
assertContains(ex.message ?: "", "val hinv = 1/h")
|
||||
}
|
||||
}
|
||||
@@ -5474,33 +5474,112 @@ class ScriptTest {
|
||||
)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun testFromCalcrus1() = runTest {
|
||||
// eval($$"""
|
||||
// import lyng.decimal
|
||||
// var x = 7.0.d
|
||||
// // глубина по звуку падения
|
||||
// val m = 1 // kg
|
||||
// val d = 0.06 // 6 cm
|
||||
// val c = 340 // скор. звука
|
||||
// val g = 9.82
|
||||
// var cnt = 0
|
||||
// var h = 0.0
|
||||
// var t = x
|
||||
// var message = ""
|
||||
//
|
||||
// while(true){
|
||||
// val h0 = 0
|
||||
// h = c*c/h*(1 + g*t/c -sqrt(1+2*g*t/c))
|
||||
// message = "iter ${cnt++}"
|
||||
// if( cnt > 100 ) {
|
||||
// message= "ошибка"
|
||||
// break 0
|
||||
// }
|
||||
// x = h
|
||||
// if( abs(h-h0)/h > 0.08 ) break h
|
||||
// }
|
||||
// println(x)
|
||||
// """.trimIndent())
|
||||
// }
|
||||
@Test
|
||||
fun testFromCalcurus1() = runTest {
|
||||
eval($$"""
|
||||
|
||||
/*
|
||||
Рассчитывает глубину провала по времени падения камня и прихода звука.
|
||||
|
||||
@param T измеренное полное время (с)
|
||||
@param m масса камня (кг)
|
||||
@param d диаметр камня (м) (предполагается сферическая форма)
|
||||
@param rho плотность воздуха (кг/м³), по умолчанию 1.2
|
||||
@param c скорость звука (м/с), по умолчанию 340.0
|
||||
@param g ускорение свободного падения (м/с²), по умолчанию 9.81
|
||||
@param Cd коэффициент лобового сопротивления, по умолчанию 0.5
|
||||
@param epsilon относительная точность (м), по умолчанию 1e-3
|
||||
@param maxIter максимальное число итераций, по умолчанию 20
|
||||
@return глубина h (м), или null если расчёт не сошёлся
|
||||
*/
|
||||
fun calculateDepth(
|
||||
T: Real,
|
||||
m: Real,
|
||||
d: Real,
|
||||
rho: Real = 1.2,
|
||||
c: Real = 340.0,
|
||||
g: Real = 9.81,
|
||||
Cd: Real = 0.5,
|
||||
epsilon: Real = 1e-3,
|
||||
maxIter: Int = 20
|
||||
): Real? {
|
||||
// Площадь миделя
|
||||
val r = d / 2.0
|
||||
val A = π * r * r
|
||||
|
||||
// Коэффициент сопротивления
|
||||
val k = 0.5 * Cd * rho * A
|
||||
|
||||
// Предельная скорость
|
||||
val vTerm = sqrt(m * g / k)
|
||||
|
||||
// Функция времени падения с высоты h
|
||||
fun tFall(h: Real): Real {
|
||||
val arg = exp(g * h / (vTerm * vTerm))
|
||||
// arcosh(x) = ln(x + sqrt(x^2 - 1))
|
||||
val arcosh = ln(arg + sqrt(arg * arg - 1.0))
|
||||
return vTerm / g * arcosh
|
||||
}
|
||||
|
||||
// Производная времени падения по h
|
||||
fun dtFall_dh(h: Real): Real {
|
||||
val expArg = exp(2.0 * g * h / (vTerm * vTerm))
|
||||
return 1.0 / (vTerm * sqrt(expArg - 1.0))
|
||||
}
|
||||
|
||||
// Полное расчётное время T_calc(h) = tFall(h) + h/c
|
||||
fun Tcalc(h: Real): Real = tFall(h) + h / c
|
||||
|
||||
// Производная T_calc по h
|
||||
fun dTcalc_dh(h: Real): Real = dtFall_dh(h) + 1.0 / c
|
||||
|
||||
// Начальное приближение (без сопротивления)
|
||||
val term = 1.0 + g * T / c
|
||||
val sqrtTerm = sqrt(1.0 + 2.0 * g * T / c)
|
||||
var h = (c * c / g) * (term - sqrtTerm)
|
||||
|
||||
// Проверка на валидность начального приближения
|
||||
if (h.isNaN() || h <= 0.0) {
|
||||
// Если формула дала некорректный результат, используем оценку по свободному падению
|
||||
h = 0.5 * g * T * T // грубая оценка, всё равно будет уточняться
|
||||
if (h.isNaN() || h <= 0.0) return null
|
||||
}
|
||||
|
||||
// Итерации Ньютона
|
||||
var iter = 0
|
||||
while (iter < maxIter) {
|
||||
val f = Tcalc(h) - T
|
||||
val df = dTcalc_dh(h)
|
||||
|
||||
// Если производная близка к нулю, выходим
|
||||
if (abs(df) < 1e-12) return null
|
||||
|
||||
val hNew = h - f / df
|
||||
|
||||
// Проверка сходимости
|
||||
if (abs(hNew - h) < epsilon) {
|
||||
return hNew
|
||||
}
|
||||
|
||||
h = hNew
|
||||
iter++
|
||||
}
|
||||
|
||||
// Не сошлось за maxIter
|
||||
return null
|
||||
}
|
||||
|
||||
// Пример использования
|
||||
val T = 6.0 // секунды
|
||||
val m = 1.0 // кг
|
||||
val d = 0.1 // м (10 см)
|
||||
|
||||
val depth = calculateDepth(T, m, d)
|
||||
if (depth != null) {
|
||||
println("Глубина: %.2f м".format(depth))
|
||||
} else {
|
||||
println("Расчёт не сошёлся")
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -12,17 +12,14 @@
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.FrameSlotRef
|
||||
import net.sergeych.lyng.Pos
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.bytecode.BytecodeConst
|
||||
import net.sergeych.lyng.bytecode.CmdFrame
|
||||
import net.sergeych.lyng.bytecode.CmdFunction
|
||||
import net.sergeych.lyng.bytecode.CmdVm
|
||||
import net.sergeych.lyng.bytecode.seedFrameLocalsFromScope
|
||||
import net.sergeych.lyng.bytecode.*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertNull
|
||||
|
||||
@@ -39,6 +36,8 @@ class SeedLocalsRegressionTest {
|
||||
scopeSlotIndices = intArrayOf(0),
|
||||
scopeSlotNames = arrayOf(null),
|
||||
scopeSlotIsModule = booleanArrayOf(false),
|
||||
scopeSlotRequiresPreparedBinding = booleanArrayOf(false),
|
||||
scopeSlotRefPos = arrayOfNulls(1),
|
||||
localSlotNames = arrayOf("x"),
|
||||
localSlotMutables = booleanArrayOf(true),
|
||||
localSlotDelegated = booleanArrayOf(false),
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.ScriptError
|
||||
import net.sergeych.lyng.eval
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContains
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
|
||||
class UndefinedSymbolDiagnosticTest {
|
||||
|
||||
@Test
|
||||
fun laterDeclaredTopLevelSymbolReportsUndefinedNameAtUseSite() = runTest {
|
||||
val ex = assertFailsWith<ScriptError> {
|
||||
eval(
|
||||
"""
|
||||
var x = 7.0
|
||||
val hinv = 1/h
|
||||
var h = x
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
assertEquals(1, ex.pos.line)
|
||||
assertEquals(13, ex.pos.column)
|
||||
assertContains(ex.errorMessage, "symbol 'h' is not defined")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user