-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1439.html
More file actions
203 lines (203 loc) · 18.3 KB
/
Copy path1439.html
File metadata and controls
203 lines (203 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SmallBASIC | CHAIN</title>
<meta name="description" content="Compile 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.">
<link rel="canonical" href="1439">
<link rel="keywords" href="CHAIN source">
<link rel="stylesheet" href="/css/style.css">
<link rel="icon" type="image/png" href="/images/sb-desktop-32x32.png">
<script src="/clipboard.js"></script>
</head>
<body>
<button onclick="topFunction()" id="BackToTopBtn" title="Go to top">⯅</button>
<script src="/backtotop.js"></script>
<div class="wrapAll clearfix">
<nav class="navigation">
<div class="logo">
<a href="/"><img src='/images/sb-logo.png?v=2' alt="logo"></a>
</div>
<div class="navlinks">
<a href="/pages/download.html">Download</a>
<a href="/pages/news.html">News</a>
<a href="/pages/community.html">Community</a>
<a href="/pages/articles.html">Resources</a>
<a class='active' href="/pages/reference.html">Language Reference</a>
<a href="/pages/guide.html">SmallBASIC Manual</a>
</div>
</nav>
<div class="mainsection">
<div class="tabs clearfix">
<div class="tabsRight">
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/blob/master/_build/reference/1439-system-chain.markdown">Edit</a>
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/commits/master/_build/reference/1439-system-chain.markdown">History</a>
</div>
</div>
<div class="article">
<h1>CHAIN</h1>
<blockquote>CHAIN source</blockquote>
<div class="siteSub">
<p>
<a href="/">Home</a> >
<a href="/pages/reference.html">Reference</a> >
<a href="/pages/system.html">System</a>
</p>
</div>
<p>Compile and run the given source. <code>source</code> can be a file
name, a line of code or an array of code. Use ENV to share variables
with the parent process.</p>
<h3 id="example-1-using-constants">Example 1: Using constants</h3>
<p>Note: <code>\"</code> is used to create a quote inside the string</p>
<div class="sourceCode" id="cb1"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Chain</span> <span class="st">"? \"</span><span class="dv">100</span> + <span class="dv">50</span> is: \<span class="st">"; 100 + 50"</span> </span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co">' Output: 100 + 50 is: 150</span></span></code></pre></div>
<h3 id="example-2-using-variables">Example 2: Using variables</h3>
<div class="sourceCode" id="cb2"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Env</span> <span class="st">"SB1=6"</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">Env</span> <span class="st">"SB2=2"</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="fu">Chain</span> <span class="st">"? Env(\"</span>SB1\<span class="st">") ^ Env(\"</span>SB2\<span class="st">")"</span> </span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="co">' Output: 36</span></span></code></pre></div>
<h3 id="example-3-using-an-array">Example 3: Using an array</h3>
<div class="sourceCode" id="cb3"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Env</span> <span class="st">"SB1=3"</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="dt">Dim</span> a() </span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"x = Env(\"</span>SB1\<span class="st">")"</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"For i = 1 To 5"</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"? i * x; Spc(1);"</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"Next i"</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="fu">Chain</span> a </span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a><span class="co">' Output: 3 6 9 12 15</span></span></code></pre></div>
<h3 id="example-4-using-a-file-and-returning-a-value">Example 4: Using a
file and returning a value</h3>
<div class="sourceCode" id="cb4"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co">' First we have to create a bas-file to show how chain works with files</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co">' Create an array</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="fu">Env</span> <span class="st">"SB1=4"</span> </span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="dt">Dim</span> a() </span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"x = Env(\"</span>SB1\<span class="st">")"</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"For i = 1 To 5"</span></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"? i * x; Spc(1);"</span></span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"Next i"</span></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"Env \"</span>SB1=\<span class="st">" + Str(i):"</span> <span class="co">' Return value using SB1</span></span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a><span class="co">' Write array to file</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a><span class="fu">tsave</span>(<span class="st">"chaindemo.bas"</span>, a)</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a><span class="co">' Peparations are done. Now a bas-file can be chained</span></span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a><span class="fu">Chain</span> <span class="st">"chaindemo.bas"</span> </span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span></span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> <span class="st">"Return value SB1 is: "</span>; <span class="fu">Env</span>(<span class="st">"SB1"</span>); </span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a><span class="co">' Output: </span></span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a><span class="co">' 4 8 12 16 20</span></span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a><span class="co">' Return value SB1 is: 6</span></span></code></pre></div>
<h3 id="example-5">Example 5:</h3>
<div class="sourceCode" id="cb5"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co">' Create demo bas file (could be any SmallBASIC file):</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="fu">Open</span> <span class="st">"chaindemo.bas"</span> <span class="kw">For Output As</span> #<span class="dv">1</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">"Sub count10(n)"</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">" Local i"</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">" Color 14"</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">" For i = n To 10</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">" Print i; "</span> + <span class="fu">Enclose</span>(<span class="st">", "</span>) + <span class="st">";"</span> </span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">" Next i"</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">" Env "</span> + <span class="fu">Enclose</span>(<span class="st">"SB1="</span>) + <span class="st">" + Str(i) ' return value to parent</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">"End Sub"</span></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, </span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">"Color 7: ? "</span> + <span class="fu">Enclose</span>(<span class="st">"I'm The Child Program...! I can count!"</span>) + <span class="st">": ?"</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, </span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">"n = Val(Env("</span> + <span class="fu">Enclose</span>(<span class="st">"SB1"</span>) + <span class="st">")) ' get value from parent"</span></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, </span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">"count10 n ' run sub-routine to count n to 10"</span></span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, </span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a>? #<span class="dv">1</span>, <span class="st">"?:?"</span></span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a><span class="fu">Close</span> #<span class="dv">1</span></span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a><span class="co">' Load demo bas file into array:</span></span>
<span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a><span class="fu">Tload</span> <span class="st">"chaindemo.bas"</span>, lines</span>
<span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a><span class="fu">Env</span> <span class="st">"SB1=2"</span> <span class="co">' Set value for child program (1..10)</span></span>
<span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a><span class="co">' Execute the demo bas file (the array):</span></span>
<span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a><span class="fu">Chain</span> lines</span>
<span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a><span class="co">' now print the return value from child program:</span></span>
<span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a><span class="fu">Color</span> <span class="dv">7</span>: ? <span class="st">"I'm The Parent Program..."</span></span>
<span id="cb5-28"><a href="#cb5-28" aria-hidden="true" tabindex="-1"></a>?</span>
<span id="cb5-29"><a href="#cb5-29" aria-hidden="true" tabindex="-1"></a><span class="fu">Color</span> <span class="dv">15</span>: ? <span class="st">"Child program returned value: "</span>; <span class="fu">Env</span>(<span class="st">"SB1"</span>)</span></code></pre></div>
<h3 id="example-6-creating-an-eval-function">Example 6: Creating an eval
function</h3>
<div class="sourceCode" id="cb6"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co">' Dedicated to MGA.</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="co">' s is any legal SmallBASIC Math Expression as String, e.g. "1 + 2 / 4"</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="kw">Func </span>eval(s)</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">Chain</span> <span class="st">"Env "</span> + <span class="fu">Enclose</span>(<span class="st">"SBEVAL="</span>) + <span class="st">" + Str("</span> + s + <span class="st">")"</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> eval = <span class="fu">Val</span>(<span class="fu">Env</span>(<span class="st">"SBEVAL"</span>))</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="kw">End Func</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="co">' now run few demos:</span></span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a>? eval(<span class="st">"1+2"</span>) </span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a>? eval(<span class="st">"Rad(45) * 2"</span>) </span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a>? eval(<span class="st">"PI / 2 + PI"</span>)</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a>? eval(<span class="st">"0b1111 * Pow(2, 4)"</span>)</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a>? eval(<span class="st">"Sin(2) * Tan(4) / Cos(6)"</span>)</span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a>? eval(<span class="st">"1 + 2 / 4"</span>)</span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a>? eval(<span class="st">"6 * (Pow(2, 4) * 8)"</span>)</span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a>? eval(<span class="st">"Rad((45 * 3) - 20) * 2"</span>)</span></code></pre></div>
<div class="lavenderBox">
<div class="header">Code samples using CHAIN</div>
<div class="linklist">
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/pirate.bas">pirate.bas </a>
</div>
</div>
<div class="lavenderBox">
<div class="header">System</div>
<div class="linklist">
<a href="/reference/1439.html"><strong>CHAIN</strong> </a>
<a href="/reference/1521.html">COMMAND </a>
<a href="/reference/1522.html">CWD </a>
<a href="/reference/806.html">DELAY </a>
<a href="/reference/807.html">ENV </a>
<a href="/reference/815.html">ENV </a>
<a href="/reference/1443.html">EXEC </a>
<a href="/reference/1440.html">EXPORT </a>
<a href="/reference/606.html">FRE </a>
<a href="/reference/1525.html">HOME </a>
<a href="/reference/1441.html">IMPORT </a>
<a href="/reference/1462.html">INCLUDE </a>
<a href="/reference/1736.html">MAXINT </a>
<a href="/reference/1735.html">NIL </a>
<a href="/reference/1442.html">OPTION </a>
<a href="/reference/809.html">PAUSE </a>
<a href="/reference/1524.html">PI </a>
<a href="/reference/817.html">PROGLINE </a>
<a href="/reference/810.html">RANDOMIZE </a>
<a href="/reference/818.html">RUN </a>
<a href="/reference/1523.html">SBVER </a>
<a href="/reference/1734.html">SELF </a>
<a href="/reference/812.html">STKDUMP </a>
<a href="/reference/813.html">TROFF </a>
<a href="/reference/814.html">TRON </a>
<a href="/reference/1446.html">UNIT </a>
</div>
</div>
</div>
<div class="pagefooter">
This page was last edited on Tue, 19 Sep 2023 16:02:02 +0200
|
<a href="https://en.wikipedia.org/wiki/Markdown" target="_blank" rel="nofollow">Markdown</a>
processed with
<a href="https://pandoc.org/MANUAL.html#pandocs-markdown" target="_blank" rel="nofollow">pandoc 3.1.12.1</a>
</div>
</div>
</div>
</body>
</html>