44
55Compile and run the given source. Source can be a file name, a line of code or an array of code. Use ENV to share variables with the parent process.
66
7+ Example 1: Using constants; Note: ` \" `  is used to create a quote inside the string
78
8- ~~~ 
9+ ``` 
10+ Chain "? \"100 + 50 is: \"; 100 + 50"  
911
10- ' Note: CHAIN behaves like EVAL function in other languages. 
11- Option Base 1 ' (for 'IN' keyword which is 1-Based) 
12- ' Code using constants: 
13- Chain "? \\"100 + 50 is: \\"; 100 + 50: Pause" ' (100 + 50 = 150) 
12+ ' Output: 100 + 50 is: 150 
13+ ``` 
1414
15- ' Code using variables: 
16- Env "SB1=6" ' add two unique variables (for reuse) 
15+ Example 2: Using variables
16+ 
17+ ``` 
18+ Env "SB1=6" 
1719Env "SB2=2" 
18- Chain "? Env(\\"SB1\\") ^ Env(\\"SB2\\"): Pause" ' (SB1 ^ SB2 = 36) 
20+ Chain "? Env(\"SB1\") ^ Env(\"SB2\")"  
21+ 
22+ ' Output: 36 
23+ ``` 
24+ 
25+ Example 3: Using an array
1926
20- ' Code using an array: 
21- Env "SB1=3"  ' reuse SB1 variable 
22- Dim a()          ' append code to array a 
23- a << "x = Env(\\"SB1\\")" 
27+ ``` 
28+ Env "SB1=3" 
29+    
30+ Dim a()           
31+ a << "x = Env(\"SB1\")" 
2432a << "For i = 1 To 5" 
2533a << "? i * x; Spc(1);" 
2634a << "Next i" 
27- a << "Pause" 
28- Chain a ' prints 3 6 9 12 15 
29- 
30- ' Code using a file name (output a array to demo file): 
31- Const FILE_NAME = "demo.bas" 
32- Env "SB1=4"  ' reuse SB1 variable 
33- Open FILE_NAME For Output as #1 
34- For i In a 
35-   ? #1, i; ":"; ' output all code as a single string, ":"; 
36- Next i 
37- Close #1 
38- ? 
39- Chain FILE_NAME ' prints 4 8 12 16 20 
4035
41- ' Now append to file name a return value (on the same line): 
42- Env "SB1=5"  ' reuse SB1 variable 
43- Open FILE_NAME For Append As #1 
44- ? #1, "Env \\"SB1=\\" + Str(i):"; ' add extra space or ":" 
45- Close #1 
46- ? 
47- Chain FILE_NAME ' prints 5 10 15 20 25 
48- Color 15 ' print the return value from file 
49- ? " (Return value SB1 is: "; Env("SB1"); ")" ' (i is 6) 
50- Pause 
36+ Chain a  
37+ 
38+ ' Output: 3 6 9 12 15 
39+ ``` 
40+ 
41+ Example 4: Using a file and returning a value
42+ 
43+ ``` 
44+ ' First we have to create a bas-file to show how chain works with files 
5145
52- ~~~ 
46+ ' Create an array 
47+ Env "SB1=4"   
5348
49+ Dim a()       
50+ a << "x = Env(\"SB1\")" 
51+ a << "For i = 1 To 5" 
52+ a << "? i * x; Spc(1);" 
53+ a << "Next i" 
54+ a << "Env \"SB1=\" + Str(i):"    ' Return value using SB1 
55+ 
56+ ' Write array to file 
57+ tsave("chaindemo.bas", a) 
58+ 
59+ ' Peparations are done. Now a bas-file can be chained 
60+ Chain "chaindemo.bas"  
5461
55- ~~~ 
62+ print 
63+ print "Return value SB1 is: "; Env("SB1");  
5664
57- Const FILENAME = "demo.bas" 
65+ ' Output:  
66+ ' 4 8 12 16 20 
67+ ' Return value SB1 is: 6 
68+ ``` 
69+ 
70+ Example 5:
71+ 
72+ ``` 
5873' Create demo bas file (could be any SmallBASIC file): 
59- Open FILENAME  For Output As #1 
74+ Open "chaindemo.bas"  For Output As #1 
6075? #1, "Sub count10(n)" 
6176? #1, "  Local i" 
6277? #1, "  Color 14" 
@@ -74,41 +89,38 @@ Open FILENAME For Output As #1
7489? #1,  
7590? #1, "?:?" 
7691Close #1 
92+ 
7793' Load demo bas file into array: 
78- Tload FILENAME , lines 
94+ Tload "chaindemo.bas" , lines 
7995Env "SB1=2" ' Set value for child program (1..10) 
8096' Execute the demo bas file (the array): 
8197Chain lines 
8298' now print the return value from child program: 
8399Color 7:  ? "I'm The Parent Program..." 
84100? 
85101Color 15: ? "Child program returned value: "; Env("SB1") 
86- Pause 
87- 
88- ~~~ 
89- 
102+ ``` 
90103
91- ~~~ 
104+ Example 6: Creating an eval function 
92105
106+ ``` 
93107' Dedicated to MGA. 
94108' s is any legal SmallBASIC Math Expression as String, e.g. "1 + 2 / 4" 
95- Func EVAL(s) 
96-   ' It takes 2 lines of SmallBASIC code to implement Math EVAL Function: 
109+ Func eval(s) 
97110  Chain "Env " + Enclose("SBEVAL=") + " + Str(" + s + ")" 
98111  eval = Val(Env("SBEVAL")) 
99112End Func 
100113
101114' now run few demos: 
102- ? eval("1+2") ' prints 3, ...  
115+ ? eval("1+2")  
103116? eval("Rad(45) * 2")  
104117? eval("PI / 2 + PI") 
105118? eval("0b1111 * Pow(2, 4)") 
106119? eval("Sin(2) * Tan(4) / Cos(6)") 
107120? eval("1 + 2 / 4") 
108121? eval("6 * (Pow(2, 4) * 8)") 
109122? eval("Rad((45 * 3) - 20) * 2") 
110- Pause 
123+ ``` 
111124
112- ~~~ 
113125
114126
0 commit comments