Extension Modules > DLLC

Script BASIC Virtual DLL

(1/1)

Support:
Viirtual DLL is what I call the DLLC OxygenBasic JIT function(s). Below is an example of calling an ASM string reverse function, a word parsing function and a column align function in O2 BASIC from Script BASIC as an extension module IMPORT.

testo2.sb

--- Code: Script BASIC ---IMPORT O2.inc FOR x = 65 TO 90  alpha &= CHR(x)NEXTPRINT alpha,"\n"PRINT O2::RevStr(alpha),"\n"p = 1Next_Word:  wd = O2::GetWords("abc(d[xx]+7/6,\"qwerty\")", p)  IF wd <> "" THEN    PRINT wd,"\n"    GOTO Next_Word  END IFq = """Given;;a;;text;;file;;of;;many;;lines,;;where;;fields;;within;;a;;line;;are;;delineated;;by;;a;;single;;'dollar';;character,;;write;;a;;programthat;;aligns;;each;;column;;of;;fields;;by;;ensuring;;that;;words;;in;;each;;column;;are;;separated;;by;;at;;least;;one;;space.Further,;;allow;;for;;each;;word;;in;;a;;column;;to;;be;;either;;left;;justified,;;right;;justified,;;or;;center;;justified;;within;;its;;column."""PRINT O2::ColumnAlign(q, "LLLLCCCRRRRR", ";;", "\n") O2::Done() 
O2.inc

--- Code: Script BASIC ---MODULE O2 include "dllcinc.sb" oxy=dllfile("/scriptbasic/Debugger/modules/oxygen.dll") o2_basic = dllproc( oxy, "o2_basic i =(c*source) " )o2_exec  = dllproc( oxy, "o2_exec  i =(i call)   " )o2_error = dllproc( oxy, "o2_error c*=()         " )o2_errno = dllproc( oxy, "o2_errno i =()         " )o2_len   = dllproc( oxy, "o2_len   i =()         " )o2_mode  = dllproc( oxy, "o2_mode     (i mode)   " ) dllcall o2_mode,1 OPEN "/scriptbasic/Debugger/include/O2.src" FOR INPUT AS #1src = INPUT(LOF(1), 1)CLOSE(1) a = oxygen(src)Finish  = dllproc(a,"Finish ()", dllcald(a,0))Reverse = dllproc(a,"Reverse (c*value)", dllcald(a,1))Words   = dllproc(a,"getword c* = (c* strraw, i* start)", dllcald(a,2))ColAlign   = dllproc(a,"AlignText c* = (c* in, c* ju, C* dl, c* cr)", dllcald(a,3)) FUNCTION oxygen(src)  dllcall o2_basic,src  IF (dllcall(o2_errno)<> 0) THEN    dllprnt dllcall(o2_error)    a = 0  ELSE    a = dllcall(o2_exec,0)  END IF  oxygen = aEND FUNCTION FUNCTION Done  rtnval = dllcall(Finish)  dllfile  Done = rtnvalEND FUNCTION ' SB wrapper functions FUNCTION RevStr(strarg)  dllcall(Reverse, strarg)  RevStr = strargEND FUNCTION FUNCTION GetWords(strarg, longarg)  GetWords = dllcall(Words, strarg, longarg)END FUNCTION FUNCTION ColumnAlign(in_str, just_str, dlm_str, eol_str)  ColumnAlign = dllcall(ColAlign, in_str, just_str, dlm_str, eol_str)END FUNCTION END MODULE 
O2.src

