Skip to content

Commit 8315ab2

Browse files
committed
Adds information on target and syntax files to the compiler manual.
1 parent 0fca0e1 commit 8315ab2

1 file changed

Lines changed: 152 additions & 0 deletions

File tree

compiler/USAGE.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,155 @@ There some parts of the syntax file that needs explanation:
421421
here, and the result will be placed in the `A` and `X` registers.
422422

423423

424+
### Syntax of the Target files
425+
426+
The target files (with `.tgt` extension) allows changing many aspects of the
427+
compilation process.
428+
429+
The syntax of the files is line oriented, with one command per line. The list
430+
of commands is the following:
431+
432+
- `#`: A line that starts with a sharp symbol is a comment. The rest of the
433+
line is ignored.
434+
435+
- `include`: Includes all the target definitions for the named file. If the
436+
file name given does not have an extension, `.tgt` is added.
437+
438+
- `library`: Gives the name of the library to link with a compiled FastBasic
439+
program for this target. Only one library file is alowed, the last
440+
one read takes precendence.
441+
442+
- `config`: Gives the name of the linker configuration file used for this
443+
target.
444+
445+
- `extension`: Gives the extension of the compiled file for this target.
446+
447+
- `ca65`: Gives a list of options to pass to the `CA65` assembler. Multiple
448+
options are simply added after the previous.
449+
450+
- `syntax`: Gives a list of syntax files to read, defining the syntax of all
451+
the language. Multiple files are read in the order given, and
452+
all definitions are merged together.
453+
454+
455+
### Understanding the Syntax files
456+
457+
The syntax files (with `.syn` extension) define the full parsing rules of the
458+
FastBasic language - there are no predefined rules in the parser, all the
459+
language is defined in the included `.syn` files.
460+
461+
In the top level, the syntax files are composed of *tokens*, *symbols*,
462+
*external routines* and *rules*.
463+
464+
The *tokens* are a list of bytecode language instructions available to the
465+
parser for the compiled code. The FastBasic interpreter supports up to 128
466+
tokens, mos implements core functionality (like adding two numbers, or loading
467+
the value of a variable, etc.). The tokens are listed in a special `TOKENS`
468+
section:
469+
470+
TOKENS {
471+
token-1, token-2
472+
token-3
473+
...
474+
}
475+
476+
The *external routines* are routines that can be called from the parser to
477+
parse special constructs, or modify compiler state outside the parser, examples
478+
are adding a variable to the list of variables, checking if a variable name is
479+
already defined, parsing a number or string, etc. The external routines are
480+
listed in a special `EXTERN` section:
481+
482+
EXTERN {
483+
name_1, name_2
484+
name-4
485+
...
486+
}
487+
488+
There is an important external rule `E_EOL` that matches at the end of the
489+
input line.
490+
491+
All external routines used in the parser needs to be implemented in the
492+
compiler in C++ and in the 6502 compiler in assembly, or the parser will fail.
493+
494+
The *symbols* are a list of symbols available to the parser to use for the
495+
compiled code, this allows the parser to include names instead of numeric
496+
constants for the compiled code, and allows using symbols from the library.
497+
498+
The named *rules* define a part of the syntax that the parser understand, and
499+
are defined using a name followed by a colon:
500+
501+
NAME: rule description
502+
pattern-1
503+
pattern-2
504+
...
505+
506+
The *NAME* of the rule must start with a letter, and can contain any number of
507+
letters, numbers or underscores, on the other hand the *rule description* is
508+
one line of any text that is shown when the parser encounters a syntax error
509+
when expecting this rule.
510+
511+
Each *pattern* define a posible parsing for this rule. Each pattern is tried
512+
from the first to the last, and the first pattern that matches allows the rule
513+
to match. If after trying all the patterns, the last one does not match, the
514+
rule fails to match.
515+
516+
The patterns must be in one line, and must begin with at least one space to
517+
differentiate from a rule definition.
518+
519+
The patterns can contain the following:
520+
521+
- Text enclosed in double quotes (`"`). The rule matches if all the text in the
522+
quotes matches the input, ignoring case - the parser is case insensitive.
523+
524+
The FastBasic parser also supports abbreviations: Any lower-case letter in the
525+
quoted text is optional, if in that place of the input is a dot, the rest of
526+
the quoted text is skipped.
527+
528+
For example, the pattern `"HEllo!!"` matches the text `HELLO!!`, `HE.`,
529+
`HEL.` or `HELL.`.
530+
531+
- The name of a rule. This matches if the rule matches here.
532+
533+
- The name of an external rule. This matches if called code returns true.
534+
535+
- The word `emit` followed by a token, symbol or number. This instructs the
536+
parser to output the given token, symbol or number to the compiler. The
537+
symbols and numbers produces one byte in the output, except when the symbol
538+
is prepended with an ampersand (`&`) that produces two bytes (a `word`).
539+
540+
You can also follow the `emit` with a list of symbols enclosed in curly
541+
braces ( `{` and `}` ).
542+
543+
The last pattern in a rule can also be the special word `pass`, this makes the
544+
parser to accept the rule even if no other pattern matched, effectively making
545+
the rule optional.
546+
547+
Finally, there is a special rule named `PARSE_START` that the parser calls for
548+
each line in the input.
549+
550+
The parsing rules are based in the PEG grammar syntax, but simplified to make
551+
the 6502 parser simpler and faster. You can convert PEG rules to FastBasic
552+
syntax rules by simple replacing:
553+
554+
- An optional rule `RULE ?`:
555+
556+
RULE_OPT:
557+
RULE
558+
pass
559+
560+
- An optional and repeatable rule `RULE *`:
561+
562+
RULE_OPT_REP:
563+
RULE RULE_OPT_REP
564+
pass
565+
566+
- A repeatable rule `RULE +`:
567+
568+
RULE_REP:
569+
RULE RULE_MORE
570+
571+
RULE_MORE:
572+
RULE RULE_MORE
573+
pass
574+
575+

0 commit comments

Comments
 (0)