LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2014, 11:23 PM   #91
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36

complicatediftest3:
Code:
if (3 + 2)
{
 ;
}
else if (false)
{
 ;
}
else
 ;
compiles to:
Code:
VERSION TVOID 0V


; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_1"
GOTOL TSTRING "else_part_or_end_1"


LBL TSTRING "if_part_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_1"
; Begin if :2
ALOAD TBOOLEAN false
CGOTOL TSTRING "if_part_2"
GOTOL TSTRING "else_part_or_end_2"


LBL TSTRING "if_part_2"
VOID TVOID 0V
; End if code :2
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_2"
VOID TVOID 0V
; End if or else code :2
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :2
; End if or else code :1
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :1

END TVOID 0V
and should compile to:
Code:
VERSION TVOID 0V


; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_1"
GOTOL TSTRING "else_part_or_end_1"


LBL TSTRING "if_part_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_1"
; Begin if :2
ALOAD TBOOLEAN false
CGOTOL TSTRING "if_part_2"
GOTOL TSTRING "else_part_or_end_2"


LBL TSTRING "if_part_2"
VOID TVOID 0V
; End if code :2
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_2"
VOID TVOID 0V
; End if or else code :2
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :2
; End if or else code :1
GOTOL TSTRING "end1"


LBL TSTRING "end1"
; End if :1

END TVOID 0V
 
Old 09-10-2014, 11:43 PM   #92
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Finally, complicatediftest4:

Code:
if (3 + 2)
{
 ;
}
else if (false)
{
 ;
}
else if (true)
{
 ;
}
else
 ;
compiles to:
Code:
VERSION TVOID 0V


; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_1"
GOTOL TSTRING "else_part_or_end_1"


LBL TSTRING "if_part_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_1"
; Begin if :2
ALOAD TBOOLEAN false
CGOTOL TSTRING "if_part_2"
GOTOL TSTRING "else_part_or_end_2"


LBL TSTRING "if_part_2"
VOID TVOID 0V
; End if code :2
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_2"
; Begin if :3
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_3"
GOTOL TSTRING "else_part_or_end_3"


LBL TSTRING "if_part_3"
VOID TVOID 0V
; End if code :3
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_3"
VOID TVOID 0V
; End if or else code :3
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :3
; End if or else code :2
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :2
; End if or else code :1
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :1

END TVOID 0V
but should compile to:
Code:
VERSION TVOID 0V


; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_1"
GOTOL TSTRING "else_part_or_end_1"


LBL TSTRING "if_part_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_1"
; Begin if :2
ALOAD TBOOLEAN false
CGOTOL TSTRING "if_part_2"
GOTOL TSTRING "else_part_or_end_2"


LBL TSTRING "if_part_2"
VOID TVOID 0V
; End if code :2
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_2"
; Begin if :3
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_3"
GOTOL TSTRING "else_part_or_end_3"


LBL TSTRING "if_part_3"
VOID TVOID 0V
; End if code :3
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_3"
VOID TVOID 0V
; End if or else code :3
GOTOL TSTRING "end"


LBL TSTRING "end1"
; End if :3
; End if or else code :2
GOTOL TSTRING "end"


LBL TSTRING "end2"
; End if :2
; End if or else code :1
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :1

END TVOID 0V
 
Old 09-10-2014, 11:45 PM   #93
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
What is an algorithm to fix up these (and any other) if-else statement so it turns out how it's supposed to?
 
Old 09-10-2014, 11:50 PM   #94
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Or is this too complicated to solve, because something in my AST needs to be adjusted?
 
Old 09-13-2014, 01:11 AM   #95
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
My problems with code generation seem to be caused by the optional else clause. The desire is to have this function as it does in C++, but if I can't make that work, maybe there is a better way to do it. I could omit the optional else, but it still has to have a construct for being able to write code such as the if-else ladder. I seem to be having trouble, because the code for the if-else ladder is complicated.
 
