6 Replies - 489 Views - Last Post: 20 February 2012 - 11:31 PM Rate Topic: -----

#1 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Assigning strings as variable names. (incognito)

Posted 14 February 2012 - 08:40 PM

I know that strings can't be actual variable names in C.

What I was thinking is how can I provide the allusion to a string being a variable name.

Kind of like in scripting languages that have assignments within and have duck typing.

From what I've read one suggestion is to make a key value relation with parsed values.
Kind of like:
"begin code
name = "value";
end code"

// The above becomes c code like

set(values,"name","value");



Key's represent variable names and the values are just data.

The key value data structure allows the allusion of strings being variable names without the user being the wiser.

So far I'm not sure how to do it even with the key value pair scenario.
I'm just being suggestive to that side of you that likes a challenge and would like to offer up a dialogue on this subject. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Assigning strings as variable names. (incognito)

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,635
  • Joined: 19-February 09

Re: Assigning strings as variable names. (incognito)

Posted 14 February 2012 - 11:50 PM

A structure with variables for a name, datatype, with a nested union for data values.
Was This Post Helpful? 2
  • +
  • -

#3 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito)

Posted 15 February 2012 - 09:16 AM

View Post#define, on 15 February 2012 - 12:50 AM, said:

A structure with variables for a name, datatype, with a nested union for data values.


Ya. If I understand what your saying.

The structure would contain the names as strings.

I suppose the union would be the mechanism to perform duck typing.

That would lead into some other things like garbage collection when the interpreter was done executing.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,635
  • Joined: 19-February 09

Re: Assigning strings as variable names. (incognito)

Posted 15 February 2012 - 10:50 AM

Hi, yes, let us suppose a simple/naive implementation for perhaps single data types. You could have a name and a value as strings. For data types other than a string the value would need to be converted and you may still need to remember the data type. You could also try separate structs for each data type but you would still need to distingush between the data type - probably by having separate arrays.


struct Variables 
{
  char name[MAXNAME+1];
  char value[MAXVALUE+1];
};




Another way, when multiple data types are used, is to have structure elements for each value type.

struct Variables 
{
  char name[MAXNAME+1];
  char datatype;

  int     i;
  double  d;
  char    ch;   

};




There is only one data type at a time for each variable, so a union can be used for the values - to save space/memory.

This post has been edited by #define: 15 February 2012 - 11:00 AM

Was This Post Helpful? 1
  • +
  • -

#5 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito)

Posted 15 February 2012 - 11:51 AM

View Post#define, on 15 February 2012 - 11:50 AM, said:

Hi, yes, let us suppose a simple/naive implementation for perhaps single data types. You could have a name and a value as strings. For data types other than a string the value would need to be converted and you may still need to remember the data type. You could also try separate structs for each data type but you would still need to distingush between the data type - probably by having separate arrays.


struct Variables 
{
  char name[MAXNAME+1];
  char value[MAXVALUE+1];
};



I'm not quite sure which has the most benefit.

View Post#define, on 15 February 2012 - 11:50 AM, said:

Another way, when multiple data types are used, is to have structure elements for each value type.

struct Variables 
{
  char name[MAXNAME+1];
  char datatype;

  int     i;
  double  d;
  char    ch;   

};




There is only one data type at a time for each variable, so a union can be used for the values - to save space/memory.


Say storing the type, variable name, and value in a struct
created from a token.

Like:
loop(){
STORE "name"; /* A new name is assigned to STORE struct */
TYPE "int"; /* A new type is assigned to TYPE struct */
VALUE "200"; /* A new value is assigned to VALUE struct */
variable_create(STORE, TYPE, VALUE);
}



Or maybe:
VARIABLE("name = 200"); /* The type is formed with dynamic casting. */
/* The casting could be formed by the parsers check of syntax like
$name = "200";
would become a string because the interpreter would know $ means variable and " mean string.
$name = 200;
would become an int because it has no ".
or
name = 200;
could be used as an int based on left right assignment on the = that the interpreter would use
to figure out that it's a variable and the interpreter knows there's no quotes . 
*/



The expansion would create a new indexed struct.

Either way some how the names and values become keys and values in a struct
containing an array.

Not great examples, but there's an idea in there.

I have not used macros much so there may be some wrong syntax in there.
Just some quick pseudo-code.

This post has been edited by hiddenghost: 15 February 2012 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito)

Posted 15 February 2012 - 11:58 AM

In some sense I know there will have to be some sort of loop creating indexes and converting types.
Was This Post Helpful? 0
  • +
  • -

#7 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito)

Posted 20 February 2012 - 11:31 PM

Honestly I don't know what I'm doing.

I always learn the hard stuff first.

If the hard stuff requires some pre-knowledge that requires less skill I regress to that point of learning.

Seems to have worked so far.

I'll be posting some really nonsensical code as I go along.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1