November 28, 2020, 11:15:10 AM

Author Topic: [bb] Nonsense VM by Spencer [ 1+ years ago ]  (Read 445 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Nonsense VM by Spencer [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Nonsense VM
Author : Spencer
Posted : 1+ years ago

Description : Not really tested, just a thing I've been tinkering with, thought it might spark some ideas in others.

Update June-13-2010 : another version that is contained within a function which can call itself.

Update Jun-16-2010: this version enables a sort of threading of programs. Not true separate windows threads in parallel on separate cores, but threads cycling thru the VM which executes one command at a time. Other programs can be loaded as "classes" then new-ed up as "Object" programs which can be run linearly using the "run" command or can be threaded with current processes using the "invoke" command. The "focus" and "relax" commands allow any currently running program thread to take focus of the VM and execute solo until the program relaxes and returns the VM to cycle thru the loaded programs executing one command at a time.


Code :
Code: BlitzBasic
  1. Const cs = 16383
  2.  
  3.  
  4. ;********************************************************************************************************
  5. ;Program "Class"
  6. Type cProgram
  7.     Field ClassName$
  8.     Field c$[cs+1]
  9.     Field LastPosition
  10. End Type
  11. ;--------------------------------------------------------------
  12. Function cProgram_Load.cProgram(ClassName$, FilePath$)
  13.     Local n.cProgram = New cProgram
  14.     Local FileStream=ReadFile(FilePath)
  15.     Local p = 0
  16.     Local VarRef$ = ""
  17.     Local Pointer$ = ""
  18.     nClassName= Lower(ClassName)
  19.     While Not Eof(FileStream) ;read in code
  20.         nc[p]=Trim(ReadLine(FileStream))
  21.         p=p+(nc[p]<>"" And Left(nc[p],2)<>"//")
  22.     Wend
  23.     CloseFile(FileStream)
  24.     nLastPosition=p+1
  25.     p=0
  26.     While p < nLastPosition ;replace var declarations and ref points with position values
  27.         VarRef=Left(Lower(nc[p]),4)
  28.         If VarRef="%var" Or VarRef="%ref" Then
  29.             Pointer=Lower(Trim(Mid(nc[p],5)))
  30.             nc[p]=p
  31.             For tp=0 To nLastPosition
  32.                 If Lower(nc[tp])=Pointer Then
  33.                     nc[tp]="@"+p
  34.                 EndIf
  35.             Next
  36.         EndIf
  37.         p=p+1
  38.     Wend
  39.     Return n
  40. End Function
  41. ;--------------------------------------------------------------
  42. Function cProgram_PrintTokens(Name$)
  43.     Local cp.cProgram = cProgram_Get(Name)
  44.     For x=0 To cpLastPosition
  45.         Print RSet(x,3) + ":" + cpc[x]
  46.     Next
  47.     Input("Press Enter To Continue")
  48. End Function
  49. ;--------------------------------------------------------------
  50. Function cProgram_Get.cProgram(ClassName$)
  51.     ClassName = Lower(ClassName)
  52.     For class.cProgram = Each cProgram
  53.         If classClassName = ClassName Then
  54.             Return class
  55.         EndIf
  56.     Next
  57.     Input("Class Program '" + ClassName + "' was not loaded or has been collected from memory")
  58.     End
  59. End Function
  60. ;End Program "Class"
  61. ;********************************************************************************************************
  62.  
  63.  
  64.  
  65. ;********************************************************************************************************
  66. ;Program Instance "Object"
  67. Const STATUS_STOPPED = 0
  68. Const STATUS_RUNNING = 1
  69. Const STATUS_PAUSED  = 2
  70. Const THREADED_ON    = 0
  71. Const THREADED_OFF   = 1
  72.  
  73. Global gCurrentProgram.objProgram
  74. Global gThreadedStatus = THREADED_ON
  75. ;--------------------------------------------------------------
  76. Type objProgram
  77.     Field Name$
  78.     Field c$[cs+1]
  79.     Field p
  80.         Field LastPosition
  81.     Field Status
  82. End Type
  83. ;--------------------------------------------------------------  
  84. Function objProgram_New.objProgram(ClassName$,Name$)
  85.     Local class.cProgram = cProgram_Get(ClassName)
  86.     Local obj.objProgram = New objProgram
  87.     Local p = 0
  88.         objName = Lower(Name)
  89.         For p=0 To classLastPosition
  90.         objc[p] = classc[p]
  91.         Next
  92.         objLastPosition = classLastPosition
  93.     objStatus = STATUS_STOPPED
  94.     Return obj
  95. End Function
  96. ;--------------------------------------------------------------
  97. Function objProgram_Get.objProgram(Name$)
  98.     Name = Lower(Name)
  99.     For op.objProgram = Each objProgram
  100.         If opName = Name Then
  101.             Return op
  102.         EndIf
  103.     Next
  104.     Input("Object Program '" + Name + "' is not loaded")
  105.     End
  106. End Function
  107. ;--------------------------------------------------------------  
  108. Function objProgram_Run(Name$)
  109.     Local Program.objProgram = objProgram_Get(Name)
  110.     ProgramStatus = STATUS_RUNNING
  111.     gCurrentProgram = Program
  112.     While gCurrentProgramStatus = STATUS_RUNNING
  113.         objProgram_Exec()
  114.     Wend
  115. End Function
  116. ;--------------------------------------------------------------  
  117. Function objProgram_Invoke(Name$)
  118.     Local op.objProgram = objProgram_Get(Name)
  119.     Local StatusCount = 1 ;default
  120.     opStatus = STATUS_RUNNING
  121.     While StatusCount > 0
  122.         StatusCount = 0
  123.         For rop.objProgram = Each objProgram
  124.             If ropStatus = STATUS_RUNNING Then
  125.                 StatusCount = StatusCount + 1
  126.                 gCurrentProgram = rop
  127.                 objProgram_Exec()
  128.                 If gThreadedStatus = THREADED_OFF Then
  129.                     While gThreadedStatus = THREADED_OFF And gCurrentProgramStatus = STATUS_RUNNING
  130.                         objPRogram_Exec()
  131.                     Wend
  132.                 EndIf
  133.             EndIf
  134.         Next
  135.     Wend
  136. End Function
  137. ;--------------------------------------------------------------  
  138. Function objProgram_Delete(Name$)
  139.     Name = Lower(Name)
  140.     For Program.objProgram = Each objProgram
  141.         If ProgramName = Name Then
  142.             Delete Program
  143.             Return
  144.         EndIf
  145.     Next
  146. End Function
  147. ;--------------------------------------------------------------  
  148. Function objProgram_Stop(Name$)
  149.     Name = Lower(Name)
  150.     For Program.objProgram = Each objProgram
  151.         If ProgramName = Name Then
  152.             If PRogram <> gCUrrentProgram Then
  153.                 ProgramStatus = STATUS_STOPPED
  154.                 Return
  155.             EndIf
  156.         EndIf
  157.     Next
  158. End Function
  159. ;--------------------------------------------------------------  
  160. Function objProgram_Pause(Name$)
  161.     Name = Lower(Name)
  162.     For Program.objProgram = Each objProgram
  163.         If ProgramName = Name Then
  164.             If ProgramStatus = STATUS_RUNNING Then    
  165.                 If Program <> gCurrentProgram Then
  166.                     ProgramStatus = STATUS_PAUSED
  167.                     Return
  168.                 EndIf
  169.             EndIf
  170.         EndIf      
  171.     Next
  172. End Function
  173. ;--------------------------------------------------------------  
  174. Function objProgram_UnPause(Name$)
  175.     Name = Lower(Name)
  176.     For Program.objProgram = Each objProgram
  177.         If ProgramStatus = STATUS_PAUSED Then
  178.             If ProgramName = Name Then
  179.                 If Program <> gCurrentProgram Then
  180.                     ProgramStatus = STATUS_RUNNING
  181.                     Return
  182.                 EndIf
  183.             EndIf
  184.         EndIf      
  185.     Next
  186. End Function
  187. ;--------------------------------------------------------------  
  188. Function objProgram_Exec()
  189.     If gCurrentProgramp > gCurrentProgramLastPosition Then
  190.         gCurrentProgramStatus = STATUS_STOPPED
  191.         Return
  192.     EndIf
  193.     Select Lower(l(0)) ;Lower(gCurrentProgramclassc[gCurrentProgramp])
  194.         Case "class"   : cProgram_Load(s(1),s(2))      :m(3)
  195.         Case "new"     : objProgram_New(s(1),s(2))     :m(3)
  196.         Case "run"     : objProgram_Run(s(1))          :m(2)
  197.         Case "invoke"  : m(2):objProgram_Invoke(s(-1)) ;m(2)    
  198.         Case "focus"   : gThreadedStatus = THREADED_OFF:m(1)
  199.         Case "relax"   : gThreadedStatus = THREADED_ON :m(1)
  200.         Case "delete"  : objProgram_Delete(s(1))       :m(2)
  201.         Case "set"     : u(s(2))                       :m(3)
  202.        Case "print"    : Print s(1)                    :m(2)
  203.         Case "write"   : Write s(1)                    :m(2)
  204.         Case "input"   : u(Input())                    :m(2)
  205.         Case "inc"     : u(f(1)+1)                     :m(2)
  206.         Case "dec"     : u(f(1)-1)                     :m(2)
  207.         Case "add"     : u(f(2)+f(3))                  :m(4)
  208.         Case "sub"     : u(f(2)-f(3))                  :m(4)
  209.         Case "mul"     : u(f(2)*f(3))                  :m(4)
  210.         Case "div"     : u(f(2)/f(3))                  :m(4)
  211.         Case "pwr"     : u(f(2)^f(3))                  :m(4)
  212.         Case "nrt"     : u(f(3)^(1/f(2)))              :m(4)
  213.         Case "sgn"     : u(Sgn(f(2)))                  :m(3)
  214.         Case "invrs"   : u(1/f(1))                     :m(2)
  215.         Case "opp"     : u(-f(1))                      :m(2)
  216.         Case "pos"     : u( Abs(f(1)))                 :m(2)
  217.         Case "neg"     : u(-Abs(f(1)))                 :m(2)
  218.         Case "sin"     : u( Sin(f(2)))                 :m(3)
  219.         Case "cos"     : u( Cos(f(2)))                 :m(3)
  220.         Case "tan"     : u( Tan(f(2)))                 :m(3)
  221.         Case "asin"    : u(ASin(f(2)))                 :m(3)
  222.         Case "acos"    : u(ACos(f(2)))                 :m(3)
  223.         Case "atan"    : u(ATan(f(2)))                 :m(3)
  224.         Case "atan2"   : u(ATan2(f(2),f(3)))           :m(4)
  225.         Case "ifeq"    : u(i(1)+(f(2) =f(3)))          :m(4)
  226.         Case "ifne"    : u(i(1)+(f(2)<>f(3)))          :m(4)
  227.         Case "if$eq"   : u(i(1)+(s(2) =s(3)))          :m(4)
  228.         Case "if$ne"   : u(i(1)+(s(2)<>s(3)))          :m(4)
  229.         Case "ifgt"    : u(i(1)+(f(2)> f(3)))          :m(4)
  230.         Case "ifge"    : u(i(1)+(f(2)>=f(3)))          :m(4)
  231.         Case "jmp"     : gCurrentProgramp=i(1)    
  232.         Case "jnz"     : If i(1)<>0    Then:gCurrentProgramp=i(2):Else:m(3):EndIf
  233.         Case "jz"      : If i(1) =0    Then:gCurrentProgramp=i(2):Else:m(3):EndIf
  234.         Case "jmpgt"   : If f(2)> f(3) Then:gCurrentProgramp=i(1):Else:m(4):EndIf
  235.         Case "jmpge"   : If f(2)>=f(3) Then:gCurrentProgramp=i(1):Else:m(4):EndIf
  236.         Case "jmplt"   : If f(2)< f(3) Then:gCurrentProgramp=i(1):Else:m(4):EndIf
  237.         Case "jmple"   : If f(2)<=f(3) Then:gCurrentProgramp=i(1):Else:m(4):EndIf
  238.         Case "jmpeq"   : If f(2) =f(3) Then:gCurrentProgramp=i(1):Else:m(4):EndIf
  239.         Case "jmpne"   : If f(2)<>f(3) Then:gCurrentProgramp=i(1):Else:m(4):EndIf
  240.         Case "jmp$eq"  : If s(2) =s(3) Then:gCurrentProgramp=i(1):Else:m(4):EndIf
  241.         Case "jmp$ne"  : If s(2)<>s(3) Then:gCurrentProgramp=i(1):Else:m(4):EndIf
  242.         Case "end"     : gCurrentProgramStatus = STATUS_STOPPED
  243.             Default        : m(1)
  244.     End Select
  245. End Function
  246. ;--------------------------------------------------------------
  247. ;Helper functions...    
  248. Function GetValue$(rp)
  249.     Local Token$ = gCurrentProgramc[rp]
  250.     Local FirstCharacter$=Left(Token,1)
  251.     If FirstCharacter="@" Then
  252.         Return GetValue(Mid(Token,2))
  253.     ElseIf FirstCharacter=Chr(34) Then
  254.          Return Mid(Token,2,Len(Token)-2)
  255.     Else
  256.         Return Token
  257.     EndIf
  258. End Function
  259. ;--------------------------------------------------------------
  260. ;Token set and get functions  
  261. Function u(v$)
  262.     Local Pointer = Mid(gCurrentProgramc[gCurrentProgramp+1],2)
  263.     gCurrentProgramc[Pointer]=v
  264. End Function
  265.  
  266. Function l$(o)
  267.     Return gCurrentProgramc[gCurrentProgramp+o]
  268. End Function
  269.  
  270. Function f#(o)
  271.     Return Float(GetValue(gCurrentProgramp+o))
  272. End Function
  273.  
  274. Function s$(o)
  275.     Return GetValue(gCurrentProgramp+o)
  276. End Function
  277.  
  278. Function i(o)
  279.     Return Int(GetValue(gCurrentProgramp+o))
  280. End Function
  281.  
  282. Function m(o)
  283.     gCurrentProgramp=gCurrentProgramp+o
  284. End Function
  285. ;--------------------------------------------------------------
  286. ;End Program Instance "Object"
  287. ;********************************************************************************************************
  288.  
  289. Function Main()
  290.     cProgram_Load("main","main.c")
  291.     objProgram_New("main","main")
  292.     cProgram_PrintTokens("main")
  293.     objProgram_Run("main")
  294. End Function
  295.  
  296. BuildDemoFiles() ; for demo only
  297. Main()
  298. Input("Demo Complete")
  299.  
  300. ;********************************************************************************************************
  301. ;Function for building demo files
  302. Function BuildDemoFiles()
  303.        
  304.         Local main_c = WriteFile("main.c")
  305.                 WriteLine(main_c,"%var x")
  306.                 WriteLine(main_c,"")
  307.                 WriteLine(main_c,"class")
  308.                 WriteLine(main_c,"foo")
  309.                 WriteLine(main_c,"foo.c")
  310.                 WriteLine(main_c,"")
  311.                 WriteLine(main_c,"new")
  312.                 WriteLine(main_c,"foo")
  313.                 WriteLine(main_c,"myFoo")
  314.                 WriteLine(main_c,"")
  315.                 WriteLine(main_c,"set")
  316.                 WriteLine(main_c,"x")
  317.                 WriteLine(main_c,"0")
  318.                 WriteLine(main_c,"")
  319.                 WriteLine(main_c,"invoke")
  320.                 WriteLine(main_c,"myFoo")
  321.                 WriteLine(main_c,"")
  322.                 WriteLine(main_c,"%ref loop")
  323.                 WriteLine(main_c,"    inc")
  324.                 WriteLine(main_c,"    x")
  325.                 WriteLine(main_c,"    focus")
  326.                 WriteLine(main_c,"        write")
  327.                 WriteLine(main_c,"        "+Chr(34)+"from main x=" + Chr(34))
  328.                 WriteLine(main_c,"        print")
  329.                 WriteLine(main_c,"        x")
  330.             WriteLine(main_c,"    relax")
  331.             WriteLine(main_c,"jmplt")
  332.                 WriteLine(main_c,"loop")
  333.                 WriteLine(main_c,"x")
  334.                 WriteLine(main_c,"100")
  335.         CloseFile(main_c)
  336.  
  337.         Local foo_c = WriteFile("foo.c")
  338.                 WriteLine(foo_c,"%var x")
  339.                 WriteLine(foo_c,"set")
  340.                 WriteLine(foo_c,"x")
  341.                 WriteLine(foo_c,"0")
  342.                 WriteLine(foo_c,"%ref loop")
  343.                 WriteLine(foo_c,"    inc")
  344.                 WriteLine(foo_c,"    x")
  345.                 WriteLine(foo_c,"    focus")
  346.                 WriteLine(foo_c,"        write")
  347.                 WriteLine(foo_c,"        "+Chr(34) + "From Foo x=" + Chr(34))
  348.                 WriteLine(foo_c,"        print")
  349.                 WriteLine(foo_c,"        x")
  350.                 WriteLine(foo_c,"    relax")
  351.                 WriteLine(foo_c,"jmplt")
  352.                 WriteLine(foo_c,"loop")
  353.                 WriteLine(foo_c,"x")
  354.                 WriteLine(foo_c,"50")          
  355.         CloseFile(foo_c)
  356.  
  357. End Function


Comments :


DareDevil(Posted 1+ years ago)

 this a good idea ;)


stanrol(Posted 1+ years ago)

 nice


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal