makegles20.monkey 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Import brl.filesystem
  2. Import brl.process
  3. Import brl.filepath
  4. Import brl.filestream
  5. Global pos:=0
  6. Global tokes:String[]
  7. Function LoadString:String( path:String )
  8. Local stream:=FileStream.Open( path,"r" )
  9. If Not stream Return ""
  10. Local str:=stream.ReadString()
  11. stream.Close
  12. Return str
  13. End
  14. Function SaveString:Void( str:String,path:String )
  15. Local stream:=FileStream.Open( path,"w" )
  16. stream.WriteString str
  17. stream.Close
  18. End
  19. Function GetTokes:Void( text:String )
  20. Local pos:=0
  21. Local buf:=New StringStack
  22. Repeat
  23. While pos<text.Length And text[pos]<=32
  24. pos+=1
  25. Wend
  26. If pos=text.Length
  27. .pos=0
  28. .tokes=buf.ToArray()
  29. Return
  30. Endif
  31. Local start:=pos
  32. Local chr:=text[pos]
  33. pos+=1
  34. If (chr>=65 And chr<65+26) Or (chr>=97 And chr<97+26) Or chr=95
  35. While pos<text.Length
  36. chr=text[pos]
  37. If (chr>=65 And chr<65+26) Or (chr>=97 And chr<97+26) Or( chr>=48 And chr<58) Or chr=95
  38. pos+=1
  39. Continue
  40. Endif
  41. Exit
  42. Wend
  43. Else If chr>=48 And chr<58
  44. If pos<text.Length And text[pos]="x"[0]
  45. pos+=1
  46. While pos<text.Length
  47. chr=text[pos]
  48. If (chr>=48 And chr<58) Or (chr>=65 And chr<70) Or (chr>=97 And chr<102)
  49. pos+=1
  50. Continue
  51. Endif
  52. Exit
  53. Wend
  54. Else
  55. While pos<text.Length
  56. chr=text[pos]
  57. If chr>=48 And chr<58
  58. pos+=1
  59. Continue
  60. Endif
  61. Exit
  62. Wend
  63. Endif
  64. Endif
  65. buf.Push text[start..pos]
  66. Forever
  67. End
  68. Function Parse:String()
  69. If pos=tokes.Length Return ""
  70. Local toke:=tokes[pos]
  71. pos+=1
  72. Return toke
  73. End
  74. Function CParse:Bool( toke:String )
  75. If pos=tokes.Length Or tokes[pos]<>toke Return False
  76. pos+=1
  77. Return True
  78. End
  79. Function Parse:Void( toke:String )
  80. If Not CParse( toke ) DebugStop
  81. End
  82. Function ParseType:String()
  83. Local isconst:=CParse( "const" )
  84. Local ty:=Parse()
  85. If ty="void" ty="Void"
  86. If CParse( "*" ) ty+=" Ptr"
  87. If CParse( "*" ) ty+=" Ptr"
  88. If isconst And ty="GLchar Ptr" ty="String"
  89. Return ty
  90. End
  91. Function Main()
  92. While FileType( "gles20.h" )=FILETYPE_NONE
  93. ChangeDir( ".." )
  94. Wend
  95. Local file:=LoadString( "gles20.h" )
  96. Local lines:=file.Split( "~n" )
  97. Local consts:=New StringStack
  98. Local functions:=New StringStack
  99. For Local line:=Eachin lines
  100. GetTokes( line )
  101. If CParse( "#" ) And CParse( "define" )
  102. Local id:=Parse()
  103. If id.StartsWith( "GL_" )
  104. Local ty:="Int"
  105. consts.Push( "Const "+id+":"+ty )
  106. Endif
  107. Else If CParse( "GL_APICALL" )
  108. Local retTy:=ParseType()
  109. Parse( "GL_APIENTRY" )
  110. Local id:=Parse()
  111. Parse( "(" )
  112. Local argTys:=""
  113. If Not CParse( ")" )
  114. Repeat
  115. Local ty:=ParseType()
  116. If ty="Void" Exit
  117. Local id:=Parse()+"_"
  118. If argTys argTys+=","
  119. argTys+=id+":"+ty
  120. Until Not CParse( "," )
  121. Parse( ")" )
  122. Endif
  123. functions.Push( "Function "+id+":"+retTy+"("+argTys+")" )
  124. Endif
  125. Next
  126. Local gles20:=LoadString( "gles20.monkey2" )
  127. Local i0:Int,i1:Int,i2:Int
  128. i0=gles20.Find( "~n'${CONSTS}" ) ; If i0=-1 DebugStop
  129. i1=gles20.Find( "~n",i0+1 ) ; If i1=-1 DebugStop
  130. i2=gles20.Find( "~n'${END}",i1 ) ; If i2=-1 DebugStop
  131. gles20=gles20[..i1+1]+consts.Join( "~n" )+gles20[i2..]
  132. i0=gles20.Find( "~n'${FUNCTIONS}" ) ; If i0=-1 DebugStop
  133. i1=gles20.Find( "~n",i0+1 ) ; If i1=-1 DebugStop
  134. i2=gles20.Find( "~n'${END}",i1 ) ; If i2=-1 DebugStop
  135. gles20=gles20[..i1+1]+functions.Join( "~n" )+gles20[i2..]
  136. SaveString( gles20,"gles20.monkey2" )
  137. Print "Done!"
  138. End