43 lines
791 B
C
43 lines
791 B
C
#include "parser.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
Module* parser_parse(TokenStream* ts) {
|
|
Token t = tokenstream_next(ts);
|
|
if (t.token != TOKEN_MODULE) {
|
|
return NULL;
|
|
}
|
|
|
|
t = tokenstream_next(ts);
|
|
if (t.token != TOKEN_IDENTIFIER) {
|
|
return NULL;
|
|
}
|
|
|
|
Module* module = (Module*)malloc(sizeof(Module));
|
|
if (module == NULL) return NULL;
|
|
|
|
module->name = (char*)malloc(t.text_length + 1);
|
|
if (module->name == NULL) {
|
|
free(module);
|
|
return NULL;
|
|
}
|
|
|
|
memcpy(module->name, t.text, t.text_length);
|
|
module->name[t.text_length] = '\0';
|
|
|
|
t = tokenstream_next(ts);
|
|
if (t.token != 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);
|
|
}
|