Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Include "system/string.bas"
- const CRLF = Chr(13,10)
- const CR = Chr(13)
- const LF = Chr(10)
- 'INPUT SOURCE
- Dim As String source="test2.epp"
- function trimWord(ByVal s As String) As String
- Return Trim(str_ltrim (s, " "))
- End function
- Function sanitize(ByVal ln As String) As String
- ln = replace(ln, " = ", "=")
- ln = replace(ln, "= ", "=")
- ln = replace(ln, " =", "=")
- ln = replace(ln, " + ", "+")
- ln = replace(ln, "+ ", "+")
- ln = replace(ln, " +", "+")
- ln = replace(ln, " - ", "-")
- ln = replace(ln, "- ", "-")
- ln = replace(ln, " -", "-")
- ln = replace(ln, " / ", "/")
- ln = replace(ln, "/ ", "/")
- ln = replace(ln, " /", "/")
- ln = replace(ln, " * ", "*")
- ln = replace(ln, "* ", "*")
- ln = replace(ln, " *", "*")
- ln = replace(ln, " && ", "&&")
- ln = replace(ln, "&& ", "&&")
- ln = replace(ln, " &&", "&&")
- ln = replace(ln, " == ", "==")
- ln = replace(ln, "== ", "==")
- ln = replace(ln, " ==", "==")
- ln = replace(ln, " != ", "!=")
- ln = replace(ln, "!= ", "!=")
- ln = replace(ln, " !=", "!=")
- ln = replace(ln, " <= ", "<=")
- ln = replace(ln, "<= ", "<=")
- ln = replace(ln, " <=", "<=")
- ln = replace(ln, " >= ", ">=")
- ln = replace(ln, ">= ", ">=")
- ln = replace(ln, " >=", ">=")
- ln = replace(ln, " ;", ";")
- Return ln
- End Function
- Sub addItem (s As String, list() As String)
- Dim As Integer i
- For i = 0 To UBound(list)-1
- If (s = list(i)) Then Exit For
- Next i
- i = UBound(list)
- list(i) = s
- ReDim Preserve list(i+1)
- End Sub
- Sub fileToList(file As string, list() As string)
- ReDim list(0)
- Dim As Integer f = FreeFile
- Dim As String ln
- Open ExePath & "/" & file For Input As #f
- Do
- Line Input #f, ln
- addItem(ln, list())
- Loop While Not Eof(f)
- Close #f
- ReDim Preserve list(UBound(list)-1)
- End Sub
- Sub fileToString(file As String, list As String)
- Dim As Integer f = FreeFile
- Dim As String ln
- Open ExePath & "/" & file For Input As #f
- Do
- Line Input #f, ln
- list = list & ln & CRLF
- Loop While Not Eof(f)
- Close #f
- End Sub
- Sub getMatches(ByVal pattern As string, ByVal buffer As String, result() As String)
- Dim As Integer i
- Dim As String ret(), ret2(), b
- b = buffer
- Split(pattern, "*", ret())
- For i = 0 To UBound(ret)
- Split(b, Trim(ret(i)), ret2())
- b = join("§§§§§", ret2())
- Next i
- b = replace(b, "§§§§§§§§§§", "§§§§§")
- Split(b, "§§§§§", ret())
- ReDim result(0)
- For i = 0 To UBound(ret)
- ret(i) = Trim(ret(i))
- If (ret(i) <> "") Then
- result(UBound(result)) = ret(i)
- ReDim Preserve result(UBound(result)+1)
- EndIf
- Next i
- ReDim Preserve result(UBound(result)-1)
- End Sub
- Function getType(s As String) As integer
- Dim As String w
- w = Mid(s, 1, 1)
- If (Asc(w)>47 and Asc(w)<58) Then
- If (InStr(s, ".")) Then
- Return 1 'float
- Else
- Return 0 'integer
- EndIf
- EndIf
- If (w=Chr(34)) Then Return 2 'string
- Return -1
- End Function
- Dim Shared As String types(2)
- types(0) = "integer"
- types(1) = "float"
- types(2) = "string"
- Dim Shared As Integer ob = 0, ow = 0
- Dim Shared As String of
- of = ""
- Type variable
- n As String
- t As Integer
- v As String
- End Type
- Dim Shared As variable var_main()
- ReDim var_main(0)
- Sub addVar (n As String, t As Integer, v As String, list() As variable)
- Dim As Integer i
- For i = 0 To UBound(list)-1
- If (n = list(i).n) Then Exit For
- Next i
- i = UBound(list)
- list(i).n = n
- list(i).t = t
- list(i).v = v
- ReDim Preserve list(i+1)
- End Sub
- Function castVar (ByVal n As String, list() As variable) As String
- Dim As Integer i
- For i = 0 To UBound(list)-1
- If (n = list(i).n) Then
- If(InStr(n, "["))Then
- Else
- If (list(i).t = 0) Then Return n & ".value.i"
- If (list(i).t = 1) Then Return n & ".value.f"
- If (list(i).t = 2) Then Return n & ".value.s"
- If (list(i).t = 3) Then Return n & ".value.a"
- EndIf
- EndIf
- Next i
- Return n
- End Function
- Function castVar2 (ByVal n As String, list() As variable) As variable
- Dim As Integer i
- Dim As variable v
- For i = 0 To UBound(list)-1
- If (n = list(i).n) Then
- If (list(i).t = 0) Then
- v.n = n & ".value.i"
- v.t = list(i).t
- ElseIf (list(i).t = 1) Then
- v.n = n & ".value.f"
- v.t = list(i).t
- ElseIf (list(i).t = 2) Then
- v.n = n & ".value.s"
- v.t = list(i).t
- ElseIf (list(i).t = 3) Then
- v.n = n & ".value.a"
- v.t = list(i).t
- EndIf
- Return v
- EndIf
- Next i
- v.n = n
- Return v
- End Function
- Dim Shared As String ReservedWords()
- Dim Shared As String Punctuators()
- fileToList("ReservedWords.txt", ReservedWords())
- fileToList("Punctuators.txt", Punctuators())
- Function inList(s As String, list() As String, result() As String) As String
- Dim As Integer i
- For i=0 To UBound(list)
- If (InStr(s, list(i))) Then
- getMatches("*" & list(i) & "*", s, result())
- Return list(i)
- EndIf
- Next i
- Return ""
- End Function
- Function lex (w As String) As String
- Return ""
- End function
- Function isNumber(s As String) As Integer
- If (Asc(s)>47 And Asc(s)<58) Then Return 1
- Return 0
- End Function
- Function isChar(s As String) As Integer
- If (Asc(s)>64 And Asc(s)<91) Then Return 1
- If (Asc(s)>96 And Asc(s)<123) Then Return 1
- Return 0
- End Function
- Function isAlphaNum(s As String) As Integer
- If (isNumber(s)) Then Return 1
- If (isChar(s)) Then Return 1
- Return 0
- End Function
- Function isWhitespace(s As String) As Integer
- If (asc(s)=9) Then Return 1
- If (asc(s)=32) Then Return 1
- Return 0
- End Function
- Dim As Integer i, j, f, sw=0, p, f2
- Dim As String ln, c, w, tokenType="", o="", lino="", lastTag="", lastVar="", parentTag=""
- 'Print Chr(34)
- 'Sleep
- Print "[EPP] Version 0.1"
- f = FreeFile
- Open ExePath & "/cache/functions" For Output As #f
- Close #f
- f = FreeFile
- Open ExePath & "/cache/main" For Output As #f
- Close #f
- f2 = FreeFile
- Open ExePath & "/cache/main" For Append As #f2
- f = FreeFile
- Open ExePath & "/" & source For Input As #f
- Do
- Line Input #f, ln
- ln = sanitize(ln)
- ln = sanitize(ln)
- ln = sanitize(ln)
- ln = trimWord(ln)
- 'Print ln
- o=""
- ln = replace(ln, "[", "[ ")
- ln = replace(ln, "]", " ]")
- For i=1 To Len(ln)
- 'LEXER
- c = Mid(ln, i, 1)
- If (Mid(ln, i,2) = "//") Then Exit For 'remove comments
- 'Print c & " whitespace[" & isWhitespace(c) & "] alphanum[" & isAlphaNum(c) & "] char[" & isChar(c) & "] number[" & isNumber(c) & "]"
- If(tokenType="") Then
- If (isAlphaNum(c)) Then
- If (isChar(c)) Then tokenType="name"
- If (isNumber(c)) Then tokenType="number"
- Else
- If (Asc(c)=34) Then
- tokenType="string"
- Else
- tokenType="nonalpha"
- EndIf
- EndIf
- EndIf
- 'Print "[" & tokenType & "]"
- Select Case (tokenType)
- Case "name"
- If(isAlphaNum(c) or c="_" or c="." or c="[") Then
- w=w & c
- Else
- 'Print w
- o=w
- 'Print c
- i-=1
- w=""
- tokenType=""
- EndIf
- Case "string"
- If(Asc(c)=34) Then sw+=1
- w=w & c
- If (sw>1) Then
- 'Print w
- o=w
- w=""
- tokenType=""
- sw=0
- EndIf
- Case "number"
- If(isNumber(c) Or c=".") Then
- w=w & c
- Else
- 'Print w
- o=w
- 'Print c
- i-=1
- w=""
- tokenType=""
- EndIf
- Case "nonalpha"
- p=-1
- For j=0 To UBound(Punctuators)
- If( Mid(ln,i,Len(Punctuators(j)))=Punctuators(j) ) Then
- p=j
- Exit For
- EndIf
- Next j
- If(p>-1) Then
- i+=1
- 'Print punctuators(p)
- o=punctuators(p)
- else
- 'Print c
- o=c
- EndIf
- w=""
- tokenType=""
- End Select
- 'PARSER
- If(Trim(o)<>"") then
- 'o=castVar(o, var_main())
- Select Case(lastTag)
- Case "VAR"
- 'addVar(o, 0, "0", var_main())
- lastVar=o
- o="var " & o
- parentTag=lastTag
- lastTag=""
- Case "NEW"
- If (o="int" ) Then o="new_integer" : addVar(lastVar, 0, "", var_main())
- If (o="float" ) Then o="new_float" : addVar(lastVar, 1, "", var_main())
- If (o="string") Then o="new_string" : addVar(lastVar, 2, "", var_main())
- If (o="any") Then o="new_any" : addVar(lastVar, 3, "", var_main())
- lastVar=""
- parentTag=lastTag
- lastTag=""
- Case "IF"
- o="if " & o
- parentTag=lastTag
- lastTag=""
- Case "ELSE"
- o="else " & o
- parentTag=lastTag
- lastTag=""
- Case "FOR"
- o="for " & o
- parentTag=lastTag
- lastTag=""
- Case "DO"
- o="do " & o
- parentTag=lastTag
- lastTag=""
- Case "WHILE"
- o="while " & o
- parentTag=lastTag
- lastTag=""
- Case "FUNCTION"
- o="function " & o
- parentTag=lastTag
- lastTag=""
- Case "NULL"
- o="NULL" & o
- parentTag=lastTag
- lastTag=""
- Case "VOID"
- o="void " & o
- parentTag=lastTag
- lastTag=""
- Case "CONSOLE.LOG"
- o="console_log " & o
- parentTag=lastTag
- lastTag=""
- Case "FILE.OPEN"
- o="file_open " & o
- parentTag=lastTag
- lastTag=""
- Case "FILE.CLOSE"
- o="file_close " & o
- parentTag=lastTag
- lastTag=""
- Case "FILE.WRITE"
- o="file_write " & o
- parentTag=lastTag
- lastTag=""
- Case "FILE.READ"
- o="file_read " & o
- parentTag=lastTag
- lastTag=""
- Case "FILE.READLINE"
- o="file_readLine " & o
- parentTag=lastTag
- lastTag=""
- Case "OS.PATH"
- o="os_path" & o
- lastTag=""
- parentTag=lastTag
- Case "OS.PLATFORM"
- o="os_platform" & o
- lastTag=""
- parentTag=lastTag
- Case Else
- If(parentTag<>"CONSOLE.LOG") Then o=castVar(o, var_main())
- lastTag=""
- parentTag=""
- End Select
- For j=0 To UBound(ReservedWords)
- If(UCase(o)=ReservedWords(j)) Then
- Print "[" & ReservedWords(j) & "]"
- lastTag = ReservedWords(j)
- 'o=o & " "
- o=""
- Exit for
- EndIf
- Next j
- lino=lino & o
- Print o
- o=""
- EndIf
- Next i
- Print "--------------------------------------"
- Print lino
- Print #f2, lino
- Print "--------------------------------------"
- lino=""
- w=""
- c=""
- Loop While Not Eof(f)
- Close #f
- Close #f2
- 'TRANSPILING
- Print "-----------------------------------------"
- Print "[EPP] compiling..."
- Print "-----------------------------------------"
- Dim As String rtl_types, rtl_string, tpl_main, main, functions, variables, rtl
- fileToString("targets/BC32/main.c", tpl_main)
- 'Print tpl_main
- fileToString("cache/main", main)
- 'Print main
- fileToString("cache/functions", functions)
- 'Print functions
- fileToString("targets/BC32/rtl.c", rtl)
- 'Print rtl_types
- 'fileToString("targets/BC32/string.c", rtl_string)
- 'Print rtl_string
- functions = rtl & functions
- tpl_main = replace(tpl_main, "[VARIABLES]", variables)
- tpl_main = replace(tpl_main, "[FUNCTIONS]", functions)
- tpl_main = replace(tpl_main, "[MAIN]", main)
- f = FreeFile
- Open ExePath & "/" & source & ".c" For output As #f
- Print #f, tpl_main
- Close #f
- Shell("cli.exe -s " & source & " -t BC32")
- Sleep
- End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement