Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3ae298

Browse files
authored
Merge pull request smallbasic#42 from Joe7M/master
Update manual
2 parents ac43210 + 6d2f61d commit e3ae298

File tree

2 files changed

+294
-33
lines changed

2 files changed

+294
-33
lines changed

_build/pages/guide.markdown

Lines changed: 140 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,22 @@ Contents
4646
* [Key-Value Pairs with References](#KeyValuePairsWithReferences)
4747
* [Maps as Pseudo Objects for OOP](#MapsAsPseudoObjectForOOP)
4848
* [Operators](#Operators)
49-
* [Pseudo-operators](#pseudo)
49+
* [Supported Operators](#SupportedOperators)
50+
* [Pseudo-Operators](#PseudoOperators)
5051
* [Expressions](#Expressions)
5152
* [Subroutines and Functions](#SubroutinesAndFunctions)
5253
* [Names](#SubroutinesAndFunctionsNames)
5354
* [Declaration of Subroutines](#DeclarationOfSubroutines)
5455
* [Declaration of Functions](#DeclarationOfFunctions)
5556
* [Parameters](#Parameters)
5657
* [Exit a function or subroutine](#ExitAFunctionOrSubroutine)
57-
* [Single-line Functions](#SingleLineFunctions)
58+
* [Single-Line Functions](#SingleLineFunctions)
5859
* [Using Local Variables](#UsingLocalVariables)
5960
* [Nested Routines](#NestedRoutines)
6061
* [Declarations in PalmOS](#DeclarationsInPalmOS)
6162
* [Conditions](#Conditions)
6263
* [IF-THEN-ELSIF-ELSE](#IfThenElseifEndif)
63-
* [Single-line IF-THEN-ELSE](#SingleLineIfThenElse)
64+
* [Single-Line IF-THEN-ELSE](#SingleLineIfThenElse)
6465
* [Inline Version of IF](#InlineVersionOfIf)
6566
* [SELECT CASE](#SelectCase)
6667
* [Loops](#Loops)
@@ -76,6 +77,10 @@ Contents
7677
* [Print on Screen](#PrintOnScreen)
7778
* [Read Input from the Keyboard](#ReadInputFromKeyboard)
7879
* [Draw Graphics on Screen](#DrawGraphicsOnScreen)
80+
* [Basic File Read and Write](#BasicFileReadAndWrite)
81+
* [Save Arrays and Maps to a File](#SaveArraysAndMapsToAFile)
82+
* [Save Variables to a File](#SaveVariablesToAFile)
83+
* [Binary Files](#BinaryFiles)
7984
* [The USE Keyword](#TheUseKeyword)
8085
* [OPTION](#Statement1)
8186
* [OPTION BASE](#Statement2)
@@ -881,6 +886,8 @@ end
881886

882887
## Operators {#Operators}
883888

889+
### Supported Operators {#SupportedOperators}
890+
884891
SmallBASIC supports the following operators, sorted by priority:
885892

886893
| Operator | Description |
@@ -912,7 +919,7 @@ SmallBASIC supports the following operators, sorted by priority:
912919
| `NOR` | bitwise NOR |
913920
| `XNOR` | bitwise XNOR |
914921

915-
### Pseudo-operators {#pseudo}
922+
### Pseudo-Operators {#PseudoOperators}
916923

917924
These operators are replaced by the compiler with a command or an expression.
918925

@@ -1125,7 +1132,7 @@ the return value of the function will be `0`. All commands following
11251132
Subroutines can be exited using the keywords `EXIT SUB`. All commands following
11261133
`EXIT SUB` will not be executed.
11271134

1128-
### Single-line Functions {#SingleLineFunctions}
1135+
### Single-Line Functions {#SingleLineFunctions}
11291136

11301137
```
11311138
FUNC name(par1, ..., parN) = expression
@@ -1381,7 +1388,7 @@ be nested.
13811388
Instead of `ELSEIF` and `ENDIF`, `ELIF` and `FI` can be used. Instead of `THEN`,
13821389
`DO` can be used, but this is not suggested.
13831390

1384-
### Single-line IF-THEN-ELSE {#SingleLineIfThenElse}
1391+
### Single-Line IF-THEN-ELSE {#SingleLineIfThenElse}
13851392

13861393
```smallbasic
13871394
IF expression THEN command1 ELSE command2
@@ -1593,7 +1600,7 @@ RECT 0,0,50,50 ' draw a rectangle
15931600
To specify “world” coordinates for the screen use the command `WINDOW(x1,x2,y2,y1)`. `WINDOW`
15941601
allows you to redefine the corners of the display screen as a pair of “world” coordinates. The
15951602
coordinates of the upper-left corner of the screen is given by [x1, y1], the lower-left corner
1596-
by [x2, y2].The world space defined by WINDOW is disabled by a WINDOW command without parameters.
1603+
by [x2, y2]. The world space defined by WINDOW is disabled by a WINDOW command without parameters.
15971604

15981605
`VIEW(x1,y1,x2,y2)` defines a viewport with starting point (upper left corner)) [x1,y1] and end
15991606
point (lower right corner) [x2,y2]. Drawing outside the viewport is not possible.
@@ -1615,6 +1622,132 @@ w.setSize(800, 680) ' Set window size
16151622

16161623
For more information about graphics commands please see [Graphics](/pages/graphics.html).
16171624

1625+
## Basic File Read and Write {#BasicFileReadAndWrite}
1626+
1627+
SmallBASIC supports various commands to read and write a file. Best suited for beginners are `PRINT` and `INPUT`. `PRINT` writes to a file in the same way as described in the section "Print on Screen". `INPUT` without additional parameters reads one line from a file. To read or write a file, the file needs first to be opened. This is done using the command `OPEN`:
1628+
1629+
```smallbasic
1630+
OPEN file for INPUT|OUTPUT|APPEND as #1
1631+
```
1632+
1633+
Using the command `OPEN`, a file with the file name `file` can be opened for reading (`INPUT`), for writing (`OUTPUT`), and writing to an existing file (`APPEND`). If you use the parameter `OUTPUT` and the file already exist, the file will be deleted. `#1` is a ID number you can chose freely, but it must start with `#`
1634+
1635+
To write to a file, `PRINT` can be used:
1636+
1637+
```smallbasic
1638+
PRINT #1, "Hello world"
1639+
```
1640+
1641+
`PRINT` has the same syntax as the print command in section "Print on Screen". To tell `PRINT`, that the output should be written to a file, use the file ID number as the first parameter.
1642+
1643+
With `INPUT`, data can be read from a file:
1644+
1645+
```smallbasic
1646+
INPUT #1, s
1647+
```
1648+
1649+
`INPUT` without additional parameters reads a line from file with the file ID `#1` into the variable `s`.
1650+
1651+
```smallbasic
1652+
IF EOF(1) THEN PRINT "End of file reached"
1653+
```
1654+
1655+
`EOF(FileID)` returns `1` if the end of the file is reached and no more data can be read from file. Please note, that in case of `EOF` the File ID is given without `#`.
1656+
1657+
To close a file, use `CLOSE #1`.
1658+
1659+
The following example writes the numbers 1 to 10 to a file and reads these numbers from a file and prints them on screen.
1660+
1661+
```smallbasic
1662+
' create a text file
1663+
OPEN "MyDemoFile.txt" FOR OUTPUT AS #1
1664+
1665+
FOR i = 1 TO 10
1666+
PRINT #1, i
1667+
NEXT
1668+
1669+
CLOSE #1
1670+
1671+
' open text file and print content line by line
1672+
OPEN "MyDemoFile.txt" FOR INPUT AS #1
1673+
1674+
WHILE(!EOF(1)) ' eof works only without #
1675+
INPUT #1, c
1676+
PRINT c
1677+
WEND
1678+
1679+
CLOSE #1
1680+
```
1681+
1682+
### Save Arrays and Maps to a File {#SaveArraysAndMapsToAFile}
1683+
1684+
`TLOAD` and `TSAVE` provide a simple way to store arrays, maps or strings in a file and read them. Using a map, json data can be created and written to a file.
1685+
1686+
```smallbasic
1687+
' Create an array with some data
1688+
A << 1
1689+
A << "test"
1690+
A << 2
1691+
PRINT A ' Output: [1,test,2]
1692+
1693+
' Save the array. This will create the file myfile.txt in
1694+
' the same directory as your BASIC file
1695+
TSAVE "myfile.txt", A
1696+
1697+
' Load the file
1698+
TLOAD "myfile.txt", B
1699+
PRINT B ' Output: [1,test,2,]
1700+
```
1701+
1702+
### Save Variables to a File {#SaveVariablesToAFile}
1703+
1704+
With `READ` and `WRITE` numbers, strings and arrays can be read and written. The resulting file is a binary file. Viewing this file in a text editor will not show you anymore the context in a human readable form.
1705+
1706+
```smallbasic
1707+
a = 12.3
1708+
b = "test"
1709+
c = [1,2,3,4]
1710+
1711+
' Write variables to file
1712+
OPEN "text.bin" FOR OUTPUT AS #1
1713+
WRITE #1, a, b, c
1714+
CLOSE #1
1715+
1716+
' Read variables from file
1717+
OPEN "text.bin" FOR INPUT AS #1
1718+
READ #1, a, b, c
1719+
CLOSE #1
1720+
1721+
PRINT a
1722+
PRINT b
1723+
PRINT c
1724+
```
1725+
1726+
### Binary Files {#BinaryFiles}
1727+
1728+
`BGETC` and `BPUTC` can be used to read and write a byte from/to a binary file.
1729+
1730+
```smallbasic
1731+
' create a binary file
1732+
OPEN "BinaryFile.txt" for output as #1
1733+
1734+
FOR c = 0 TO 255
1735+
BPUTC #1, c
1736+
NEXT
1737+
1738+
CLOSE #1
1739+
1740+
' open binary file and print content
1741+
OPEN "BinaryFile.txt" FOR INPUT AS #1
1742+
1743+
FOR i = 0 TO 255
1744+
c = BGETC(1)
1745+
PRINT CHR(c);
1746+
NEXT
1747+
1748+
CLOSE #1
1749+
```
1750+
16181751
## OPTION {#Statement1}
16191752

16201753
The `OPTION` command is used to pass parameters to the SB-environment. There are

0 commit comments

Comments
 (0)