Old 09-14-2014, 12:14 AM   #96
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,885
Blog Entries: 1

Rep: Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872
For a start, stop using label "end", instead use labels like "iftrue_N", "iffalse_N" and "endif_N". (N is the 'ifcounter')

Also, you could remove word "TSTRING" from your code, it is of no use.
 
Old 09-17-2014, 12:07 AM   #97
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I could try using labels like that. TSTRING is the type for the instruction and is needed. I "could" make an even higher level language, but that is mostly an assembly type language, and remove it in that and do some modifications to the language, but it is needed in the actual runnable language, and therefore carries over to the assembly language. T stands for type, and STRING, is a string type. The LBL instruction takes either TVOID or TSTRING. TSTRING specifies a string type label, where you could jump to it with the string name, and TVOID simply specifies a label number type jump, where you do NOT have a name. Therefore with that kind of jump, you must use a label number. You can, use a label number to jump to a string type label, but you do not have to. Since I already had label number jumps, implementing it in this way made the language more powerful, rather than just allowing a string type jump. It also allowed backward compatibility for the code I'd already written, though it isn't much.
 
Old 09-17-2014, 12:14 AM   #98
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
The reason by the way, that the lower levels need the TSTRING, and cannot be assuming it in certain cases, is because it makes the syntax simpler to understand to always have an instruction, a type, and an operand. Earlier languages which were steps to this version of the language (which is a fully working language, though maybe not fully developed), had sometimes just part of it. But I decided it's clearer to always have a type, even if the type ends up being TVOID (void value). In higher levels above the assembly language, it is okay to just have a string or a value, and let the computer figure out in the lower level which value to assign it, as long as it's also specified in a way in which the user will understand it too. But for the assembly language, and the lowest level language (PNF), it is not okay. Would it make it that much easier if I created such a high level language and compiled this language to that?
 
Old 09-17-2014, 12:25 AM   #99
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,885
Blog Entries: 1

Rep: Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872
TSTRING is not 'the type of the instruction', it is 'the type of the next operand':

eg:
Code:
CGOTOL TSTRING "if_part_2"
You could simply write this:

Code:
CGOTOL "if_part_2"
Lexical parsers do recognize string-literals without putting reserved word TSTRING before them.
 
Old 09-17-2014, 01:11 AM   #100
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
That's what I meant. I know it could be done, but it would make the lower levels harder to understand. If I needed to do that, I would want a higher level language and compile to that instead.
 
Old 09-17-2014, 01:58 AM   #101
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Also, at the lowest level, 0 could be either a void value, false, the number 0, a null character, or a null string. It'd be tricky to tell which is meant. At the assembly level, where we use words instead of numbers, I could've had the assembler figure it out, but that would break it's ability to generate ANY code. Should I make a higher level that makes assembly nicer, and compile to that?
 
Old 09-17-2014, 02:35 AM   #102
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,885
Blog Entries: 1

Rep: Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872
No, right now you should read post #82.
 
Old 09-18-2014, 01:44 PM   #103
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
So I changed my code to match the action layout of post #82. My code now looks like:

