|
10 | 10 | * 2. BASIC Parser (Tokens to AST) |
11 | 11 | * 3. Execute AST sequentially (Program run) |
12 | 12 | * |
13 | | - * */ |
| 13 | + */ |
14 | 14 |
|
15 | | -#include "ast.h" |
16 | 15 | #include <data_structures/stack.h> |
17 | 16 |
|
18 | | -/* For printing Log messages */ |
19 | | -extern unsigned int log_print_mask; |
20 | | - |
21 | | -// Log level mask |
22 | | -#define LOGTYPE_DEBUG (1 << 0) |
23 | | -#define LOGTYPE_INFO (1 << 1) |
24 | | -#define LOGTYPE_MESSAGE (1 << 2) |
25 | | -#define LOGTYPE_ERROR (1 << 3) |
26 | | -#define LOGMASK_ALL 0xffff |
27 | | - |
28 | | -// Printf modified to allow only selected messages |
29 | | -void lprintf(const char *tag, unsigned int level_mask, const char *format, ...); |
30 | | - |
31 | | -/* AST parser (tokenizer) */ |
32 | | - |
33 | | -typedef enum |
34 | | -{ |
35 | | - TOKEN_IDENTIFIER, |
36 | | - TOKEN_KEYWORD, |
37 | | - TOKEN_WHITESPACE, |
38 | | - TOKEN_OPERATOR, |
39 | | - TOKEN_SEPARATOR, |
40 | | - TOKEN_NUM, |
41 | | - TOKEN_BOOL, |
42 | | - TOKEN_STRING, |
43 | | - TOKEN_END |
44 | | -} BASICTokenType; |
45 | | - |
46 | | -typedef struct |
47 | | -{ |
48 | | - StringLiteral token; |
49 | | - BASICTokenType token_type; |
50 | | - char *token_at; |
51 | | -} BASICToken; |
52 | | - |
53 | | -// Structure with all the stuff needed to convert a BASIC program to an AST |
54 | | -typedef struct |
55 | | -{ |
56 | | - // List of tokens |
57 | | - BASICToken *tokens; |
58 | | - int tokens_length; |
59 | | - // Stack for knowning current scope |
60 | | -} BASICParseTree; |
61 | | - |
62 | | -typedef struct |
63 | | -{ |
64 | | - char *program_source; |
65 | | - BASICParseTree program_tokens; |
66 | | - ASTNode *program_sequence; |
67 | | - // Map between line number and which instruction to execute on that line. For non-linear control flow |
68 | | - // BASICLineNode program_line_instruction; |
69 | | -} BASICProgram; |
70 | | - |
71 | | -BASICProgram *basic_create_program(); |
72 | | -void basic_clear_program(BASICProgram *program); |
73 | | -void basic_destroy_program(BASICProgram *program); |
74 | | - |
75 | | -// Recognized tokens |
76 | | -extern char *PARSE_KEYWORDS[]; |
77 | | -extern int PARSE_KW_COUNT; |
78 | | -extern char *PARSE_BOOLEAN[]; |
79 | | -extern char PARSE_OPERATORS[]; |
80 | | -extern char PARSE_SEPARATOR[]; |
81 | | -extern char PARSE_WS_CHAR[]; |
82 | | - |
83 | | -// Index of the keyword in the above PARSE_KEYWORDS array |
84 | | -#define KEYWORD_IDX_WHILE 0 |
85 | | -#define KEYWORD_IDX_IF 1 |
86 | | -#define KEYWORD_IDX_THEN 2 |
87 | | -#define KEYWORD_IDX_ELSE 3 |
88 | | -#define KEYWORD_IDX_END 4 |
89 | | -#define KEYWORD_IDX_GOTO 5 |
90 | | - |
91 | | -// Lexer |
92 | | -int basic_tokenize(BASICProgram *program); |
93 | | - |
94 | | -// Parser |
95 | | -int basic_parse_form_expression(BASICParseTree *ptree, ASTNode *root, int parse_from, int parse_to, int *parse_new_pos); |
96 | | -int basic_parse_form_function(BASICParseTree *ptree, ASTNode *root, int parse_from, int parse_to, int *parse_new_pos); |
97 | | - |
98 | | -int basic_parse_to_ast_between_level(BASICParseTree *ptree, ASTNode *root, int from, int to, int level, int allow_keyword, int *next_ptr); |
99 | | -int basic_parse_to_ast_between_onlyexpr(BASICParseTree *ptree, ASTNode *root, int from, int to); |
100 | | -int basic_parse_to_ast_between(BASICParseTree *ptree, ASTNode *root, int from, int to); |
101 | | -int basic_parse_to_ast(BASICProgram *program); |
102 | | - |
103 | | -/* BASIC interpreter */ |
104 | | - |
105 | | -typedef enum |
106 | | -{ |
107 | | - KW_DO_NOTHING, |
108 | | - KW_JMP_ONLY, |
109 | | - KW_JMP_AND_RET_NEXT, |
110 | | - KW_JMP_AND_RET_CURR |
111 | | -} KeywordAction; |
112 | | - |
113 | | -typedef struct |
114 | | -{ |
115 | | - ASTNodeData value; |
116 | | - StringLiteral name; |
117 | | -} BASICVariable; |
118 | | - |
119 | | -typedef struct |
120 | | -{ |
121 | | - BASICProgram *program; |
122 | | - int halt; |
123 | | - BASICVariable *variables; |
124 | | - int var_count; |
125 | | - StackNode *traverse_stack; |
126 | | -} BASICRuntime; |
127 | | - |
128 | | -typedef ASTNodeData (*basic_function)(BASICRuntime *runtime, ASTNode *args); |
129 | | - |
130 | | -/* Public functions */ |
131 | | -BASICRuntime *basic_create_runtime(BASICProgram *program); |
132 | | -void basic_free_runtime(BASICRuntime *runtime); |
133 | | - |
134 | | -ASTNodeData basic_evaluate_node(BASICRuntime *runtime, ASTNode *node); |
135 | | -ASTNodeData basic_execute(BASICRuntime *runtime, ASTNode *pc); |
136 | | - |
137 | | -/* Private functions */ |
138 | | - |
139 | | -BASICVariable *basic_find_variable(BASICRuntime *runtime, char var_name[]); |
140 | | -ASTNodeData basic_get_variable(BASICRuntime *runtime, char var_name[]); |
141 | | -void basic_set_variable(BASICRuntime *runtime, char var_name[], ASTNodeData value); |
142 | | -void basic_var_assignment(BASICRuntime *runtime, ASTNode *args); |
143 | | -void basic_init_constants(BASICRuntime *runtime); |
144 | | -KeywordAction basic_evaluate_keyword_block(BASICRuntime *runtime, ASTNode *pc, ASTNode **nextpc); |
145 | | - |
146 | | -// Keyword handlers |
147 | | -KeywordAction basic_eval_kw_if(BASICRuntime *runtime, ASTNode *pc, ASTNode **nextpc); |
148 | | -KeywordAction basic_eval_kw_while(BASICRuntime *runtime, ASTNode *pc, ASTNode **nextpc); |
149 | | - |
150 | | -// Built-in functions |
151 | | -ASTNodeData _basic_fn_print(BASICRuntime *runtime, ASTNode *args); |
152 | | -ASTNodeData _basic_fn_max(BASICRuntime *runtime, ASTNode *args); |
153 | | -ASTNodeData _basic_fn_min(BASICRuntime *runtime, ASTNode *args); |
154 | | -ASTNodeData _basic_fn_sleep(BASICRuntime *runtime, ASTNode *arg); |
155 | | -ASTNodeData _basic_fn_toint(BASICRuntime *runtime, ASTNode *arg); |
156 | | -ASTNodeData _basic_fn_toflt(BASICRuntime *runtime, ASTNode *arg); |
157 | | -ASTNodeData _basic_fn_rand(BASICRuntime *runtime, ASTNode *arg); |
158 | | -ASTNodeData _basic_fn_irand(BASICRuntime *runtime, ASTNode *arg); |
| 17 | +#include "ast.h" |
| 18 | +#include "basic_token.h" |
| 19 | +#include "basic_parser.h" |
| 20 | +#include "basic_program.h" |
| 21 | +#include "basic_runner.h" |
0 commit comments