Skip to content

Commit 7f88f30

Browse files
committed
Documentation edits to improve clarity.
1 parent b56f3e7 commit 7f88f30

1 file changed

Lines changed: 70 additions & 61 deletions

File tree

compiler/USAGE.md

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Basic Usage
2121
===========
2222

2323
For simple compilation of BAS files to XEX (Atari DOS executable), call the
24-
`fastbasic` program, passing the basic source file as the first parameter.
24+
`fastbasic` program, passing the BASIC source file as the first parameter.
2525

2626
- On Linux:
2727

@@ -31,10 +31,11 @@ For simple compilation of BAS files to XEX (Atari DOS executable), call the
3131

3232
C:\path\to\fastbasic myprog.bas
3333

34-
35-
The compiler supports multiple possible targets, with different capabilities,
36-
you can select between the target with the `-t:` compiler option, between the
37-
following:
34+
Compiler targets
35+
----------------
36+
The compiler supports multiple possible targets, with different capabilities.
37+
You can select the target with the `-t:` compiler option, from the following
38+
list:
3839

3940
- `atari-fp`: Compile to an Atari 8-bit executable (`.xex`) with floating-point
4041
support. This is the default if no other target is selected.
@@ -57,6 +58,8 @@ This example produces a cartridge image for the Atari 8-bit computers:
5758

5859
fastbasic -t:atari-cart-fp myprog.bas
5960

61+
Output files
62+
------------
6063

6164
The compiler generates various files from the basic source:
6265

@@ -83,6 +86,9 @@ compilation, except on errors or when the `-keep` options is enabled:
8386
- `ASM` file, the assembly source. Each FastBasic source file produces one
8487
assembly file with the compiled program.
8588

89+
Advanced Usage
90+
==============
91+
8692
The compilation is a three step process:
8793

8894
- The compiler reads the basic source and produces an assembly file.
@@ -96,10 +102,6 @@ The compilation is a three step process:
96102
searches in the installation path and in the path in the `FASTBASIC_HOME`
97103
environment variable.
98104

99-
100-
Advanced Usage
101-
==============
102-
103105
Passing options to the compiler
104106
-------------------------------
105107

@@ -117,20 +119,20 @@ an `:` or an `=` to separate the option from the argument.
117119
Shows the compiler version.
118120

119121
- **-n**
120-
Disable all the optimizations, the produced code will be the same as the
121-
native IDE. This is useful to debug problems with the optimizations passes,
122-
should not be used normally.
122+
Disable all optimizations. The produced code will be the same as that
123+
produced by the native IDE. This is useful to debug problems with the
124+
optimizations passes. This option should not be used normally.
123125

124126
- **-prof**
125-
Helps profiling the compiler generated code. Outputs statistics of the most
126-
used tokens and token pairs.
127+
Helps in profiling the compiler generated code. Outputs statistics of
128+
the most used tokens and token pairs.
127129

128130
- **-g**
129131
Generates a label file (`.lbl`) and an assembly listing for each source
130132
(`BAS` or `ASM`).
131133

132134
- **-d**
133-
Enable parser debug options. This is only useful to debug parser, as it
135+
Enable parser debug options. This is only useful to debug the parser, as it
134136
writes the full parsing tree with all the tried constructs.
135137

136138
- **-l** / **-l**:*extension*
@@ -148,14 +150,14 @@ an `:` or an `=` to separate the option from the argument.
148150
**WARNING**: You should always verify that the minimized version compiles to
149151
the same as the original version, as the parser does not examine the full
150152
code when minimizing and it could write invalid code. Also, the minimizer
151-
does nor remove extra parenthesis or joins data statements, you should
152-
manually do those transformations before.
153+
does not remove extra parenthesis or join data statements. You should
154+
manually do those transformations beforehand.
153155

154156
- **-h**
155157
Shows available compiler options.
156158

157159
- **-C**:*linker-file.cfg*
158-
Use a different linker configuration file than the default.
160+
Use a different linker configuration file. The default ia `fastbasic.cfg`.
159161

160162
- **-S**:*address*
161163
Sets the start address of the compiled binary. The default value is `$2000`,
@@ -164,6 +166,9 @@ an `:` or an `=` to separate the option from the argument.
164166
don't use a DOS. The address can be specified as decimal or hexadecimal with
165167
`0x` at front. This option is ignored when producing cartridge images.
166168

