Forums › Forums › SIMPOL Programming › Using indirection in Simpol
- This topic has 3 replies, 2 voices, and was last updated 14 years, 5 months ago by Jim Locker.
- AuthorPosts
- November 30, 2009 at 8:02 pm #297Jim LockerMember
Take a string of the format variable;value In SBL, I can separate this string, thus extracting both the variable name and the associated desired value, and I can then use indirection like this to assign the value to my actual variable in the program. kw$ = MID$ (a$,1,div%% – 1) kwval$ = MID$ (a$,div%% + 1,slen%% – div%%) kw$ = kw$ + “$” kw$$ = kwval$ Now of course I can separate a string in simpol, but can I assign a value like this? How, syntactically, would I use indirection to achieve this result?
December 2, 2009 at 12:41 am #1808Jim LockerMemberWell, I did wind up implementing it as a hashed array. That works fine
but isn't what I originally intended.The SBL code I provided essentially did the SBL analog of the C pointer
organization:int foo
int *barbar = foo
*foo = 3printf("%dn",foo)
except, of course, that SBL would take the variable reference by name,
rather than by an address pointer.Since simpol takes function references by name at runtime it seems to me
that it could also easily take variable references by name at runtime.
You have a symbol table someplace internally; you have to. The
indirection by name just involves walking that symbol table to find the
variable name, just as you walk a function symbol table to find a
function.In my specific case, I have a config file (actually several config files)
that are organized as keyword;value pairs. I have global variables that
match the keywords, and assign the values to those variables which then
are used effectively as constants throughout the program run.The path of least resistance turned out to be to define an array then load
the array as array[keyword] = value.This worked fine, but there are cases where pointers are very nice.
December 2, 2009 at 11:00 am #1694MichaelKeymasterJim wrote:
> Take a string of the format variable;value
>
> In SBL, I can separate this string, thus extracting both the variable
> name and the associated desired value, and I can then use indirection
> like this to assign the value to my actual variable in the program.
>
> kw$ = MID$ (a$,1,div%% – 1) kwval$ = MID$ (a$,div%% + 1,slen%% –
> div%%)
>
> kw$ = kw$ + "$" kw$$ = kwval$
>
> Now of course I can separate a string in simpol, but can I assign a
> value like this? How, syntactically, would I use indirection to
> achieve this result?
>I don't think it is possible to define a new variable at runtime. This
gets dealt with at compile time. Having said that, I have just about
never found a need for this approach in the past in my SIMPOL
programming. Take a step back and describe what this is trying to
accomplish. You may find that an array can do things for you that you
otherwise couldn't have done, for example.Ciao, Neil
December 3, 2009 at 10:58 am #1671MichaelKeymasterJim wrote:
> Well, I did wind up implementing it as a hashed array. That works
> fine but isn't what I originally intended.
>
> The SBL code I provided essentially did the SBL analog of the C
> pointer organization:
>
> int foo int *bar
>
> bar = foo *foo = 3
>
> printf("%dn",foo)
>
> except, of course, that SBL would take the variable reference by
> name, rather than by an address pointer.
>
> Since simpol takes function references by name at runtime it seems to
> me that it could also easily take variable references by name at
> runtime. You have a symbol table someplace internally; you have to.
> The indirection by name just involves walking that symbol table to
> find the variable name, just as you walk a function symbol table to
> find a function.
>
> In my specific case, I have a config file (actually several config
> files) that are organized as keyword;value pairs. I have global
> variables that match the keywords, and assign the values to those
> variables which then are used effectively as constants throughout the
> program run.
>
> The path of least resistance turned out to be to define an array then
> load the array as array[keyword] = value.
>
> This worked fine, but there are cases where pointers are very nice.Actually, it isn't really an issue of pointers, since you basically have
that in SIMPOL. What you were looking for is a way to take a string
value and create a variable from it. The thing is, variable names are
not preserved through compilation. If, on the other hand, you had a huge
type and wanted to track down a matching property name for a given
string, that you could do.Ciao, Neil
- AuthorPosts
- You must be logged in to reply to this topic.