class constants added and class instances for integral types

This commit is contained in:
sergeych committed 2025-05-28 14:36:04 +04:00
1 parent f885300d18
commit 47c061d2b4
7 files changed
+98 -16

No files matched your search

@@ -13,6 +13,12 @@ data class Arguments(val list: List<Info>): Iterable<Obj> {
return list.first().value
}
inline fun <reified T: Obj>required(index: Int, context: Context): T {
if( list.size <= index ) context.raiseError("Expected at least ${index+1} argument, got ${list.size}")
return (list[index].value as? T)
?: context.raiseError("Expected type ${T::class.simpleName}, got ${list[index].value::class.simpleName}")
}
companion object {
val EMPTY = Arguments(emptyList())
}
@@ -67,7 +67,7 @@ class Context(
}
}
inline fun <reified T> addConst(value: T, vararg names: String) {
inline fun <reified T> addConstWithAliases(value: T, vararg names: String) {
val obj = Obj.from(value)
for (name in names) {
addItem(
@@ -78,6 +78,8 @@ class Context(
}
}
fun addConst(name: String,value: Obj) = addItem(name, false, value)
suspend fun eval(code: String): Obj =
Compiler().compile(code.toSource()).execute(this)
@@ -190,6 +190,13 @@ data class ObjString(val value: String) : Obj() {
}
override fun toString(): String = value
override val objClass: ObjClass
get() = type
companion object {
val type = ObjClass("String")
}
}
interface Numeric {
@@ -273,10 +280,14 @@ data class ObjInt(var value: Long) : Obj(), Numeric {
}
override fun toString(): String = value.toString()
override val objClass: ObjClass = type
companion object {
val type = ObjClass("Int")
}
}
@Serializable
@SerialName("bool")
data class ObjBool(val value: Boolean) : Obj() {
override val asStr by lazy { ObjString(value.toString()) }
@@ -286,6 +297,12 @@ data class ObjBool(val value: Boolean) : Obj() {
}
override fun toString(): String = value.toString()
override val objClass: ObjClass = type
companion object {
val type = ObjClass("Bool")
}
}
data class ObjNamespace(val name: String) : Obj() {
@@ -300,3 +317,4 @@ open class ObjError(val context: Context, val message: String) : Obj() {
class ObjNullPointerError(context: Context) : ObjError(context, "object is null")
class ObjAssertionError(context: Context, message: String) : ObjError(context, message)
@@ -21,8 +21,8 @@ class Script(
val defaultContext: Context = Context().apply {
addFn("println") {
print("yn: ")
for( (i,a) in args.withIndex() ) {
if( i > 0 ) print(' ' + a.asStr.value)
for ((i, a) in args.withIndex()) {
if (i > 0) print(' ' + a.asStr.value)
else print(a.asStr.value)
}
println()
@@ -30,28 +30,39 @@ class Script(
}
addFn("floor") {
val x = args.firstAndOnly()
if( x is ObjInt ) x
if (x is ObjInt) x
else ObjReal(floor(x.toDouble()))
}
addFn("ceil") {
val x = args.firstAndOnly()
if( x is ObjInt ) x
if (x is ObjInt) x
else ObjReal(ceil(x.toDouble()))
}
addFn("round") {
val x = args.firstAndOnly()
if( x is ObjInt ) x
if (x is ObjInt) x
else ObjReal(round(x.toDouble()))
}
addFn("sin") {
sin(args.firstAndOnly().toDouble())
}
addFn("assert") {
val cond = args.required<ObjBool>(0, this)
if( !cond.value == true )
raiseError(ObjAssertionError(this,"Assertion failed"))
}
addConst("Real", ObjReal.type)
addConst("String", ObjString.type)
addConst("Int", ObjInt.type)
addConst("Bool", ObjBool.type)
val pi = ObjReal(PI)
val z = pi.objClass
println("PI class $z")
addConst(pi, "π")
addConst("π", pi)
getOrCreateNamespace("Math").apply {
addConst( "PI", pi)
addConst("PI", pi)
}
}
}
+7 -1
View File
@@ -189,7 +189,13 @@ class BookTest {
}
@Test
fun testsFromOOPrinciples() = runTest {
fun testsFromOOP() = runTest {
runDocTests("../docs/OOP.md")
}
@Test
fun testFromReal() = runTest {
runDocTests("../docs/real.md")
}
}