169+
- **-c**
170+
Emit bytecode. See section "Extending the language".
171+
167172
- **-X**:*ca65-option*
168173
Passes the given option to the CA65 assembler. See the CA65 documentation for
169174
valid options, some useful options are listed bellow:
@@ -175,13 +180,14 @@ an `:` or an `=` to separate the option from the argument.
175180
- **-X:-D***symbol*
176181
Define an assembly symbol, used in custom ASM sources.
177182

178-
When using `-X:` you can't leave spaces for the option, use multiple `-X:`
179-
for each space separated argument. For example, you can use `-X:-I -X:path `
180-
as two options or `-X:-Ipath`, as both will pass the same option and value.
183+
When using `-X:` you can't use spaces between assembler options. Use multiple
184+
`-X:` for each space separated argument. For example, you can use
185+
`-X:-I -X:path ` as two options or `-X:-Ipath`, as both will pass the same
186+
option and value.
181187

182188
- **-s:**:*segment_name*
183189
Sets the name of the "section" where the compiler will place the generated
184-
code. The default segment is `BYTECODE`, if you change the segment you must
190+
code. The default segment is `BYTECODE`. If you change the segment you must
185191
ensure that there is a segment with that name in the linker configuration.
186192

187193
- **-DL:***symbol=value*
@@ -211,14 +217,14 @@ an `:` or an `=` to separate the option from the argument.
211217
Linking other assembly files
212218
----------------------------
213219

214-
The compiler support linking to external assembly modules, you can pass them to
215-
the `fastbasic` command line:
220+
The compiler supports linking to external assembly modules. You can pass them
221+
on the `fastbasic` command line:
216222

217223
fastbasic myprog.bas myasm.asm
218224

219225
This will compile `myprog.bas` to `myprog.asm` and then assemble the two files
220-
together using CA65 and LD65. You can pass multiple `.asm` (or `.o`) files to the
221-
command line, all the files will be assembled and linked together.
226+
together using CA65 and LD65. You can pass multiple `.asm` (or `.o`) files on
227+
the command line. All the files will be assembled and linked together.
222228

223229
From the FastBasic source, you can reference any symbol via `@name`, for example:
224230

@@ -253,12 +259,12 @@ You can also export ZP symbols, to import them use `@@name`.
253259
Extending the language
254260
----------------------
255261

256-
The FastBasic compiler is extensible, the syntax is read at compilation time
262+
The FastBasic compiler is extensible. The syntax is read at compilation time
257263
from various files with the grammar and compilation rules (the `.syn` files in
258264
the `syntax` folder).
259265

260266
The following sections show how to add simple functions and statements to the
261-
language, you can look at the existing syntax files for more advanced usage.
267+
language. You can look at the existing syntax files for more advanced usage.
262268

263269

264270
### Adding new functions
@@ -287,11 +293,11 @@ following contents:
287293
include atari-fp
288294
syntax console.syn
289295

290-
The first line simply reads the `atari-fp` target, so the new target will have
296+
The first line reads the `atari-fp` target, so the new target will have
291297
all the definitions already in the standard target.
292298

293-
The second line, adds a new syntax, from the file `console.syn`, this will be
294-
appended to the syntax in the standard `atari-fp`.
299+
The second line adds a new syntax from the file `console.syn`. This will be
300+
appended to the syntax in the standard `atari-fp` target.
295301

296302
#### 2. Create a new syntax file
297303

@@ -303,20 +309,21 @@ content inside:
303309
"CSELECT()"
304310
"COPTION()"
305311

306-
This defines three new functions, without parameters, and without any coed
312+
This defines three new functions, without parameters, and without any code
307313
generated. You could now compile a simple program, but the resulting executable
308314
will not run because the code for the functions is missing.
309315

310-
To add the code, you need to use the `emit` keyword, and inspect the code
311-
generated for our test examples.
316+
To add the code, you need to inspect the code generated for our test examples
317+
and then use the `emit` keyword in the `console.syn` file to generate bytecode
318+
based on what you found.
312319

313320
#### 3. Adding code for the new functions
314321

315322
Compile to assembler a simple basic source:
316323

317324
PRINT NOT (PEEK(53279) & 1)
318325

319-
Using `fastbasic -c simple.bas`, and examine the generated assembly file, you
326+
Use `fastbasic -c simple.bas` and examine the generated assembly file. You
320327
will see the following bytecode at the end:
321328

