Building a Language in PB

Everything else that doesn't fall into one of the other PB categories.
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Building a Language in PB

Post by User Mike »

I've been reading for the last two weeks on building compilers and script systems. Lately(about two weeks ago) I stumbled upon PureBasic. It seems to be a very powerful language, and something I'd be able to get a healthy grasp of within a few months. I've been messing around with PB for a little(making a chat application/and small game), and am on the verge of purchasing.

Now I 1 small question that requires big thought.

"How should one go about building another programming language, withing Purebasic?"

First off, I would in no means boost or compete with PB. This is a project I"m taken upon for a learning experience, and build on in the future.

I've got a book I'm currently reading on Compilers,Principles, and Techniques that's very good. I just need help breaking down what tools/concepts I need to build a language in PB.

So, to break down building a compiler/language I would need...

- To build a GUI
- A lexical analyzer
- Something to break the scripts into machine code

So basically, I'm looking for reassurance on building another programming language in purebasic complete with a GUI and compiler.

Just post any advice,hints,and past experiences that you may have had. (Don't be shy to post links either :wink: )
(Don't be shy to lead me to C++, lex, and other "non PB" resources if it'll help me on my journey)

Thanks for your support.

8)
-Pure Basic User and loving it!-
cykotic
User
User
Posts: 24
Joined: Wed Nov 12, 2003 6:24 am
Location: Canada
Contact:

Post by cykotic »

someone has written a programming (scripting) language in Purebasic called D-LIB. You can check it out at http://www.thinkrelative.de/dlib/index.htm
maybe you can contact the author to get some tips..

There are many methods of approaching writing a compiler.. I've had people tell me to port yacc/lex to purebasic as it would be an easy feat.. (maybe for them, not for me) and use that.

Have you tried reading Jack Crenshaw's tutorial? it uses pascal but it's pretty easy to port over to PB. the link is http://compilers.iecc.com/crenshaw/

here is a nice resource site on compiler tools (lexers, scanners..)
http://www.thefreecountry.com/programmi ... tion.shtml

google is also your friend.

you can also try the BCX approach.. writing all the parsing and file scanning in your language of choice (which is PB i'm assuming).. and then generate C/C++ code that could be compiled with many of the free compilers out there.. like gcc.. mingw.. PellesC is nice which is also based of the free Lcc Win32 compiler..

I've only stepped into writing compilers just a bit.. so I am probably not the right person to give advice to but I'm trying to help ;)

I would use the 2pass method where you would scan the source file in 1 pass.. then generate the C/C++/ASM code in the second pass ( you could also call a 3rd to compile the generated source)

Hope some of this has helped :)
cry havoc and let slip the dogs of war...

Image
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

there's a nice compiler construction tutorial by Crenshaw, somebody translated his tutorial to QuickBasic HERE
or his original tutorial in Pascal HERE , also there's a small basic writen in PowerBasic HERE :)

[edit] cykotic beat me to it. [/edit]
cykotic
User
User
Posts: 24
Joined: Wed Nov 12, 2003 6:24 am
Location: Canada
Contact:

Post by cykotic »

http://www.peroxide.dk/tuts_scr.shtml

another good tutorial.. i think it focuses more on scripting but it would work the same way..
cry havoc and let slip the dogs of war...

Image
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Thanks for those links.

Since I previously(And still) used DarkBasic Professional, I had started a discussion on the forum over there. Might be very helpful to others, and get us thinking.

http://darkbasicpro.thegamecreators.com ... =24693&b=2

8)
-Pure Basic User and loving it!-
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

i forgot, here's free downloadable book on compiler construction :)
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Jack, thank you very much. This is exactly the resource I was looking for.

Kudos.

:D
-Pure Basic User and loving it!-
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

jack wrote:there's a nice compiler construction tutorial by Crenshaw, somebody translated his tutorial to QuickBasic HERE
or his original tutorial in Pascal HERE , also there's a small basic writen in PowerBasic HERE :)

[edit] cykotic beat me to it. [/edit]
have anybody converted this tutorial over to PB as of yet?
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

