ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My line numbers had were origionally needed when I started, to "move" the code to the right spot. Now that's been dealt with a long time ago. Line numbers in the source code are not needed. It generates the code, and it's in the right spot.
It first generates psudocode, that is in a data structure with a name, and some code. These are code "chunks", that make up the whole thing. Each part of the syntax, has a name, and a chunk of psudocode. Later on from that, it is joined and put into an array, which could still be modified if necessary. Then the array is output to a file. From my current understanding, though this is flat, this is basically a type of AST.
What I needed to do, was know how to fix up the label numbers in the generated code. The transformation occurs after the code is in the array, but before the code is dumped to a file. This is so that I can see all the label numbers in the program.
It is how to fix it up that remains, unless my clues to use to fix it up are wrong, I can fix it up using my clues and then end up with correct code for all if-else scenerios. I just am not sure how to fix it up and if my clues are wrong, I'm not sure what kind of clues to leave to fix it up.
I could, go back and add features in the interpreter for string label names. This would end up with code to jump to a label name, instead of a label number. But this might sort of shift the problem a little. Since no two labels can be the same, or it won't know which one to jump to, I'd have to fix up the label names with numbers as well as names, so that the code for the if-else wouldn't make them all the same label names for it's parts, over and over. Then I'd need code to know which number as well as which name. Probably I could do this using the ifcounter, which counts the number of ifs.
If this problem is indeed to complicated you guys think to solve without more knowledge, then I will close it and study more before finishing, but I'm not sure that it is, as I am getting very close to a solution I believe.
1. Count the labels until there is an if-else.
2. Count the if else labels.
3. Replace end with the results of (step 1 - 1) + (step 2 - 1).
4. Count the labels from the if begin until the if code end.
5. Replace if_part with (step 4) - 1.
6. Count the labels from the if begin until the if or else end.
7. Replace else_part_or_end with (step 6) - 1.
I think that would work if there was no ladder, but there is a the possibility of a ladder, so I need to take that into account.
Perhaps I didn't understand. My intent with the ifcounter variable, is to count the number of if statements. In my current code, I was using ifcounter to tell apart the ifs, such as:
Code:
VERSION TVOID 0V
VOID TVOID 0V
; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TVOID if_part
GOTOL TVOID else_part_or_end
LBL TVOID 0V
VOID TVOID 0V
; End if code :1
GOTOL TVOID end
LBL TVOID 0V
; End if or else code :1
QUIT TVOID 0
END TVOID 0V
The 1's, are telling me that part belongs to if number 1.
Well, I don't understand (nor wish I to understand) your Assembly language, but the ifcounter-problem is quit interesting (partly because of the optional 'else' part), so I've hacked a minimal example with minimal/no code-generation:
Code:
if: IF { /* $2 */
int current_if;
current_if= $$.ifcounter= ++ifcounter;
fprintf (stderr, "Here starts if#%d\n",
current_if);
fflush (stderr); }
'(' expr ')' { /* $6 */
int current_if= $2.ifcounter;
fprintf (stderr, "We are after the code of if#%d / condition-part\n",
current_if);
fprintf (stderr, "\tJMP_conditional NMATCH_%d\n",
current_if); }
stmt { /* $8 */
int current_if= $2.ifcounter;
fprintf (stderr, "We are after the code of if#%d / then-part\n",
current_if);
fflush (stderr); }
opt_else { /* $10 */ };
opt_else: { /* $1 */
/* WATCH OUT: GETTING VALUE FROM 'if:' rule */
int current_if= $-6.ifcounter;
$$.obj = NULL;
fprintf (stderr, "NMATCH_%d: LABEL\n",
current_if); }
| ELSE { /* $2 */
/* WATCH OUT: GETTING VALUE FROM 'if:' rule */
int current_if= $-6.ifcounter;
fprintf (stderr, "\tJMP ENDIF_%d\n",
current_if);
fprintf (stderr, "NMATCH_%d: LABEL\n",
current_if);
fflush (stderr);
}
stmt { /* $4 */
/* WATCH OUT: GETTING VALUE FROM 'if:' rule */
int current_if= $-6.ifcounter;
fprintf (stderr, "We are after the code of if#%d / else-part\n",
current_if);
fprintf (stderr, "ENDIF_%d: LABEL\n",
current_if);
fflush (stderr); };
example input:
Code:
IF (1 == 2) PUT ('then1')
ELSE IF (2 == 3) PUT ('then2')
ELSE PUT ('else');
example output:
Code:
Here starts if#8
We are after the code of if#8 / condition-part
JMP_conditional NMATCH_8
We are after the code of if#8 / then-part
JMP ENDIF_8
NMATCH_8: LABEL
Here starts if#9
We are after the code of if#9 / condition-part
JMP_conditional NMATCH_9
We are after the code of if#9 / then-part
JMP ENDIF_9
NMATCH_9: LABEL
We are after the code of if#9 / else-part
ENDIF_9: LABEL
We are after the code of if#8 / else-part
ENDIF_8: LABEL
I just modified the interpreter so that I can use label names as well as label numbers. I will try to use my label names to write the code now instead of the label numbers. I may be just shifting the problem, but I'll try it and see if I shifted the problem or not. I think ifcounter already does what it is intended to do.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.