Android Example B4A -interpreter for Android / programming on the device

B4A-Interpreter

I've made a B4A interpreter that runs B4A-Code on Android
to write easy B4A-test programs on my Phone.
To make an app only copy the source code back to the B4A IDE and compile it to an apk file.


b4ai.png


Everbody is invited to expand this interpreter.

in this first version the interpreter can only create panels with click event and set colors

The new src is attached down here.
B4AInterpreter0_4.zip...
11.png



here is the source code from the first version (androidb4a.zip)
activity : main
B4X:
Sub Process_Globals
End Sub
Sub Globals
Dim editCode As EditText
Dim code As String
Dim bStart As Button
Dim apan As apanel  'activity panel
End Sub

Sub Activity_Create(FirstTime As Boolean)

code=File.ReadString(File.DirAssets,"test.b4a")
editCode.Initialize("editcode")
editCode.Text=code
Activity.addview(editCode,0%x,8%y,100%x,42%y)

bStart.Initialize("bstart")
bStart.Text="Start >"
Activity.addview(bStart,0,0,28%x,8%y)

apan.Initialize
bStart_click

Activity.addview(apan.pmain,0,50%y,100%x,50%y)


End Sub
Sub editCode_click
editCode.BringToFront
End Sub

Sub RemoveViews
Dim i As Int
For i=apan.pmain.NumberOfViews-1 To 0 Step -1
  apan.pmain.RemoveViewAt(i)
Next
End Sub


Sub bStart_click
RemoveViews
Dim command As String
Dim e As Boolean
initGlobals
runSub( "Activity_Create(FirstTime As Boolean)")

End Sub

Sub runSub(command As String)
Dim e As Boolean
command=runAt(command)
Do Until e
runCode(command)
command=runAt(command)
If command.IndexOf("End Sub")>-1 Then e=True
Loop

End Sub


Sub getValues(objectProp As String)As String()
Dim apos, epos,count As Int
Dim s1,s2 As String
Dim v(10) As String
apos=objectProp.IndexOf("(")

If apos>-1 Then
  epos=objectProp.IndexOf(")")
  s1=objectProp.SubString2(apos+1,epos)
  apos=0
   Dim findEnd As Boolean
   Do Until findEnd
     epos=s1.IndexOf2(",",apos+1)

    If epos<0 Then
      findEnd=True
      epos=s1.Length
    End If
     v(count)=s1.SubString2(apos,epos)
     count=count+1
     apos=epos+1
   Loop
End If

Return v

End Sub

Sub getFunction(objectProp As String)As String

If objectProp.IndexOf("(") >-1 Then Return objectProp.SubString2(0,objectProp.IndexOf("("))
If objectProp.IndexOf("=") >-1 Then Return objectProp.SubString2(0,objectProp.IndexOf("="))

End Sub
Sub runCode(command)
Dim apos, epos, index As Int
Dim objectType,objectName, objectProp ,eventName,functionsName As String
Dim params(10) As String


objectName=command.SubString2(1,command.IndexOf("."))
objectProp=command.SubString(command.IndexOf(".")+1)
functionsName=getFunction(objectProp)
params=getValues(objectProp)
index=apan.findindex(objectName)

If functionsName="Initialize" Then
   eventName=params(0)
  initview(objectName,eventName)
End If

If functionsName="AddView" Then
params=getValues(objectProp)
index=apan.findindex(params(0))
addview(apan.p(index),params)
End If

If functionsName.IndexOf("Color=")>-1 Then
params=getValues(objectProp)
index=apan.findindex(objectName)
apan.p(index).Color=Colors.RGB(params(0),params(1),params(2))
End If



End Sub
Sub runAt(subname As String)As String
Dim apos, epos As Int
Dim findstr As String

   apos=code.IndexOf2(subname,apos)
   epos=code.IndexOf2(Chr(13),apos+subname.Length+3)
   findstr=code.SubString2(apos+subname.Length+1,epos)

Return findstr

End Sub

Sub initGlobals
'in this first Version it define all Dim .. as of the hole code
Dim apos, epos As Int
Dim findEnd As Boolean
Dim objectType,s1,objectName As String

