Difference between revisions of "Forth parser"
From ScienceZero
Line 1: | Line 1: | ||
+ | == Variable inference == | ||
+ | Variables are stored locally on the return stack. | ||
+ | Any word that starts with ">" is a write to a variable. | ||
+ | |||
+ | |||
+ | For each word that starts with ">" | ||
+ | Add the word to an ordered list of all unique words that starts with ">", remove the leading ">" | ||
+ | Emit '''STL #<index in the ordered list>''' | ||
+ | For each word that is in the list but does not start with ">" | ||
+ | Emit '''LDL #<index in the ordered list>''' | ||
+ | If there are more than 16 variables emit error | ||
+ | if there are more than 0 variables emit '''DIM #<largest index>''' as the first instruction of the function. | ||
+ | |||
+ | This will | ||
+ | |||
== Example 1 == | == Example 1 == | ||
− | : lsl for dup + next ; ( Simple complete function that implements left shift by repeated addition ) | + | ''': lsl for dup + next ;''' ( Simple complete function that implements left shift by repeated addition ) |
":" this tells the parser to define a new function | ":" this tells the parser to define a new function | ||
Line 23: | Line 38: | ||
How to encode numbers: | How to encode numbers: | ||
− | if number = 0 then emit LDC #0 | + | if number = 0 then emit '''LDC #0''' |
else | else | ||
find the topmost hex digit that is not 0, call it x | find the topmost hex digit that is not 0, call it x | ||
− | emit LDC #x | + | emit '''LDC #x''' |
zero out that digit | zero out that digit | ||
for each remaining digit y, zero or not | for each remaining digit y, zero or not | ||
− | emit LDE #y | + | emit '''LDE #y''' |
== Default opcode equivalency for forth words == | == Default opcode equivalency for forth words == |
Revision as of 17:07, 24 April 2018
Variable inference
Variables are stored locally on the return stack. Any word that starts with ">" is a write to a variable.
For each word that starts with ">" Add the word to an ordered list of all unique words that starts with ">", remove the leading ">" Emit STL #<index in the ordered list> For each word that is in the list but does not start with ">" Emit LDL #<index in the ordered list> If there are more than 16 variables emit error if there are more than 0 variables emit DIM #<largest index> as the first instruction of the function.
This will
Example 1
: lsl for dup + next ; ( Simple complete function that implements left shift by repeated addition ) ":" this tells the parser to define a new function "lsl" is the name of the function Adr Value 0 0xF0 ( for ) 1 0xE0 ( dup ) 2 0xC0 ( add ) 3 0xF1 ( next ) ";" tells the parser that it has reached the end of the definition The parser should create an "object" in memory like: "lsl",0xF0,0xE0,0xC0,0xF1 input text --> look up tables --> output binary How to encode numbers: if number = 0 then emit LDC #0 else find the topmost hex digit that is not 0, call it x emit LDC #x zero out that digit for each remaining digit y, zero or not emit LDE #y
Default opcode equivalency for forth words
Forth string - opcode + - add - - sub * - mul / - div
dup - dup