mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
WE ARE THE CHAMPIONS!
This commit is contained in:
1023
test/sample_code.toy
Normal file
1023
test/sample_code.toy
Normal file
File diff suppressed because it is too large
Load Diff
49
test/test_lexer.c
Normal file
49
test/test_lexer.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "lexer.h"
|
||||
|
||||
#include "console_colors.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
{
|
||||
//source
|
||||
char* source = "print null;";
|
||||
|
||||
//test init & quit
|
||||
Lexer lexer;
|
||||
initLexer(&lexer, source);
|
||||
|
||||
//get each token
|
||||
Token print = scanLexer(&lexer);
|
||||
Token null = scanLexer(&lexer);
|
||||
Token semi = scanLexer(&lexer);
|
||||
Token eof = scanLexer(&lexer);
|
||||
|
||||
//test each token is correct
|
||||
if (strncmp(print.lexeme, "print", print.length)) {
|
||||
fprintf(stderr, ERROR "ERROR: print lexeme is wrong: %s" RESET, print.lexeme);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if (strncmp(null.lexeme, "null", null.length)) {
|
||||
fprintf(stderr, ERROR "ERROR: null lexeme is wrong: %s" RESET, null.lexeme);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strncmp(semi.lexeme, ";", semi.length)) {
|
||||
fprintf(stderr, ERROR "ERROR: semicolon lexeme is wrong: %s" RESET, semi.lexeme);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (eof.type != TOKEN_EOF) {
|
||||
fprintf(stderr, ERROR "ERROR: Failed to find EOF token" RESET);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf(NOTICE "All good\n" RESET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -10,8 +10,11 @@ int main() {
|
||||
//test literals
|
||||
char* str = "foobar";
|
||||
|
||||
Literal literal = TO_STRING_LITERAL(copyString(str, strlen(str)), strlen(str));
|
||||
|
||||
Node* node;
|
||||
emitNodeLiteral(&node, TO_STRING_LITERAL(copyString(str, strlen(str)), strlen(str)) );
|
||||
emitNodeLiteral(&node, literal);
|
||||
freeLiteral(literal);
|
||||
freeNode(node);
|
||||
}
|
||||
|
||||
@@ -24,9 +27,12 @@ int main() {
|
||||
Node* left;
|
||||
Node* right;
|
||||
|
||||
Literal identifier = TO_IDENTIFIER_LITERAL(copyString(idn, strlen(idn)), strlen(idn));
|
||||
Literal string = TO_STRING_LITERAL(copyString(str, strlen(str)), strlen(str));
|
||||
|
||||
emitNodeCompound(&dictionary, LITERAL_DICTIONARY);
|
||||
emitNodeLiteral(&left, TO_IDENTIFIER_LITERAL(copyString(idn, strlen(idn)), strlen(idn)) );
|
||||
emitNodeLiteral(&right, TO_STRING_LITERAL(copyString(str, strlen(str)), strlen(str)) );
|
||||
emitNodeLiteral(&left, identifier);
|
||||
emitNodeLiteral(&right, string);
|
||||
|
||||
//grow the node if needed
|
||||
if (dictionary->compound.capacity < dictionary->compound.count + 1) {
|
||||
@@ -37,12 +43,12 @@ int main() {
|
||||
}
|
||||
|
||||
//store the left and right in the node
|
||||
Node* pair = NULL;
|
||||
emitNodePair(&pair, left, right);
|
||||
dictionary->compound.nodes[dictionary->compound.count++] = *pair;
|
||||
setNodePair(&dictionary->compound.nodes[dictionary->compound.count++], left, right);
|
||||
|
||||
//the real test
|
||||
freeNode(dictionary);
|
||||
freeLiteral(identifier);
|
||||
freeLiteral(string);
|
||||
}
|
||||
|
||||
printf(NOTICE "All good\n" RESET);
|
||||
|
||||
120
test/test_parser.c
Normal file
120
test/test_parser.c
Normal file
@@ -0,0 +1,120 @@
|
||||
#include "parser.h"
|
||||
|
||||
#include "console_colors.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
//IO functions
|
||||
char* readFile(char* path, size_t* fileSize) {
|
||||
FILE* file = fopen(path, "rb");
|
||||
|
||||
if (file == NULL) {
|
||||
fprintf(stderr, ERROR "Could not open file \"%s\"\n" RESET, path);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
fseek(file, 0L, SEEK_END);
|
||||
*fileSize = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
char* buffer = (char*)malloc(*fileSize + 1);
|
||||
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, ERROR "Not enough memory to read \"%s\"\n" RESET, path);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
size_t bytesRead = fread(buffer, sizeof(char), *fileSize, file);
|
||||
|
||||
buffer[*fileSize] = '\0'; //NOTE: fread doesn't append this
|
||||
|
||||
if (bytesRead < *fileSize) {
|
||||
fprintf(stderr, ERROR "Could not read file \"%s\"\n" RESET, path);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int main() {
|
||||
{
|
||||
//source
|
||||
char* source = "print null;";
|
||||
|
||||
//test init & quit
|
||||
Lexer lexer;
|
||||
Parser parser;
|
||||
initLexer(&lexer, source);
|
||||
initParser(&parser, &lexer);
|
||||
|
||||
freeParser(&parser);
|
||||
}
|
||||
|
||||
{
|
||||
//source
|
||||
char* source = "print null;";
|
||||
|
||||
//test parsing
|
||||
Lexer lexer;
|
||||
Parser parser;
|
||||
initLexer(&lexer, source);
|
||||
initParser(&parser, &lexer);
|
||||
|
||||
Node* node = scanParser(&parser);
|
||||
|
||||
//inspect the node
|
||||
if (node == NULL) {
|
||||
fprintf(stderr, ERROR "ERROR: Node is null" RESET);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (node->type != NODE_UNARY || node->unary.opcode != OP_PRINT) {
|
||||
fprintf(stderr, ERROR "ERROR: Node is not a print instruction" RESET);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (node->unary.child->type != NODE_LITERAL || !IS_NULL(node->unary.child->atomic.literal)) {
|
||||
fprintf(stderr, ERROR "ERROR: Node to be printed is not a null value" RESET);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
freeNode(node);
|
||||
freeParser(&parser);
|
||||
}
|
||||
|
||||
{
|
||||
//get the source file
|
||||
size_t size = 0;
|
||||
char* source = readFile("sample_code.toy", &size);
|
||||
|
||||
//test parsing a chunk of junk (valgrind will find leaks)
|
||||
Lexer lexer;
|
||||
Parser parser;
|
||||
initLexer(&lexer, source);
|
||||
initParser(&parser, &lexer);
|
||||
|
||||
Node* node = scanParser(&parser);
|
||||
|
||||
while (node != NULL) {
|
||||
if (node->type == NODE_ERROR) {
|
||||
fprintf(stderr, ERROR "ERROR: Error node detected" RESET);
|
||||
return -1;
|
||||
}
|
||||
|
||||
freeNode(node);
|
||||
node = scanParser(&parser);
|
||||
}
|
||||
|
||||
//cleanup
|
||||
freeParser(&parser);
|
||||
free((void*)source);
|
||||
}
|
||||
printf(NOTICE "All good\n" RESET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user