Code: Select all
''
'' t
''
'' written by Fabrizio Davia in 2010
''
'' version 002
''
'' constants
const false = 0
const true = not false
type datum
w as integer
a as string
r as integer
end type
redim shared as string stack (100)
dim shared as integer s_index
s_index = 0
dim shared as integer previoustab
'' functions
function push ( a as string ) as integer
s_index += 1
if s_index > 100 then
redim preserve as string stack ( s_index + 99 )
endif
stack ( s_index ) = a
function = 0
end function
function pop () as string
function = stack ( s_index )
stack ( s_index ) = ""
s_index -= 1
end function
function whitespace ( a as string ) as integer
select case a
case " " , chr (9)
function = true
case else
function = false
end select
end function
function alphanum ( a as string ) as integer
function = false
select case asc ( a )
case 48 to 57 , 65 to 90 , 97 to 122
function = true
end select
end function
function getStatement ( a as string ) as datum
'' count tabs
dim as integer n , x
n = 1
x = 0
while n <= len ( a ) and x = 0
if ( mid ( a , n , 1 ) = chr (9) ) then
n += 1
else
x = 1
endif
wend
'' get first word
x = 0
dim as string s
dim as integer m = n
n -= 1
s = mid ( a , m , 1 )
m += 1
while m <= len ( a ) and x = 0
if alphanum ( mid ( a , m , 1 ) ) then
s = s + mid ( a , m , 1 )
m += 1
else
x = 1
endif
wend
dim as datum h
h.a = s
h.w = n
h.r = m
function = h
end function
function transform ( a as string ) as string
dim as string b
dim as datum c
c = getStatement ( a )
if c.w < previoustab and c.a <> "else" then
dim as integer q , r , j
dim as string l
q = previoustab - c.w
for r = 1 to q
l = mid ( a , 1 , c.w )
for j = 1 to q - r
l = l + chr (9)
next
l = l + pop ()
print #2 , l
next
endif
previoustab = c.w
select case c.a
case "namespace"
push ( "end namespace" )
b = a
case "def"
b = mid ( a , 1 , c.w ) + "function" + mid ( a , c.r , len (a) - c.r + 1 )
push ( "end function" )
case "for"
push ( "next" )
b = a
case "if"
push ( "endif" )
b = a + " then"
case "else"
b = a
case "while"
push ( "wend" )
b = a
case "integer"
b = mid ( a , 1 , c.w ) + "dim as integer" + mid ( a , c.r , len (a) - c.r + 1 )
case "string"
b = mid ( a , 1 , c.w ) + "dim as string" + mid ( a , c.r , len (a) - c.r + 1 )
case "single"
b = mid ( a , 1 , c.w ) + "dim as single" + mid ( a , c.r , len (a) - c.r + 1 )
case "switch"
b = mid ( a , 1 , c.w ) + "select case" + mid ( a , c.r , len (a) - c.r + 1 )
case "#"
b = mid ( a , 1 , c.w ) + mid ( a , c.r , len (a) - c.r + 1 )
case "'"
b = a
case else
b = a
end select
function = b
end function
function main() as integer
open "new2.bas" for input as 1
open "transform2.bas" for output as 2
dim as string a , b
while not eof (1)
line input #1 , a
b = transform ( a )
''print b
print #2 , b
wend
close #1
close #2
print "t"
print "Fabrizio Davia 2009"
print "transformation done"
sleep
return 0
end function
main()
Code: Select all
' t new
namespace s
integer a
string b
single c
def e ( w as integer ) as integer
integer b
for b = 1 to 199
if ( b / 2 > 50 )
print b
else
print b / 2
return 0
s.e (1)
To here:
Code: Select all
' t new
namespace s
dim as integer a
dim as string b
dim as single c
function e ( w as integer ) as integer
dim as integer b
for b = 1 to 199
if ( b / 2 > 50 ) then
print b
else
print b / 2
endif
next
return 0
end function
end namespace
s.e (1)