Results 1 to 8 of 8

Thread: Partial automation of code translation

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Partial automation of code translation

    ThinBASIC contains very powerful Tokenizer module, which helps a lot when you need to analyze text, especially source code of some programming language.

    Recently, I faced a situation where I needed to port code of other person prototyped using MatLAB to C, which was the only choice for ATMega CPU. Conversions from language to language can be very annoying procedure when doing manually, not speaking of possibility of doing lot of mistakes just because of inattention.
    With little help of the script, you can do this very fast, thanks to automation of some routine operations.

    What I attach is ThinBASIC script which helps me to translate MatLAB code to C, but it can be easily extended to support other languages, thanks to possibility of masked #include files.

    How to use?
    Just load source file and hit "Translate". Partially translated code will appear on the right, and you can finish the job now.
    The listbox on the bottom of the window shows some warnings, double clicking on the items will take you to problematic areas.

    I hope it might be useful and inspiring for you, the Tokenizer module is really worth investing some time!


    Petr

    13-05-2010: Works from resolution 640x480 up
    Attached Images Attached Images
    Attached Files Attached Files
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2

    Re: Partial automation of code translation

    Hi Petr
    i think you can convert your project to a commercial one on a larger scale if you developed the project to a larger one, since matlab used by technical people like electrical engineers.
    on the other hand there is a very expensive product "MathCode C++" which convert mathematica functions to c++ code http://www.mathcore.com/products/mat...athcodec++.php
    indeed it is the only product i have'nt seen a pirated copy on the torrent sites, and it is very expensive (more than $2000 ) ie more than mathematica itself.
    there is the problem of converting the graphics code, which i think can be directed to the free gnuplot instead of the original matlab ways.

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Partial automation of code translation

    Hi Zak,

    thank you very much for the idea, but the fact is my knowledge of MatLAB is very surfacial, not speaking of C!

    Friends on University use MatLAB for prototyping in cases, where I use ThinBASIC - elemental number crunching, simulations and visualisations.
    In the end the output is just few number handling functions, without too much language specific code.

    For anything more complex, I think the translator from/to language is not good idea, and in such a cases I am all for often recommended approach of understanding the "source" code, and rewriting it to "destination" source shape using all resources the "destination" language provides.

    Will see, soon I will leave school and will have to make living, maybe this could be one of the possibilities, but I guess better to go into areas I understand more in depth


    Thanks!,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    51
    Posts
    934
    Rep Power
    109

    Re: Partial automation of code translation

    hi petr, many thanks for this interesting code examples with tokenizer module!

    perhaps this basically idea could be also good for translation design form from c++ to thinbasic ?

    in general: what's

    %lbWarning (listbox)
    good for ? I see I haven't noticed because this listbox was out of my screen resolution size.

    my version has smaller screen solution / changed only _dialog_main.tbasic.

    [code=thinbasic]' Main dialog

    ' -- Dialog controls
    Begin ControlID
    %bLoadFrom
    %bSaveTo

    %tbFrom
    %tbTo

    %cbTranslators
    %bTranslate

    %lbWarning
    End ControlID

    Sub dialog_Main_Show() As DWord

    Local hDlg As DWord

    Dialog Font "Courier New", 9
    Dialog New 0, $g_TITLE,-1,-1, 580, 420, _ '630, 470, _
    %WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg


    ' -- Place controls here
    Control Add Button, hDlg, %bLoadFrom, "Load", 5, 5, 60, 14
    Control Add Textbox hDlg, %tbFrom, "", 5, 25, 270, 360, %ES_AUTOHSCROLL Or %ES_LEFT Or %WS_BORDER Or %WS_TABSTOP Or %ES_MULTILINE Or %ES_WANTRETURN Or %WS_VSCROLL, %WS_EX_CLIENTEDGE Or %WS_EX_LEFT

    Control Add Button, hDlg, %bSaveTo, "Save", 510, 5, 60, 14
    Control Add Textbox hDlg, %tbTo , "", 300, 25, 270, 360, %ES_AUTOHSCROLL Or %ES_LEFT Or %WS_BORDER Or %WS_TABSTOP Or %ES_MULTILINE Or %ES_WANTRETURN Or %WS_VSCROLL, %WS_EX_CLIENTEDGE Or %WS_EX_LEFT

    Control Add COMBOBOX hDlg, %cbTranslators, , 150, 5, 100, 200, %CBS_DROPDOWNLIST
    COMBOBOX Add hDlg, %cbTranslators, Translador_GetTranslators() Using ","
    COMBOBOX Select hDlg, %cbTranslators, 1

    Control Add Button, hDlg, %bTranslate, "Translate", 255, 5, 60, 14

    'Control Add LISTBOX, hDlg, %lbWarning, , 5, 430, 630, 35, %LBS_NOINTEGRALHEIGHT Or %WS_BORDER Or %WS_VSCROLL
    'Control Add Button, hDlg, %bClose, "Click to close", 95, 100, 60, 14

    Dialog Show Modal hDlg, Call dialog_Main_Callback

    End Sub

    ' -- Callback for dialog
    CallBack Function dialog_Main_Callback()
    Static sFile As String
    Static sExtension As String
    Static sContent As String
    Static sContentNew As String
    Static sType As String
    Static position As Long

    ' -- Test for messages
    Select Case CBMSG

    Case %WM_INITDIALOG
    ' -- Put code to be executed after dialog creation here

    Case %WM_COMMAND
    ' -- You can handle controls here

    Select Case CBCTL

    Case %bLoadFrom
    If CBCTLMSG = %BN_CLICKED Then
    sExtension = Translador_GetFromExtensions()
    If InStr(sExtension, ",") Then
    sExtension = Grab$(sExtension, "|", ",")
    Else
    sExtension = Parse$(sExtension, "|", 2)
    End If
    sFile = Dialog_OpenFile(CBHNDL, "Open file", APP_SourcePath, Translador_GetFromExtensions(), sExtension, %OFN_FILEMUSTEXIST Or %OFN_HIDEREADONLY Or %OFN_ENABLESIZING)

    If Len(sFile) Then
    Control Set Text CBHNDL, %tbFrom, FILE_Load(sFile)
    End If

    End If

    Case %bSaveTo
    If CBCTLMSG = %BN_CLICKED Then
    sExtension = Translador_GetToExtensions()
    If InStr(sExtension, ",") Then
    sExtension = Grab$(sExtension, "|", ",")
    Else
    sExtension = Parse$(sExtension, "|", 2)
    End If
    sFile = Dialog_SaveFile(CBHNDL, "Save file", APP_SourcePath, Translador_GetToExtensions(), sExtension, %OFN_HIDEREADONLY Or %OFN_ENABLESIZING)

    If Len(sFile) Then
    Control Get Text CBHNDL, %tbTo To sContent
    FILE_Save(sFile, sContent)
    End If

    End If

    Case %bTranslate
    If CBCTLMSG = %BN_CLICKED Then
    LISTBOX Reset CBHNDL, %lbWarning

    COMBOBOX Get Text CBHNDL, %cbTranslators To sType
    Control Get Text CBHNDL, %tbFrom To sContent

    Call "Translador_"+sType+"_Translate" (sContent) To sContentNew
    Control Set Text CBHNDL, %tbTo, sContentNew

    LISTBOX Add CBHNDL, %lbWarning, Trim$(Translador_AddWarning("", 0, 0), $CRLF) Using $CRLF ' -- The function also returns
    End If

    Case %lbWarning
    If CBCTLMSG = %LBN_DBLCLK Then
    LISTBOX Get Text CBHNDL, %lbWarning To sContent

    position = Val(Parse$(sContent, "@", -1))
    If InStr(sContent, "@OUT@") Then
    Control Set Focus CBHNDL, %tbTo
    Control Send CBHNDL, %tbTo, %EM_SETSEL, position, position
    Control Send CBHNDL, %tbTo, %EM_SCROLLCARET, 0,0
    Else
    End If

    End If


    End Select


    Case %WM_CLOSE
    ' -- Put code to be executed before dialog end here

    End Select

    End Function[/code]

    I can remember you have done some month ago some good translations for tom lebowski from simple c++ examples to thinbasic. I found this chapter very very useful. don't find the link at the moment.

    thanks, best regards, frank
    Attached Files Attached Files
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Partial automation of code translation

    Hi Frank,

    the listbox lists any problems the translator script finds during the translating process.
    You can then click on each warning and it will show you where in the code the problem or something to check is.
    Please have a look at the screenshot in the first post of this thread to get idea.

    I apologize for the 1280x1024 screen lock, I will fix it during summer, but now I have to write thesis or I will be ritually executed Fixed


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6

    Re: Partial automation of code translation

    Hi Petr,

    thanks for it. That will become usefull for me.

    Michael

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Partial automation of code translation

    Hi Michael,

    I hope it will serve you well, let me know.
    I just uploaded new version with splitter, which runs from 640x480 and up, it is fully resizable and allows maximization.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  8. #8
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    51
    Posts
    934
    Rep Power
    109

    Re: Partial automation of code translation

    thanks for fixing and update, now here it's looking very nice!

    personal question to another topic: why there isn't any new update for thinbasic ? perhaps it's useful for some users here at the board to know what's going on... a little letter from eros or other man he's involved with thinbasic development would be very grateful

    best regards, servus, frank
    you can't always get what you want, but if you try sometimes you might find, you get what you need

Similar Threads

  1. sample code for automation
    By sgsong in forum thinBasic General
    Replies: 3
    Last Post: 20-07-2006, 13:24

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •