minor refactoring
This commit is contained in:
		
							parent
							
								
									60384060bf
								
							
						
					
					
						commit
						33fdb0934a
					
				
							
								
								
									
										60
									
								
								docs/development/including_modules.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								docs/development/including_modules.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,60 @@
 | 
			
		||||
# Modules inclusion
 | 
			
		||||
 | 
			
		||||
Module is, at the low level, a statement that modifies a given context by adding
 | 
			
		||||
here local and exported symbols, performing some tasks and even returning some value
 | 
			
		||||
we don't need for now.
 | 
			
		||||
 | 
			
		||||
The compiled module is therefore a statement. When we execute it on some context,
 | 
			
		||||
it wills it with all it symbols, private too.
 | 
			
		||||
 | 
			
		||||
If we call it on another context, it will do it once more, no caching. This is unnecessary
 | 
			
		||||
if not dangerous repetition.
 | 
			
		||||
 | 
			
		||||
## What is inclusion?
 | 
			
		||||
 | 
			
		||||
The _goal_ of the inclusion is to make _exported symbols_ available in a given context,
 | 
			
		||||
without re-executing included module initialization code. So, when we hit the `import foo`,
 | 
			
		||||
we should check that foo module was executed, execute it if not on special context we store in the library, then copy all public symbols from fooContext into current one.
 | 
			
		||||
 | 
			
		||||
## Class pseudo-module
 | 
			
		||||
 | 
			
		||||
Mostly same we can do with a class
 | 
			
		||||
 | 
			
		||||
## Module initialization
 | 
			
		||||
 | 
			
		||||
We can just put the code into the module code:
 | 
			
		||||
 | 
			
		||||
    module lying.samples.module
 | 
			
		||||
    // or package?
 | 
			
		||||
    
 | 
			
		||||
    val startuptTime = Instant.now()
 | 
			
		||||
 | 
			
		||||
    // private: not available from outside
 | 
			
		||||
    private fun moduleInitialization() {
 | 
			
		||||
        // long code
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // this will be called only once
 | 
			
		||||
    moduleInitialization()
 | 
			
		||||
 | 
			
		||||
## class initialization
 | 
			
		||||
 | 
			
		||||
    class foo {
 | 
			
		||||
        
 | 
			
		||||
        private static var instanceCounter = 0
 | 
			
		||||
        
 | 
			
		||||
        val instanceId = instanceCounter
 | 
			
		||||
        
 | 
			
		||||
        fun close() {
 | 
			
		||||
            instanceCounter--
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // instance initializatino could be as this:
 | 
			
		||||
        if( instanceId > 100 )
 | 
			
		||||
            throw Exception("Too many instances")
 | 
			
		||||
 | 
			
		||||
        static {
 | 
			
		||||
            // static, one-per-class initializer could be posted here
 | 
			
		||||
            instanceCounter = 1
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@ -0,0 +1,13 @@
 | 
			
		||||
package net.sergeych.lyng
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Symbols(
 | 
			
		||||
    unitType: UnitType,
 | 
			
		||||
    val name: String,
 | 
			
		||||
    val x: TypeDecl
 | 
			
		||||
) {
 | 
			
		||||
    enum class UnitType {
 | 
			
		||||
        Module, Function, Lambda
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,5 +1,21 @@
 | 
			
		||||
package net.sergeych.lyng
 | 
			
		||||
 | 
			
		||||
sealed class TypeDecl {
 | 
			
		||||
    // ??
 | 
			
		||||
    data class Fn(val argTypes: List<ArgsDeclaration.Item>, val retType: TypeDecl) : TypeDecl()
 | 
			
		||||
    object Obj : TypeDecl()
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
To use in the compiler, we need symbol information when:
 | 
			
		||||
 | 
			
		||||
- declaring a class: the only way to export its public/protected symbols is to know it in compiler time
 | 
			
		||||
- importing a module: actually,  we cam try to do it in a more efficient way.
 | 
			
		||||
 | 
			
		||||
Importing module:
 | 
			
		||||
 | 
			
		||||
The moudule is efficiently a statement, that initializes it with all its symbols modifying some context.
 | 
			
		||||
 | 
			
		||||
The thing is, we need only
 | 
			
		||||
 | 
			
		||||
 */
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user