Skip to content

Commit 19b4555

Browse files
committed
fbdoc: update examples/manual from wiki 2019-08-24
1 parent f323766 commit 19b4555

28 files changed

+731
-70
lines changed

examples/manual/check/FBWikiKeywordTemplate_1.bas

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/manual/datatype/pointer.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
44
'' be included in other distributions without authorization.
55
''
6-
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgPointer
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgPtr
77
'' --------
88

99
Dim p As ZString Pointer

examples/manual/debug/assert.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ End Sub
1414

1515
foo
1616

17-
'' If -g is used this code stops with: test.bas(3): assertion failed at FOO: a=1
17+
'' If -g or -eassert is used, this code stops with: test.bas(3): assertion failed at FOO: a=1
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'' examples/manual/proguide/arrays/passing.bas
2+
''
3+
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
4+
'' be included in other distributions without authorization.
5+
''
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgPassingArrays
7+
'' --------
8+
9+
Declare Sub splitString(ByVal As String, (Any) As String, ByVal As UByte = Asc(","))
10+
11+
12+
Dim As String s = "Programmer's Guide/Variables and Datatypes/Arrays/Passing Arrays to Procedures"
13+
Dim As String array(Any)
14+
15+
splitString(s, array(), Asc("/"))
16+
17+
Print "STRING TO SPLIT:"
18+
Print s
19+
Print
20+
Print "RESULT ARRAY FROM SPLITTING:"
21+
For i As Integer = LBound(array) To UBound(array)
22+
Print i, array(i)
23+
Next i
24+
25+
Sleep
26+
27+
28+
Sub splitString(ByVal source As String, destination(Any) As String, ByVal delimitor As UByte)
29+
Do
30+
Dim As Integer position = InStr(1, source, Chr(delimitor))
31+
ReDim Preserve destination(UBound(destination) + 1)
32+
If position = 0 Then
33+
destination(UBound(destination)) = source
34+
Exit Do
35+
End If
36+
destination(UBound(destination)) = Left(source, position - 1)
37+
source = Mid(source, position + 1)
38+
Loop
39+
End Sub
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'' examples/manual/proguide/iterators/fraction.bas
2+
''
3+
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
4+
'' be included in other distributions without authorization.
5+
''
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgTypeIterators
7+
'' --------
8+
9+
Type fraction
10+
' user interface
11+
Declare Constructor (ByVal n As Integer, ByVal d As Integer)
12+
Declare Operator Cast () As String
13+
' overload iteration operators when Step is defined in For...Next statement
14+
Declare Operator For (ByRef iterateStep As fraction)
15+
Declare Operator Next (ByRef iterateCondition As fraction, ByRef iterateStep As fraction) As Integer
16+
Declare Operator Step (ByRef step_var As fraction)
17+
' internal variables and cast operator
18+
As Integer num, den
19+
Declare Operator Cast () As Double
20+
End Type
21+
22+
Constructor fraction (ByVal n As Integer, ByVal d As Integer)
23+
this.num = n
24+
this.den = d
25+
End Constructor
26+
27+
Operator fraction.Cast () As String
28+
' search for the highest common factor (a) between numerator and denominator
29+
Dim As Integer a = Abs(This.num), b = Abs(This.den)
30+
If a <> 0 Then
31+
While a <> b
32+
If a > b Then
33+
a -= b
34+
Else
35+
b -= a
36+
End If
37+
Wend
38+
Else
39+
a = 1
40+
End If
41+
' reduce the fraction
42+
Return num \ a & "/" & den \ a
43+
End Operator
44+
45+
Operator fraction.Cast () As Double
46+
Return This.num / This.den
47+
End Operator
48+
49+
Operator fraction.For (ByRef iterateStep As fraction)
50+
' search for the least common multiple (a) between the two denominators
51+
Dim As Integer a = Abs(This.den), b = Abs(iterateStep.den), c = a, d = b
52+
While a <> b
53+
If a > b Then
54+
b += d
55+
Else
56+
a += c
57+
End If
58+
Wend
59+
' align at the same denominator the 2 fractions
60+
This.num *= a \ This.den
61+
This.den = a
62+
iterateStep.num *= a \ iterateStep.den
63+
iterateStep.den = a
64+
End Operator
65+
66+
Operator fraction.Next (ByRef iterateCondition As fraction, ByRef iterateStep As fraction) As Integer
67+
If iterateStep.num < 0 Or iterateStep.den < 0 Then
68+
Return This >= iterateCondition
69+
Else
70+
Return This <= iterateCondition
71+
End If
72+
End Operator
73+
74+
Operator fraction.Step (ByRef iterateStep As fraction)
75+
This.num += iterateStep.num
76+
End Operator
77+
78+
79+
Print "iteration from 1/8 to 1/2 by step of 1/12:"
80+
For iterator As fraction = fraction(1, 8) To fraction(1, 2) Step fraction(1, 12)
81+
Print " " & iterator;
82+
Next
83+
Print
84+
Print
85+
Print "iteration from 7/10 to -8/5 by step of -8/15:"
86+
For iterator As fraction = fraction(7, 10) To fraction(-8, 5) Step fraction(-8, 15)
87+
Print " " & iterator;
88+
Next
89+
Print
90+
91+
Sleep
92+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'' examples/manual/proguide/iterators/resolution.bas
2+
''
3+
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
4+
'' be included in other distributions without authorization.
5+
''
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgTypeIterators
7+
'' --------
8+
9+
Type screenResolution
10+
' user interface
11+
Declare Constructor (ByVal colorBit As Long)
12+
Declare Property colorDepth () As Long
13+
Declare Property screenWidth () As Long
14+
Declare Property screenHeigth () As Long
15+
' overload iteration operators when Step is not defined in For...Next statement
16+
Declare Operator For ()
17+
Declare Operator Next (ByRef iterateCondition As screenResolution) As Integer
18+
Declare Operator Step ()
19+
' internal variables
20+
Dim As Long colorBit, resolutionWH
21+
End Type
22+
23+
Constructor screenResolution (ByVal colorBit As Long)
24+
This.colorBit = colorBit
25+
End Constructor
26+
27+
Property screenResolution.colorDepth () As Long
28+
Return This.colorBit
29+
End Property
30+
31+
Property screenResolution.screenWidth () As Long
32+
Return HiWord(This.resolutionWH)
33+
End Property
34+
35+
Property screenResolution.screenHeigth () As Long
36+
Return LoWord(This.resolutionWH)
37+
End Property
38+
39+
Operator screenResolution.For ()
40+
This.resolutionWH = ScreenList(This.colorBit)
41+
End Operator
42+
43+
Operator screenResolution.Next (ByRef iterateCondition As screenResolution) As Integer
44+
While This.resolutionWH = 0
45+
If This.colorBit < iterateCondition.colorBit Then
46+
This.colorBit += 1
47+
This.resolutionWH = ScreenList(This.colorBit)
48+
Else
49+
Exit While
50+
End If
51+
Wend
52+
Return (This.resolutionWH <> iterateCondition.resolutionWH)
53+
End Operator
54+
55+
Operator screenResolution.Step ()
56+
This.resolutionWH = ScreenList()
57+
End Operator
58+
59+
60+
Print "Screen resolutions supported within [1 bpp , 64 bpp]:"
61+
For iterator As screenResolution = screenResolution(1) To screenResolution(64)
62+
Print " " & iterator.colorDepth & " bpp ",
63+
Print ":" & iterator.screenWidth & "x" & iterator.screenHeigth
64+
Next iterator
65+
Print "End of supported screen resolutions"
66+
67+
Sleep
68+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'' examples/manual/proguide/namespaces/access.bas
2+
''
3+
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
4+
'' be included in other distributions without authorization.
5+
''
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgNamespaces
7+
'' --------
8+
9+
Dim As Integer i ' Declare i in the global scope
10+
11+
Namespace A
12+
Dim As Integer i = 2 ' Declare i in Namespace A
13+
Dim As Integer j = 3 ' Declare j in Namespace A
14+
End Namespace
15+
16+
i = 1 ' Use i from global scope (.i)
17+
A.i = 4 ' Use i from Namespace A (A.i)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'' examples/manual/proguide/namespaces/conflict.bas
2+
''
3+
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
4+
'' be included in other distributions without authorization.
5+
''
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgNamespaces
7+
'' --------
8+
9+
Namespace A
10+
Dim As Integer i ' Declare A.i
11+
Dim As Integer j ' Declare A.j
12+
End Namespace
13+
14+
Namespace B
15+
Dim As Integer i ' Declare B.i
16+
Dim As Integer j ' Declare B.j
17+
Using A ' A.i/j and B.i/j are in conflict, but no error is given
18+
End Namespace
19+
20+
Dim As Integer j ' Declare also j the global scope
21+
22+
Using B
23+
'i = 1 ' error: Ambiguous symbol access, explicit scope resolution required (between B.i and A.i)
24+
B.i = 1 ' ambiguity resolution solved by using full name
25+
j = 2 ' ambiguity (between .j, B.j, A.j) solved by compiler, by choosing override .j in the global scope
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'' examples/manual/proguide/namespaces/extension1.bas
2+
''
3+
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
4+
'' be included in other distributions without authorization.
5+
''
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgNamespaces
7+
'' --------
8+
9+
Namespace A ' Declaration of Namespace A
10+
Dim As Integer i
11+
End Namespace
12+
13+
Namespace B ' Declaration of Namespace B
14+
Dim As Integer i
15+
End Namespace
16+
17+
Namespace A ' Extension of Namespace A
18+
Dim As Integer j
19+
End Namespace
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'' examples/manual/proguide/namespaces/extension2.bas
2+
''
3+
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
4+
'' be included in other distributions without authorization.
5+
''
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgNamespaces
7+
'' --------
8+
9+
Namespace A
10+
Dim As Integer i
11+
End Namespace
12+
13+
Using A
14+
15+
Namespace A
16+
Dim As Integer j
17+
End Namespace
18+
19+
i = 0 ' Initialize A.i
20+
j = 0 ' Initialize A.j

0 commit comments

Comments
 (0)