Script Language

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

Script Language

Post by User Mike »

Hello PB'ers,

I'm really interested in writing a simple script engine. I just need a little help getting started. Write now, I basically read strings from a .txt file and then display them. While it's a start, it's nowhere near what I'd like. The part that confuses me is where you get your syntax rules down.

For ex.

For my script system, if I want something to be printed to screen, I want the user to be forced to type print "insert_text_here".

How am I suppose to get this to work? Would I have some kind of check in my loop that looks for the string "Print", and then moves onto the next line to see if it has print or whatever command?

I think I'm starting to get the hold of writing a script system, yet I need just a little more guidance.

Thanks for all your help,
Mike

8)
-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 »

Sorry I cant help, but I really really hope you make it cross-platformed :) Ive seen lots of scripting engines in PB but limited to windows.. erk.
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Definitly would like to make it cross-platform. That's why I've come to PureBasic, because it has cross-platform capibilities.

First things first though, I just need to figure out how to get the script system working.

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

Post by User Mike »

So far I've made some progress. :D

Finally got the print command working along with a way to ignore the commands in a script -> //.

So far it's very exciting for me to get something like this done. Next I'm going to move onto something more advanced. Would like to get some basic math operators working (+,-,*,/,(,),^).

After that comes variables, and then who knows what. (Special thanks to Balrogsoft, who has introduced me to a method of parsing variables.)

8)
-Pure Basic User and loving it!-
BalrogSoft
Enthusiast
Enthusiast
Posts: 203
Joined: Sat Apr 26, 2003 6:33 pm
Location: Spain
Contact:

Post by BalrogSoft »

Hi, i think that parse the code with maths operations like +,-,/,* can be more dificult that make it as functions like Add Sub Div and Mul, think on this:
Add(2,Sub(5,Mul(2,7)))

Equivalent with other operators:
2+(5-(2*7))

To make it with brackets can be more dificult to parse on this last way than with math functions like Add, its easy to make a compiler for first one than the last one that can be more complicated, think that all the rest of functions will work like Add functions, you will not add more extra code to parse complex maths functions.

Another tip, to make flow control functions like If - Endif, Repeat - Until, While - Wend. It can be implemented with only one internal function a Jump function that can examine and, or, xor, not operators, and conditions like x=2, z<3, etc, it must be a not jump conditional function.

To understand better i will explain the if - endif.

If instruction need operators of condition and jump address(to endif)
Endif dont have code really only we put this address on If jump address.

When we are parsing the code if we found an if function we increment a IfCounter(to be sure that every If is associated to its endif) and save the Address on a StackPointer for IfJumpAddresses with IfCounter as index of this array, that will contain the jump address (endif address) to make it if NotJump Condition function ist not true, and when we found EndIf function we put the address before endif function, and put it on IfJumpAddresses(IfCounter) and we decrease this IfCounter.

If you have any question email me at balrog@balrogsoftware.com
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Thanks for your support BalrogSoft.

I'll keep you updated on what's going on. Started working on a basic IDE today. Just something nice and simple to save the text as files and then be able to compile(or rather interpret) them. Coming along rather nicely I must say.

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

Post by User Mike »

I've stumpled into a problem. So far the IDE for my script language is coming along nicely, although there's one major problem. I can right hundreds of lines of code, compile it and it'll work fine. Then I can successfully save it.

Here's where the problem lies.

When I load the file, I only get 1 line of source back. After doing some research for most of today, I found that the readstring() command will only recover 1 line of text.

I've tried many experimentation, yet to no success. :(

Can anyone help me load multiple strings back into my editor?


I'll post the opening code source. At the moment I'm not using any arrays to save files, just to try to re-load them.

Thanks for any help you can provide.

Code: Select all

Case 2 ; Open
            
            textFileName$ = OpenFileRequester("Choose a text file", "", "Text|*.*",0)
            If textFileName$
               OpenFile(1,textFileName$) 
               
               GetGadgetState(2)                
               
               Readl=0
               
               Repeat        
                
                
               String$=ReadString() 
               FindString(String$," ",0)       
               ;SetGadgetText(0,String$)
               
               Readl=Readl+1
               Dim Readarray.s(Readl)
                Readarray.s(Readl)=String$
                             
               Until Eof(1)
               
               For a = 1 To Readl   
               
               FindString(Readarray.s(Readl)," ",0)
               SetGadgetText(0,Readarray.s(Readl))
               
               Next a
-Pure Basic User and loving it!-
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Alright I'm trying to move forward on this. I should also note exactly what I'm trying to do.

Basically, I'm using an editor gadget to right my text in. Saving the text is easy, because I can just use getgadgettext(gadget#). Now I'm trying to load the text.

I first start off with a file requester, and then open up the text file. Now I'm just trying to read the strings in the file, and get them to print back onto the editor.

(Case 2 Is just a simple menu case)

So right now, I'm just trying to individually get each string into an array, and then use a for/next routine to open each string up individually, getting more than one open.

Any help would be much appreciated.

Code: Select all

 Case 2 ; Open
            
            textFileName$ = OpenFileRequester("Choose a text file", "", "Text|*.*",0)
            If textFileName$
               OpenFile(1,textFileName$)     
                                          
                lines=0
                
                Repeat
                
                                            
               String$=ReadString() 
               lines=lines+1
               
               Dim Readline.s(lines)
               Readline.s(lines)=String$
               
               Until Eof(1)
               
               For a = 0 To lines
               
               SetGadgetText(0,Readline.s(lines))
               
               Next a
