s!c!Buffer=Script@Buffer
macro ifSection [a,b,s] { local .e1
        cmp word [eax-1],a
        jnz .e1
        cmp dword [eax-1+2],b
        jnz .e1
	cmp dword [s],0
	jnz .e1
        mov [s],eax
.e1:
}
macro getSlot t { local .l1,.l2,.ll
        mov eax,t
.l1:    inc eax
        cmp eax,ebp     ;file size
        jae .exit
ifSection '@@','init',.initsection,'@@','pool',.poolsection 
        cmp word [eax-1],'##'
        jnz .l1
        cmp byte [eax-1+2],'['
        jnz .l1
        add eax,2
        push eax
        cinvoke strtod,eax,0
        fistp dword [.p]
        mov ecx,2048*4
        mov esi,.alloc
        call allocElement
        pop eax
        }
macro iftkn t,mtk,[tkn,lb] { local .ee
        cmp word [t],tkn
        jnz .ee
        call lb
        jmp mtk
.ee:
}
macro puMsg t { pusha
Msg t
     popa
}
macro getElements t { local .up1
.up1: inc t
cmp byte [t],'}'
jz  .exit
cmp byte [t],0
jz  .exit
cmp byte [t],'='
jnz .up1
mov [edi],esi
add edi,8       ;ptr for elements
iftkn t,.up1,"='",.txt,"=p",.ptr,"=b",.bufr
inc t
;call .ptr
mov ebx,eax
push eax
call mygetDigs
pop eax
jmp .up1
}
macro dbgScriptMode t { local .1,.2
	cmp dword [t],'#dbg'
	jnz @f
	add edx,5
	mov byte [flg@dbgScriptMode],1
	cmp dword [scriptInit.p@TabProcs],0
	jz .1
	mov byte [flg@dbgScriptMode+16],'+'
.1:	cmp dword [scriptInit.linkerTab],0
	jz .2
	mov byte [flg@dbgScriptMode+26],'+'
.2:
	puMsg flg@dbgScriptMode+1
@@:
}
macro DbgScriptInfa [v] { common 
			cmp byte [flg@dbgScriptMode],1
			jnz @f
		forward
			puMsg v
@@:
}
flg@dbgScriptMode db 0
		db 'debug',13,'procsTab 0',13,'linkTab 0',0
align 64
scriptSlots:    cmp dword [.allSlots],0
                jnz @f
                push edx
                invoke  VirtualAlloc,0,2048*8,MEM_COMMIT,PAGE_READWRITE
                mov     dword [.allSlots],eax
                pop edx
@@:	dbgScriptMode edx
        getSlot edx
        getElements eax
.exit: 

mov edx,eax                     ;last text
mov eax,[.alloc+4]
mov esi,.alloc
movsd xmm0,qword [esi]
mov ecx,[.allSlots]
add ecx,[.allSlots+4]
movsd qword [ecx],xmm0
add dword [.allSlots+4],8       ;offset
ret
.ptr: add esi,4
      mov byte [edi-1],3 ;2=txt,3=ptr
      ;puMsg eax
ret
.bufr: add esi,4
      mov byte [edi-1],4 ;2=txt,3=ptr,4=buffer array
      ;puMsg eax
ret
.txt: add eax,2
      mov byte [edi-1],2
      ;puMsg eax
.0:
  mov dl,[eax]
  cmp dl,13
  jbe .1
  cmp dl,"'"
  jz .1
  mov [esi],dl
  inc esi
  inc eax
  jmp .0
.1:  inc esi
;add eax,2
ret
.p              dd 0
.alloc          dd 0,0
.allSlots       dd 0,0
.initsection    dd 0
.poolsection    dd 0
align 64
allocElement:   invoke  VirtualAlloc,0,ecx,MEM_COMMIT,PAGE_READWRITE
                mov     dword [esi],eax
                ;add     esi,8 ;for type,size
                invoke  VirtualAlloc,0,1024*4,MEM_COMMIT,PAGE_READWRITE ;for each elements
                mov     dword [esi+4],eax
                mov     edi,eax
                mov esi,[esi]
ret
align 64
getSSlot: 
mov ecx,[scriptSlots.allSlots]
lea eax,[ecx+eax*8]
mov edx,[eax+4] ;get ptrs to slot elements
mov ebx,[edx+8*ebx]
mov ecx,[ebx]
ret

macro cmdInit [v,p] { local .l1
        cmp word [eax],v
        jz p
.l1:
 }