--- Code: OxygenBasic ---' O2 source extern function reverse(char*s)'=======================  addr ecx,s  mov edx,0 .rlen  mov al,[ecx]  cmp al,0  jz xlen  inc edx  inc ecx  jmp rlen .xlen  ;  addr ecx,s  add  edx,ecx  dec ecx  ; .rswap  inc ecx  dec edx  cmp edx,ecx  jle xswap  mov al,[ecx]  mov ah,[edx]  mov [ecx],ah  mov [edx],al  jmp rswap .xswap  end function function getword(char*ss,sys*b) as char*'=======================================if b=0 then b=1byte s at @ssbyte c,dsys bb,bcstatic char z[128]a=0bb=b 'SKIP LEADING SPACESdo  c=s[b]  select c   case 33 to 255,0 : exit do 'SKIP SPACE  end select  b++end dobc=b ''QUOTESselect c case 34,39   do     b+=1     d=s[b]     if d=0 or d=c then b+=1 : jmp fwd done   end doend select'WORDS AND SYMBOLSdo  c=s[b]  select c  case 0 to 32    : exit do  case 35         : jmp fwd more  case 33 to 47   : 'symbols  case 48 to 57   : jmp fwd more 'numbers  case 58 to 64   : 'symbols  case 65 to 90   : jmp fwd more 'capitals  case 95         : jmp fwd more 'underscore  case 91 to 96   : 'symbols  case 97 to 122  : jmp fwd more 'lower case  case 123 to 127 : 'symbols  case 128 to 255 : jmp fwd more 'higher asciiend select if b=bc then b++  exit do   more:  b++end do done: if b > bb then  z=mid ss,bc,b-bcelse  z = ""end ifreturn z end function =================Class AlignedText================= indexbase 1 string  buf, bufo, pr, cr, tab, jus, dlmsys     Cols, Rows, ColWidth[0x100], TotWidth, ColPad, ld method SetText(char*s)======================if not len cr then cr=chr(13,10)tab=chr(9)if not len jus then jus=string 200,"L"buf=smeasureend method  method measure()================sys a, b, wa, wb, cm, c, cwa=1 : b=1Cols=0 : Rows=0 : ColPad=3ld=len dlmif not ld then dlm="," : ld=1 'default to commado  wb=b  a=instr b,buf,cr  if a=0 then exit do  cm=0  c++  do    wa=instr wb,buf,dlm    if wa=0 or wa>a then exit do    cm++    if cm>cols then cols=cm    cw=wa-wb    if cw > ColWidth[cm] then ColWidth[cm]=cw    wb=wa+ld  end do  b=a+len crend dorows=c'c=0for i=1 to cols  ColWidth[ i ]+=ColPad  c+=ColWidth[ i ]nextTotWidth=c+len cr'print ShowMetricsend method  method ShowMetrics() as char*=============================pr="METRICS:" cr crpr+=rows tab cols tab totwidth cr crpr+="column" tab "spacing" crfor i=1 to cols  pr+=i tab ColWidth[ i ] crnextreturn prend method  method justify(char*j)======================jus=jend method method delimiter(char*j)========================dlm=jend method method endofline(char*j)========================cr=jend method  method layout() as char*========================sys a, b, wa, wb, wl, cm, lpos, cposbufo=space Rows*TotWidtha=1 : b=1do  wb=b  a=instr(b,buf,cr)  if a=0 then exit do  cm=0  cpos=1  do    wa=instr(wb,buf,dlm)    if wa=0 or wa>a then exit do    '    cm++    '    'JUSTIFICATION    '    wl=wa-wb    p=lpos+cpos 'default "L" LEFT ALIGN    '    select case asc(jus,cm)      case "R" : p=lpos+cpos+ColWidth[cm]-wl-Colpad      case "C" : p=lpos+cpos+( ColWidth[cm]-wl-Colpad )*.5    end select    '    mid bufo,p, mid buf,wb,wl    cpos+=colwidth[cm]    wb=wa+ld  end do  b=a+len cr  lpos+=TotWidth  if lpos<len(bufo) then mid bufo,lpos-1,crend doreturn bufoend method end class '#recordof AlignedText AlignedText atxt function AlignText(char *in,*ju,*dl,*cr) as char*=================================================atxt.justify         juatxt.delimiter       dlatxt.endofline       cratxt.SetText         inreturn               atxt.layoutend function sub finish()'===========  terminateend sub function link(sys n) as sys'==========================  select n  case 0 : return @finish  case 1 : return @reverse  case 2 : return @getword  case 3 : return @AlignText  end selectend function end extern addr link 
Output

C:\scriptbasic\o2dev>scriba testo2.sb
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ZYXWVUTSRQPONMLKJIHGFEDCBA
abc
(
d
[
xx
]
+
7
/
6
,
"qwerty"
)

  Given        a            text         file       of       many        lines,        where   fields   within        a   line
  are          delineated   by           a        single    'dollar'   character,      write        a
  that         aligns       each         column     of       fields        by       ensuring     that    words       in   each
  column       are          separated    by         at       least        one
  Further,     allow        for          each      word        in          a          column       to       be   either   left
  justified,   right        justified,   or       center   justified     within          its

C:\scriptbasic\o2dev>

Navigation

[0] Message Index

Go to full version