mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Basically, all Toy varaibles, functions, etc. are prepended with "Toy_", and macros are prepended with "TOY_". This is to reduce namespace pollution, which was an issue pointed out to be - blame @GyroVorbis. I've also bumped the minor version number - theoretically I should bump the major number, but I'm not quite ready for 1.0 yet.
90 lines
1.9 KiB
C
90 lines
1.9 KiB
C
#include "toy_parser.h"
|
|
|
|
#include "toy_console_colors.h"
|
|
|
|
#include "../repl/repl_tools.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int main() {
|
|
{
|
|
//source
|
|
char* source = "print null;";
|
|
|
|
//test init & quit
|
|
Toy_Lexer lexer;
|
|
Toy_Parser parser;
|
|
Toy_initLexer(&lexer, source);
|
|
Toy_initParser(&parser, &lexer);
|
|
|
|
Toy_freeParser(&parser);
|
|
}
|
|
|
|
{
|
|
//source
|
|
char* source = "print null;";
|
|
|
|
//test parsing
|
|
Toy_Lexer lexer;
|
|
Toy_Parser parser;
|
|
Toy_initLexer(&lexer, source);
|
|
Toy_initParser(&parser, &lexer);
|
|
|
|
Toy_ASTNode* node = Toy_scanParser(&parser);
|
|
|
|
//inspect the node
|
|
if (node == NULL) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: ASTNode is null" TOY_CC_RESET);
|
|
return -1;
|
|
}
|
|
|
|
if (node->type != TOY_AST_NODE_UNARY || node->unary.opcode != TOY_OP_PRINT) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: ASTNode is not a unary print instruction" TOY_CC_RESET);
|
|
return -1;
|
|
}
|
|
|
|
if (node->unary.child->type != TOY_AST_NODE_LITERAL || !TOY_IS_NULL(node->unary.child->atomic.literal)) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: ASTNode to be printed is not a null literal" TOY_CC_RESET);
|
|
return -1;
|
|
}
|
|
|
|
//cleanup
|
|
Toy_freeASTNode(node);
|
|
Toy_freeParser(&parser);
|
|
}
|
|
|
|
{
|
|
//get the source file
|
|
size_t size = 0;
|
|
char* source = Toy_readFile("scripts/parser_sample_code.toy", &size);
|
|
|
|
//test parsing a chunk of junk (valgrind will find leaks)
|
|
Toy_Lexer lexer;
|
|
Toy_Parser parser;
|
|
Toy_initLexer(&lexer, source);
|
|
Toy_initParser(&parser, &lexer);
|
|
|
|
Toy_ASTNode* node = Toy_scanParser(&parser);
|
|
|
|
while (node != NULL) {
|
|
if (node->type == TOY_AST_NODE_ERROR) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Error node detected" TOY_CC_RESET);
|
|
return -1;
|
|
}
|
|
|
|
Toy_freeASTNode(node);
|
|
node = Toy_scanParser(&parser);
|
|
}
|
|
|
|
//cleanup
|
|
Toy_freeParser(&parser);
|
|
free((void*)source);
|
|
}
|
|
|
|
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
|
return 0;
|
|
}
|
|
|