Qbasicnews.com

Full Version: ALIASES.BAS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Something that's better than the plain ol' nearly non-usable ALIAS command:

This piece of code converts variables to other variables (almost search and replace...)

The function works like this:
loadin.alias [source file name], [target file name]

You need to put this at the beginning of your program to convert from c and r to chipmunk and roadrunner:

Code:
'<ALIAS>
'chipmunk ALIAS c
'roadrunner ALIAS r
'</ALIAS>

Code:
DECLARE SUB loadin.aliases (filename1$, filename2$)
DIM SHARED convert.comments%: convert.comments% = 0
DIM SHARED write.back.to.source%: write.back.to.source% = 0
DIM SHARED remove.aliastag%: remove.aliastag% = 0
DIM SHARED case.sensitive%: case.sensitive% = 0
DIM SHARED nsquared.test%: nsquared.test% = 1
'Aliases 1.3
'Last updated: October 13, 2003
'(C) Agamemnus (Michael Romanovsky)

'format:
'<ALIAS>
'[long name] ALIAS [short name]
'</ALIAS>
'[short name] must before it and after it one of these characters: " ():" to be
'converted into [long name].

'Intended for variable names, function names, line numbers, and labels.

'Future versions will allow for:
'1) Distinctions between variables, functions, and line numbers/labels.
'2) ALIAS scope (global or local) rules.
'3) All parameters (both vars and output location) recognized in the <ALIAS> block.

CLS
loadin.aliases "test1.bas", "test2.bas"

SUB loadin.aliases (filename1$, filename2$)
DIM long.name$(1 TO 2048)
DIM short.name$(1 TO 2048)
DIM count%(1 TO 2048)
DIM count.max%(1 TO 2048)

'[long name] ALIAS [short name]

OPEN filename1$ FOR INPUT AS #1
IF remove.aliastag% = 0 THEN OPEN filename2$ FOR OUTPUT AS #2

DO
IF EOF(1) THEN EXIT DO
LINE INPUT #1, temp$
IF remove.aliastag% = 0 THEN PRINT #2, temp$
IF UCASE$(MID$(temp$, 1, 8)) = "'<ALIAS>" THEN get.aliases% = 1: EXIT DO
LOOP

IF get.aliases% = 0 THEN CLOSE : EXIT SUB
DO
IF EOF(1) THEN EXIT DO
LINE INPUT #1, temp$
IF remove.aliastag% = 0 THEN PRINT #2, temp$
IF MID$(temp$, 1, 1) = "'" THEN
alias.exists% = INSTR(UCASE$(temp$), " ALIAS ")
IF alias.exists% THEN
len.temp% = LEN(temp$)
IF len.temp% > 8 THEN
a$ = RTRIM$(LTRIM$(MID$(temp$, 2, alias.exists% - 1)))
b$ = RTRIM$(LTRIM$(MID$(temp$, alias.exists% + 7)))
len.a% = LEN(a$)
len.b% = LEN(b$)
IF len.a% > 2 THEN IF MID$(a$, len.a% - 2, 2) = "()" THEN a.is.array% = 1: a$ = MID$(a$, 2, len.a% - 1)
IF len.b% > 2 THEN IF MID$(a$, len.b% - 2, 2) = "()" THEN b.is.array% = 1: b$ = MID$(b$, 2, len.b% - 1)
tot.r% = tot.r% + 1
long.name$(tot.r%) = a$
short.name$(tot.r%) = b$
count.max%(tot.r%) = len.b%
END IF
ELSE
END IF
END IF
IF UCASE$(MID$(temp$, 1, 9)) = "'</ALIAS>" THEN EXIT DO
a.is.array% = 0: b.is.array% = 0
LOOP

'N^2 test. (and potentially n^3/2 or thereabouts...)
'[name1] ALIAS [name2]
'[name3] ALIAS [name1]
'becomes
'[name3] ALIAS [name1]
'[name3] ALIAS [name2]
IF nsquared.test% = 1 THEN
redo1:
FOR i% = 1 TO tot.r%
FOR j% = 1 TO tot.r%
IF i% = j% THEN GOTO contfor1
IF long.name$(i%) = short.name$(j%) THEN
IF count%(i%) THEN IF count%(j%) THEN GOTO contfor1
redo1% = 1
count%(i%) = 1
count%(j%) = 1
long.name$(i%) = long.name$(j%)
END IF
contfor1:
NEXT j%, i%
IF redo1% = 1 THEN redo1% = 0: GOTO redo1
FOR i% = 1 TO tot.r%
count%(i%) = 0
NEXT i%
END IF