322329
TOK_NUM
@@ -332,7 +339,7 @@ will see the following bytecode at the end:
332339
You can see that the code is similar to the basic source:
333340

334341
- Load number `53279`
335-
- `PEEK` from that location
342+
- `PEEK` into that location
336343
- Load number `1`
337344
- Perform the `&` operation
338345
- Compare the result with `0`
@@ -347,8 +354,8 @@ the `console.syn` file, and add the emitted code:
347354
"CSELECT()" emit { TOK_NUM, &53279, TOK_PEEK, TOK_PUSH, TOK_BYTE, 2, TOK_BIT_AND, TOK_COMP_0, TOK_L_NOT }
348355
"COPTION()" emit { TOK_NUM, &53279, TOK_PEEK, TOK_PUSH, TOK_BYTE, 4, TOK_BIT_AND, TOK_COMP_0, TOK_L_NOT }
349356

350-
Note that the `emit` code needs to be in one line, and the `&` symbol before
351-
the `53279` indicates a word value (two bytes) instead of a simple byte.
357+
Note that the `emit` code needs to be on one line. The `&` symbol before
358+
the `53279` indicates a 16-bit word value (two bytes) instead of a single byte.
352359

353360
#### 4. Testing the new language extensions
354361

@@ -368,7 +375,7 @@ Compile with:
368375

369376
fastbasic -t:atari-extra myprog.bas
370377

371-
#### 5. Factorizing the code
378+
#### 5. Refactoring the code
372379

373380
Instead of writing the full code for each function, you can rearrange the code
374381
to make the syntax smaller, and use constants to make it more generic, by
@@ -387,15 +394,17 @@ defining a new syntax table:
387394
"COPTION" emit { TOK_BYTE, 4 } READ_CONSOLE
388395

389396
Note the use of the symbol `CONSOL` instead of the number, and the rearranging
390-
of the code to allow factorization.
397+
of the code to be more concise.
398+
399+
Read more about bytecode in "Understanding the syntax files".
391400

392401

393402
### Adding new statements
394403

395404
To add new statements, instead of expanding the table `INT_FUNCTIONS`, you need
396405
to expand the table `STATEMENT`, but the logic is the same.
397406

398-
Also, to interface the interpreter with new assembly functions, you can emit
407+
Also, to interface the interpreter with new assembly functions, you can `emit`
399408
code similar to an `USR()` call, and process the parameters in the assembly.
400409

401410
Let's implement a new statement, `WAIT` that can wait for a vertical line in
@@ -423,7 +432,7 @@ to it:
423432
include atari-fp
424433
syntax console.syn wait.syn
425434

426-
And same as the first example, just add a new syntax file, `wait.syn`, with
435+
And same as the first example, add a new syntax file, `wait.syn`, with
427436
this content:
428437

