Author Topic: Elastic String Array  (Read 9727 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Re: Elastic String Array
« Reply #15 on: June 05, 2012, 06:47:29 PM »
Hi Kent,

If you remove the colon on line 15 it will work. := is an assignment operator, and you cant conditionally assign to string functions.


    if left( s, 2 ) = "% " then  ...

Here is a demo using a dynamic TextArray

Code: OxygenBasic
  1. include "..\..\inc\StringUtil.inc"  
  2.  
  3. new TextArray t  
  4.  
  5. t.Lines "one
  6. two
  7. % three
  8. % four
  9. five"
  10.  
  11.  
  12. eof = t.LastLine
  13.  
  14. string s,ou
  15.  
  16. for i = 1 to eof
  17.     s=t.line(i)
  18.     if left(s,2)= "% " then
  19.       ou+=s+cr
  20.     end if
  21. next
  22.  
  23. print ou
  24.  
  25. del t
  26.  

This procedure in a new class:

Code: OxygenBasic
  1. include "..\..\inc\StringUtil.inc"  
  2.  
  3. '====================
  4. Class FilterTextArray
  5. '====================
  6. '
  7. has TextArray
  8. '
  9. method LeftFilter(string k) as string
  10. string s,u
  11. sys i,e=mx
  12. for i = 1 to e
  13.     s=line i
  14.     if left(s,2)=k then
  15.       u+=s+cr
  16.     end if
  17. next
  18. return u
  19. end method
  20. '
  21. end class
  22.  
  23. '--test--
  24.  
  25. new FilterTextArray t
  26.  
  27. t.Lines "one
  28. two
  29. % three
  30. % four
  31. five"
  32.  
  33. print t.LeftFilter "% "
  34.  
  35. del t
  36.  


Charles

PS: A new method for StringArray:

Code: OxygenBasic
  1.  
  2.   method append(string s)
  3.   '======================
  4.  insert mx+1,s
  5.   end method
  6.  
  7.  
« Last Edit: June 05, 2012, 07:57:21 PM by Charles Pegge »

kryton9

  • Guest
Re: Elastic String Array
« Reply #16 on: June 05, 2012, 09:48:01 PM »
Very nice, thanks.

That := is confusing me all the time. I thought in conditionals we needed to use it, like you use == in C.
I see in your examples for conditionals you use it, but not sure when?

Anyways thanks for the troubleshooting and new examples and code.

Charles Pegge

  • Guest
Re: Elastic String Array
« Reply #17 on: June 05, 2012, 11:58:12 PM »
In Oxygen ':=' can be used to force assign in a condtional statement. C alway uses '='.

In C this is normally used to test the zero or non-zero outcome of the assignment

Oxygen also understands '==' and '!='

Code: OxygenBasic
  1. function fn() as sys
  2. return 42
  3. end function
  4.  
  5. a=43
  6.  
  7. if a==fn
  8.    print "equal"
  9. end if
  10.  
  11. if a!=fn
  12.    print "unequal"
  13. end if
  14.  
  15. 'ASSIGN AND BOOLEAN TEST
  16.  
  17. if a:=fn
  18.   print "fn returned non-zero value: new value for a=" a
  19. end if
  20.  

Charles

kryton9

  • Guest
Re: Elastic String Array
« Reply #18 on: June 06, 2012, 01:10:01 AM »
Ohhhh, so you are assigning and checking a condition at the same time. Ok, now I get it!

Aurel

  • Guest
Re: Elastic String Array
« Reply #19 on: June 15, 2012, 01:04:37 AM »
I just looking into this topic and thinking about interpreter.
Charles what you think...
It would be maybe good idea for interpreter to avoid constant parsing or using translate code
to bytecode - use some sort of array to store all parsed(extracted strings ) into arrays.
So what you think about that?

for example:

a[0]   a[1]   a[2]   a[3]
'print'   'x'     'y'    'string'

Charles Pegge

  • Guest
Re: Elastic String Array
« Reply #20 on: June 15, 2012, 01:59:01 AM »
Hi Aurel,

Yes, reading a string as an array of bytes is easily done and makes parsing much more efficient.

Oops! I see a glitch
I'll get a working example

Charles
« Last Edit: June 15, 2012, 02:06:44 AM by Charles Pegge »

Charles Pegge

  • Guest
Re: Elastic String Array
« Reply #21 on: June 15, 2012, 02:14:13 AM »

Code: OxygenBasic
  1. string s="0123456789@abcdefghi"
  2.  
  3. p=strptr(s)
  4. byte b at p
  5.  
  6. print b 'answer 48 '0'
  7.  
  8. indexbase 0
  9.  
  10. print b[1] 'answer 49 '1'
  11.  
  12. 'pointer p can also be moved
  13.  
  14. p+=10
  15.  
  16. print b 'answer 64 '@'
  17.  

Aurel

  • Guest
Re: Elastic String Array
« Reply #22 on: June 15, 2012, 03:19:22 AM »
Hi Charles...
Yes that is cool but i don't understand from where you get this results.
hmmm i see that 48 present 0 ,right?
maybe im not quite clear..uff
In first place i mean as i show
argument1[1024],argument2[1024],argument3[1024]
like:
lineIndex=0 : 'line index -> lndx
argument1[lndx]= "print"
argument2[lndx]= "x"
argument3[lndx]= "y"

without bycode...
how wold this work,for example?

Charles Pegge

  • Guest
Re: Elastic String Array
« Reply #23 on: June 15, 2012, 03:39:33 AM »
I think I see what you mean now. you want to put non-token words into an array to improve parsing efficiency?

You could then encode the array indexes as tokens.

Your byte code would then be a uniform stream of tokens

Am I on the right track, Aurel?

I do something rather similar for string literals in Oxygen. All the literals are stored in the data section, and only their pointers remain in the main code.


Charles

Aurel

  • Guest
Re: Elastic String Array
« Reply #24 on: June 15, 2012, 04:01:19 AM »
Quote
Am I on the right track, Aurel?

I do something rather similar for string literals in Oxygen. All the literals are stored in the data section, and only their pointers remain in the main code.

Yes Charles you right...

In another words:
Get Line of source like a 'print x y'
parse this line into (folowing syntax rules) agruments -> print ,  x  , y
And each argument put into arrays of arguments as i show before.
so if line index is for example 1
index=1  ' <- line number
argument1[1]="print"
argument2[1]="x"
argument3[1]="y"

So i probably must have minimum 32 arrays for arguments,right?
After all parsing is done all this elemnts are completed.
Im interested now how fast this can be executed becuse no more need for parsing again,right?
I hope that im clear now... ;)