The Parser converts a Simple source program to the Sea of Nodes intermediate representation directly in one pass. There is no intermediate Abstract Syntax Tree structure. This is a simple recursive descent parser. All lexical analysis is done here as well.
| 13 | * This is a simple recursive descent parser. All lexical analysis is done here as well. |
| 14 | */ |
| 15 | public class Parser { |
| 16 | |
| 17 | /** |
| 18 | * A Global Static, unique to each compilation. This is a public, so we |
| 19 | * can make constants everywhere without having to thread the StartNode |
| 20 | * through the entire parser and optimizer. |
| 21 | * <p> |
| 22 | * To make the compiler multithreaded, this field will have to move into a TLS. |
| 23 | */ |
| 24 | public static StartNode START; |
| 25 | |
| 26 | public static ConstantNode ZERO; // Very common node, cached here |
| 27 | public static XCtrlNode XCTRL; // Very common node, cached here |
| 28 | |
| 29 | // Next available memory alias number |
| 30 | static int ALIAS; |
| 31 | |
| 32 | public StopNode STOP; |
| 33 | |
| 34 | // Debugger Printing. |
| 35 | public static boolean SCHEDULED; // True if debug printer can use schedule info |
| 36 | |
| 37 | // The Lexer. Thin wrapper over a byte[] buffer with a cursor. |
| 38 | private final Lexer _lexer; |
| 39 | |
| 40 | /** |
| 41 | * Current ScopeNode - ScopeNodes change as we parse code, but at any point of time |
| 42 | * there is one current ScopeNode. The reason the current ScopeNode can change is to do with how |
| 43 | * we handle branching. See {@link #parseIf()}. |
| 44 | * <p> |
| 45 | * Each ScopeNode contains a stack of lexical scopes, each scope is a symbol table that binds |
| 46 | * variable names to Nodes. The top of this stack represents current scope. |
| 47 | * <p> |
| 48 | * We keep a list of all ScopeNodes so that we can show them in graphs. |
| 49 | * @see #parseIf() |
| 50 | * @see #_xScopes |
| 51 | */ |
| 52 | public ScopeNode _scope; |
| 53 | |
| 54 | /** |
| 55 | * List of keywords disallowed as identifiers |
| 56 | */ |
| 57 | private static final HashSet<String> KEYWORDS = new HashSet<>(){{ |
| 58 | add("bool"); |
| 59 | add("break"); |
| 60 | add("byte"); |
| 61 | add("continue"); |
| 62 | add("else"); |
| 63 | add("f32"); |
| 64 | add("f64"); |
| 65 | add("false"); |
| 66 | add("flt"); |
| 67 | add("i16"); |
| 68 | add("i32"); |
| 69 | add("i64"); |
| 70 | add("i8"); |
| 71 | add("if"); |
| 72 | add("int"); |