Oxygen Basic

Programming => Problems & Solutions => Topic started by: Emil_halim on March 31, 2013, 11:47:40 AM

Title: bcx to Oxygen basic
Post by: Emil_halim on March 31, 2013, 11:47:40 AM
Hi all,

I have a function from BCX that i want to convert to work with Oxygen

here it is
Code: [Select]
char *AppExePath (void)
{
 static char strtmp[ 2048 ];
 register int i;
 i=GetModuleFileName(GetModuleHandle(0),strtmp, 2048);
 while(i && strtmp[i] != 0x5C)
   i--;
 strtmp[i+1] = 0;
 return strtmp;
}

Title: Re: bcx to Oxygen basic
Post by: Emil_halim on March 31, 2013, 12:06:52 PM

i was think that , Oxygen will understand c function , so what is the minimal changes to make the previous
function to work with Oxygen basic?
Title: Re: bcx to Oxygen basic
Post by: Charles Pegge on March 31, 2013, 03:34:34 PM
Close. (Stand-alone example)

extern lib kernel32.dll
! GetModuleHandle   alias "GetModuleHandleA"  
! GetModuleFileName alias "GetModuleFileNameA"
end extern

sys x 'workaround for strange glitch

char* AppExePath(void)
{
  static char strtmp[2048]
  sys i
  i=GetModuleFileName(GetModuleHandle(0), strptr strtmp, 2048)
  while asc(strtmp,i) != 0x5c
    i--
  wend
  mid strtmp,i+1,chr 0
  return strtmp
}

print AppExePath
Title: Re: bcx to Oxygen basic
Post by: Charles Pegge on April 01, 2013, 04:24:48 AM
Closer:

extern lib kernel32.dll
! GetModuleHandle   alias "GetModuleHandleA"  
! GetModuleFileName alias "GetModuleFileNameA"
end extern

sys x 'workaround for strange glitch

char* AppExePath(void)
{
  indexbase 1
  static byte b[512]
  sys i=GetModuleFileName(GetModuleHandle(0), @b, 512)
  while b[ i ] != 0x5c
    i--
  wend
  b[ i+1 ]=0
  = @b 'low level
  return
}

print AppExePath

I have corrected the glitch, so I will post an update to Oxygen.
Title: Re: bcx to Oxygen basic
Post by: Emil_halim on April 01, 2013, 10:52:48 AM

very nice work Charles,

this example works very well , but when i put this function "AppExePath" in my code , it compiled okay but did not run.

when comment it , every thing is okay , that drive my crazy  >:(

any help please   
Title: Re: bcx to Oxygen basic
Post by: Emil_halim on April 01, 2013, 11:11:39 AM

to be more specific

this work okay
Code: [Select]

print AppExePath()

string cmd,File,AppPath
sys ii
  cmd = GetCommandLine()
  ii=instr(cmd," ")
  if ii
    ii++
    File=mid(cmd,ii,len(cmd)-ii+1)
  end if


but this ont
Code: [Select]
string cmd,File,AppPath
sys ii
  cmd = GetCommandLine()
  ii=instr(cmd," ")
  if ii
    ii++
    File=mid(cmd,ii,len(cmd)-ii+1)
  end if

print AppExePath()

Title: Re: bcx to Oxygen basic
Post by: Emil_halim on April 01, 2013, 11:39:50 AM

after many tries i successefly it worked.

there was two things

first , when using this , it did not work
Code: [Select]
extern lib kernel32.dll
! GetModuleHandle   alias "GetModuleHandleA"   
! GetModuleFileName alias "GetModuleFileNameA"
end extern
   
but this work
Code: [Select]
! GetCommandLine lib "kernel32.dll" alias "GetCommandLineA" () as char* 
! GetModuleFileName lib "kernel32.dll" alias "GetModuleFileNameA" (sys a,char* b,sys c) as int

second ,this not work
Code: [Select]
sys i=GetModuleFileName(GetModuleHandle(0), @b, 512)

but this works
Code: [Select]
push 512 : addr eax , b :  push eax : GetModuleHandle(0) : push eax
  call GetModuleFileName
  mov i , eax

i think @b did not return the correct address in long code , but do wrok with shourt code , strange things
is'nt it?

Charles is there a BUG
Title: Re: bcx to Oxygen basic
Post by: Charles Pegge on April 01, 2013, 12:53:23 PM
Hi Emil,

Try the latest update. Should be clear of the declarations bug.

This works, but you will need to do some parsing work on the CmmandLine file: (stripping quote marks)

extern lib kernel32.dll

! GetCommandLine    alias "GetCommandLineA"
! GetModuleFileName alias "GetModuleFileNameA"
! GetModuleHandle   alias "GetModuleHandleA"  

end extern


'includepath "$\inc\"
'include "minwin.inc"


char* AppExePath()
{
  static byte b[256]
  sys i
  a=GetModuleHandle(0)
  i=GetModuleFileName(a, @b, 256)
  while b[ i ] != 0x5c
    i--
  wend
  b[ i+1 ]=0
  = @b
  return
}

print AppExePath


string cmd,File,AppPath
sys ii
  cmd = cast zstring GetCommandLine()
  ii=instr(cmd," ")
  if ii
    ii++
    File=mid(cmd,ii,len(cmd)-ii+1)
  end if

print file
Title: Re: bcx to Oxygen basic
Post by: Emil_halim on April 02, 2013, 07:51:57 AM

Hi Charles,

no luck , after updating has the same error.
Title: Re: bcx to Oxygen basic
Post by: Emil_halim on April 02, 2013, 09:03:46 AM

okay , it works now , do not ask me why , because i really do not know.

Does Oxygen unstable ? 
Title: Re: bcx to Oxygen basic
Post by: Charles Pegge on April 02, 2013, 10:48:03 AM
Sometimes, I think the OS picks up the wrong DLL.

And don't forget, this is alpha software. There are more permutations than many languages have. Error trapping is not very tight at this stage of its development, so you have to code defensively, and test your program as you grow it.

The other extreme would be to have a very strict language with only one coding style. IronJawBasic ? :)