IF remove.aliastag% = 1 THEN OPEN filename2$ FOR OUTPUT AS #2
DO
IF EOF(1) THEN EXIT DO
LINE INPUT #1, temp$
len.temp% = LEN(temp$)
start.copy% = 1

FOR i% = 1 TO len.temp%
a$ = MID$(temp$, i%, 1)
IF a$ = CHR$(34) THEN in.quotes% = 1 - in.quotes%

IF in.quotes% = 0 THEN
IF a$ = "'" THEN IF convert.comments% = 0 THEN EXIT FOR

'start matching

FOR j% = 1 TO tot.r%

IF case.sensitive% THEN
IF a$ <> MID$(short.name$(j%), count%(j%) + 1, 1) THEN GOTO cont2
ELSE
IF UCASE$(a$) <> UCASE$(MID$(short.name$(j%), count%(j%) + 1, 1)) THEN GOTO cont2
END IF

IF count%(j%) <> count.max%(j%) - 1 THEN count%(j%) = count%(j%) + 1: GOTO cont2
IF INSTR("() ", MID$(temp$, i% + 1, 1)) = 0 THEN GOTO cont1
IF i% - count%(j%) >= 2 THEN IF INSTR(":() ", MID$(temp$, i% - count%(j%) - 2, 1)) = 0 THEN GOTO cont1
IF i% - count%(j%) < 2 THEN temp2$ = temp2$ + long.name$(j%) ELSE temp2$ = temp2$ + MID$(temp$, start.copy%, i% - count%(j%) - start.copy%) + long.name$(j%)
start.copy% = i% + 1: count%(j%) = 0
EXIT FOR
cont1: count%(j%) = 0
cont2: NEXT j%
END IF
NEXT i%
'end matching

temp2$ = temp2$ + MID$(temp$, start.copy%)
PRINT #2, temp2$
temp2$ = "": in.quotes% = 0
LOOP
CLOSE


IF write.back.to.source% = 0 THEN EXIT SUB
OPEN filename2$ FOR INPUT AS #1
OPEN filename1$ FOR OUTPUT AS #2
DO: IF EOF(1) THEN EXIT DO
LINE INPUT #1, temp$
PRINT #2, temp$
LOOP
KILL filename2$
CLOSE
END SUB
Excellent way to cut down on bas size!!

I might very well use it Smile
The code didn't work straight away. Firstly this:
Code:
LINE INPUT #1, temp$
IF MID$(a$, 1, 1) = "'" THEN
Should be:
Code:
LINE INPUT #1, temp$
IF MID$(temp$, 1, 1) = "'" THEN
Otherwise it only works for the first alias definition and the rest are ignored. Secondly you are writing to file #2 before you open in:
Code:
ELSE
PRINT #2, temp$
END IF
END IF
IF MID$(temp$, 1, 9) = "'</ALIAS>" THEN EXIT DO
a.is.array% = 0: b.is.array% = 0
LOOP

OPEN filename2$ FOR OUTPUT AS #2
So the open command for file #2 needs to be placed somewhere higher in the code.

After fixing those two things I got wierd behaviour, the following for test1.bas:
Code:
'<ALIAS>
' First ALIAS f
' Second ALIAS s
' Thrid ALIAS t
'</ALIAS>
Resulted in the following for test2.bas:
Code:
'</ALIAS>
x = Firstx = f + Secondx = f + s + Third

You should also note in your documentation that the following definition:
Code:
'<ALIAS>
' First ALIAS f
' Second ALIAS First
'</ALIAS>
Doesn't work as some people might expect.
EDIT: Everything, even the very last point, was fixed. The last point has a maximum of about n^3/2 running time, though.. Smile

Yes, good points, I didn't test it enough.

That PRINT #2 thingo was a mistake. Shouldn't have been there.

The "first" thingo too. I am gonna fix that after I figure out what's going on. Thanks.

1) Array aliases "longname() ALIAS s()" weren't working. I fixed that.
2) I made the keywords case-insensitive..
3) I made an option to keep the ALIAS tag in the new file.

I'm going to add a case-insensitive option for the variables too.