Still on my quest to make a programming language that will win the nobel prize... It will happen.. I know it will! Ahh!!!!
Anyway!! *Doo doo*
My last one (the tokenizer thing) would turn something like this:
text(-10.0,len("Rofl! Lol."),message(3-2))
into
text
(
-
10
.
0
,
len
(
"
Rofl
!
Lol
.
"
)
,
message
(
3
-
2
)
)
Well, this one's similar - instead it groups up some tokens. I.e., negative numbers will become one token. So will floats. And also everything enclosed between apostrophes ( ' ) and inverted commas ( " ) will become one token.
So that same expression would turn into a more useful:
text
(
-10.0
,
len
(
"Rofl! Lol."
)
,
message(
3
-2
)
)
Now, one can continue to make ones programming language!
Here is your code:
`Setup
sync on : sync rate 60
set display mode 1024,768,32
`Code Data
global code_str as string
`Lexer Data
type ttok
tok as string
ttype as integer
endtype
global lex_count as integer
global lex_str as string
global lex_type as integer
`Read Code
`file$="scr1.txt" : lines=fileLines(file$)
`open to read 1,file$
`for l=1 to lines
`read string 1,st$
`code_str=code_str+st$
`next l
`close file 1
code_str="text(-10.0,len("+chr$(34)+"Rofl! Lol."+chr$(34)+"),message(3-2))"
lex_count=stLexCount(code_str)
dim token(lex_count) as ttok
`Parsing
for cur_lex=0 to lex_count
lex_str=stGetLex(code_str,cur_lex)
lex_type=stLexType(code_str,cur_lex)
token(cur_lex).tok=lex_str
token(cur_lex).ttype=lex_type
text 10,20*cur_lex,token(cur_lex).tok
sync
next cur_lex
`End
wait key
end
`Functions - Parsing
`Functions - Lexing and Tokenizing and stuff
function stLexType(st$,num)
lex$=stGetLex(st$,num)
if isAlpha(mid$(lex$,1)) then ltype=1
if isNum(mid$(lex$,1)) then ltype=2
if mid$(lex$,1)=chr$(34) or mid$(lex$,1)="'" then ltype=3
if mid$(lex$,1)="(" then ltype=4
if mid$(lex$,1)=")" then ltype=5
if mid$(lex$,1)="," then ltype=6
if mid$(lex$,1)="{" then ltype=7
if mid$(lex$,1)="}" then ltype=8
if mid$(lex$,1)="." then ltype=9
if mid$(lex$,1)="+" then ltype=10
if mid$(lex$,1)="-" then ltype=11
if mid$(lex$,1)="*" then ltype=12
if mid$(lex$,1)="/" then ltype=13
if mid$(lex$,1)="^" then ltype=14
endfunction ltype
function stLexCount(st$)
for t=1 to stTokenCount(st$)
curtok$=stGetToken(st$,t)
if curtok$=chr$(34) or curtok$="'" then onC=1-onC
if isNum(stGetToken(st$,t-1)) and isNum(stGetToken(st$,t+1)) and curtok$="." then isfloat=1 else isfloat=0
if isNum(stGetToken(st$,t+1)) and curtok$="-" then dec count
if onC=0 and isfloat=0 then inc count
if isfloat=1 then dec count
next t
endfunction count
function stGetLex(st$,num)
for t=1 to stTokenCount(st$)
curtok$=stGetToken(st$,t)
if curtok$=chr$(34) or curtok$="'" then onC=1-onC
if isNum(stGetToken(st$,t-1)) and isNum(stGetToken(st$,t+1)) and curtok$="." then isfloat=1 else isfloat=0
if isfloat=1 then dec count
if count=num then settok$=curtok$ : nexttok$=stGetToken(st$,t+1) : lex$=lex$+curtok$
if isNum(stGetToken(st$,t+1)) and curtok$="-" then dec count
if onC=0 and isfloat=0 then inc count
if isNum(mid$(settok$,1)) and nexttok$="."
if isNum(mid$(curtok$,1))=0 and curtok$<>"." then exit
else
if count>num then exit
endif
next t
endfunction lex$
function stTokenCount(st$)
for c=1 to len(st$)
oldtok=tok
cur$=mid$(st$,c)
if isAlpha(cur$) then tok=1
if isNum(cur$) then tok=1
if isOther(cur$) then inc tok
if oldtok<>tok
inc count
endif
next c
endfunction count
function stGetToken(st$,num)
for c=1 to len(st$)
oldtok=tok
cur$=mid$(st$,c)
if isAlpha(cur$) then tok=1
if isNum(cur$) then tok=1
if isOther(cur$) then inc tok
if oldtok<>tok
inc curtok
endif
if curtok=num
ret$=ret$+cur$
endif
next c
endfunction ret$
function isAlpha(text$)
if asc(text$)>=asc("a") and asc(text$)<=asc("z") then alpha=1
if asc(text$)>=asc("A") and asc(text$)<=asc("Z") then alpha=1
if text$="_" then alpha=1
endfunction alpha
function isNum(text$)
if asc(text$)>=asc("0") and asc(text$)<=asc("9") then num=1
endfunction num
function isOther(text$)
if isAlpha(text$)=0 and isNum(text$)=0 then exitfunction 1
endfunction 0