1. ## Dynamic variable assignment

The other thread got me thinking about this. Before I go and look at GNU Octave's source code, say I wanted to make a similar program that interprets strings from a prompt. I know how to parse a string like "a=5" and recognize that "a" is being assigned the value of "5", but how would I go about actually storing that? Can you do some sort of cast to a char array so that you can assign it as an actual variable? Otherwise, all I could think of is making a struct with a char array for the name and a double for the value.

Tldr, how would one get from "a=5" to int a = 5; ?

2. If I got it then use fscanf() to read stdin and store it in an integer variable.

3. Originally Posted by Epy
Tldr, how would one get from "a=5" to int a = 5; ?
You can't -- at least, not directly in a C program.

Is Octave interpreted or compiled? Since it has a CLI interface, I would guess the former.

all I could think of is making a struct with a char array for the name and a double for the value.
Well, from what I have read about compilers/interpreters, that is literally how a "symbol table" -- which could be a binary tree or a linked list or something similar -- is constructed.

Maybe you will find this interesting, I wrote it in the summer, it is short n' sweet and includes eg. a prog for parsing math expressions like "2+2*(14/6)-12" by using compiler type techniques:

Parsing

So, in fact, if you wanted a program that accepted variable assignments as inputs, you could proceed from there. The variable would simply be a "leaf node" like a number/value, since that is what it stands in for.

4. this week i have been working on a similar sort of thing (not C++, but thats irrelevant). among other work, the two key things were: 1) creating classes to represent the equations, 2) using (hash)maps to map a variable to its value.

1) the equation "a = 5" has two parts, left hand side (LHS) and right hand side (RHS). the LHS is always just a string (or a char) to identify the value of its RHS. the RHS is a sequence of values (identifiers or numbers) and binary operations (+, -, /, *). the RHS in this example is a "primitive" one and is just a number "5". another equation could be "b = 1 + 2 * 3". this time the RHS is a "complex" sequence of operations, which also has a "left" and "right" and an "operator". the "left" is the primitive value "1", the operation is "+", the right is the complex expression "2*3". now, compex right is broken down into an "expression" with left "2", op "*", right "3". note that this is right-associative, and you may not be doing it like this, and rather doing it using BEDMAS or introducing brackets to enforce ordering. (assuming either left or right associativity makes things easier).

2) use maps to associate a key to an number value (integer, real, whatever). in the first case above, we would insert into the map the key "a" and its numerical value of 5. similarly, we would insert the key "b" with value of its expression, 7 (right associative, it would have been 9 if left associative).

of course some expressions can use variables in it. such as "c = 1 + a + b". however, in math, just as in programming, you have to define something before you use it. that means that we already know the values of a and b (they are in the map, values 5 and 6 respectively). so when we see these variables in the expression, we look them up in the map and replace them with their value, so "c = 1 + a + b" becomes "c = 1 + 5 + 6" and we save key c with value 12 into the map.

i never even mentioned the process of converting a string "5" (from "a=5") into a number, but that is the least of your worries! . you are basically writing a parser to take input ("a=5"), do lexical analysis (split it into 3 parts, lhs, rhs, symbol "="), and do semantic analysis to take these 3 "symbols" and make sense of it (i.e. make the c++ objects from them; "equation", "expression", etc). this is what i did also... its definitely challenging, but quite interesting and rewarding! good luck.

5. I've been so spoiled by LISP. haha