⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yabasic.htm

📁 一个小巧的BASIC解释器的源代码很小可它的确做到了
💻 HTM
📖 第 1 页 / 共 5 页
字号:
        face="Courier New"><strong>clear window</strong></font>-statement.        If you have your printer open (i.e. if you have issued        the <font size="2" face="Courier New"><strong>open        printer</strong></font>-command) <font size="2"        face="Courier New"><strong>clear window</strong></font>        finishes the current page, sends it to the printer and        starts a new one.<br>        <br>        The next command in the example is the <font        face="Courier New"><strong>text</strong></font>-statement,        which writes its text at the specified position. Aligned        with this position is the left lower corner of the text.        To change the alignement, you can add as a third argument        a two character string; The first one specifies the        horizontal alignement and can be <font face="Courier New"><strong>&quot;l&quot;</strong></font>        (text is left aligned), <font face="Courier New"><strong>&quot;r&quot;        </strong></font>(right aligned) or <font        face="Courier New"><strong>&quot;c&quot;</strong></font>        (centered); the second character specifies the vertical        alignement and can be <font face="Courier New"><strong>&quot;t&quot;</strong></font>        (top of text is aligned), <font face="Courier New"><strong>&quot;b&quot;</strong></font>        (bottom) or <font face="Courier New"><strong>&quot;c&quot;</strong></font>        (center). Some valid arguments would be <font        face="Courier New"><strong>&quot;ct&quot;</strong></font>,        <font face="Courier New"><strong>&quot;rb&quot;</strong></font>,        <font face="Courier New"><strong>&quot;lc&quot;</strong></font>,        ... By the way: Textalignement can also be changed by <a        href="#Peek and Poke"><font face="Courier New"><strong>poking</strong></font></a><font        face="Courier New"> </font>into <font face="Courier New"><strong>&quot;textalign&quot;</strong></font>.<br>        <br>        Finally <font face="Courier New"><strong>close window</strong></font>        closes the graphics-window.</dd>    <dt><a name="Getting a key from the keyboard"><strong>Getting        a key from the keyboard</strong></a></dt>    <dd>But before the window is closed, the <a        name="inkey$-statement"><font face="Courier New"><strong>inkey$</strong></font>-statement</a>        waits, until the user presses any key and returns this        key as a string. In this example the key, which is        actually pressed is not important, so you may just write <font        face="Courier New"><strong>inkey$</strong></font>        (without assignment). Some important nonprintable keys        (e.g. the function or cursor keys) are returned as        strings: <strong>up, down, left, right, f1, f2, f3, f4,        f5, f6, f7, f8, f9, f10, f11, f12, esc, ins, del, home,        end, scrnup, scrndown, enter, tab, backspace. </strong>If        your keyboard gives other keycodes than mine, or if you        press a key, which is unknown to yabasic, you will        receive a rather lengthy string (e.g. key1b5b31317e).<br>        Normally yabasic's <font face="Courier New"><strong>inkey$</strong></font>        waits until the user presses a key; but if you want the        inkey$-function to return even if no key has been        pressed, you may add a timeout (in seconds) argument.        E.g.<font face="Courier New"> <strong>inkey$(2)</strong></font>        returns immediately, if the user hits a key and after 2        seconds (returning an empty string) if not; note that a        timeout of 0 seconds is possible, which is the normal        behaviour of other basics.</dd>    <dt><a name="printing"><strong>Printing</strong></a></dt>    <dd>Getting a hardcopy of your graphics involves two new        commands:</dd></dl><blockquote>    <blockquote>        <pre><strong>open window 200,200open printercircle 100,100,80close printerclose window</strong></pre>    </blockquote></blockquote><dl>    <dd>Everything between <font face="Courier New"><strong>open        printer</strong></font> and <font face="Courier New"><strong>close        printer</strong></font> appears on paper. If you prefer        sending your hardcopy to a file, you may add a filename,        e.g. <font face="Courier New"><strong>open printer        &quot;foo&quot;</strong></font> sends the output to the        file <font face="Courier New"><strong>foo</strong></font>.        Note that the <font face="Courier New"><strong>open        printer</strong></font> statement has to appear after the        window has been opened. <font face="Courier New"><strong>close        printer</strong></font> can be omitted; it is done        automatically, if the window is closed.</dd></dl><p><a href="#Table of contents">Back to table of contents ...</a></p><hr><h2><a name="Plotting">Plotting</a></h2><p>Yabasics grafic is rather simple: It does not support color,nor animation; but it <strong>does</strong> support plotting.Have a look at the following program, which plots the sinefunction, complete with axis and errors:</p><blockquote>    <p><font size="2" face="Courier New"><strong>open window    600,400<br>    open printer<br>    </strong></font><a href="#window origin"><font size="2"    face="Courier New"><strong>window origin &quot;lb&quot;</strong></font></a><font    size="2" face="Courier New"><strong><br>    </strong></font><a href="#rectangle"><font size="2"    face="Courier New"><strong>rectangle 10,10 to 590,390</strong></font></a><font    size="2" face="Courier New"><strong><br>    <br>    </strong></font><a href="#map"><font size="2"    face="Courier New"><strong>map -pi,-1,pi,1 to 40,60,560,330</strong></font></a><font    size="2" face="Courier New"><strong><br>    arrow </strong></font><a href="#map"><font size="2"    face="Courier New"><strong>map(-3.2,0)</strong></font></a><font    size="2" face="Courier New"><strong> to map(3.2,0): rem this    draws the x-axis<br>    </strong></font><a href="#arrow"><font size="2"    face="Courier New"><strong>arrow map(0,-1.2) to map(0,1.2)</strong></font></a><font    size="2" face="Courier New"><strong>: rem this is the y-axis<br>    for x=-3 to 3<br>    if (x&lt;&gt;0) then </strong></font><a href="#xtick"><font    size="2" face="Courier New"><strong>xtick map(x,0),str$(x)</strong></font></a><font    size="2" face="Courier New"><strong> fi: rem these are ticks<br>    if (x&lt;3) then xtick map(x+0.5,0) fi<br>    next x<br>    </strong></font><a href="#map"><font size="2"    face="Courier New"><strong>ytick map(0,-1),&quot;-1&quot;</strong></font></a><font    size="2" face="Courier New"><strong><br>    ytick map(0,1),&quot;+1&quot;<br>    <br>    </strong></font><a href="#new line"><font size="2"    face="Courier New"><strong>new curve</strong></font></a><font    size="2" face="Courier New"><strong><br>    for x=-3 to 3 step 0.3: rem this loop actually draws the    function<br>    y=sin(x)<br>    </strong></font><a href="#new line"><font size="2"    face="Courier New"><strong>line to map(x,y)</strong></font></a><font    size="2" face="Courier New"><strong><br>    </strong></font><a href="#marker"><font size="2"    face="Courier New"><strong>marker map(x,y),&quot;dot&quot;</strong></font></a><font    size="2" face="Courier New"><strong>: rem mark the function    points with &quot;dots&quot;<br>    rem even plot some made-up errors<br>    marker map(x,y),&quot;err&quot;,ran(0.3),ran(0.5)<br>    next x<br>    <br>    map 0,0,100,4 to 400,250,590,320<br>    </strong></font><a href="#rectangle"><font size="2"    face="Courier New"><strong>box map(0,2) to map(90,4)</strong></font></a><font    size="2" face="Courier New"><strong>: rem draw the legend<br>    </strong></font><font color="#000000" size="2"    face="Courier New"><strong>marker</strong></font><font    size="2" face="Courier New"><strong>    map(10,3),&quot;dot&quot;<br>    marker map(10,3),&quot;err&quot;,0.5<br>    text map(20,3),&quot;Sine with errors&quot;,&quot;lc&quot;<br>    <br>    inkey$<br>    close printer<br>    close window</strong></font></p></blockquote><p>First thing you will notice: The program is rather long;plotting with yabasic still requires a lot of code, but at leastthere are some commands, which reduce the work; these areexplained below:</p><dl>    <dt><a name="window origin"><font size="2" face="Courier New"><strong>window        origin &quot;lb&quot;</strong></font></a></dt>    <dd>In normal cases the coordinate origin (i.e. the point        0,0) of any window lies in the upper left corner; the        command <font size="2" face="Courier New"><strong>window        origin</strong></font> can move this origin to any of the        four corners of a window. The string argument        (&quot;lb&quot; in the example) consists of two chars;        the first one can be &quot;l&quot; (for left) or        &quot;r&quot; (for right); the second char can be        &quot;t&quot; (for top) or &quot;b&quot; (for bottom).        This gives you a total of four variants &quot;lb&quot;,        &quot;lt&quot;, &quot;rb&quot; and &quot;rt&quot;, which        correspond with the four corners of a window.<br>        Once the origin has been moved, it applies to any grafic        operation whatsoever.</dd>    <dt><a name="rectangle"><font size="2" face="Courier New"><strong>rectangle        10,10 to 590,390</strong></font></a></dt>    <dd>This command simply draws a rectangle, defined by any two        opposite corners. Closely related with <font size="2"        face="Courier New"><strong>rectangle</strong></font> is        the <font size="2" face="Courier New"><strong>box</strong></font>-command,        which draws a rectangle too, but additionally clears its        interior.</dd>    <dt><a name="map"><font size="2" face="Courier New"><strong>map        -pi,-1,pi,1 to 40,60,560,330</strong></font></a><font        size="2" face="Courier New"><strong> </strong></font>and<font        size="2" face="Courier New"><strong> map(10,3)</strong></font></dt>    <dd>The <font size="2" face="Courier New"><strong>map</strong></font>-command        comes in two variants: The first one (<font size="2"        face="Courier New"><strong>map -pi,-1,pi,1 to        40,60,560,330</strong></font> in the example) defines a        mapping, the second one (<font size="2"        face="Courier New"><strong>map(10,3)</strong></font> in        the example) may be used <strong>at any point</strong>,        where a coordinate pair is required (e.g. you may write <font        size="2" face="Courier New"><strong>dot map(10,10)</strong></font>        instead of <font size="2" face="Courier New"><strong>dot        10,10</strong></font>). <font size="2" face="Courier New"><strong>map(x,y)</strong></font>        transforms a coordinate pair, depending on the previously        defined mapping; As an example: After <font size="2"        face="Courier New"><strong>map p1,q1,p2,q2 to x1,y1,x2,y2</strong></font>        the commands <font size="2" face="Courier New"><strong>dot        map(p1,q1)</strong></font> and <font size="2"        face="Courier New"><strong>dot x1,y1</strong></font>        would draw the same point; also <font size="2"        face="Courier New"><strong>map(p2,q2)</strong></font> and        <font size="2" face="Courier New"><strong>x2,y2</strong></font>        designate the same point. Any intermediate point would be        interpolated linearly.<br>        As can be seen in the example, the map-command is used to        map the x and y-range (-1 ... +1 and -pi ... +pi in the        example) of a function to the part of the window reserved        for the plot. This mapping saves a lot of trivial        calculations.<br>        Finally note, that there are two additional functions <font        size="2" face="Courier New"><strong>mapx()</strong></font>        and <font size="2" face="Courier New"><strong>mapy()</strong></font>,        both with a single argument, which transform just one        coordinate.</dd>    <dt><a name="arrow"><font size="2" face="Courier New"><strong>arrow        map(0,-1.2) to map(0,1.2)</strong></font></a></dt>    <dd>The <font size="2" face="Courier New"><strong>arrow</strong></font>-command        draws an arrow between the specified points; the example        employs the <font size="2" face="Courier New"><strong>map(,)</strong></font>-function,        but you could just as well specify the coordiantes        explicitly (e.g. <font size="2" face="Courier New"><strong>arrrow        10,10 to 100,100</strong></font>).</dd>    <dt><a name="xtick"><font size="2" face="Courier New"><strong>xtick        map(x,0),str$(x)</strong></font></a><font size="2"        face="Courier New"><strong> </strong></font>and<font        size="2" face="Courier New"><strong> ytick        map(0,-1),&quot;-1&quot;</strong></font></dt>    <dd>The xtick (and ytick) function simply draw a tick at the        specified location (e.g. <font size="2"        face="Courier New"><strong>map(x,0)</strong></font> or <font        size="2" face="Courier New"><strong>100,100</strong></font>);        if you add a string as a third argument (<font size="2"        face="Courier New"><strong>str$(x)</strong></font> in the        example), the tick is drawn somewhat longer and the        string is written near its end.</dd>    <dt><a name="new line"><font size="2" face="Courier New"><strong>new        </strong></font></a><font size="2" face="Courier New"><strong>curve        </strong></font>and<font size="2" face="Courier New"><strong>        line to map(x,y)</strong></font></dt>    <dd>These functions help to plot a curve as a sequence of        many lines. Let's look at an example: <font size="2"        face="Courier New"><strong>line to 10,10:line to 100,100</strong></font>;        the <strong>second </strong><font size="2"        face="Courier New"><strong>line to</strong></font>-command        just draws a line from the point specified in the first <font        size="2" face="Courier New"><strong>line to</strong></font>-command        (i.e. 10,10) to the point specified in the command itself        (i.e. 100,100); to add more line segments, you just have        to specify further <font size="2" face="Courier New"><strong>line        to</strong></font>-commands. If you want to start with a        new curve (i.e. a new sequence of lines) just issue the <font        size="2" face="Courier New"><strong>new curve

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -