PBScript is a lightweight scripting engine that lets you embed and run PureBasic-syntax scripts at runtime. It supports a large subset of PureBasic syntax and are generally compatible with the IDE.
Variables & Types
The standard PureBasic type suffixes are supported for variable declarations: but only i,d,s are supported
Code: Select all
Protected x.i ; integer
Protected d.d ; double
Protected s.s ; string
Global counter.i = 0
Operators
Arithmetic: + - * / % (modulus)
Comparison: = <> < > <= >=
Logical: And Or Not
Bitwise: & | ! ~ << >>
Augmented assignment: the above operators combined with = , e.g:
Code: Select all
x + 1 ; same as x = x + 1
x * 2
x & 255
Code: Select all
If x > 10
Print("big")
ElseIf x > 5
Print("medium")
Else
Print("small")
EndIf
For i = 1 To 10 Step 2
Print(i)
Next
While x > 0
x - 1
Wend
Repeat
x + 1
Until x >= 10
Structures
Code: Select all
Structure Point
x.d
y.d
EndStructure
Protected p.Point
p\x = 3.0
p\y = 4.0
Code: Select all
Structure Node
value.i
*next.Node
EndStructure
Code: Select all
Protected *p.Point = AllocateMemory(SizeOf(Point))
*p\x = 10.0
*p\y = 20.0
Passing by address:
Code: Select all
Procedure doubleX(*p.Point)
*p\x = *p\x * 2.0
EndProcedure
doubleX(@pt) ; pass address of pt
doubleX(@*pPtr) ; pass address of what *pPtr points to
Code: Select all
Procedure.d distFromOrigin(*p.Point)
ProcedureReturn Sqr(*p\x * *p\x + *p\y * *p\y)
EndProcedure
Procedure makemPoint(px.d, py.d)
Protected *p.mPoint = AllocateMemory(SizeOf(mPoint))
*p\x = px
*p\y = py
ProcedureReturn *p
EndProcedure
Global *pt.mPoint = makemPoint(3.0, 4.0)
Raw Memory
Code: Select all
Protected *buf = AllocateMemory(512)
PokeI(*buf, 12345)
Protected val.i = PeekI(*buf)
FreeMemory(*buf)
Built-in Functions
Print(x) — output any value
Str(x), StrD(x), Val(s), ValD(s) — type conversions
Len(s) — string operations
Sqr(x), Pow(x,y), Abs(x)
AllocateMemory(size), FreeMemory(ptr), SizeOf(type), MemorySize(ptr)
PeekX / PokeX family (B W L I D S variants)
FFI — Calling Native Functions
Runtime-declared PureBasic procedures can be registered and called from scripts:
Code: Select all
; In the host PureBasic program:
Runtime Procedure.d MyAdd(a.d, b.d)
ProcedureReturn a + b
EndProcedure
RegisterFFIFunction("MyAdd", @MyAdd(), "d", "d,d")
; Then in the script:
Protected result.d = MyAdd(1.5, 2.5)
Code: Select all
XIncludeFile "pbscript.pbi"
; Load from string
Global *script = PBScriptNew(code.s)
; Load from file
Global *script = PBScriptLoad("myscript.pb")
PBScriptRun(*script)
PBScriptFree(*script) ;free script
PBScriptDestruct() ;clear close the script engine
Arrays, linked lists (NewList) and maps (NewMap) are not currently supported. Interfaces, modules and compiler directives are not supported.
download with test code here
https://Atomicwebserver.com/PBScript.zip