If you really want to learn something new why not try implementing LISP.

The following online book is studied by MIT students in the first year:

http://mitpress.mit.edu/sicp/full-text/book/book.html

It talks you through creating your own version of LISP. The benefits of this are that you learn how to look at programming in a whole new way.

Also, s-expressions are very, very easy to write the parser for.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Just for kiding ...

The smallest language that ever was designed.

Here you have a source editor, and the possibility to make instant execution !

Code: Select all

Global CurrentDirectory.s
CurrentDirectory = Space(#MAX_PATH)

Enumeration
  #Window_Main
  #Gadget_Image
  #Gadget_Image1
  #Gadget_Image2
  #Gadget_Editor
  #Menu_Item_New
  #Menu_Item_Load
  #Menu_Item_Run
  #Menu_Item_Save
  #Menu_Item_SaveAs
  #Menu_Item_Exit
  #PopupMenu
EndEnumeration

Procedure.s ReadSource(FileName.s)
  If ReadFile(0, FileName)
      *b = AllocateMemory(Lof())
      ReadData(*b, Lof())
      CloseFile(0)
      SetGadgetText(#Gadget_Editor, PeekS(*b))
      While WindowEvent() : Wend
      ProcedureReturn FileName
    Else
      ProcedureReturn ""
  EndIf
EndProcedure

Procedure.s SaveSource(FileName.s)
  If CreateFile(0, FileName)
      WriteStringN(GetGadgetText(#Gadget_Editor))
      CloseFile(0)
      ProcedureReturn FileName
    Else
      ProcedureReturn ""
  EndIf
EndProcedure

  WindowXSize.l = 640
  WindowYSize.l = 480
  GetCurrentDirectory_(255, @CurrentDirectory)
  CurrentDirectory + "\"
  FileName.s
  GetCursorPos_(CursorPosition.POINT)
  Quit.l = #FALSE
  If OpenWindow(#Window_Main, CursorPosition\x - WindowXSize / 2, CursorPosition\y - WindowYSize / 2, WindowXSize, WindowYSize - 20, #PB_Window_Invisible, "YABL")
      AddKeyboardShortcut(#Window_Main, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
      GetWindowRect_(WindowID(), MyRect.RECT)
      ImageID = CreateImage(0, WindowXSize, WindowYSize)
      StartDrawing(ImageOutput())
        For i = 0 To 31
          Box(i, i, WindowXSize - 2 * i, WindowYSize - 2 * i, RGB(0, 0, 255 - 8 * i)) ; , 255 - 8 * i, 255 - 16 * i))
        Next
        DrawingMode(4)
        Box(0, 0, WindowXSize, WindowYSize, $C0FFFF)
      StopDrawing()
      If CreateGadgetList(WindowID())
          SetGadgetFont(#PB_Default, LoadFont(0, "Verdana", 7, #PB_Font_HighQuality))
          ImageGadget(#Gadget_Image, 0, 0, WindowXSize, WindowYSize, ImageID)
          EditorGadget(#Gadget_Editor, 25, 25, WindowXSize - 50, WindowYSize - 50)
      EndIf
      If CreatePopupMenu(#PopupMenu)
          MenuItem(#Menu_Item_New, "New")
          MenuItem(#Menu_Item_Load, "Load")
          MenuItem(#Menu_Item_Run, "Run")
          MenuBar()
          MenuItem(#Menu_Item_Save, "Save")
          MenuItem(#Menu_Item_SaveAs, "SaveAs")
          MenuBar()
          MenuItem(#Menu_Item_Exit, "Exit")
      EndIf
      SetWindowLong_(WindowID(), #GWL_STYLE, #WS_DLGFRAME | #WS_CLIPCHILDREN | #WS_CLIPSIBLINGS)
      SetWindowPos_(WindowID(), 0, 0, 0, 0, 0, #SWP_NOSIZE | #SWP_NOMOVE | #SWP_FRAMECHANGED)
      While WindowEvent()
      Wend
      HideWindow(0,0)
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Quit = #TRUE
          Case #PB_Event_Menu
            Select EventMenuID()
              Case #PB_Shortcut_Escape
                Quit = #TRUE
              Case #Menu_Item_New
                SetGadgetText(#Gadget_Editor, "")
              Case #Menu_Item_Load
                ReadSource(OpenFileRequester("Select a source file to load", CurrentDirectory + "*.YABL", "YABL files|*.YABL|All files|*.*", 0))
              Case #Menu_Item_Run
                FileName.s = SaveSource(CurrentDirectory + Str(GetTickCount_()) + ".YABL")
                RunProgram("pbcompiler.exe", FileName + " /EXE " + FileName + ".exe", CurrentDirectory, 1 | 2)
                RunProgram(FileName + ".exe", "", CurrentDirectory)
              Case #Menu_Item_Save
                SaveSource(SaveFileRequester("Enter the file name to save", CurrentDirectory + "*.YABL", "YABL files|*.YABL|All files|*.*", 0))
              Case #Menu_Item_SaveAs
                SaveSource(SaveFileRequester("Enter the file name to save", CurrentDirectory + "*.YABL", "YABL files|*.YABL|All files|*.*", 0))
              Case #Menu_Item_Exit
                Debug "Exit selected"
                Quit = #TRUE
            EndSelect
          Case #PB_Event_Gadget
          Case #WM_LBUTTONDOWN
            ReleaseCapture_()
            SendMessage_(WindowID(), #WM_NCLBUTTONDOWN, #HTCAPTION, #NULL)
          Case #WM_RBUTTONDOWN
            Debug "R Button clicked"
            DisplayPopupMenu(#PopupMenu, WindowID())
        EndSelect
      Until Quit
  EndIf
  TerminateProcess_(GetCurrentProcess_(), 0)
End 
Focus in the editor gadget and enter whatever you want to do !

ie :

a = 12
b = 13
openconsole()
printn(str(a + b))
while inkey() = "" : wend
closeconsole()


Then right click to select "Run" tab in the popup menu. It should run (except if pbcompiler is not accessible from %PATH% variable of your environment variables ... in this case, just tune the program accordingly.

Well ... it was just a joke but, I guess it is a good possibility to enter any kind of code that you can parse and translate to PureBasic compatible code before running ...
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Thanks GedB, I'll definitly take a look into it.

8)
-Pure Basic User and loving it!-
johndehope3
New User
New User
Posts: 9
Joined: Tue Jan 13, 2004 7:00 am

Post by johndehope3 »

Wow! I have been doing exactly the same thing. My goal is to build an interpreter, not a compiler. The purpose is to give very extensive and context-sensitive error messages. For example, in a normal case-sensitive language you might write this code...

Code: Select all

int x;
X = 10;
An you'd get an error something like this...

Code: Select all

undeclared symbol X on line 2.
In my interpreter you'd get an error like this...

Code: Select all

On line 2 you used a variable named X. Your program hasn't yet created a variable with that name, so you cannot use the variable yet. You did create a variable named 'x' so maybe that is what you meant?
Of course this is meant to be used to teach programming to newbies at school.

I built a tokenizer and a parser with good success. But then I stopped because I can't figure out how to get my parsed tokens into a structure where I could execute them. Anybody who wants a copy of what I've got can email me at "john dehope 3 at hotmail dot com" no spaces of course.
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Hey sounds good.

I'm still working on building an interpreter in my spare time. It's quite fun but unfortuantly I haven't had much time so my project hasn't moved much.

I'll send you an e-mail for source as I'm interested in your works.

:)
-Pure Basic User and loving it!-
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Hey folks just wanted to do some updates.

I want to thank all the people who've posted here, as I've really learned a lot. I'm now in the process of planning out a simple language and some features. Recently, I've also been playing around with Traumatic's GlWrapper, and thought it might be a good idea to add some OpenGL functionality.

At this point, making this language(which will be interpreted) is my priority programming project. I hope to have a strong core commands, and some 2D commands. (I also want to throw in spinning OpenGL cubes for kicks ;))

Thanks to all who've helped. I'll post some screenshots as I advance.
-Pure Basic User and loving it!-
Post Reply