Implement parser module and update AST
This commit is contained in:
+45
@@ -0,0 +1,45 @@
|
||||
#include "parser.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Module* parser_parse(TokenStream* ts) {
|
||||
Token t = tokenstream_next(ts);
|
||||
if (t != TOKEN_MODULE) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
t = tokenstream_next(ts);
|
||||
if (t != TOKEN_IDENTIFIER) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TokenInfo info;
|
||||
tokenstream_info(ts, &info);
|
||||
|
||||
Module* module = (Module*)malloc(sizeof(Module));
|
||||
if (module == NULL) return NULL;
|
||||
|
||||
module->name = (char*)malloc(info.text_length + 1);
|
||||
if (module->name == NULL) {
|
||||
free(module);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(module->name, info.text, info.text_length);
|
||||
module->name[info.text_length] = '\0';
|
||||
|
||||
t = tokenstream_next(ts);
|
||||
if (t != TOKEN_SEMICOLON) {
|
||||
free(module->name);
|
||||
free(module);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return module;
|
||||
}
|
||||
|
||||
void parser_free(Module* module) {
|
||||
if (module == NULL) return;
|
||||
free(module->name);
|
||||
free(module);
|
||||
}
|
||||
Reference in New Issue
Block a user