PDA

View Full Version : TOMDK - creating TB modules with Oxygen



Charles Pegge
19-08-2008, 19:42
Creating thinBasic Oxygen Modules:

TOMDK is a single script which can be used as a basis for creating DLLs for thinBasic. The zip file below is the first release. You can adapt the TOM script to build your own modules. No other software is required to generate the DLL, and you can see all the nuts and bolts involved in building a DLL from scratch.

Once again I had to make some adjustments to Oxygen. But no new commands.

Oxygen Update

http://community.thinbasic.com/index.php?topic=1845.0

Petr Schreiber
19-08-2008, 21:48
;D

I have no words ... thinBasic module compiling thinBasic module ...
And with those clean macros it is very easy to understand.


Karma up :),
Petr

kryton9
20-08-2008, 03:53
Charles with all that you are doing and with possibly what new PowerBasic brings, I am at a loss to categorize what can and will be possible with thinBasic. Your module is breaking all the rules for scripting languages and what can be done with them. I can't comment more as out of my league, but am watching in admiration that is for sure!

Charles Pegge
20-08-2008, 04:30
Well thank you Petr. It was quite hard work putting it together as the information available is rather limited. I've used a macro to place the Export code, out of sequence at the top of the file. The rest can be left unaltered in most instances.

Larger modules will require an extended imports section but only in the virtual image.
If you see [esi+3600] in the listing then its time to add another 4096 bytes but I can automate this in future versions.

Charles Pegge
20-08-2008, 13:12
Hi Kent,
As long as the language is Turing-complete it can be used to generate another language of any complexity. It is certainly possible to build a compiler in pure thinBASIC though it might perform at a leisurely pace. My first attempt at a a compiler many years ago, for an 8088 based computer board I designed. It was written with mBasic, the standard Microsoft interpreter. (Later we compiled it with BASCOM). It was adequate for testing the board, and establishing basic communications.

With Oxygen, I've been pushing the boundaries - but neglected the basics which are at least as important. So I will try to remedy this by posting some short pieces which are relatively easy to follow. These will form part of the h2o kernel.

Michael Hartlef
20-08-2008, 14:40
It is definately possible to write a compiler in thinbasic. With and without the help of Charles great modules.

Charles Pegge
21-08-2008, 06:31
Compiler in thinBasic ;D

This will convert simple expressions like
a + b * c - d
into assembly code taking account of operator precedence.
(converted from PB)




DIM ascw,dtf,swd,i as Long
DIM s AS STRING
'
s="a + b * c - d"
'
MSGBOX 0,s+$CRLF+$CRLF+compile_expr(s,1)
stop

' Compile expression using operator precedence
FUNCTION compile_expr(s AS STRING, i AS LONG) AS STRING
LOCAL w,op,np,ins,t AS STRING
LOCAL prec,stk AS LONG
stk=1
' stack arrays
DIM sprec(16) AS LONG ' precedence
DIM sop(16) AS STRING ' operator
DO
w=nword(s,i)
IF w="" THEN EXIT DO
op="":ins=""
IF w="+" THEN op="fadd qword ":prec=5
IF w="-" THEN op="fsub qword ":prec=5
IF w="*" THEN op="fmul qword ":prec=6
IF w="/" THEN op="fdiv qword ":prec=6
IF LEN(op) THEN w=nword(s,i)
IF op="" THEN op="fld qword ":prec=7
np=nword(s,i) ' next operator
IF (stk>1) then
IF (sprec(stk-1)>=prec) THEN
DECR stk
ins= _
"fst qword [esi]"+$CR _
+"fld qword ptr [esi-8]"+$CR _
+sop(stk)+"[esi]"+$CR _
+ "sub esi,8"+$CR
END IF
END IF
i=swd ' restore reader position
IF ((np="*")OR(np="/"))AND(prec<6) THEN
ins= _
"fstp qword [esi]"+$CR _
+"add esi,8"+$CR
sprec(stk)=prec:sop(stk)=op: op="fld qword "
INCR stk
END IF
t=t+ins+op+w+$CR
LOOP
FUNCTION=t
END FUNCTION

GET NEXT WORD
FUNCTION nword(BYREF s AS STRING, BYREF i AS LONG) AS STRING
' s source
' i position in source
' swd start of word position
' a ascii code
' b general purpose
' wr word

LOCAL a,b,v AS LONG
DIM wr AS STRING

DO ' skip leading white space and blank lines
a=ASC(s,i)
IF a<=0 THEN EXIT DO
'if a=10 then lct+=1
IF a>32 THEN EXIT DO
INCR i
LOOP
swd=i:dtf=0
ascw=a
DO
a=ASC(s,i)
IF a<33 THEN EXIT DO
INCR i
IF (a=34)OR(a=39)OR(a=96) THEN
IF i>swd+1 THEN EXIT DO ' as terminating symbol
b=a
DO
a=ASC(s,i)
INCR i
IF (a=b)OR(a=0) THEN EXIT DO
LOOP
a=ASC(s,i)
IF a<>46 THEN EXIT DO
END IF
IF a=46 THEN
IF dtf=0 THEN dtf=i-swd
ITERATE DO
END IF
IF (a>96)AND(a<123) THEN ITERATE DO
IF (a>64)AND(a<91) THEN ITERATE DO
IF (a>47)AND(a<58) THEN ITERATE DO
IF (a=35)OR(a=95) THEN ITERATE DO ' # _
IF a>126 THEN ITERATE ' higher ascii
IF i>swd+1 THEN DECR i' symbol as delimiter
EXIT DO
LOOP
wr=MID$(s,swd,i-swd)
'IF (ncase AND lowercase) THEN
IF b=0 THEN wr=LCASE$(wr)
'END IF
FUNCTION=wr
END FUNCTION