FreeBASIC syntax challenge games

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

My solution:

Code: Select all

 '============= 3 lines to produce ABCDEF on the console (colons) without cls ==============
#undef __function__
Function __function__ () As String : Static As Integer I = Asc("A") : Function = Chr(I) : I += 1 : End Function
'==============================================================================
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

fxm wrote:My solution:

Code: Select all

 '============= 3 lines to produce ABCDEF on the console (colons) without cls ==============
#undef __function__
Function __function__ () As String : Static As Integer I = Asc("A") : Function = Chr(I) : I += 1 : End Function
'==============================================================================
Nice.

In two lines this also is running:

Code: Select all

 '============= 3 lines to produce ABCDEF on the console (colons) without cls ==============
print "ABCDEH"
end
'==============================================================================
Ok, this doesn't pause! So here a version with pause before ending.

Code: Select all

 '============= 3 lines to produce ABCDEF on the console (colons) without cls ==============
input "ABCDEH", *(new integer)
end
'==============================================================================
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

"ABCDEH" ?
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

fxm wrote:"ABCDEH" ?
Sorry, silly typo!

However I'm meeting an issue here, with a strategy for remapping the keys inspired from your previous suggestion.

Code: Select all

for i as integer = 0 to 5
    *@(chr(asc("A") + i)) = chr(asc("F") - i)
next i

So it would work with 'i' a literal number , but it's broken when it is a variable (necessary here to make 3 lines of code). That's a quite incomprehensible limitation. Is there a way to fix it?

The problem concerns only the first member of the assignment.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

fxm's solution is good with -gen gas.
Tourist Trap's lines don't seem to work, I still see FEDCBA

fxm's pile of errors with -gen gcc.

Code: Select all

Compiler output:
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:30:21: error: expected identifier or '(' before '__FUNCTION__'
 FBSTRING* __stdcall __FUNCTION__( void );
                     ^
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:32:21: error: expected identifier or '(' before '__FUNCTION__'
 FBSTRING* __stdcall __FUNCTION__( void )
                     ^
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c: In function 'A':
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:102:19: error: called object '__FUNCTION__' is not a function or function pointer
  FBSTRING* vr$2 = __FUNCTION__(  );
                   ^
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:102:19: note: declared here
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c: In function 'B':
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:118:19: error: called object '__FUNCTION__' is not a function or function pointer
  FBSTRING* vr$2 = __FUNCTION__(  );
                   ^
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:118:19: note: declared here
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c: In function 'C':
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:134:19: error: called object '__FUNCTION__' is not a function or function pointer
  FBSTRING* vr$2 = __FUNCTION__(  );
                   ^
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:134:19: note: declared here
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c: In function 'D':
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:150:19: error: called object '__FUNCTION__' is not a function or function pointer
  FBSTRING* vr$2 = __FUNCTION__(  );
                   ^
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:150:19: note: declared here
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c: In function 'E':
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:166:19: error: called object '__FUNCTION__' is not a function or function pointer
  FBSTRING* vr$2 = __FUNCTION__(  );
                   ^
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:166:19: note: declared here
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c: In function 'F':
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:182:19: error: called object '__FUNCTION__' is not a function or function pointer
  FBSTRING* vr$2 = __FUNCTION__(  );
                   ^
C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\FBIde0.4.6r4\FBIDETEMP.c:182:19: note: declared here

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc:   FreeBASIC Compiler - Version 1.05.0 (01-31-2016), built for win32 (32bit)
OS:    Windows NT 6.2 (build 9200)
 
My own solution works in 32 and 64 bits.
I am still using version 1.05.0 of course, maybe these above errors are not there in version 1.06.0.

Clue to my method:
There are ways to initiate without Preprocessor directives.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Code: Select all

 '============= 3 lines to produce ABCDEF on the console (colons) without cls ==============
  Sub Start () Constructor
    Print "ABCDEF" : Sleep : End
  End Sub
'==============================================================================
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

Yes of course, trivial.
Mine:
Sub z Constructor
A:B:C:D:E:F:Sleep:End
End Sub
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

dodicat wrote: Tourist Trap's lines don't seem to work, I still see FEDCBA
Hot wine abuse?
Ok, anyway I'm learning this rather underused subroutine constructor stuff thanks to you. Then here is another possible solution, working this time, and based on remapping the string literals, but in 5 lines!

Code: Select all

Declare Sub A
Declare Sub B
Declare Sub C
Declare Sub D
Declare Sub E
Declare Sub F

F:E:D:C:B:A:Sleep:End

'============= 3 lines to produce ABCDEF on the console (colons) without cls ==============
sub Main constructor
        swap *@(chr(asc("A") + 0)) , *@(chr(asc("A") + 5))
        swap *@(chr(asc("A") + 1)) , *@(chr(asc("A") + 4))
        swap *@(chr(asc("A") + 2)) , *@(chr(asc("A") + 3))
end sub
'==============================================================================

Sub A
    Print __function__;
End Sub

Sub B
    Print __function__;
End Sub

Sub C
    Print __function__;
End Sub

Sub D
    Print __function__;
End Sub

Sub E
    Print __function__;
End Sub

Sub F
    Print __function__;
End Sub

Sleep
And here again the remark made before, it doesn't support variables in the expression... Any fix??

Code: Select all

Declare Sub A
Declare Sub B
Declare Sub C
Declare Sub D
Declare Sub E
Declare Sub F

F:E:D:C:B:A:Sleep:End

'============= 3 lines to produce ABCDEF on the console (colons) without cls ==============
sub Main constructor
    for i as integer = 0 to 2 
        swap *@(chr(asc("A") + i)) , *@(chr(asc("A") + 5 - i))  ''code broken by introduction of variable...
    next i
end sub
'==============================================================================

Sub A
    Print __function__;
End Sub

Sub B
    Print __function__;
End Sub

Sub C
    Print __function__;
End Sub

Sub D
    Print __function__;
End Sub

Sub E
    Print __function__;
End Sub

Sub F
    Print __function__;
End Sub

Sleep
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Your code crashes with gcc (32-bit + gcc, and 64-bit) because only gas allows to write in .data section (what is bad).

Your code (working only with gas) can be simplified:

Code: Select all

'============= 3 lines to produce ABCDEF on the console (colons) without cls ==============
sub Main constructor
        swap *@"A" , *@"F"
        swap *@"B" , *@"E"
        swap *@"C" , *@"D"
end sub
'==============================================================================
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

fxm wrote:

Code: Select all

'============= 3 lines to produce ABCDEF on the console (colons) without cls ==============
sub Main constructor
        swap *@"A" , *@"F"
        swap *@"B" , *@"E"
        swap *@"C" , *@"D"
end sub
'==============================================================================
Thanks for explaning the reason why it would probably crash for most of configurations. But it can not be simplified like you show for the reason that the way it is expanded in my example is for holding a variable because then it runs in a simple loop rather than writting a full collection of instructions.
Have you by luck any idea on how to make your version a loop with a variable inside? I don't see what is bad in this case (if the long version runs, the short version should do the same?).
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Before executing program, the 6 characters "A", "B", "C", "D", "E", "F" are set in the .data section.
Generally, they are not contiguous in memory.
The compiler allows only to access to the 6 addresses of the string literals in .data section, so @"A", @"B", @"C", @"D", @"E", @"F".

Code: Select all

.section .data
.balign 4
_Lt_0006:	.ascii	"A\0"
.balign 4
_Lt_0008:	.ascii	"D:\\Users\\T0003830\\Documents\\Mes Outils Personnels\\FBIde0.4.6r4_fbc1.06.0\\FBIDETEMP.bas\0"
.balign 4
_Lt_0009:	.ascii	"F\0"
.balign 4
_Lt_000B:	.ascii	"B\0"
.balign 4
_Lt_000D:	.ascii	"E\0"
.balign 4
_Lt_000F:	.ascii	"C\0"
.balign 4
_Lt_0011:	.ascii	"D\0"
.balign 4
_Lt_0014:	.ascii	"MAIN\0"
.balign 4
_Lt_002F:	.ascii	"{MODLEVEL}\0"
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

fxm wrote:Before executing program, the 6 characters "A", "B", "C", "D", "E", "F" are set in the .data section.
Generally, they are not contiguous in memory.
I admit this without any problem. My only issue is with the syntax equivalence that make the multiple call of a function be reduced to a single loop with a variable. In general this works, and this is basically what programming is about most of time. Is there a known reason why here it doesn't (I've tried to fix it up by the function returning byref trick, but without any success in this case).
Maybe I miss an obvious thing, or something that I don't know about the operator @.

Code: Select all

? *@(chr(asc("A") + 0)) ''ok

dim as integer i = 0    
? *@(chr(asc("A") + i)) ''nok
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Compiler knows the addresses of the 6 only characters ("A", "B", "C", "D", "E", "F"), those which are used in the program and written in the .data section at different locations (it is not a table and there is no formula to determine an address from another).
So to determine the address of Chr(i), even when 'i' corresponds to the ASCII code of one of the 6 characters, that must be done at compile time (and not at execution time), and so 'i' must be a constant expression fixed at compile time.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Control to verify that previous challenges are well assimilated!

Enigma #13, force 1

By only adding code in the dedicated insertion zone, and without modifying the rest of program, make so that the line 'Print UDT.stringRtn()' of the program displays "You won!":

Code: Select all

Type UDT Extends Object
  Public:
    Declare Static Function stringRtn () As String
  Protected:
    Static As String s
End Type

Const As String s0 = "You won!"

'Dim As String UDT.s = s0        '' error 86: Var-len strings cannot be initialized
Dim As String UDT.s

Static Function UDT.stringRtn () As String
  If UDT.s = "" Then Return "You lost!"
  Return UDT.s
End Function

Print UDT.stringRtn()

'--- Zone beginning of code insertion ---

'--- Zone ending of code insertion ------

Sleep
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

I have this done.
It took 9 lines! --- (but no cheating).
I'll wait a while though, maybe somebody can do it in fewer lines.
Post Reply