Writting a CODE converter

Started by Baggey, April 02, 2023, 10:28:57

Previous topic - Next topic

Baggey

Some of my programs are very long and have many different Modules to them.

Sometimes you want to rename a variable or function name but its repated a thousand times!

This becomes a nightmare to alter! But i have a progam written in Blitzmax "see Below" It sorts through the code and changes everything in an instance!
https://www.syntaxbomb.com/blitzmax-blitzmax-ng/altering-text-within-a-file/

This got me thinking into how a Compiler / Interpretuer works.

My thoughts so far are for modifying my "Text modifyer" to do say convert a program from another language!

I tried using the "Import bb" in BlitzMaxNG and it dosent work! ???

Ive been experimenting with going through the code to find the text i want to alter and check either side of my position to see if i want to alter it.
This works well for obvious things! like Const, :Byte , ' ie Comments etc. I sort of called these simple finds as group 1

So we goto the next type of thing to change. Lets say "Type and End Type" i found experimenting with this that it's best to alter longer names first and do the shorter was last! Otherwise you get un predicatable results. So im calling this group 2

Next Im finding that the Function decleration in BlitzMax is almost the same as lets say PUREBasic but back to front. So i suppose the way to alter this then you look for the function screen read through and sort of know which characters to look for and then re-trim and cut and paste to make the new line! group 3

Ah your still with me at this point ;D

So without "boring the hind legs of a Donkey" :))

Im intrigued as to how a Compiler / Interpreter works.

An 8 bit micro uses Token word tables that are indexed with pointers as it runs through a Line of code. It knows a code for the start of a line then line number then a code for instruction then characters and so on to the end of line normaly with 13 Carrige Return as a terminator and so on. Within this it knows when it encounters certain codes it will call a Machine code routine in memory to execute. But this is all known! or it will throw a Syntax error.

Im looking at code in a format of something and rearranging it to be something similair. So maybe you'd write a routine for each command to change.

Still with me? ::)

Does anyone have experience in this?

What i want todo is write lets say a "Blitzmax .bb to .bmx converter" or a "PureBasic .pbp to BlitmaxNG .bmx convertor" Google searching doesnt give much!

So I ask, Is this an impossible task? I think you would'nt get a fool proof translation as sometimes code is spaced differently or the Then statement is used and sometimes not. So only a Human could decide whats appropriate. This could be group 4. When as much code is translated as possible we then go line by line altering! :o

If anyone has tried altering code line by line its a nightmare. So this would be a very useful tool in deed. I suppose Blitzmax is doing this to C. So not entirely impossible.

Kind Regards Baggey


Running a PC that just Aint fast enough!? i7 Quad core 16GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!

dawlane

#1
If you really want to know how a compiler or interpreter works. I would suggest getting hold of a couple of good books, such as the Dragon Compiler book, Loaders and Linkers and The Compiler Design Hand book. But for the time being, have a look here and search around on compilers, lexers, code parsers and abstract sysntax trees.

If you want to see an example, then look at the Cerberus-X sources. BlitzMax NG would be a bit over complicated with how it works.

Midimaster

Isn't it a "Lexer" or a "Parser" he is looking for?

Only translate a source language command line into a target language command line...

  • Reading one line
  • Finding and Checking the Tokens
  • Put it together again in the new language line
...on the way to Egypt

dawlane

Quote from: Midimaster on April 02, 2023, 12:28:15Isn't it a "Lexer" or a "Parser" he is looking for?

Only translate a source language command line into a target language command line...

  • Reading one line
  • Finding and Checking the Tokens
  • Put it together again in the new language line

He's used 'how a Compiler/Interpreter works' twice in his post. And as he would still have to read a book or internet articles on compilers to learn about lexers. And most questions being asked on parsers and lexers will mention somewhere about the Dragon book.

Baggey

Thanks for the Links. Looks like im in for a heavy read :-X

Looking for the ideas on how to go about getting started really.

Baggey
Running a PC that just Aint fast enough!? i7 Quad core 16GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!

Baggey

Quote from: Midimaster on April 02, 2023, 12:28:15Isn't it a "Lexer" or a "Parser" he is looking for?

Only translate a source language command line into a target language command line...

  • Reading one line
  • Finding and Checking the Tokens
  • Put it together again in the new language line

Yes to this as well! is Line by line the best approach as im doing every thing in one go at the moment well trying too :-\ . Been looking through the Forum to find some code i can try the concept out with. ;D

I know once you get the general code started it's building on it! Its like writting 800 odd Z80 commands out there frame work is proabably 80% the same it's a lot of typing and working out what you need to tweak to get the instruction to work in Basic.

Kind Regards
Running a PC that just Aint fast enough!? i7 Quad core 16GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!

Baggey

#6
So, Here's were im at so far today! When you've compiled. Ignore the main window! Slide it out the way! Its something else im thinking of implementing.
Put your .pbi File in the same directory as your .bmx. Alter the file name in the Code to the File you want to open/Translate! or Parse