Code:
if_statement:		IF
			{
			 ASTNode if_statementp1("if_statementp1");

			 ++ifcounter;
			 ifgoto = ifcounter < 2 ? ifcounter : ifcounter + 1;

			 String if_body_str = "if_body_";
			 if_body_str += ifgoto - 1;

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(if_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 if_statementp1.conprint("; Begin if :%u\n", ifcounter);

			 tree.add_node(if_statementp1);		
			}
			LEFTP expression RIGHTP
			{
			 ASTNode if_statementp2("if_statementp2");


			 if_statementp2.conprint("ALOAD TBOOLEAN %s\n", $2.to_boolean().get().getString().c_str());
			 if_statementp2.conprint("CGOTOL TSTRING \"iftrue_%u\"\n", ifcounter);
			 if_statementp2.conprint("GOTOL TSTRING \"iffalse_or_end_%u\"\n", ifcounter);

			 if_statementp2.conprint("\n\nLBL TSTRING \"iftrue_%u\"\n", ifcounter);


			 tree.add_node(if_statementp2);
			}
			stmt
			{
			 ASTNode if_statementp3("if_statementp3");
			 // Code for if will be inserted here...
			 if_statementp3.conprint("; End if code :%u\n", ifcounter);
			 ifbegin = outcount;

			 String after_if_str = "after_if_";
 			 after_if_str += ifgoto - 1;

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(after_if_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);
			 endifbegin = outcount - 1;

			 if_statementp3.conprint("GOTOL TSTRING \"end_%u\"\n", ifcounter);

			 tree.add_node(if_statementp3);
			}
			opt_else
			{
			 --ifcounter;
			 ++ifnest;
			}
			;

opt_else:		{
			 ASTNode if_statementp4("if_statementp4");
			 if_statementp4.conprint("\n\nLBL TSTRING \"iffalse_or_end_%u\"\n", ifcounter);
			 tree.add_node(if_statementp4);
			}
			| ELSE
			{
			 ASTNode if_statementp5("if_statementp5");

			 ++elsecounter;
			 
			 // Code for else will be inserted here if exists...
			 if_statementp5.conprint("; End if or else code :%u\n", ifcounter);
			 elsebegin = outcount;

			 String else_body_str = "else_body_";
			 else_body_str += ifcounter;
			 String after_if_str = "after_if_";
 			 after_if_str += ifgoto - 1;

			 labelTable.remove();

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(else_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(after_if_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 if_statementp5.conprint("GOTOL TSTRING \"end_%u\"\n", ifcounter);


			 tree.add_node(if_statementp5);
			}
			stmt
			{
			 ASTNode if_statementp6("if_statementp6");
			 if_statementp6.conprint("\n\nLBL TSTRING \"end_%u\"\n", ifcounter);
			 if_statementp6.conprint("; End if :%u\n", ifcounter);
			 tree.add_node(if_statementp6);
			}
			;
 
Old 09-18-2014, 01:46 PM   #104
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
iftest, which looks like this:
Code:
;
if (3 + 2)
 ;
end 0;
Now compiles to:
Code:
VERSION TVOID 0V


VOID TVOID 0V
; Begin if :1
ALOAD TBOOLEAN false
CGOTOL TSTRING "iftrue_1"
GOTOL TSTRING "iffalse_or_end_1"


LBL TSTRING "iftrue_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end_1"


LBL TSTRING "iffalse_or_end_1"
QUIT TVOID 0

END TVOID 0V
It should compile to:
Code:
VERSION TVOID 0V


VOID TVOID 0V
; Begin if :1
ALOAD TBOOLEAN false
CGOTOL TSTRING "iftrue_1"
GOTOL TSTRING "end_1"


LBL TSTRING "iftrue_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end_1"


LBL TSTRING "end_1"
QUIT TVOID 0

END TVOID 0V
 
Old 09-18-2014, 01:51 PM   #105
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
ifelsetest:
Code:
if (3 + 2)
 ;
else
 ;
compiles to:
Code:
VERSION TVOID 0V


; Begin if :1
ALOAD TBOOLEAN false
CGOTOL TSTRING "iftrue_1"
GOTOL TSTRING "iffalse_or_end_1"


LBL TSTRING "iftrue_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end_1"
; End if or else code :1
GOTOL TSTRING "end_1"
VOID TVOID 0V


LBL TSTRING "end_1"
; End if :1

END TVOID 0V
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Portable Numbers Format: PNFHA des_a Programming 14 07-18-2014 10:51 PM
[SOLVED] Prime Generation Code Not Working Properly ashok.g Programming 29 12-14-2009 03:59 AM
A GCC code generation problem? dogbird Programming 4 12-09-2005 11:52 AM
tool for code generation? bcalmac Linux - Software 0 08-22-2005 10:41 AM
UML and C++ code generation GŠutama Linux - Software 2 11-13-2003 06:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration