202 lines
3.7 KiB
QBasic
202 lines
3.7 KiB
QBasic
' ============================================
|
|
' Test program for the basic2c transpiler
|
|
' Tests all major features
|
|
' ============================================
|
|
|
|
' --- Variable declarations ---
|
|
DIM x AS INTEGER
|
|
DIM y AS INTEGER
|
|
DIM pi AS DOUBLE
|
|
DIM greeting AS STRING
|
|
DIM name$ AS STRING
|
|
|
|
' --- Assignments ---
|
|
x = 10
|
|
y = 20
|
|
pi = 3.14159
|
|
greeting = "Hello, World!"
|
|
name$ = "BASIC"
|
|
|
|
' --- PRINT with various separators ---
|
|
PRINT greeting
|
|
PRINT "x = "; x; " y = "; y
|
|
PRINT "PI is approximately "; pi
|
|
PRINT "Name: "; name$
|
|
|
|
' --- Arithmetic expressions ---
|
|
DIM result AS INTEGER
|
|
result = x + y * 2 - 5
|
|
PRINT "x + y * 2 - 5 = "; result
|
|
|
|
DIM quotient AS DOUBLE
|
|
quotient = x / 3
|
|
PRINT "x / 3 = "; quotient
|
|
|
|
DIM remainder AS INTEGER
|
|
remainder = y MOD 3
|
|
PRINT "y MOD 3 = "; remainder
|
|
|
|
' --- String operations ---
|
|
DIM full$ AS STRING
|
|
full$ = greeting + " from " + name$
|
|
PRINT full$
|
|
PRINT "Length of greeting: "; LEN(greeting)
|
|
PRINT "First 5 chars: "; LEFT$(greeting, 5)
|
|
PRINT "Last 6 chars: "; RIGHT$(greeting, 6)
|
|
PRINT "Middle: "; MID$(greeting, 3, 5)
|
|
PRINT "Upper: "; UCASE$(name$)
|
|
PRINT "Lower: "; LCASE$(greeting)
|
|
|
|
' --- IF / ELSEIF / ELSE ---
|
|
IF x > 15 THEN
|
|
PRINT "x is greater than 15"
|
|
ELSEIF x > 5 THEN
|
|
PRINT "x is between 6 and 15"
|
|
ELSE
|
|
PRINT "x is 5 or less"
|
|
END IF
|
|
|
|
' --- Single-line IF ---
|
|
IF y = 20 THEN PRINT "y is twenty"
|
|
|
|
' --- FOR loop ---
|
|
PRINT "Counting 1 to 5:"
|
|
DIM i AS INTEGER
|
|
FOR i = 1 TO 5
|
|
PRINT i;
|
|
NEXT i
|
|
PRINT ""
|
|
|
|
' --- WHILE loop ---
|
|
DIM count AS INTEGER
|
|
count = 5
|
|
PRINT "Countdown:"
|
|
WHILE count > 0
|
|
PRINT count;
|
|
count = count - 1
|
|
WEND
|
|
PRINT " Go!"
|
|
|
|
' --- DO LOOP WHILE (bottom test) ---
|
|
DIM n AS INTEGER
|
|
n = 1
|
|
PRINT "DO LOOP WHILE:"
|
|
DO
|
|
PRINT n;
|
|
n = n + 1
|
|
LOOP WHILE n <= 5
|
|
PRINT ""
|
|
|
|
' --- DO WHILE LOOP (top test) ---
|
|
n = 10
|
|
PRINT "DO WHILE LOOP:"
|
|
DO WHILE n > 5
|
|
PRINT n;
|
|
n = n - 1
|
|
LOOP
|
|
PRINT ""
|
|
|
|
' --- DO UNTIL ---
|
|
n = 1
|
|
PRINT "DO UNTIL:"
|
|
DO UNTIL n > 5
|
|
PRINT n;
|
|
n = n + 1
|
|
LOOP
|
|
PRINT ""
|
|
|
|
' --- Dynamic arrays ---
|
|
DIM arr(10) AS INTEGER
|
|
FOR i = 0 TO 10
|
|
arr(i) = i * i
|
|
NEXT i
|
|
PRINT "Array squares:"
|
|
FOR i = 0 TO 10
|
|
PRINT arr(i);
|
|
NEXT i
|
|
PRINT ""
|
|
|
|
' --- FUNCTION with BYVAL ---
|
|
FUNCTION Square(BYVAL n AS INTEGER) AS INTEGER
|
|
Square = n * n
|
|
END FUNCTION
|
|
|
|
FUNCTION Factorial(BYVAL n AS INTEGER) AS INTEGER
|
|
IF n <= 1 THEN
|
|
Factorial = 1
|
|
ELSE
|
|
Factorial = n * Factorial(n - 1)
|
|
END IF
|
|
END FUNCTION
|
|
|
|
PRINT "Square(7) = "; Square(7)
|
|
PRINT "Factorial(6) = "; Factorial(6)
|
|
|
|
' --- FUNCTION returning DOUBLE ---
|
|
FUNCTION CircleArea(BYVAL radius AS DOUBLE) AS DOUBLE
|
|
CircleArea = 3.14159 * radius * radius
|
|
END FUNCTION
|
|
|
|
PRINT "Area of circle r=5: "; CircleArea(5.0)
|
|
|
|
' --- SUB with BYREF (modifies caller's variables) ---
|
|
SUB Swap(BYREF a AS INTEGER, BYREF b AS INTEGER)
|
|
LOCAL temp AS INTEGER
|
|
temp = a
|
|
a = b
|
|
b = temp
|
|
END SUB
|
|
|
|
DIM p AS INTEGER
|
|
DIM q AS INTEGER
|
|
p = 100
|
|
q = 200
|
|
PRINT "Before swap: p="; p; " q="; q
|
|
CALL Swap(p, q)
|
|
PRINT "After swap: p="; p; " q="; q
|
|
|
|
' --- SUB with BYVAL ---
|
|
SUB ShowMessage(BYVAL msg AS STRING)
|
|
PRINT ">>> "; msg; " <<<"
|
|
END SUB
|
|
|
|
CALL ShowMessage("This is a test message")
|
|
|
|
' --- STATIC variable in a function ---
|
|
FUNCTION Counter() AS INTEGER
|
|
STATIC c AS INTEGER
|
|
c = c + 1
|
|
Counter = c
|
|
END FUNCTION
|
|
|
|
PRINT "Counter: "; Counter()
|
|
PRINT "Counter: "; Counter()
|
|
PRINT "Counter: "; Counter()
|
|
|
|
' --- Nested IF ---
|
|
DIM score AS INTEGER
|
|
score = 85
|
|
IF score >= 90 THEN
|
|
PRINT "Grade: A"
|
|
ELSEIF score >= 80 THEN
|
|
PRINT "Grade: B"
|
|
ELSEIF score >= 70 THEN
|
|
PRINT "Grade: C"
|
|
ELSE
|
|
PRINT "Grade: F"
|
|
END IF
|
|
|
|
' --- Boolean expressions ---
|
|
IF x > 5 AND y < 30 THEN
|
|
PRINT "x>5 AND y<30 is TRUE"
|
|
END IF
|
|
|
|
IF x > 100 OR y = 20 THEN
|
|
PRINT "x>100 OR y=20 is TRUE"
|
|
END IF
|
|
|
|
IF NOT (x = 5) THEN
|
|
PRINT "NOT (x=5) is TRUE"
|
|
END IF
|
|
|
|
PRINT "Done!"
|