Im thinking the two Arrays for Alter and new text Could be changed to compensate .bb Commands or even my old friend "PlayBasic"

RUN

And BOOM Converted! Loads to add though Does Group 1 and group 2 sort off! Thinking of converting HEX to Decimal and vis versa!

This is runnable CODE! in BlitMaxNG 8)
'
        ' PureBasic To BlitzMax Conversion Tool
        '
        ' Written by Lee Chatt
        '
        ' This will save you a S**T Load of typing!
        '
        ' It's not Fool Proof and may not be Fully Runnable!
        '
        ' It will get you almost there! But YU get it!
        '
        ' Summary :- Was written to convert Sid Code and anything else Intersting
    '
    ' Usage :- Use this as you will! But if you convert anything interesting you PROMISE to post on the FORUM!
    '
    ' Kind Regards and Enjoy!
    '
    SuperStrict

    AppTitle = "PureBasic To BlitzMax Converter"

    Graphics 800,600
    
    Global File:String = "xxxxxxx.pbi" ' FILE to me Modified HERE!
    Global Path:String = "" ' Leave BLANK if file is in Same Directory!
   
        'load the original file:-
    If FileSize(Path+File)=-1 Then

            Notify "File ~r" + Chr(34) + Path+File  + Chr(34) + "~rnot found!"
            End

    EndIf
 
    Global Original:String  = LoadText(Path+File)
    Global OriginalLength:Int = Original.Length
        Global ReplaceText:String
        Global NewText:String
       
        Global Word:String
    Global FoundTextPos:Int
        Global TextLength:Int
        Global TextNext:Int
        Global TextBefore:Int
        Global TextAfter:Int
        Global FoundSomething:Byte=0
        Global StartOFline:Int=0
       
        Global Commands:Int=0
        Global TryChar:String[]=["XIncludeFile","Static","CompilerEndIf","CompilerIf","DisableExplicit","ProcedureReturn","Protected","EndIf",";",":","EndMacro","Macro","EndProcedure","Procedure","EnableExplicit","EndStructure","Structure","#",".a",".d",".w",".s",".l",".i",".u",".d",">>","<<","\"] ' <--- Text we want to alter
        Global ReplaceWith:String[]=["Include","Local","End If","If","","Return","Local","End If","'",";","End Function","Function","End Function","Function","SuperStrict","End Type","Type","Const ",":Byte",":Double",":Short",":String",":Int",":Int",":Short",":Double","Shr","Shl","."] ' <--- String we want to Replace OLD TEXT with
       
        'Dump array dimensions
    For Local i:Int=EachIn TryChar.Dimensions()
          Commands=i
    Next
    'Print Commands ; WaitKey ' Helper to see Eachin
       
        Global SpecialCharacter0:Int = Asc(";") ' <--- If Special character after Text will replace!
        Global SpecialCharacter1:Int = Asc("(")
        Global SpecialCharacter2:Int = Asc(")")
        Global SpecialCharacter3:Int = Asc(",")
       
        Global TryCharNum:Int=0
        Global wordlength:Int=0
               
        Replacetext=TryChar[TryCharNum]
      NewText=ReplaceWith[TryCharNum]
       
        Global ReplaceTextLength:Int=ReplaceText.Length
               
        Global FirstRUN:Int=0
        Global RunMODE:Int=0
        Print
       
        ' A newline to start of file
        Original = Chr(10) + Original
                           
        Start()
       
        Run()
       
        End
       
        '''''''''''''
        ' Functions '
        '           '
        '''''''''''''
        Function Start()  
             
           Cls
       
              DrawVariables()
       
           Flip(0)
             
              If FirstRUN=0 Then
                      
                       FirstRUN:+1
                       'Print "ReplaceTextLength="+ReplaceTextLength
                 Else
                       'Print "Running Again!"
                       'Print "ReplaceTextLength="+ReplaceTextLength
                       ''Print "TryCharNum="+TryCharNum
       
                        RunMODE:+1
       
                        If TryCharNum<Commands-1 Then TryCharNum:+1
                        If RunMODE=Commands Then Terminate()
                      
              End If
       
             
              Replacetext=TryChar[TryCharNum]
            Print ; Print "Replacetext="+Replacetext
            ReplaceTextLength=ReplaceText.Length
            Print "ReplaceTextLength="+ReplaceTextLength
           
   
            NewText=ReplaceWith[TryCharNum]
            Print "Newtext="+Newtext
            'SpecialCharacter=Asc(SpecialChar[TryCharNum])
   
           
    
           
   
           
            'StartOFline=1
            wordlength=0
            FoundTextPos=0
            'ReplaceTextLength=ReplaceText.Length
              'word=""
              'Original=Original
             
              'Print ; Print "About to Run!" ; Print
        Rem
              Select RunMODE
                
                
                 ' Note, Within the Case statements we may need to change Specialcharacters to fore changes in special circumstances
       
                 Case 0
                          Print "Converting PureBasic '"+ReplaceText+"' to BlitzMax '"+NewText+"'" ; Print
       
                 Case 1,2,3,4,5,6,7,8,9
       
                          Print "Converting PureBasic '"+ReplaceText+"' to BlitzMax '"+NewText+"'" ; Print
                
                 Default
       
                       Print "CASE "+RunMODE+" isnt Implemented yet!" ; Print '  Press any KEY!" ; WaitKey
       
              End Select
              Print "RunMODE="+RunMODE
       
        End Rem
              Run()
             
        End Function

    Function Run()

        ''Print "Im in run"
       
        ''Print "Special Character = "+SpecialCharacter
   
    Repeat
              ''Print
              ''Print "Calculating Text Length!"
                TextLength = Original.Length
                            ''Print
                ''Print "whole Text length = " + TextLength
                         ''Print
             
                            ' find the next position of our keyword and what comes next
                          FindText()
                          ' If your Back here you found Something
                            WhatsNext()
                           
                            'Print "Case "+RunMODE
                           
                            'Select RunMODE
                             
                               Rem
                                 Case 0,9 ' Convert Constants
                                   
                                    Print "Case "+RunMODE+" is Happening!"
                                    'Print "Converting PureBasic Constants to BlitzMaxNG" ; Print
                                               
                                    If ( TextNext = Asc(Left(ReplaceText,1) ) ) Then
                           
                                          ''Print ""
                                          ''Print "Found Character at "+FoundText+" ["+word+"]"
                                          ''Print ""
                                                                       
                                                  wordlength=FoundText+ReplaceTextLength
                                                  AlterText()
                                                  FoundSomething=1
                                                                                             
                                         Else
                                                 
                                                  GetText()
                                           
                                                   ''Print ""
                                                  ''Print "Found at Pos "+FoundText+" ["+word+"]"
                                                  ''Print ""
                                                  ''Print "SO IGNORE!"
                                                  ''Print ""
                                   
                                     End If
                            End Rem
                                 
                                 ' Case 0,9
                           
                                  '   Print "Case "+RunMODE+" is Happening!"
                           
                            'Rem
                                  'Case 0'1,2,3,4,5,6,7,8,10 ' Convert Variable's int, short, Double etc...
                           
                                     'Print "Case "+RunMODE+" is Happening!";WaitKey
                           
                                     'Print "TextNext is "+Chr(TextNext)
                                     'Print "Found is"+FoundText
                           
                                   'If ( TextNext = Asc(Right(ReplaceText,1) ) ) Then Print "It does Match"
                           
                                     'If ( TextNext =  32 ) Or ( TextNext =  13 ) Or ( TextNext = SpecialCharacter0 ) Or ( TextNext = SpecialCharacter1 ) Or ( TextNext = SpecialCharacter2 ) Or ( TextNext = SpecialCharacter3 ) Or ( TextNext = Asc(Right(ReplaceText,1) ) ) Then
                                            
                                         'Print "IM not getting in to Convert Variables"
                                              If FoundTextPos<>-1
                                               wordlength=FoundTextPos+ReplaceTextLength
                                               Print "wordLength="+wordLength
                                               AlterText()
                                               FoundSomething=1
                                               'WaitKey()
                                       Else
                                               GetText()
                                           
                                               'Print ""
                                               'Print "Found at Pos "+FoundText+" ["+word+"]"
                                               'Print ""
                                               'Print "SO IGNORE!"
                                               'Print ""
                                    End If
                            'End Rem
                   
                         'End Select
                           
        Forever
 
    End Function
       
       
        Function AlterText()                   
                           
                            GetText()
                            'Rem
                                    Print ""
                                    Print "Found at Pos "+FoundTextPos+"  word="+word
                                    Print
                                    Print "Length of Program line is "+(Wordlength-FoundTextPos)+" characters Long Until Newline!"
                                    Print '; WaitKey
                            'End Rem
                           
                            word=Right(word,Wordlength-FoundTextPos-ReplaceTextLength)
                            Print "word="+word
                            Local LengthOFword:Int=word.length
                            Print "word Length="+LengthOFword
                           
                          'Print "Replace Text is"+ReplaceText   
                            'WaitKey
                           
                'Select ReplaceText
               
               
                 '   Case "#",";","\" ' To "Const"   
                            
                             Rem
                             If ReplaceText = ";" Then
                                
                                If TextNext = 10 Then Print "TextNext="+TextNext Else Print "TextNext="+Chr(TextNext)
                                If TextBefore = 10 Then Print "TextBefore="+TextBefore Else Print "TextBefore="+Chr(TextBefore)
                                If TextAfter = 10 Then Print "TextAfter="+TextAfter Else Print "TextAfter="+Chr(TextAfter)
                                'WaitKey
                   
                             End If
                             End Rem
                   
                    '         Select True
                   
                     '           Case (TextBefore = Asc(" ") And TextNext = SpecialCharacter0 And TextAfter = Asc(" "))
                   
                      '              word=Newtext+word
                   
                       '         Case (StartOFline And TextNext = Asc(ReplaceText))
                    '
                     '               word=Newtext+word ; StartOFline=0
                    '
                     '           Case (TextBefore = Asc("(") And TextNext=Asc(ReplaceText))
                    '
                     '               word=word
                    '
                     '           Case (TextBefore = Asc(" ") And StartOFline=0)
                      '        
                       '             word=word
                    '
                     '           Case (TextBefore = Asc("[") And TextNext=Asc(ReplaceText))
                    '
                     '               word=word
                    '
                     '     
                    '
                     '           Default
                      '        
                       '             word=Newtext+word
                        '     
                         '    End Select           
                             
                    Rem
                           Case ".a",".w",".s",".l",".i",".u",".d",">>","<<" ' To :Byte 
                   
                                                
                              Select True
                   
                                 Case TextNext = Asc(Right(ReplaceText,1) ) And TextBefore <> Asc(" ") ' ie Space before can never happen or ERROR!
                   
                                     word=Newtext+word
                       
                              Default
                                       Print "No CONVERSION!" ; End
                                     
                              End Select
                       
                       
                       
                           Case "EnableExplicit"
                       
                                  'Select True
                   
                                 'Case TextNext = Asc(Right(ReplaceText,1) ) And TextBefore <> Asc(" ") ' ie Space before can never happen or ERROR!
                   
                                     word=Newtext+word
                   
                                'End Select
                       
                       
                        
                        Default
                          
                               Print "No more Cases to RUN!" '; RunMODE=Commands
                       
                 End Select   
               
                            Rem
                                    Print "New Program Line is ["+word+"]"
                                    Print ""
                            End Rem
                           
                            Original=Original[0..FoundTextPos] + NewText + Original[FoundTextPos+LengthOFword+ReplaceTextLength..TextLength]
                           
                            word=""
                           
                            'Print Original ; WaitKey
                            StartOFline=1
                            'Terminate()
                           
        End Function
       
       
        Function DrawVariables()
       
            DrawText("    File Opened = "+Chr(34)+Path+FIle+Chr(34),14,500)
            DrawText("Original Length = "+OriginalLength+" Characters",14,528)
            DrawText("Modified Length = "+Original.Length+" Characters",14,542)
       
       
        End Function
       
       
        Function WhatsNext()
                       
                        ' Chr(34) forces " to be printed
                        If SpecialCharacter0="" Then
                            
                             TextNext = Original [ FoundTextPos + ReplaceText.Length ]
                         
                          Else
                       
                             TextNext = Original [ FoundTextPos + ReplaceText.Length-1 ]
                       
                        End If
                       
                        TextBefore = Original [ FoundTextPos + ReplaceText.Length-2 ]
                        TextAfter = Original [ FoundTextPos + ReplaceText.Length ]
                       
                       
                        ''If TextNext <> 13 Then
                         
                                ''Print "Whats next = " + TextNext + " ... Which is " + Chr(34) + Chr ( TextNext ) + Chr(34)
                                ''Print "Text Before = " + TextBefore + " ... Which was " + Chr(34) + Chr ( TextBefore ) + Chr(34)
                               
                            ''Else
                               
                                ''Print "Whats next = " + TextNext + " ... Which is " + Chr(34) + "CR" + Chr(34)
                           
                        ''End If
                       
                           
        End Function
       
       
        Function GetText()
       
                        ' Now copy string Upto newline or Carrige Return CR
                           
                            For Local line:Int=FoundTextPos To wordlength-1
                                    word:+Chr(Original[Line])       
                            Next
                            Print "word="+word '; WaitKey
                                                                           
        End Function
       
       
        Function FindText()
                           
                        ' ReplaceText is our string to find. When its found we get the index to it!
                        ' If the String was at the beginning it would be 0
                           
                        FoundTextPos = Original.Find( ReplaceText , FoundTextPos + StartOFline )
                       
                        Print "Found Text Position="+FoundTextPos
                        'Print "RunMODE="+RunMODE
                        'Print "Commands="+Commands
                       
            If FoundTextPos=-1 Then Terminate()

                  
        End Function
       
       
        Function Terminate()
                   
                    ' If Print OUT is OKAY! then uncomment SaveText BELOW!
                   
                    'Print Original
                   
                    'Print "Found Text"+FoundText
                        'Print "RunMODE="+RunMODE
                        'Print "Commands="+Commands
                        'If RunMODE=Commands Then Print "It matches" ; WaitKey
   
                    ' //////////////////////////////////////////////////////
                    ' WARNING THESES LINES WILL OVERWRITE ORIGINAL FILE!        SAVE A COPY!
                    'Rem     
                                            If FoundSomething=1 And RunMODE=>Commands Then
                                                 
                           Print "Finished!" ; Print
                           Print "Would you like To see File before Saving? (Y/N)" ; Print ; WaitKey

                           If KeyDown(Key_Y) Then Print Original
                                           
                                                        Print
                                                        Print "PRESS ENTER to SaveText!"
                                                        WaitKey
                                                        If KeyHit(key_ENTER) Then
                                                              ''''''''''''           SaveText Original, Path + File
                                                              Print ""
                                                              Print "Text Saved!"
                                                        End If
                                                       
                                                        End
                                               
                                                Else If FoundSomething=0 Then
                                               
                                                        Print
                                                        Print "NOTHING TO ALTER!"
                                                       
                                                        End                                                                   
                                            End If
                    'End Rem
                    ' WARNING THESE LINES WILL OVERWRITE ORIGINAL FILE!         SAVE A COPY!
                    ' //////////////////////////////////////////////////////

                    'End ' This Will STOP Program!
                   
                  Start()
                   
        End Function
    

Any ideas to alter or modify etc.. Very gladly welcome!

Welcome Baggey



Running a PC that just Aint fast enough!? i7 Quad core 16GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!

lucidapogee

Quote from: dawlane on April 02, 2023, 11:54:19If you really want to know how a compiler or interpreter works. I would suggest getting hold of a couple of good books, such as the Dragon Compiler book, Loaders and Linkers and The Compiler Design Hand book. But for the time being, have a look here and search around on compilers, lexers, code parsers and abstract sysntax trees.

If you want to see an example, then look at the Cerberus-X sources. BlitzMax NG would be a bit over complicated with how it works.
I have that book and unless you have a degree in computer science, that book will be like reading alien language.

If you're going to tackle a book like that, also check out "Compiler Construction" by Niklaus Wirth (the designer of Pascal). I have a copy if you need any page scans.

Quote from: Baggey on April 02, 2023, 15:48:20So, Here's were im at so far today! When you've compiled. Ignore the main window! Slide it out the way! Its something else im thinking of implementing.
Put your .pbi File in the same directory as your .bmx. Alter the file name in the Code to the File you want to open/Translate! or Parse

Im thinking the two Arrays for Alter and new text Could be changed to compensate .bb Commands or even my old friend "PlayBasic"

RUN

And BOOM Converted! Loads to add though Does Group 1 and group 2 sort off! Thinking of converting HEX to Decimal and vis versa!

This is runnable CODE! in BlitMaxNG 8)
'
        ' PureBasic To BlitzMax Conversion Tool
        '
        ' Written by Lee Chatt
        '
        ' This will save you a S**T Load of typing!
        '
        ' It's not Fool Proof and may not be Fully Runnable!
        '
        ' It will get you almost there! But YU get it!
        '
        ' Summary :- Was written to convert Sid Code and anything else Intersting
    '
    ' Usage :- Use this as you will! But if you convert anything interesting you PROMISE to post on the FORUM!
    '
    ' Kind Regards and Enjoy!
    '
    SuperStrict

    AppTitle = "PureBasic To BlitzMax Converter"

    Graphics 800,600
    
    Global File:String = "xxxxxxx.pbi" ' FILE to me Modified HERE!
    Global Path:String = "" ' Leave BLANK if file is in Same Directory!
   
        'load the original file:-
    If FileSize(Path+File)=-1 Then

            Notify "File ~r" + Chr(34) + Path+File  + Chr(34) + "~rnot found!"
            End

    EndIf
 
    Global Original:String  = LoadText(Path+File)
    Global OriginalLength:Int = Original.Length
        Global ReplaceText:String
        Global NewText:String
       
        Global Word:String
    Global FoundTextPos:Int
        Global TextLength:Int
        Global TextNext:Int
        Global TextBefore:Int
        Global TextAfter:Int
        Global FoundSomething:Byte=0
        Global StartOFline:Int=0
       
        Global Commands:Int=0
        Global TryChar:String[]=["XIncludeFile","Static","CompilerEndIf","CompilerIf","DisableExplicit","ProcedureReturn","Protected","EndIf",";",":","EndMacro","Macro","EndProcedure","Procedure","EnableExplicit","EndStructure","Structure","#",".a",".d",".w",".s",".l",".i",".u",".d",">>","<<","\"] ' <--- Text we want to alter
        Global ReplaceWith:String[]=["Include","Local","End If","If","","Return","Local","End If","'",";","End Function","Function","End Function","Function","SuperStrict","End Type","Type","Const ",":Byte",":Double",":Short",":String",":Int",":Int",":Short",":Double","Shr","Shl","."] ' <--- String we want to Replace OLD TEXT with
       
        'Dump array dimensions
    For Local i:Int=EachIn TryChar.Dimensions()
          Commands=i
    Next
    'Print Commands ; WaitKey ' Helper to see Eachin
       
        Global SpecialCharacter0:Int = Asc(";") ' <--- If Special character after Text will replace!
        Global SpecialCharacter1:Int = Asc("(")
        Global SpecialCharacter2:Int = Asc(")")
        Global SpecialCharacter3:Int = Asc(",")
       
        Global TryCharNum:Int=0
        Global wordlength:Int=0
               
        Replacetext=TryChar[TryCharNum]
      NewText=ReplaceWith[TryCharNum]
       
        Global ReplaceTextLength:Int=ReplaceText.Length
               
        Global FirstRUN:Int=0
        Global RunMODE:Int=0
        Print
       
        ' A newline to start of file
        Original = Chr(10) + Original
                           
        Start()
       
        Run()
       
        End
       
        '''''''''''''
        ' Functions '
        '           '
        '''''''''''''
        Function Start()  
             
           Cls
       
              DrawVariables()
       
           Flip(0)
             
              If FirstRUN=0 Then
                      
                       FirstRUN:+1
                       'Print "ReplaceTextLength="+ReplaceTextLength
                 Else
                       'Print "Running Again!"
                       'Print "ReplaceTextLength="+ReplaceTextLength
                       ''Print "TryCharNum="+TryCharNum
       
                        RunMODE:+1
       
                        If TryCharNum<Commands-1 Then TryCharNum:+1
                        If RunMODE=Commands Then Terminate()
                      
              End If
       
             
              Replacetext=TryChar[TryCharNum]
            Print ; Print "Replacetext="+Replacetext
            ReplaceTextLength=ReplaceText.Length
            Print "ReplaceTextLength="+ReplaceTextLength
           
   
            NewText=ReplaceWith[TryCharNum]
            Print "Newtext="+Newtext
            'SpecialCharacter=Asc(SpecialChar[TryCharNum])
   
           
    
           
   
           
            'StartOFline=1
            wordlength=0
            FoundTextPos=0
            'ReplaceTextLength=ReplaceText.Length
              'word=""
              'Original=Original
             
              'Print ; Print "About to Run!" ; Print
        Rem
              Select RunMODE
                
                
                 ' Note, Within the Case statements we may need to change Specialcharacters to fore changes in special circumstances
       
                 Case 0
                          Print "Converting PureBasic '"+ReplaceText+"' to BlitzMax '"+NewText+"'" ; Print
       
                 Case 1,2,3,4,5,6,7,8,9
       
                          Print "Converting PureBasic '"+ReplaceText+"' to BlitzMax '"+NewText+"'" ; Print
                
                 Default
       
                       Print "CASE "+RunMODE+" isnt Implemented yet!" ; Print '  Press any KEY!" ; WaitKey
       
              End Select
              Print "RunMODE="+RunMODE
       
        End Rem
              Run()
             
        End Function

    Function Run()

        ''Print "Im in run"
       
        ''Print "Special Character = "+SpecialCharacter
   
    Repeat
              ''Print
              ''Print "Calculating Text Length!"
                TextLength = Original.Length
                            ''Print
                ''Print "whole Text length = " + TextLength
                         ''Print
             
                            ' find the next position of our keyword and what comes next
                          FindText()
                          ' If your Back here you found Something
                            WhatsNext()
                           
                            'Print "Case "+RunMODE
                           
                            'Select RunMODE
                             
                               Rem
                                 Case 0,9 ' Convert Constants
                                   
                                    Print "Case "+RunMODE+" is Happening!"
                                    'Print "Converting PureBasic Constants to BlitzMaxNG" ; Print
                                               
                                    If ( TextNext = Asc(Left(ReplaceText,1) ) ) Then
                           
                                          ''Print ""
                                          ''Print "Found Character at "+FoundText+" ["+word+"]"
                                          ''Print ""
                                                                       
                                                  wordlength=FoundText+ReplaceTextLength
                                                  AlterText()
                                                  FoundSomething=1
                                                                                             
                                         Else
                                                 
                                                  GetText()
                                           
                                                   ''Print ""
                                                  ''Print "Found at Pos "+FoundText+" ["+word+"]"
                                                  ''Print ""
                                                  ''Print "SO IGNORE!"
                                                  ''Print ""
                                   
                                     End If
                            End Rem
                                 
                                 ' Case 0,9
                           
                                  '   Print "Case "+RunMODE+" is Happening!"
                           
                            'Rem
                                  'Case 0'1,2,3,4,5,6,7,8,10 ' Convert Variable's int, short, Double etc...
                           
                                     'Print "Case "+RunMODE+" is Happening!";WaitKey
                           
                                     'Print "TextNext is "+Chr(TextNext)
                                     'Print "Found is"+FoundText
                           
                                   'If ( TextNext = Asc(Right(ReplaceText,1) ) ) Then Print "It does Match"
                           
                                     'If ( TextNext =  32 ) Or ( TextNext =  13 ) Or ( TextNext = SpecialCharacter0 ) Or ( TextNext = SpecialCharacter1 ) Or ( TextNext = SpecialCharacter2 ) Or ( TextNext = SpecialCharacter3 ) Or ( TextNext = Asc(Right(ReplaceText,1) ) ) Then
                                            
                                         'Print "IM not getting in to Convert Variables"
                                              If FoundTextPos<>-1
                                               wordlength=FoundTextPos+ReplaceTextLength
                                               Print "wordLength="+wordLength
                                               AlterText()
                                               FoundSomething=1
                                               'WaitKey()
                                       Else
                                               GetText()
                                           
                                               'Print ""
                                               'Print "Found at Pos "+FoundText+" ["+word+"]"
                                               'Print ""
                                               'Print "SO IGNORE!"
                                               'Print ""
                                    End If
                            'End Rem
                   
                         'End Select
                           
        Forever
 
    End Function
       
       
        Function AlterText()                   
                           
                            GetText()
                            'Rem
                                    Print ""
                                    Print "Found at Pos "+FoundTextPos+"  word="+word
                                    Print
                                    Print "Length of Program line is "+(Wordlength-FoundTextPos)+" characters Long Until Newline!"
                                    Print '; WaitKey
                            'End Rem
                           
                            word=Right(word,Wordlength-FoundTextPos-ReplaceTextLength)
                            Print "word="+word
                            Local LengthOFword:Int=word.length
                            Print "word Length="+LengthOFword
                           
                          'Print "Replace Text is"+ReplaceText   
                            'WaitKey
                           
                'Select ReplaceText
               
               
                 '   Case "#",";","\" ' To "Const"   
                            
                             Rem
                             If ReplaceText = ";" Then
                                
                                If TextNext = 10 Then Print "TextNext="+TextNext Else Print "TextNext="+Chr(TextNext)
                                If TextBefore = 10 Then Print "TextBefore="+TextBefore Else Print "TextBefore="+Chr(TextBefore)
                                If TextAfter = 10 Then Print "TextAfter="+TextAfter Else Print "TextAfter="+Chr(TextAfter)
                                'WaitKey
                   
                             End If
                             End Rem
                   
                    '         Select True
                   
                     '           Case (TextBefore = Asc(" ") And TextNext = SpecialCharacter0 And TextAfter = Asc(" "))
                   
                      '              word=Newtext+word
                   
                       '         Case (StartOFline And TextNext = Asc(ReplaceText))
                    '
                     '               word=Newtext+word ; StartOFline=0
                    '
                     '           Case (TextBefore = Asc("(") And TextNext=Asc(ReplaceText))
                    '
                     '               word=word
                    '
                     '           Case (TextBefore = Asc(" ") And StartOFline=0)
                      '        
                       '             word=word
                    '
                     '           Case (TextBefore = Asc("[") And TextNext=Asc(ReplaceText))
                    '
                     '               word=word
                    '
                     '     
                    '
                     '           Default
                      '        
                       '             word=Newtext+word
                        '     
                         '    End Select           
                             
                    Rem
                           Case ".a",".w",".s",".l",".i",".u",".d",">>","<<" ' To :Byte 
                   
                                                
                              Select True
                   
                                 Case TextNext = Asc(Right(ReplaceText,1) ) And TextBefore <> Asc(" ") ' ie Space before can never happen or ERROR!
                   
                                     word=Newtext+word
                       
                              Default
                                       Print "No CONVERSION!" ; End
                                     
                              End Select
                       
                       
                       
                           Case "EnableExplicit"
                       
                                  'Select True
                   
                                 'Case TextNext = Asc(Right(ReplaceText,1) ) And TextBefore <> Asc(" ") ' ie Space before can never happen or ERROR!
                   
                                     word=Newtext+word
                   
                                'End Select
                       
                       
                        
                        Default
                          
                               Print "No more Cases to RUN!" '; RunMODE=Commands
                       
                 End Select   
               
                            Rem
                                    Print "New Program Line is ["+word+"]"
                                    Print ""
                            End Rem
                           
                            Original=Original[0..FoundTextPos] + NewText + Original[FoundTextPos+LengthOFword+ReplaceTextLength..TextLength]
                           
                            word=""
                           
                            'Print Original ; WaitKey
                            StartOFline=1
                            'Terminate()
                           
        End Function
       
       
        Function DrawVariables()
       
            DrawText("    File Opened = "+Chr(34)+Path+FIle+Chr(34),14,500)
            DrawText("Original Length = "+OriginalLength+" Characters",14,528)
            DrawText("Modified Length = "+Original.Length+" Characters",14,542)
       
       
        End Function
       
       
        Function WhatsNext()
                       
                        ' Chr(34) forces " to be printed
                        If SpecialCharacter0="" Then
                            
                             TextNext = Original [ FoundTextPos + ReplaceText.Length ]
                         
                          Else
                       
                             TextNext = Original [ FoundTextPos + ReplaceText.Length-1 ]
                       
                        End If
                       
                        TextBefore = Original [ FoundTextPos + ReplaceText.Length-2 ]
                        TextAfter = Original [ FoundTextPos + ReplaceText.Length ]
                       
                       
                        ''If TextNext <> 13 Then
                         
                                ''Print "Whats next = " + TextNext + " ... Which is " + Chr(34) + Chr ( TextNext ) + Chr(34)
                                ''Print "Text Before = " + TextBefore + " ... Which was " + Chr(34) + Chr ( TextBefore ) + Chr(34)
                               
                            ''Else
                               
                                ''Print "Whats next = " + TextNext + " ... Which is " + Chr(34) + "CR" + Chr(34)
                           
                        ''End If
                       
                           
        End Function
       
       
        Function GetText()
       
                        ' Now copy string Upto newline or Carrige Return CR
                           
                            For Local line:Int=FoundTextPos To wordlength-1
                                    word:+Chr(Original[Line])       
                            Next
                            Print "word="+word '; WaitKey
                                                                           
        End Function
       
       
        Function FindText()
                           
                        ' ReplaceText is our string to find. When its found we get the index to it!
                        ' If the String was at the beginning it would be 0
                           
                        FoundTextPos = Original.Find( ReplaceText , FoundTextPos + StartOFline )
                       
                        Print "Found Text Position="+FoundTextPos
                        'Print "RunMODE="+RunMODE
                        'Print "Commands="+Commands
                       
            If FoundTextPos=-1 Then Terminate()

                  
        End Function
       
       
        Function Terminate()
                   
                    ' If Print OUT is OKAY! then uncomment SaveText BELOW!
                   
                    'Print Original
                   
                    'Print "Found Text"+FoundText
                        'Print "RunMODE="+RunMODE
                        'Print "Commands="+Commands
                        'If RunMODE=Commands Then Print "It matches" ; WaitKey
   
                    ' //////////////////////////////////////////////////////
                    ' WARNING THESES LINES WILL OVERWRITE ORIGINAL FILE!        SAVE A COPY!
                    'Rem     
                                            If FoundSomething=1 And RunMODE=>Commands Then
                                                 
                           Print "Finished!" ; Print
                           Print "Would you like To see File before Saving? (Y/N)" ; Print ; WaitKey

                           If KeyDown(Key_Y) Then Print Original
                                           
                                                        Print
                                                        Print "PRESS ENTER to SaveText!"
                                                        WaitKey
                                                        If KeyHit(key_ENTER) Then
                                                              ''''''''''''           SaveText Original, Path + File
                                                              Print ""
                                                              Print "Text Saved!"
                                                        End If
                                                       
                                                        End
                                               
                                                Else If FoundSomething=0 Then
                                               
                                                        Print
                                                        Print "NOTHING TO ALTER!"
                                                       
                                                        End                                                                   
                                            End If
                    'End Rem
                    ' WARNING THESE LINES WILL OVERWRITE ORIGINAL FILE!         SAVE A COPY!
                    ' //////////////////////////////////////////////////////

                    'End ' This Will STOP Program!
                   
                  Start()
                   
        End Function
    

Any ideas to alter or modify etc.. Very gladly welcome!

Welcome Baggey




Wow, great start!

As you are showing yourself, it's not impossible, just tedious.
To have a translator convert code back and forth between two BASIC languages sounds interesting. To have all the major BASIC dialects convert to others and back sound wild and will get very complicated.

Some things to look into are recursive descent and reverse polish notation. These are methods of evaluating expressions. For my projects, I am using the RPN.

Another idea to play with: write a BASIC to asm compiler (still essentially a translator). You seem to be familiar with 6502 and z80 enough to pull that off. Start with PRINT, INPUT, IF, LET, GOTO. A cross platform 8 bit compiler would be AWESOME. This is a project that I would personally love to try.
Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

dawlane

Quote from: lucidapogee on April 02, 2023, 18:32:03I have that book and unless you have a degree in computer science, that book will be like reading alien language.

If you're going to tackle a book like that, also check out "Compiler Construction" by Niklaus Wirth (the designer of Pascal). I have a copy if you need any page scans.
A search on the net should throw up a few pdf file links for the Compiler Construction book. But it would be pot luck on which revision the book would be. And yeah. The Dragon book does tend to be a bit of a hard read. There are books out there that show an applied approach to writing compiler and interpreters, one by Ronald Mak is the only one I can think of off the top of my head uses C++ as the programming language to write a simple PASCAL compiler.

Baggey

#9
The only one I can think of off the top of my head uses C++ as the programming language to write a simple PASCAL compiler.

I love this. This as i remember was that Delphi wrote 'C' aka C++ :o

It's Early days yet but id love to see something high level to get going with. My program is working but it may be long winded. I seem to remember shanon fano translation for packets coming in! That was a very long time ago and the names might not even be right! ???

So far i learning just get something to work and then trim back and modify and perfect and make faster seems to be the way!

In another forum someone thought that 'inc r' or 'r:+1' was quicker than 'r=r+1' as it is shorter or neater!?

yeah thats right, i thought it would be too. But it's not! :-X  Something todo with Macros being inserted! for the life of me why these exit it's a function. We have giga bytes of memory Space. Quad core CPU's etc why are we trying to minimise code these day's "SPEED is ALWAYS KEY!"

Anyway always the Kindest Regards with my writeing's Baggey

Running a PC that just Aint fast enough!? i7 Quad core 16GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!

dawlane

Old tread resurrection time.
@Baggey 
If you are still thinking of creating a translation tool and don't mind using java. Then check out Antlr4, which should make writing a translator easier.