Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Include "system/string.bas"
  2.  
  3. const CRLF = Chr(13,10)
  4. const CR = Chr(13)
  5. const LF = Chr(10)
  6.  
  7. 'INPUT SOURCE
  8. Dim As String source="test2.epp"
  9.  
  10. function trimWord(ByVal s As String) As String
  11.     Return Trim(str_ltrim (s, " "))
  12.  
  13. End function
  14.  
  15. Function sanitize(ByVal ln As String) As String
  16.         ln = replace(ln, " = ", "=")
  17.         ln = replace(ln, "= ", "=")
  18.         ln = replace(ln, " =", "=")
  19.  
  20.         ln = replace(ln, " + ", "+")
  21.         ln = replace(ln, "+ ", "+")
  22.         ln = replace(ln, " +", "+")
  23.        
  24.         ln = replace(ln, " - ", "-")
  25.         ln = replace(ln, "- ", "-")
  26.         ln = replace(ln, " -", "-")
  27.        
  28.         ln = replace(ln, " / ", "/")
  29.         ln = replace(ln, "/ ", "/")
  30.         ln = replace(ln, " /", "/")
  31.        
  32.         ln = replace(ln, " * ", "*")
  33.         ln = replace(ln, "* ", "*")
  34.         ln = replace(ln, " *", "*")
  35.        
  36.         ln = replace(ln, " && ", "&&")
  37.         ln = replace(ln, "&& ", "&&")
  38.         ln = replace(ln, " &&", "&&")
  39.        
  40.         ln = replace(ln, " == ", "==")
  41.         ln = replace(ln, "== ", "==")
  42.         ln = replace(ln, " ==", "==")
  43.        
  44.         ln = replace(ln, " != ", "!=")
  45.         ln = replace(ln, "!= ", "!=")
  46.         ln = replace(ln, " !=", "!=")
  47.  
  48.         ln = replace(ln, " <= ", "<=")
  49.         ln = replace(ln, "<= ", "<=")
  50.         ln = replace(ln, " <=", "<=")
  51.  
  52.         ln = replace(ln, " >= ", ">=")
  53.         ln = replace(ln, ">= ", ">=")
  54.         ln = replace(ln, " >=", ">=")
  55.        
  56.         ln = replace(ln, " ;", ";")
  57.        
  58.         Return ln
  59. End Function
  60.  
  61.  
  62. Sub addItem (s As String, list() As String)
  63.     Dim As Integer i
  64.     For i = 0 To UBound(list)-1
  65.         If (s = list(i)) Then Exit For
  66.     Next i
  67.    
  68.     i = UBound(list)
  69.     list(i) = s
  70.    
  71.     ReDim Preserve list(i+1)
  72. End Sub
  73.  
  74. Sub fileToList(file As string, list() As string)
  75.     ReDim list(0)
  76.     Dim As Integer f = FreeFile
  77.     Dim As String ln
  78.    
  79.     Open ExePath & "/" & file For Input As #f
  80.         Do
  81.             Line Input #f, ln
  82.             addItem(ln, list())
  83.         Loop While Not Eof(f)
  84.     Close #f
  85.    
  86.     ReDim Preserve list(UBound(list)-1)
  87. End Sub
  88.  
  89. Sub fileToString(file As String, list As String)
  90.     Dim As Integer f = FreeFile
  91.     Dim As String ln
  92.    
  93.     Open ExePath & "/" & file For Input As #f
  94.         Do
  95.             Line Input #f, ln
  96.             list = list & ln & CRLF
  97.         Loop While Not Eof(f)
  98.     Close #f
  99.    
  100. End Sub
  101.  
  102. Sub getMatches(ByVal pattern As string, ByVal buffer As String, result() As String)
  103.     Dim As Integer i
  104.     Dim As String ret(), ret2(), b
  105.    
  106.     b = buffer
  107.    
  108.     Split(pattern, "*", ret())
  109.  
  110.     For i = 0 To UBound(ret)
  111.         Split(b, Trim(ret(i)), ret2())
  112.         b = join("§§§§§", ret2())
  113.     Next i
  114.    
  115.     b = replace(b, "§§§§§§§§§§", "§§§§§")
  116.     Split(b, "§§§§§", ret())
  117.    
  118.     ReDim result(0)
  119.    
  120.     For i = 0 To UBound(ret)
  121.         ret(i) = Trim(ret(i))
  122.         If (ret(i) <> "") Then
  123.             result(UBound(result)) = ret(i)
  124.             ReDim Preserve result(UBound(result)+1)
  125.         EndIf
  126.     Next i
  127.    
  128.     ReDim Preserve result(UBound(result)-1)
  129. End Sub
  130.  
  131.  
  132. Function getType(s As String) As integer
  133.     Dim As String w
  134.     w = Mid(s, 1, 1)
  135.    
  136.     If (Asc(w)>47 and Asc(w)<58) Then
  137.         If (InStr(s, ".")) Then
  138.             Return 1 'float
  139.         Else
  140.             Return 0 'integer
  141.         EndIf
  142.     EndIf
  143.    
  144.     If (w=Chr(34)) Then Return 2 'string
  145.    
  146.     Return -1
  147. End Function
  148.  
  149. Dim Shared As String types(2)
  150. types(0) = "integer"
  151. types(1) = "float"
  152. types(2) = "string"
  153.  
  154. Dim Shared As Integer ob = 0, ow = 0
  155. Dim Shared As String of
  156. of = ""
  157.  
  158. Type variable
  159.     n As String
  160.     t As Integer
  161.     v As String
  162. End Type
  163.  
  164. Dim Shared As variable var_main()
  165. ReDim var_main(0)
  166.  
  167. Sub addVar (n As String, t As Integer, v As String, list() As variable)
  168.     Dim As Integer i
  169.     For i = 0 To UBound(list)-1
  170.         If (n = list(i).n) Then Exit For
  171.     Next i
  172.    
  173.     i = UBound(list)
  174.     list(i).n = n
  175.     list(i).t = t
  176.     list(i).v = v
  177.    
  178.     ReDim Preserve list(i+1)
  179. End Sub
  180.  
  181. Function castVar (ByVal n As String, list() As variable) As String
  182.     Dim As Integer i
  183.     For i = 0 To UBound(list)-1
  184.         If (n = list(i).n) Then
  185.            
  186.         If(InStr(n, "["))Then
  187.         Else   
  188.             If (list(i).t = 0) Then Return n & ".value.i"
  189.             If (list(i).t = 1) Then Return n & ".value.f"
  190.             If (list(i).t = 2) Then Return n & ".value.s"
  191.             If (list(i).t = 3) Then Return n & ".value.a"
  192.         EndIf
  193.        
  194.         EndIf
  195.     Next i
  196.    
  197.     Return n
  198. End Function
  199.  
  200. Function castVar2 (ByVal n As String, list() As variable) As variable
  201.     Dim As Integer i
  202.     Dim As variable v
  203.    
  204.     For i = 0 To UBound(list)-1
  205.         If (n = list(i).n) Then
  206.             If (list(i).t = 0) Then
  207.                 v.n =  n & ".value.i"
  208.                 v.t = list(i).t            
  209.             ElseIf (list(i).t = 1) Then
  210.                 v.n =  n & ".value.f"
  211.                 v.t = list(i).t
  212.             ElseIf (list(i).t = 2) Then
  213.                 v.n = n & ".value.s"
  214.                 v.t = list(i).t
  215.             ElseIf (list(i).t = 3) Then
  216.                 v.n = n & ".value.a"
  217.                 v.t = list(i).t
  218.             EndIf
  219.            
  220.             Return v
  221.         EndIf
  222.     Next i
  223.    
  224.     v.n = n
  225.     Return v
  226. End Function
  227.  
  228. Dim Shared As String ReservedWords()
  229. Dim Shared As String Punctuators()
  230.  
  231. fileToList("ReservedWords.txt", ReservedWords())
  232. fileToList("Punctuators.txt", Punctuators())
  233.  
  234.  
  235. Function inList(s As String, list() As String, result() As String) As String
  236.     Dim As Integer i
  237.    
  238.     For i=0 To UBound(list)
  239.         If (InStr(s, list(i))) Then
  240.             getMatches("*" & list(i) & "*", s, result())   
  241.             Return list(i)
  242.         EndIf
  243.     Next i
  244.    
  245.     Return ""
  246. End Function
  247.  
  248.  
  249. Function lex (w As String) As String
  250.    
  251.  
  252.  
  253.     Return ""
  254. End function
  255.  
  256.  
  257. Function isNumber(s As String) As Integer
  258.     If (Asc(s)>47 And Asc(s)<58) Then Return 1 
  259.     Return 0
  260. End Function
  261.  
  262. Function isChar(s As String) As Integer
  263.     If (Asc(s)>64 And Asc(s)<91) Then Return 1
  264.     If (Asc(s)>96 And Asc(s)<123) Then Return 1
  265.     Return 0
  266. End Function
  267.  
  268. Function isAlphaNum(s As String) As Integer
  269.     If (isNumber(s)) Then Return 1
  270.     If (isChar(s)) Then Return 1
  271.     Return 0
  272. End Function
  273.  
  274. Function isWhitespace(s As String) As Integer
  275.     If (asc(s)=9) Then Return 1
  276.     If (asc(s)=32) Then Return 1
  277.     Return 0
  278. End Function
  279.  
  280.  
  281. Dim As Integer i, j, f, sw=0, p, f2
  282. Dim As String ln, c, w, tokenType="", o="", lino="", lastTag="", lastVar="", parentTag=""
  283.  
  284. 'Print Chr(34)
  285. 'Sleep
  286.  
  287. Print "[EPP] Version 0.1"
  288.  
  289. f = FreeFile
  290. Open ExePath & "/cache/functions" For Output As #f
  291. Close #f
  292.  
  293. f = FreeFile
  294. Open ExePath & "/cache/main" For Output As #f
  295. Close #f
  296.  
  297. f2 = FreeFile
  298. Open ExePath & "/cache/main" For Append As #f2
  299.  
  300.  
  301. f = FreeFile
  302. Open ExePath & "/" & source For Input As #f
  303.     Do
  304.         Line Input #f, ln
  305.  
  306.         ln = sanitize(ln)
  307.         ln = sanitize(ln)
  308.         ln = sanitize(ln)
  309.         ln = trimWord(ln)
  310.  
  311.         'Print ln
  312.        
  313.         o=""
  314.        
  315.         ln = replace(ln, "[", "[ ")
  316.         ln = replace(ln, "]", " ]")
  317.        
  318.         For i=1 To Len(ln)
  319.  
  320.             'LEXER
  321.            
  322.            
  323.             c = Mid(ln, i, 1)
  324.             If (Mid(ln, i,2) = "//") Then Exit For 'remove comments
  325.            
  326.             'Print c & " whitespace[" & isWhitespace(c) & "] alphanum[" & isAlphaNum(c) & "] char[" & isChar(c) & "] number[" & isNumber(c) & "]"
  327.            
  328.             If(tokenType="") Then
  329.             If (isAlphaNum(c)) Then
  330.                 If (isChar(c)) Then tokenType="name"
  331.                 If (isNumber(c)) Then tokenType="number"
  332.             Else
  333.                 If (Asc(c)=34) Then
  334.                     tokenType="string"
  335.                 Else
  336.                     tokenType="nonalpha"
  337.                 EndIf
  338.             EndIf
  339.             EndIf
  340.            
  341.             'Print "[" & tokenType & "]"
  342.            
  343.             Select Case (tokenType)
  344.                
  345.                 Case "name"
  346.                     If(isAlphaNum(c) or c="_" or c="." or c="[") Then
  347.                         w=w & c
  348.                     Else
  349.                         'Print w
  350.                         o=w
  351.                         'Print c
  352.                         i-=1
  353.                         w=""
  354.                         tokenType=""                       
  355.                     EndIf
  356.                
  357.                 Case "string"
  358.                     If(Asc(c)=34) Then sw+=1
  359.                     w=w & c
  360.                     If (sw>1) Then
  361.                         'Print w
  362.                         o=w
  363.                         w=""
  364.                         tokenType=""
  365.                         sw=0
  366.                     EndIf
  367.                    
  368.                    
  369.                 Case "number"
  370.                     If(isNumber(c) Or c=".") Then
  371.                         w=w & c
  372.                     Else
  373.                         'Print w
  374.                         o=w
  375.                         'Print c
  376.                         i-=1
  377.                         w=""
  378.                         tokenType=""
  379.                     EndIf
  380.                
  381.                 Case "nonalpha"
  382.                     p=-1
  383.                     For j=0 To UBound(Punctuators)
  384.                         If( Mid(ln,i,Len(Punctuators(j)))=Punctuators(j) ) Then
  385.                             p=j
  386.                             Exit For
  387.                         EndIf
  388.                     Next j
  389.                    
  390.                     If(p>-1) Then
  391.                         i+=1
  392.                         'Print punctuators(p)
  393.                         o=punctuators(p)
  394.                     else
  395.                         'Print c
  396.                         o=c
  397.                     EndIf
  398.                    
  399.                     w=""
  400.                     tokenType=""
  401.                
  402.             End Select
  403.            
  404.            
  405.             'PARSER
  406.             If(Trim(o)<>"") then
  407.                
  408.                 'o=castVar(o, var_main())
  409.                
  410.                 Select Case(lastTag)
  411.                    
  412.                     Case "VAR"
  413.                         'addVar(o, 0, "0", var_main())
  414.                         lastVar=o
  415.                         o="var " & o
  416.                         parentTag=lastTag
  417.                         lastTag=""
  418.                    
  419.                     Case "NEW"
  420.                         If (o="int"   ) Then o="new_integer" : addVar(lastVar, 0, "", var_main())
  421.                         If (o="float" ) Then o="new_float"   : addVar(lastVar, 1, "", var_main())
  422.                         If (o="string") Then o="new_string"  : addVar(lastVar, 2, "", var_main())
  423.                         If (o="any")    Then o="new_any"     : addVar(lastVar, 3, "", var_main())
  424.                         lastVar=""
  425.                         parentTag=lastTag
  426.                         lastTag=""
  427.  
  428.                     Case "IF"
  429.                         o="if " & o
  430.                         parentTag=lastTag
  431.                         lastTag=""
  432.  
  433.                     Case "ELSE"
  434.                         o="else " & o
  435.                         parentTag=lastTag
  436.                         lastTag=""
  437.  
  438.                     Case "FOR"
  439.                         o="for " & o
  440.                         parentTag=lastTag
  441.                         lastTag=""
  442.  
  443.                     Case "DO"
  444.                         o="do " & o
  445.                         parentTag=lastTag
  446.                         lastTag=""
  447.  
  448.                     Case "WHILE"
  449.                         o="while " & o
  450.                         parentTag=lastTag
  451.                         lastTag=""
  452.  
  453.                     Case "FUNCTION"
  454.                         o="function " & o
  455.                         parentTag=lastTag
  456.                         lastTag=""
  457.  
  458.                     Case "NULL"
  459.                         o="NULL" & o
  460.                         parentTag=lastTag
  461.                         lastTag=""
  462.  
  463.                     Case "VOID"
  464.                         o="void " & o
  465.                         parentTag=lastTag
  466.                         lastTag=""
  467.  
  468.                     Case "CONSOLE.LOG"
  469.                         o="console_log " & o
  470.                         parentTag=lastTag
  471.                         lastTag=""
  472.  
  473.                     Case "FILE.OPEN"
  474.                         o="file_open " & o
  475.                         parentTag=lastTag
  476.                         lastTag=""
  477.  
  478.                     Case "FILE.CLOSE"
  479.                         o="file_close " & o
  480.                         parentTag=lastTag
  481.                         lastTag=""
  482.  
  483.                     Case "FILE.WRITE"
  484.                         o="file_write " & o
  485.                         parentTag=lastTag
  486.                         lastTag=""
  487.  
  488.                     Case "FILE.READ"
  489.                         o="file_read " & o
  490.                         parentTag=lastTag
  491.                         lastTag=""
  492.  
  493.                     Case "FILE.READLINE"
  494.                         o="file_readLine " & o
  495.                         parentTag=lastTag
  496.                         lastTag=""
  497.                    
  498.                     Case "OS.PATH"
  499.                         o="os_path" & o
  500.                         lastTag=""
  501.                         parentTag=lastTag
  502.  
  503.                     Case "OS.PLATFORM"
  504.                         o="os_platform" & o
  505.                         lastTag=""
  506.                         parentTag=lastTag
  507.  
  508.                     Case Else
  509.                         If(parentTag<>"CONSOLE.LOG") Then o=castVar(o, var_main())
  510.                         lastTag=""
  511.                         parentTag=""
  512.                    
  513.                 End Select
  514.                
  515.                 For j=0 To UBound(ReservedWords)
  516.                     If(UCase(o)=ReservedWords(j)) Then
  517.                         Print "[" & ReservedWords(j) & "]"
  518.                         lastTag = ReservedWords(j)
  519.                         'o=o & " "
  520.                         o=""
  521.                         Exit for
  522.                     EndIf
  523.                 Next j
  524.                
  525.                 lino=lino & o
  526.                
  527.                
  528.                
  529.                 Print o
  530.                 o=""
  531.                
  532.             EndIf
  533.            
  534.            
  535.            
  536.         Next i
  537.        
  538.         Print "--------------------------------------"
  539.         Print lino
  540.         Print #f2, lino
  541.         Print "--------------------------------------"
  542.        
  543.         lino=""
  544.        
  545.         w=""
  546.         c=""
  547.        
  548.     Loop While Not Eof(f)
  549. Close #f
  550. Close #f2
  551.  
  552.  
  553. 'TRANSPILING
  554. Print "-----------------------------------------"
  555. Print "[EPP] compiling..."
  556. Print "-----------------------------------------"
  557.  
  558. Dim As String rtl_types, rtl_string, tpl_main, main, functions, variables, rtl
  559.  
  560. fileToString("targets/BC32/main.c", tpl_main)
  561. 'Print tpl_main
  562.  
  563. fileToString("cache/main", main)
  564. 'Print main
  565.  
  566. fileToString("cache/functions", functions)
  567. 'Print functions
  568.  
  569.  
  570. fileToString("targets/BC32/rtl.c", rtl)
  571. 'Print rtl_types
  572.  
  573. 'fileToString("targets/BC32/string.c", rtl_string)
  574. 'Print rtl_string
  575.  
  576. functions = rtl & functions
  577.  
  578. tpl_main = replace(tpl_main, "[VARIABLES]", variables)
  579. tpl_main = replace(tpl_main, "[FUNCTIONS]", functions)
  580. tpl_main = replace(tpl_main, "[MAIN]", main)
  581.  
  582. f = FreeFile
  583. Open ExePath & "/" & source & ".c" For output As #f
  584.     Print #f, tpl_main
  585. Close #f
  586.  
  587.  
  588. Shell("cli.exe -s " & source & " -t BC32")
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. Sleep
  599. End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement