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

Post by User Mike »

Thanks very much TheFool.

BalrogSoft has been kind enough to point me towards learning variables, and has released ScriptVM for me and others to get ideas from.

Thanks for posting your source for your compiler. I've adapted a few things that'll help make my compiler(or rather interpreter), much more efficient.

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 »

hi!

made a little modification on my program.:
here is the new one. try compile it to an exe:

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 ""
 
 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:
And make a file named "script.txt" in the exe's directory.

Put this in the file;:

Code: Select all

print test

;This is a comment!

print Yes! It works..
let daniel=hejsa
print Done
pause 1000
clearconsole 1
print yehaa
pause 2000
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

@thefool

Thank you so much for your help. I'm getting closer to having basic variables working with your 'let' statements. This is excellent and I'm really starting to love PureBasic.

:D
-Pure Basic User and loving it!-
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

Here is a fixed version so that the right values come out after a 'let' statement. This example is great I am going to try to add to this also. I noticed that comments MUST have a space after the ';' because of the way it looks for commands. I am going to see if I can improve the command interpreter first then add more commands.

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, " ", 1 )
  command.s = Trim( Left( line, commandpos ) )
  tmp = Len( line ) - commandpos 
  commandtodo.s = Right( line, tmp) 
  
  ;************* 
  
  Select command.s 
    
    Case "" 
      
    Case "print" 
      PrintN( commandtodo )
      
    Case ";"
      
    Case "let" 
      varpos = FindString( line, "=", 1 ) 
      varname.s = Trim( RemoveString( Left( line, varpos - 1 ), "let", 1 ) ) 
      vartmp = Len( line ) - varpos 
      varval.s = Right( line, vartmp )
      
      Debug "Variable '" + varname + "' will be assigned to '" + varval + "'"
      
      
    Case "pause"  
      Delay( Val( commandtodo ) ) 
      
    Case "clearconsole" 
      ClearConsole() 
      
    Default 
      PrintN( "Error in line " + Str( lncnt )  ) 
      Delay( 1000 ) 
      Goto enderr 
      
  EndSelect 
  ;************* 
Until Eof( 1 ) 

enderr:
Updated script so that comments work.

Code: Select all

print test 

; This is a comment! 

print Yes! It works.. 
let daniel=hejsa 
print Done 
pause 1000 
clearconsole 1 
print yehaa 
pause 2000
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

Yes, in fact, all code needs to have a space or some kind of character behind it to function properly. Any idea on a fix? Perhaps a space character could be added if no other characters are detected.

Right now, I've adapted my interpreter to thefool's code, because it's much more clean.

8)
-Pure Basic User and loving it!-
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

PureScript scripting functions

Post by USCode »

I wonder how difficult it would be for Fred to implement a scripting feature where the Scripting language would be PureBasic? Seems like he would be able to leverage some of the code (parser, etc.) he has already written? I have little knowledge in this area but it's a thought.

It would be fantastic to be able to give our users the ability to write PureBasic scripts that could call API functions our main applications provided and thus allow some custom application behavior.

As an example, RealBasic has a scripting control that allows the dynamic execution of scripts written in RealBasic at runtime.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Hi!

@rpj computing: Your new method to "LET" seems interesting. I will test it!

Im happy so many have shown interest in my little code. It was my very first try to make a script intepretter
NewMan
User
User
Posts: 11
Joined: Sun Jul 25, 2004 9:54 am

Post by NewMan »

how will you create a variable daniel and assign a value hejsa to it? or is there any other way of storing them?
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Re: PureScript scripting functions

Post by Shannara »

USCode wrote:I wonder how difficult it would be for Fred to implement a scripting feature where the Scripting language would be PureBasic? Seems like he would be able to leverage some of the code (parser, etc.) he has already written? I have little knowledge in this area but it's a thought.

It would be fantastic to be able to give our users the ability to write PureBasic scripts that could call API functions our main applications provided and thus allow some custom application behavior.

As an example, RealBasic has a scripting control that allows the dynamic execution of scripts written in RealBasic at runtime.
I know this is very old, but I 2nd this!
User Mike
User
User
Posts: 68
Joined: Mon Jan 26, 2004 7:06 pm

Post by User Mike »

I third this
8)
-Pure Basic User and loving it!-
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

I really want that! :D
I like logic, hence I dislike humans but love computers.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

I was really inspired by this!
I'll try to make a simple scripting language for use in games.

This is my current work:

Code: Select all

Dim Variable.s(10000)
Global String.s
Global Command.s
Global Argument.s

Procedure cmd_Var()
  Value1.s = Variable(Val(Argument))
  Pos = FindString(Argument,"#",1)
  If Pos > 0
    Value2.s = Variable(Val(RemoveString(Argument,Left(Argument,Pos))))
  Else
    Value2.s = LTrim(StringField(Argument,3," "))
  EndIf
  Select StringField(Argument,2," ")
    Case "=": Variable(Val(Argument)) = Value2
    Case "+": Variable(Val(Argument)) = Str( Val(Value1) + Val(Value2) )
    Case "-": Variable(Val(Argument)) = Str( Val(Value1) - Val(Value2) )
    Case "*": Variable(Val(Argument)) = Str( Val(Value1) * Val(Value2) )
    Case "/": Variable(Val(Argument)) = Str( Val(Value1) / Val(Value2) )
  EndSelect
EndProcedure

OpenFile(0,"Script.txt")
  While Eof(0) = #False
    String = ReadString()
    Command = StringField(String,1," ")
    Argument = LTrim(RemoveString(String,Command))
    
    Select(LCase(Command))
      Case "var": cmd_Var()
      Case "print"
        If FindString(Argument,"#",1)
          Print(Variable(Val(RemoveString(Argument,"#"))))
        Else
          Print(Argument)
        EndIf
      Case "printn"
        If FindString(Argument,"#",1)
          PrintN(Variable(Val(RemoveString(Argument,"#"))))
        Else
          PrintN(Argument)
        EndIf
      Case "waitkey"
        While Inkey()=""
          Delay(1)
        Wend
      Case "openconsole"
        OpenConsole()
      Case "clearconsole"
        ClearConsole()
      Case "closeconsole"
        CloseConsole()
      Case "delay"
        Delay(Val(Argument))
    EndSelect
  Wend
CloseFile(0)
Script:

Code: Select all

OpenConsole

Var 0 = 5
Var 0 * 100

PrintN This is a little stupid script.
Print 5*100 = 
Print #0
PrintN
PrintN And it works! :D

WaitKey
I like logic, hence I dislike humans but love computers.
Post Reply