Do Until findEnd
   apos=code.IndexOf2("Dim",apos)
   epos=code.IndexOf2("Dim",apos+1)
   If epos=-1 Then
    findEnd=True
    epos=code.IndexOf2("End Sub",apos+1)
   End If
   s1=code.SubString2(apos+3,epos)
   objectName=s1.SubString2(0,s1.IndexOf("As"))
   objectType=s1.SubString2(s1.IndexOf("As")+2,s1.Length)
   apan.makeDim(objectName,objectType)

   apos=epos
Loop

End Sub


Sub addview(o As View,pa() As String)
Dim i(4) As Int
i(0)=pa(1)
i(1)=pa(2)
i(2)=pa(3)
i(3)=pa(4)
apan.pmain.addview(o,i(0),i(1),i(2),i(3))
End Sub

Sub initview(objectName As String,eventName As String)
Dim index As Int
index=apan.findindex(objectName)
apan.Initialize2(objectName,eventName)
apan.event(index)=eventName
End Sub

Sub editcode_TextChanged (Old As String, New As String)
   code=editCode.Text
End Sub
Sub editcode_EnterPressed

End Sub
Sub editcode_FocusChanged (HasFocus As Boolean)

End Sub

class module: aPanel
B4X:
'Class module
Sub Class_Globals
Dim pmain As Panel
Dim objectCount As Int
Dim p(100) As Panel
Dim b(100) As Button
Dim tag(100) As String
Dim name(100) As String
Dim event(100) As String
Dim art(100) As String

End Sub
Public Sub makeDim(objectName As String,objectType As String)
    name(objectCount)=objectName
    art(objectCount)=objectType
    objectCount=objectCount+1
End Sub

Public Sub Initialize2(objectName As String,eventname As String)
   Dim index As Int
   index=findindex(objectName)
   p(index).Initialize("pevent")
   p(index).tag=index
   name(index)=objectName
End Sub

Public Sub Initialize()
   pmain.Initialize("acty")
   pmain.Color=Colors.Blue

End Sub
Sub acty_click
pmain.BringToFront
End Sub
Sub findindex(objectName As String) As Int
   For m = 0 To objectCount-1
    If name(m).trim=objectName.trim Then Return m
   Next
End Sub
Sub pevent_click
   Dim send As Panel
   Dim index As Int
   Dim s1 As String
   send=Sender
   index=send.tag
   s1=name(index)&"_click"
   CallSub2(Main,"runSub",s1.Trim)
End Sub

The Test Code: test.b4a (file.dirAssets)

B4X:
Sub Process_Globals

End Sub

Sub Globals
Dim p As Panel
Dim pf As Panel
Dim pg As Panel
Dim b As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)

p.Initialize("p")
p.Color=Colors.RGB(233,55,244)
Activity.AddView(p,1,133,222,111)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub p_click
pf.Initialize("pf")
pf.Color=Colors.RGB(244,33,55)
Activity.AddView(pf,252,33,222,111)
End Sub

Sub pf_click
pg.Initialize("pg")
pg.Color=colors.RGB(33,255,22)
Activity.AddView(pg,252,233,222,111)
End Sub

Sub pg_click
pf.Color=Colors.RGB(133,255,229)
p.Color=Colors.RGB(233,255,2)
End Sub
 

Attachments

  • androidb4a.zip
    8 KB · Views: 644
  • androidb4a2_7.zip
    7.3 KB · Views: 682
  • B4AInterpreter0_3.zip
    354.4 KB · Views: 860
  • B4Ainterpreter0_4.zip
    355.2 KB · Views: 571
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Seems that i must update my ide to the newest version - many samples and codes are for the new IDE only.
A little mark on each, that it is usable only with the new IDE (or is compatible with the old ones), would be helpful
 

birnesoft

Active Member
Licensed User
Longtime User
Yes I write in the future the B4A-Version. The IDE 3.8 is only a few minutes installation. It is worth it.
 

GMan

Well-Known Member
Licensed User
Longtime User
Joa, done
 

Mashiane

Expert
Licensed User
Longtime User
Tried to solve the problem to show a b4a-file as text, but stuck - brain drain :eek:
Also impmented a webserver on port :5555 to get an additional access to download the generated file to the development computer.

As the file is too large now for upload (> 5 MB) in case of the libraries, you can get it here:
http://androids.bplaced.net/B4Ainterpreter/B4Ainterpreter203.zip
Can you please share the latest on this, link above broken..
 
Top