Charles

 
Title: Re: bcx to Oxygen basic
Post by: JRS on April 02, 2013, 10:58:36 AM
Quote
IronJawBasic

There is a Blunt-Axe Basic to ASM translator Steve wrote so I think that base is covered. Please continue on with the O2 destiny as you see it.
Title: Re: bcx to Oxygen basic
Post by: Emil_halim on April 02, 2013, 12:42:33 PM

Okay , i was playing with your getword function that split the line code into tokens.

so i start a new one , getline , based on getword , but it does not act like i want

here it is ,
Quote
function getline(s as string, b as sys) as string 
'================================================   
  sys c,bb
  bb=b
  do 
    c=asc(s,b) 
    if c=0 then exit do 
    if c=13 and asc(s,b+1)=10 then exit do
    b+=1 
  end do 
  if b>bb then return mid(s,bb,b-bb) 
end function     

sys i = 1
string lin
    do
       lin =  getline(src,i)
       if not lin then exit do
       print lin
    end do
 

so what code be wrong here?
Title: Re: bcx to Oxygen basic
Post by: Charles Pegge on April 02, 2013, 04:13:22 PM
Emil,

You just need to skip the crlf

function getline(s as string, b as sys) as string 
  '
  sys c,bb
  bb=b
  do 
    c=asc(s,b) 
    if c=0 then exit do 
    if c=13 and asc(s,b+1)=10 then exit do
    b+=1 
  end do 
  if b>bb
    function=mid(s,bb,b-bb) 
    b+=2
  end if
end function     

string src=
"1 one
2 two
3 three"

sys i = 1
string lin

    do
       lin =  getline(src,i)
       if not lin then exit do
       print lin
    end do
Title: Re: bcx to Oxygen basic
Post by: Emil_halim on April 03, 2013, 04:48:13 AM

Hi Charles ,

when getline function find a blank line the loop end ignoring the rest of string , so i changed it like this
Code: [Select]
function getline(s as string, b as sys) as string 
=================================================
  sys c,bb
  bb=b
  do 
    c=asc(s,b) 
    if c=0 then exit do 
    if c=13 then b+=1 : exit do
    b+=1 
  end do 
  if asc(s,b)=10 then b+=1
  return mid(s,bb,b-bb)
end function     
 

so it works well but when using it with my next function the program end in the middel and execution did
not reach to end end of function , any help please?
Code: [Select]
void processAllLayer(string file)
{
   sys i = 1
   string src , lin
   for i = 1 to  LayerNo
     print "processing... " file chr(10) "by" chr(10) LayName[i]
     src = getfile(file)
     
     do
       lin =  getline(src,i)
       if not lin then exit do
       print lin
     end do
          '<------------------------------- program end here????????
     if ActiveLayer[i] then
           addr eax , src
           push eax
           call procProcess[i]
           push  eax   : push  eax
           putfile  file, cast bstring eax
           call freememory
     end if
   next
}
Title: Re: bcx to Oxygen basic
Post by: Charles Pegge on April 03, 2013, 08:40:41 AM
Hi Emil,
 your returned lines will always include the terminators, therefore you will never get an empty line to terminate with. This is why I captured the line string before skipping the termination chars.
Title: Re: bcx to Oxygen basic
Post by: Emil_halim on April 03, 2013, 08:57:13 AM

yes Charles ,

my source code may contain an empty line and that does not mean the end of string (source code) , so i need
an other way to  terminate the loop.
Title: Re: bcx to Oxygen basic
Post by: Emil_halim on April 03, 2013, 09:04:56 AM

I am thinking in c language , char* is a pointer to the buffer of returned string and if is null that will end the loop ,

what is your opinion ?
Title: Re: bcx to Oxygen basic
Post by: Charles Pegge on April 03, 2013, 11:03:43 AM
You can record the ascii value of the terminating character, as a global state variable possibly, and test for zero to exit the line-reading loop.

 I use global state variables in the compiler to hold parsing data like word-position word-ascii, next- word-position, word-length.