Skip to content

Commit 9ee412a

Browse files
committed
reading and writing binary, byte type, and printing file example
1 parent df19535 commit 9ee412a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Sack is a dynamically typed scripting language for beginner programmers that foc
2222
- Identifier
2323
- List (Itterable Scope)
2424
- file (File object)
25+
- byte (8 bit integer)
2526
- Sack enforces a style guide to improve readability, it is left up to the implementation whether this is a default warn or error. For simplicity, the style guide will not be described in this spec, however the example code will follow it.
2627

2728
## Syntax
@@ -112,6 +113,13 @@ if a > b {
112113
}
113114
```
114115

116+
- Byte is an 8 bit integer, and is used for binary data.
117+
The following are valid bytes:
118+
```
119+
let a = 0b00000000;
120+
let b = 0b11111111;
121+
```
122+
115123
### Lists
116124
Lists are self contained scope blocks which are itterable. See the scope section for rules on scope.
117125

@@ -180,6 +188,8 @@ The first argument is the file path, and the second is the mode. The mode can be
180188
- `r` - Read
181189
- `w` - Write
182190
- `a` - Append
191+
- `rb` - Read Binary
192+
- `wb` - Write Binary
183193

184194
Files can be read using the `read` keyword, and written to using the `write` keyword.
185195

@@ -206,6 +216,26 @@ flush ( file );
206216
close ( file );
207217
```
208218

219+
Files can also be read and written to using binary data. This is done using the `read` and `write` keywords with the file opened in binary mode. Reading and writing binary data is done using the `byte` type. In this case the file object will be a list of bytes.
220+
```
221+
let file = open ( "file.txt", "r" );
222+
let data = read ( file );
223+
224+
# data is now a list of bytes
225+
print ( data );
226+
227+
close ( file );
228+
```
229+
230+
File data can also be printed to the console. The following code will print the contents of a file to the console:
231+
```
232+
# file.txt contains the text "hello world"
233+
let file = open ( "file.txt", "r" );
234+
# prints "hello world"
235+
print ( file );
236+
close ( file );
237+
```
238+
209239
### Functions
210240

211241
Functions are declared using either the `func` or `functi` keywords followed by the name of the function.

0 commit comments

Comments
 (0)