Files
Toy/test/test_compiler.c
Kayne Ruse 2e2bee4fa3 Renemed all variables to fit into a namespace
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.
2023-01-25 12:55:55 +00:00

95 lines
1.9 KiB
C

#include "toy_lexer.h"
#include "toy_parser.h"
#include "toy_compiler.h"
#include "toy_console_colors.h"
#include "toy_memory.h"
#include "../repl/repl_tools.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
{
//test init & free
Toy_Compiler compiler;
Toy_initCompiler(&compiler);
Toy_freeCompiler(&compiler);
}
{
//source
char* source = "print null;";
//test basic compilation & collation
Toy_Lexer lexer;
Toy_Parser parser;
Toy_Compiler compiler;
Toy_initLexer(&lexer, source);
Toy_initParser(&parser, &lexer);
Toy_initCompiler(&compiler);
Toy_ASTNode* node = Toy_scanParser(&parser);
//write
Toy_writeCompiler(&compiler, node);
//collate
int size = 0;
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
//cleanup
TOY_FREE_ARRAY(unsigned char, bytecode, size);
Toy_freeASTNode(node);
Toy_freeParser(&parser);
Toy_freeCompiler(&compiler);
}
{
//source
size_t sourceLength = 0;
char* source = Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength);
//test basic compilation & collation
Toy_Lexer lexer;
Toy_Parser parser;
Toy_Compiler compiler;
Toy_initLexer(&lexer, source);
Toy_initParser(&parser, &lexer);
Toy_initCompiler(&compiler);
Toy_ASTNode* node = Toy_scanParser(&parser);
while (node != NULL) {
if (node->type == TOY_AST_NODE_ERROR) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Error node found" TOY_CC_RESET);
return -1;
}
//write
Toy_writeCompiler(&compiler, node);
Toy_freeASTNode(node);
node = Toy_scanParser(&parser);
}
//collate
int size = 0;
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
//cleanup
TOY_FREE_ARRAY(char, source, sourceLength);
TOY_FREE_ARRAY(unsigned char, bytecode, size);
Toy_freeParser(&parser);
Toy_freeCompiler(&compiler);
}
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}