Arguments.Info optimized out
This commit is contained in:
		
							parent
							
								
									b253eed032
								
							
						
					
					
						commit
						c0eba1ecf0
					
				@ -3,52 +3,43 @@ package net.sergeych.lyng
 | 
			
		||||
data class ParsedArgument(val value: Statement, val pos: Pos, val isSplat: Boolean = false)
 | 
			
		||||
 | 
			
		||||
suspend fun Collection<ParsedArgument>.toArguments(context: Context): Arguments {
 | 
			
		||||
    val list = mutableListOf<Arguments.Info>()
 | 
			
		||||
    val list = mutableListOf<Obj>()
 | 
			
		||||
 | 
			
		||||
    for (x in this) {
 | 
			
		||||
        val value = x.value.execute(context)
 | 
			
		||||
        if (x.isSplat) {
 | 
			
		||||
            when {
 | 
			
		||||
                value is ObjList -> {
 | 
			
		||||
                    for (subitem in value.list) list.add(Arguments.Info(subitem, x.pos))
 | 
			
		||||
                    for (subitem in value.list) list.add(subitem)
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                value.isInstanceOf(ObjIterable) -> {
 | 
			
		||||
                    val i = (value.invokeInstanceMethod(context, "toList") as ObjList).list
 | 
			
		||||
                    i.forEach { list.add(Arguments.Info(it, x.pos)) }
 | 
			
		||||
                    i.forEach { list.add(it) }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                else -> context.raiseClassCastError("expected list of objects for splat argument")
 | 
			
		||||
            }
 | 
			
		||||
        } else
 | 
			
		||||
            list.add(Arguments.Info(value, x.pos))
 | 
			
		||||
            list.add(value)
 | 
			
		||||
    }
 | 
			
		||||
    return Arguments(list)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
data class Arguments(val list: List<Info>) : Iterable<Obj> {
 | 
			
		||||
data class Arguments(val list: List<Obj>) : List<Obj> by list {
 | 
			
		||||
 | 
			
		||||
    data class Info(val value: Obj, val pos: Pos)
 | 
			
		||||
 | 
			
		||||
    val size by list::size
 | 
			
		||||
 | 
			
		||||
    operator fun get(index: Int): Obj = list[index].value
 | 
			
		||||
 | 
			
		||||
    val values: List<Obj>  by lazy { list.map { it.value } }
 | 
			
		||||
    val values = list
 | 
			
		||||
 | 
			
		||||
    fun firstAndOnly(): Obj {
 | 
			
		||||
        if (list.size != 1) throw IllegalArgumentException("Expected one argument, got ${list.size}")
 | 
			
		||||
        return list.first().value
 | 
			
		||||
        return list.first()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    companion object {
 | 
			
		||||
        val EMPTY = Arguments(emptyList())
 | 
			
		||||
        fun from(values: Collection<Obj>) = Arguments(values.map { Info(it, Pos.UNKNOWN) })
 | 
			
		||||
        fun from(values: Collection<Obj>) = Arguments(values.toList())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun iterator(): Iterator<Obj> {
 | 
			
		||||
        return list.map { it.value }.iterator()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -170,7 +170,7 @@ class Compiler(
 | 
			
		||||
                                            v.invokeInstanceMethod(
 | 
			
		||||
                                                context,
 | 
			
		||||
                                                next.value,
 | 
			
		||||
                                                Arguments(listOf(Arguments.Info(lambda, t.pos)))
 | 
			
		||||
                                                Arguments(listOf(lambda))
 | 
			
		||||
                                            ), isMutable = false
 | 
			
		||||
                                        )
 | 
			
		||||
                                    }
 | 
			
		||||
 | 
			
		||||
@ -41,8 +41,8 @@ class Context(
 | 
			
		||||
 | 
			
		||||
    inline fun <reified T : Obj> requiredArg(index: Int): T {
 | 
			
		||||
        if (args.list.size <= index) raiseError("Expected at least ${index + 1} argument, got ${args.list.size}")
 | 
			
		||||
        return (args.list[index].value as? T)
 | 
			
		||||
            ?: raiseClassCastError("Expected type ${T::class.simpleName}, got ${args.list[index].value::class.simpleName}")
 | 
			
		||||
        return (args.list[index] as? T)
 | 
			
		||||
            ?: raiseClassCastError("Expected type ${T::class.simpleName}, got ${args.list[index]::class.simpleName}")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    inline fun <reified T : Obj> requireOnlyArg(): T {
 | 
			
		||||
@ -80,7 +80,7 @@ class Context(
 | 
			
		||||
        value: Obj,
 | 
			
		||||
        visibility: Visibility = Visibility.Public
 | 
			
		||||
    ): ObjRecord {
 | 
			
		||||
        return ObjRecord(value, isMutable, visibility).also { objects.put(name, it) }
 | 
			
		||||
        return ObjRecord(value, isMutable, visibility).also { objects[name] = it }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun getOrCreateNamespace(name: String): ObjClass {
 | 
			
		||||
 | 
			
		||||
@ -61,7 +61,7 @@ open class Obj {
 | 
			
		||||
    fun isInstanceOf(someClass: Obj) = someClass === objClass || objClass.allParentsSet.contains(someClass)
 | 
			
		||||
 | 
			
		||||
    suspend fun invokeInstanceMethod(context: Context, name: String, vararg args: Obj): Obj =
 | 
			
		||||
        invokeInstanceMethod(context, name, Arguments(args.map { Arguments.Info(it, context.pos) }))
 | 
			
		||||
        invokeInstanceMethod(context, name, Arguments(args.toList()))
 | 
			
		||||
 | 
			
		||||
    inline suspend fun <reified T : Obj> callMethod(
 | 
			
		||||
        context: Context,
 | 
			
		||||
@ -218,7 +218,7 @@ open class Obj {
 | 
			
		||||
        callOn(
 | 
			
		||||
            context.copy(
 | 
			
		||||
                context.pos,
 | 
			
		||||
                args = Arguments(args.map { Arguments.Info(it, context.pos) }),
 | 
			
		||||
                args = Arguments(args.toList()),
 | 
			
		||||
                newThisObj = thisObj
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user