429438
SYMBOLS {
@@ -446,29 +455,29 @@ The command line to compile should be:
446455

447456
fastbasic -t:atari-extra testw.bas wait.asm
448457

449-
Testing the resulting file, you should be three color zones in the screen.
458+
Testing the resulting file, you should see three color zones on the screen.
450459

451460
There some parts of the syntax file that needs explanation:
452461

453462
- The `"WAit"` string is specified with mixed case. The lower-case characters
454463
denote "optional" part of the name, so you can abbreviate this new statement
455464
as "WA." or "WAI.".
456465

457-
- We are loafing the `DO_WAIT` address into the `USR` calling address as the
466+
- We are loading the `DO_WAIT` address into the `USR` calling address as the
458467
first step in the generated code. This is needed to be able to call the
459-
assembly code with out parameter in the accumulator.
468+
assembly code without a parameter in the accumulator.
460469

461470
- To get the value of the symbol `DO_WAIT`, we need to specify `import` as the
462-
symbol value, this means the value will be filled by the linker.
471+
symbol value. This means the value will be filled by the linker.
463472

464473
- Then, we are expecting an `EXPR`. This syntax specifies an integer
465-
expression, the parser will parse any expression that gives am integer number
474+
expression. The parser will parse any expression that gives am integer number
466475
here, and the result will be placed in the `A` and `X` registers.
467476

468477

469478
### Syntax of the Target files
470479

471-
The target files (with `.tgt` extension) allows changing many aspects of the
480+
The target files (with `.tgt` extension) allow us to change many aspects of the
472481
compilation process.
473482

474483
The syntax of the files is line oriented, with one command per line. The list
@@ -481,16 +490,16 @@ of commands is the following:
481490
file name given does not have an extension, `.tgt` is added.
482491

483492
- `library`: Gives the name of the library to link with a compiled FastBasic
484-
program for this target. Only one library file is allowed, the last
485-
one read takes precedence.
493+
program for this target. Only one library file is allowed. The
494+
last one read takes precedence.
486495

487496
- `config`: Gives the name of the linker configuration file used for this
488497
target.
489498

490499
- `extension`: Gives the extension of the compiled file for this target.
491500

492501
- `ca65`: Gives a list of options to pass to the `CA65` assembler. Multiple
493-
options are simply added after the previous.
502+
options are appended after existing assembler options.
494503

495504
- `syntax`: Gives a list of syntax files to read, defining the syntax of all
496505
the language. Multiple files are read in the order given, and
@@ -500,15 +509,15 @@ of commands is the following:
500509
### Understanding the Syntax files
501510

502511
The syntax files (with `.syn` extension) define the full parsing rules of the
503-
FastBasic language - there are no predefined rules in the parser, all the
512+
FastBasic language. There are no predefined rules in the parser. The whole
504513
language is defined in the included `.syn` files.
505514

506515
In the top level, the syntax files are composed of *tokens*, *symbols*,
507516
*external routines* and *rules*.
508517

509518
The *tokens* are a list of bytecode language instructions available to the
510519
parser for the compiled code. The FastBasic interpreter supports up to 128
511-
tokens, mos implements core functionality (like adding two numbers, or loading
520+
tokens. Most implement core functionality (like adding two numbers, or loading
512521
the value of a variable, etc.). The tokens are listed in a special `TOKENS`
513522
section:
514523

@@ -519,7 +528,7 @@ section:
519528
}
520529

521530
The *external routines* are routines that can be called from the parser to
522-
parse special constructs, or modify compiler state outside the parser, examples
531+
parse special constructs, or modify compiler state outside the parser. Examples
523532
are adding a variable to the list of variables, checking if a variable name is
524533
already defined, parsing a number or string, etc. The external routines are
525534
listed in a special `EXTERN` section:
@@ -534,13 +543,13 @@ There is an important external rule `E_EOL` that matches at the end of the
534543
input line.
535544

536545
All external routines used in the parser needs to be implemented in the
537-
compiler in C++ and in the 6502 compiler in assembly, or the parser will fail.
546+
compiler in C++ *and* in the 6502 compiler in assembly, or the parser will fail.
538547

539548
The *symbols* are a list of symbols available to the parser to use for the
540-
compiled code, this allows the parser to include names instead of numeric
549+
compiled code. This allows the parser to include names instead of numeric
541550
constants for the compiled code, and allows using symbols from the library.
542551

543-
The named *rules* define a part of the syntax that the parser understand, and
552+
The named *rules* define a part of the syntax that the parser understands. They
544553
are defined using a name followed by a colon:
545554

546555
NAME: rule description
@@ -549,11 +558,11 @@ are defined using a name followed by a colon:
549558
...
550559

551560
The *NAME* of the rule must start with a letter, and can contain any number of
552-
letters, numbers or underscores, on the other hand the *rule description* is
561+
letters, numbers or underscores. On the other hand the *rule description* is
553562
one line of any text that is shown when the parser encounters a syntax error
554563
when expecting this rule.
555564

556-
Each *pattern* define a posible parsing for this rule. Each pattern is tried
565+
Each *pattern* defines a posible parsing for this rule. Each pattern is tried
557566
from the first to the last, and the first pattern that matches allows the rule
558567
to match. If after trying all the patterns, the last one does not match, the
559568
rule fails to match.
@@ -585,8 +594,8 @@ The patterns can contain the following:
585594
You can also follow the `emit` with a list of symbols enclosed in curly
586595
braces ( `{` and `}` ).
587596

588-
The last pattern in a rule can also be the special word `pass`, this makes the
589-
parser to accept the rule even if no other pattern matched, effectively making
597+
The last pattern in a rule can also be the special word `pass`. This makes the
598+
parser accept the rule even if no other pattern matched, effectively making
590599
the rule optional.
591600

592601
Finally, there is a special rule named `PARSE_START` that the parser calls for

0 commit comments

Comments
 (0)