-Pure Basic User and loving it!-
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Bah, still havn't had any success. Could anyone give me a hint as to where I should head. Arrays, lists, for/next statments???

Thanks for your help.

8)
-Pure Basic User and loving it!-
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

Hi, I'm writing a programing game, where you have to code a robot(so it can get out of a maze). I haven't gone into the saving the source yet, but I will start now, I'll post my findings here.
BalrogSoft
Enthusiast
Enthusiast
Posts: 203
Joined: Sat Apr 26, 2003 6:33 pm
Location: Spain
Contact:

Post by BalrogSoft »

Hi User Mike, i made some changes to your code, when you use Dim ReadLine.s(lines), you redeclare again the array and clear the previous strings stored, you must use a Linked List, or declare the array before with a high number to get the code on the array:

Code: Select all

Case 2 ; Open 
            
            textFileName$ = OpenFileRequester("Choose a text file", "", "Text|*.*",0) 
            If textFileName$ 
               OpenFile(1,textFileName$)      
                                          
                lines=0 

                Dim Readline.s(1024)
 
                Repeat 
                                            
               String$=ReadString() 
               lines=lines+1 
                
               Readline.s(lines)=String$ 
                
               Until Eof(1) 

               ; and dont forget to close the file!!!!!
              CloseFile(1)

               For a = 0 To lines 
                AddGadgetItem(0, -1, ReadLine(a))   
               Next a
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

BalrogSoft beat me, but anyway here is my code:

Code: Select all

texto$=""
           
 ficha$=OpenFileRequester("Choose a file.","","  | *.uox",0)

If ReadFile(0, ficha$)

Repeat
   
     linex$=ReadString()
     texto$=texto$+linex$+Chr(13)+Chr(10)
      
     
Until Eof(0)
  
  
  CloseFile(0)
Else
  
EndIf
here is the complete code:

Code: Select all

  OpenWindow(0,0,0,350,150,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"EditorGadget")
  CreateGadgetList(WindowID(0)) 
  EditorGadget (0,0,0,340,100,#PB_Container_Raised) 
  CreateMenu(0, WindowID())
  MenuTitle("Project") 
        MenuItem(1, "Open"   +Chr(9)+"F-2") 
        MenuItem(2, "Save"   +Chr(9)+"F-3") 
        MenuItem(3, "Save as"+Chr(9)+"F-4") 
        MenuItem(4, "Close"  +Chr(9)+"F-12") 
DisableMenuItem(3,1)

AddKeyboardShortcut(0,#PB_Shortcut_F2,1)
AddKeyboardShortcut(0,#PB_Shortcut_F3,2)
AddKeyboardShortcut(0,#PB_Shortcut_F4,3)
AddKeyboardShortcut(0,#PB_Shortcut_Escape,4)

Repeat 
eventID=WaitWindowEvent()
      
  Select eventID
  
    Case #PB_Event_Menu
     
         Select EventMenuID() 
           Case 1

texto$=""
           
 ficha$=OpenFileRequester("Choose a file.","","  | *.uox",0)

If ReadFile(0, ficha$)

Repeat
   
     linex$=ReadString()
     texto$=texto$+linex$+Chr(13)+Chr(10)
      
     
Until Eof(0)
  
  
  CloseFile(0)
Else
  
EndIf

           SetGadgetText(0,texto$)
            
           Case 2
           texto$=GetGadgetText(0)
  
  ficha$=SaveFileRequester("File to save.","","  | *.uox",0)

If CreateFile(0, ficha$+".uox")
    
    
     WriteString(texto$)
 

  CloseFile(0)
Else
  
EndIf

           ClearGadgetItemList(0) 
 
           Case 3
           
           Case 4 : quit=1
             
         EndSelect 

      
     Case #PB_Event_CloseWindow
        quit=1
        
  EndSelect 
         
Until quit=1

End  

good luck!
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Excellent!

Thank you very very much to both of you.

For this, I think a screenshot is deserved.

Coded by me:
Special thanks to BalrogSoft and MadMax

Image
Enjoy!

To Do List:

- Highlight keywords
- Add core commands(file,dim,do,until,for,next,if,end if,etc.)
- Add some 2D/Image commands

8)

8)
-Pure Basic User and loving it!-
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Well, made this some time ago. Trying to implement a kind of variables. Sort out the variables, and you got a program that reads, parses and understands lines in a text file. This was just for fun.

Code: Select all

Dim varlist.s(100,100)
OpenConsole()
OpenFile(1,"script.txt")
lncnt=0
Repeat
lncnt=lncnt+1
line.s=ReadString()
commandpos=FindString(line.s," ",1)
command.s=Trim(Left(line.s,commandpos))
tmp=Len(line.s)-commandpos
commandtodo.s=Right(line.s,tmp)

;*************

Select command.s

 Case "print"
  PrintN(commandtodo.s)
 Case ";"
 
 Case "let"
 ;This creates values with no sense. will look at it!
 varpos=FindString(line.s,"=",1)
 varname.s=Left(line.s,varpos)
 vartmp=Len(line.s)-varpos
 varval.s=Right(line.s,varpos)

  Debug varval.s
  Debug varname.s
  
 Case "pause"
  
  Delay(Val(commandtodo.s))
  
  Case "clearconsole"
  ClearConsole()
  
 Default
PrintN("Error in line "+Str(lncnt))
Delay(1000)
Goto enderr

EndSelect
;*************
Until Eof(1)

enderr:
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

what i would do before implementing 2d/image commands, read more about how to make variables. Maybe arrays or something. Try to ask some and search this forum.
Post Reply