macro copyTxt@ t,to,znk { local .l0,.l1
mov ecx,to
.l0:
mov dl,[t]
cmp dl,znk
jz .l1
cmp dl,13
jbe .l1
mov [ecx],dl
inc ecx
inc t
jmp .l0
.l1: mov byte [ecx],0
}
align 64
scriptInit: mov eax,[scriptSlots.initsection]
		test eax,eax
		jz @f
                cmp byte [eax-2],';'
                jnz .1
@@:             ret
.1:     inc eax
        cmp byte [eax],0
        jz .exit
        cmdInit '..',.exit,'#p',.push,'#r',.run,'#t',.txt,'#a',.apply,'#o',.toSlot,'#d',.DimArray,'#l',.link
        jmp .1

.exit: ;Msg eax
	DbgScriptInfa .scriptok
ret
.scriptok db 'script init ok',0 
.link:  lea ebx,[eax+2]        
        mov esi,initBuffer2
        push eax
        call mygetDigs
          cvtss2si eax,[initBuffer2+4]	
          cvtss2si ebx,[initBuffer2+8]
	call getSSlot
          mov ecx,ebx
	cvtss2si eax,[initBuffer2]
	mov edx,[.linkerTab]
	test	edx,edx
	jz .ii
	mov eax,[edx+eax*4]
	mov [ecx],eax
.ii:
          pop eax
	jmp .1
.DimArray: lea ebx,[eax+2]        
         ;push ebx
        mov esi,initBuffer2
        push eax
        call mygetDigs
          mov eax,[initBuffer2+4]	;sizebytes alloc
          mov ebx,[initBuffer2]
	invoke  VirtualAlloc,0,eax,MEM_COMMIT,PAGE_READWRITE
                mov     dword [initBufferProcs+ebx*4],eax
		mov     dword [.retval.x],eax
	mov dword [eax],'allc'
          pop eax
         ;pop ebx
	jmp .1
.toSlot: lea ebx,[eax+2]          ;out
         push ebx
         add ebx,2
        mov esi,initBuffer2
        push eax
        call mygetDigs
          cvtss2si eax,[initBuffer2]
          cvtss2si ebx,[initBuffer2+4]
          call getSSlot
          mov ecx,ebx
          pop eax
         pop ebx
	cmp byte [ebx],'4'
        jnz @f
        movups xmm0,xword [.retxmval]
        movups xword [ecx],xmm0
        jmp .1
@@:     cmp byte [ebx],'1'
        jnz @f
        movss xmm0,[.retval.x]
        movss [ecx],xmm0
        jmp .1
@@:     movsd xmm0,qword [.retval.x]
        movsd qword [ecx],xmm0
        jmp .1

.txt:   lea ebx,[eax+4+1]
        movzx ecx,byte [eax+2]
        sub cl,48
        shl ecx,7
        lea ecx,[s!c!Buffer+ecx]
	movd xmm0,ecx
        ;copyTxt@ ebx,initBufferProcs,"'"
        copyTxt@ ebx,ecx,"'"
	movzx ecx,byte [eax+2]
        sub cl,48
	movss [initBufferProcs+ecx*4],xmm0
        jmp .1

.push:  lea ebx,[eax+2]
        mov esi,initBufferProcs
        push eax
        call mygetDigs
        pop eax
        jmp .1
.apply:  lea ebx,[eax+2]
        mov esi,applyParams
        push eax
        call mygetDigs
        pop eax
        jmp .1
.run:   lea ebx,[eax+5]
        mov esi,initBuffer2
        push eax
        call mygetDigs
          cvtss2si eax,[initBuffer2]
          cvtss2si ebx,[initBuffer2+4]
          call getSSlot
          
          
        mov eax,[.p@TabProcs]
	test eax,eax
	jz @f
	push initBufferProcs
        mov edx,applyParams
          call dword [eax+ecx*4]
        mov [.retval.x],eax
        mov [.retval.y],edx
	movups xword [.retxmval],xmm0
@@:     pop eax 
add eax,5
        jmp .1
.linkerTab	dd 0,0	;user linked objects to script. last obshiiBufferObmenaProcs
.retval.x       dd 0    ;for #o from running procs.
.retval.y       dd 0
.retxmval       dd 0,0,0,0
.p@TabProcs     dd 0
initBufferProcs  dd 48 dup(0)	;initBufferProcs-24=.retval.x for usr procs
applyParams      dd 32 dup(0)
initBuffer2      dd 0,0,0,0,0,0,0,0
include 'scriptPool.txt'
