PDA

View Full Version : array question



Lionheart008
02-05-2012, 10:59
what's the difference between "dim heroes(10) as string" and "dim heroes as string" in parsing technology? for me it would be great to get more info about that one. you can't use both in my example "heroes(10)" and simple "dim heroes as string", but why don't thinbasic makes that important different?



' Empty GUI script created on 05-02-2012 10:14:40 by (ThinAIR)

Dim heroes(10) As String
'Dim heroes As String ' variable name already exist (duplicated generic)
' heroes = "black widow"

heroes(1) = "THOR "
heroes(2) = "HULK "
heroes(3) = "CAPTAIN AMERICA "
MsgBox 0, heroes(1) + heroes(2) + heroes(3)
'MsgBox 0, heroes

'
' my question: 'heroes(10)' is an own variable type and
' has to define extra in 'dim variableArray() as string'
' And has his own assignment ?
' what's the difference in parsing between normal varialbe like 'heroes' and
' array type like 'heroes(10)' ?
'
' I thought that 'heroes(10)' and "heroes" as variable type must be entirely different things!? :)

best regards, frank

ErosOlmi
02-05-2012, 11:45
Frank,

in all languages variable names must be unique inside each scope (scope means: Global, Functions, blocks, ...) .
For this reason heroes(10) and heroes inside the same scope are the same name so they cannot represent different things.
The variable is named heroes.

But your question is quite interesting because in reality if used in different ways (one is an array, the other is a scalar variable) it could be possible to identify what is what.
But what could be the problem? The code would be much more "Error prone" because it would be impossible to understand if you are using wrongly or correctly the term heroes: are you using as scalar or as array? Ho can the parser or the interpreter be sure?

From an internal point of view, the 2 variables are completely different.

A dynamic string in thinBasic is 4 bytes pointer that points to an 8 bytes (2 LONGs) memory area used to store string length (in the first LONG) and a pointer (second LONG) to a dynamic allocated area that contains the real string.
A dynamic array (arrays can shrink or increase) of dynamic strings is very similar but a little more complicated.

Ciao
Eros

Lionheart008
02-05-2012, 17:09
thank you for this infos eros. my added question: is if you are using for dynamic string (dynamic array) an own dimension? (new assignment and new add_variable function?) I've tested this for my own project and only the scalar variable works fine, but I think I have to add a new key ("add_key", dictionary module) to that dynamic string and parsed this too, but the result is empty here for example for

dim heroes(10) as string
heroes(1) = "batman"

variable type in my case included for example such thing "stringArray() as string*length" and I added a new key to that dynamic array, but the message box is empty, there is no returning a value or in my case the word "batman". hard stuff, but I think I am on the right way, but some parts are missing to get right result.

thanks, frank

ErosOlmi
02-05-2012, 17:35
Dim heroes(10) As String
heroes(1) = "batman"

MsgBox 0, heroes(1)


The above code is perfectly working so I'm sure I'm missing something here.

Are you talking about:
thinBasic scripts
developing thinbasic modules
They are two completely different worlds and if I do not get what you are talking about I will reply in the wrong way.

Charles Pegge
02-05-2012, 23:56
I think Frank is asking about redim

Lionheart008
04-05-2012, 10:04
thank you both, my interests concerned primalary to "dynamic arrays" and how to parse a complicated thing like "dim heroes(1 to 10) as string" and give the right result. I am thinking a) about my module "fundialox" and b) my own interpreter project ("funbasic") and dived onto the ground (looking at bint32 examples) and I have understand how to set the string value (dim_array) and saw the variable structures working here well too.

now I am asking if it's possible to create an own dynamic array for my "fundialox module" without using thinbasic power and I wanted to understand how that's working to detect that "dim heroes(1 to 10) as string" is an array and not a single string value (scalar). I have managed to detect that there is an array but not more. for "heroes(1) = "hulk" (for example) to get the right result "hulk" from a dynamice string that's my main problem here.

it's necessary to change the varialbe type for a dynamic array like "dim heroes(1 to 10) as string" ? there are a lot of questions here. "redim" wasn't my problem charles ;) sorry for unclear words or question.


dim heroes(1 to 10) as string
heroes(1) = "hulk"
heroes(2) = "captain america"
heroes(3) = "ironman"
...
msgbox 0, heroes(1)
msgbox 0, heroes(2)
..

thanks, nice day, frank

ErosOlmi
05-05-2012, 08:35
Frank,

writing a tokenizer/parser/interpreter is not something trivial

I suggest to have a look at my old interpreter called BINT32. You can find it at: http://www.thinbasic.com/index.php?option=com_jdownloads&Itemid=95&task=viewcategory&catid=6
It contains full PowerBasic source code and can be used to get started.
BINT32 parser is built using "Recursive descendant parser" technique: http://en.wikipedia.org/wiki/Recursive_descent_parser

You can find many examples on the web using different languages: https://www.google.com/#hl=en&sugexp=tsh&gs_nf=1&tok=eYhEKe8Z7QCD7M_2b6XDKA&cp=3&gs_id=2j&xhr=t&q=recursive+descendant+parser&pf=p&sclient=psy-ab&oq=recdescendant+parser&aq=0c&aqi=g-c1&aql=f&gs_l=&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=ecb6a798695883bc&biw=1918&bih=1019&bs=1

thinBasic itself is built over the same technique but more advanced.

There is quite a lot to study and master but if you get it I'm sure you will get a lot of satisfactions


Another option could be to get Charles Oxygen language and study its source code.

Ciao
Eros

Michael Hartlef
05-05-2012, 12:09
A very good tool create a parser is Coco/R which I have used personally too. And the lecture that got me started by coding a parser by hand was Jack Crenshaws "Let's build a compiler" tutorials.

ErosOlmi
05-05-2012, 12:33
Very interesting lecture Michael.
I didn't know that document (here attached for convenience)