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.
This commit is contained in:
2023-01-25 12:52:07 +00:00
parent 047ccc5f16
commit 2e2bee4fa3
55 changed files with 4837 additions and 4707 deletions

View File

@@ -1,14 +1,14 @@
#include "ast_node.h"
#include "toy_ast_node.h"
#include "memory.h"
#include "console_colors.h"
#include "toy_memory.h"
#include "toy_console_colors.h"
#include <stdio.h>
#include <stdlib.h>
//lazy
#define ASSERT(test_for_true) if (!(test_for_true)) {\
fprintf(stderr, ERROR "assert failed: %s\n" RESET, #test_for_true); \
fprintf(stderr, TOY_CC_ERROR "assert failed: %s\n" TOY_CC_RESET, #test_for_true); \
exit(-1); \
}
@@ -17,61 +17,61 @@ int main() {
{
//test literals
char* str = "foobar";
Literal literal = TO_STRING_LITERAL(createRefString(str));
Toy_Literal literal = TOY_TO_STRING_LITERAL(Toy_createRefString(str));
//generate the node
ASTNode* node = NULL;
emitASTNodeLiteral(&node, literal);
Toy_ASTNode* node = NULL;
Toy_emitASTNodeLiteral(&node, literal);
//check node type
ASSERT(node->type == AST_NODE_LITERAL);
ASSERT(node->type == TOY_AST_NODE_LITERAL);
//cleanup
freeLiteral(literal);
freeASTNode(node);
Toy_freeLiteral(literal);
Toy_freeASTNode(node);
}
//test unary
{
//generate the child node
char* str = "foobar";
Literal literal = TO_STRING_LITERAL(createRefString(str));
ASTNode* childNode = NULL;
emitASTNodeLiteral(&childNode, literal);
Toy_Literal literal = TOY_TO_STRING_LITERAL(Toy_createRefString(str));
Toy_ASTNode* childNode = NULL;
Toy_emitASTNodeLiteral(&childNode, literal);
//generate the unary node
ASTNode* unary = NULL;
emitASTNodeUnary(&unary, OP_PRINT, childNode);
Toy_ASTNode* unary = NULL;
Toy_emitASTNodeUnary(&unary, TOY_OP_PRINT, childNode);
//check node type
ASSERT(unary->type == AST_NODE_UNARY);
ASSERT(unary->type == TOY_AST_NODE_UNARY);
//cleanup
freeLiteral(literal);
freeASTNode(unary);
Toy_freeLiteral(literal);
Toy_freeASTNode(unary);
}
//test binary
{
//generate the child node
char* str = "foobar";
Literal literal = TO_STRING_LITERAL(createRefString(str));
ASTNode* nodeHandle = NULL;
emitASTNodeLiteral(&nodeHandle, literal);
Toy_Literal literal = TOY_TO_STRING_LITERAL(Toy_createRefString(str));
Toy_ASTNode* nodeHandle = NULL;
Toy_emitASTNodeLiteral(&nodeHandle, literal);
ASTNode* rhsChildNode = NULL;
emitASTNodeLiteral(&rhsChildNode, literal);
Toy_ASTNode* rhsChildNode = NULL;
Toy_emitASTNodeLiteral(&rhsChildNode, literal);
//generate the unary node
emitASTNodeBinary(&nodeHandle, rhsChildNode, OP_PRINT);
Toy_emitASTNodeBinary(&nodeHandle, rhsChildNode, TOY_OP_PRINT);
//check node type
ASSERT(nodeHandle->type == AST_NODE_BINARY);
ASSERT(nodeHandle->binary.opcode == OP_PRINT);
ASSERT(nodeHandle->type == TOY_AST_NODE_BINARY);
ASSERT(nodeHandle->binary.opcode == TOY_OP_PRINT);
//cleanup
freeLiteral(literal);
freeASTNode(nodeHandle);
Toy_freeLiteral(literal);
Toy_freeASTNode(nodeHandle);
}
//TODO: more tests for other AST node types
@@ -82,35 +82,35 @@ int main() {
char* idn = "foobar";
char* str = "hello world";
ASTNode* dictionary;
ASTNode* left;
ASTNode* right;
Toy_ASTNode* dictionary;
Toy_ASTNode* left;
Toy_ASTNode* right;
Literal identifier = TO_IDENTIFIER_LITERAL(createRefString(idn));
Literal string = TO_STRING_LITERAL(createRefString(str));
Toy_Literal identifier = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(idn));
Toy_Literal string = TOY_TO_STRING_LITERAL(Toy_createRefString(str));
emitASTNodeCompound(&dictionary, LITERAL_DICTIONARY);
emitASTNodeLiteral(&left, identifier);
emitASTNodeLiteral(&right, string);
Toy_emitASTNodeCompound(&dictionary, TOY_LITERAL_DICTIONARY);
Toy_emitASTNodeLiteral(&left, identifier);
Toy_emitASTNodeLiteral(&right, string);
//grow the node if needed
if (dictionary->compound.capacity < dictionary->compound.count + 1) {
int oldCapacity = dictionary->compound.capacity;
dictionary->compound.capacity = GROW_CAPACITY(oldCapacity);
dictionary->compound.nodes = GROW_ARRAY(ASTNode, dictionary->compound.nodes, oldCapacity, dictionary->compound.capacity);
dictionary->compound.capacity = TOY_GROW_CAPACITY(oldCapacity);
dictionary->compound.nodes = TOY_GROW_ARRAY(Toy_ASTNode, dictionary->compound.nodes, oldCapacity, dictionary->compound.capacity);
}
//store the left and right in the node
setASTNodePair(&dictionary->compound.nodes[dictionary->compound.count++], left, right);
Toy_setASTNodePair(&dictionary->compound.nodes[dictionary->compound.count++], left, right);
//the real test
freeASTNode(dictionary);
freeLiteral(identifier);
freeLiteral(string);
Toy_freeASTNode(dictionary);
Toy_freeLiteral(identifier);
Toy_freeLiteral(string);
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,11 +1,11 @@
#include "lexer.h"
#include "parser.h"
#include "compiler.h"
#include "interpreter.h"
#include "toy_lexer.h"
#include "toy_parser.h"
#include "toy_compiler.h"
#include "toy_interpreter.h"
#include "console_colors.h"
#include "toy_console_colors.h"
#include "memory.h"
#include "toy_memory.h"
#include "../repl/repl_tools.h"
@@ -27,28 +27,28 @@ void error(char* msg) {
int main() {
{
size_t size = 0;
char* source = readFile("scripts/call-from-host.toy", &size);
unsigned char* tb = compileString(source, &size);
char* source = Toy_readFile("scripts/call-from-host.toy", &size);
unsigned char* tb = Toy_compileString(source, &size);
free((void*)source);
if (!tb) {
return -1;
}
Interpreter interpreter;
initInterpreter(&interpreter);
runInterpreter(&interpreter, tb, size);
Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter);
Toy_runInterpreter(&interpreter, tb, size);
//test answer
{
interpreter.printOutput("Testing answer");
LiteralArray arguments;
initLiteralArray(&arguments);
LiteralArray returns;
initLiteralArray(&returns);
Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Toy_LiteralArray returns;
Toy_initLiteralArray(&returns);
callFn(&interpreter, "answer", &arguments, &returns);
Toy_callFn(&interpreter, "answer", &arguments, &returns);
//check the results
if (arguments.count != 0) {
@@ -59,29 +59,29 @@ int main() {
error("Returns has the wrong number of members\n");
}
if (!IS_INTEGER(returns.literals[0]) || AS_INTEGER(returns.literals[0]) != 42) {
if (!TOY_IS_INTEGER(returns.literals[0]) || TOY_AS_INTEGER(returns.literals[0]) != 42) {
error("Returned value is incorrect\n");
}
freeLiteralArray(&arguments);
freeLiteralArray(&returns);
Toy_freeLiteralArray(&arguments);
Toy_freeLiteralArray(&returns);
}
//test identity
{
interpreter.printOutput("Testing identity");
LiteralArray arguments;
initLiteralArray(&arguments);
LiteralArray returns;
initLiteralArray(&returns);
Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Toy_LiteralArray returns;
Toy_initLiteralArray(&returns);
//push an argument
float pi = 3.14;
Literal arg = TO_FLOAT_LITERAL(pi);
pushLiteralArray(&arguments, arg);
Toy_Literal arg = TOY_TO_FLOAT_LITERAL(pi);
Toy_pushLiteralArray(&arguments, arg);
callFn(&interpreter, "identity", &arguments, &returns);
Toy_callFn(&interpreter, "identity", &arguments, &returns);
//check the results
if (arguments.count != 0) {
@@ -94,24 +94,24 @@ int main() {
float epsilon = 0.1; //because floats are evil
if (!IS_FLOAT(returns.literals[0]) || fabs(AS_FLOAT(returns.literals[0]) - pi) > epsilon) {
if (!TOY_IS_FLOAT(returns.literals[0]) || fabs(TOY_AS_FLOAT(returns.literals[0]) - pi) > epsilon) {
error("Returned value is incorrect\n");
}
freeLiteralArray(&arguments);
freeLiteralArray(&returns);
Toy_freeLiteralArray(&arguments);
Toy_freeLiteralArray(&returns);
}
//test makeCounter (closures)
{
interpreter.printOutput("Testing makeCounter (closures)");
LiteralArray arguments;
initLiteralArray(&arguments);
LiteralArray returns;
initLiteralArray(&returns);
Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Toy_LiteralArray returns;
Toy_initLiteralArray(&returns);
callFn(&interpreter, "makeCounter", &arguments, &returns);
Toy_callFn(&interpreter, "makeCounter", &arguments, &returns);
//check the results
if (arguments.count != 0) {
@@ -123,19 +123,19 @@ int main() {
}
//grab the resulting literal
Literal counter = popLiteralArray(&returns);
Toy_Literal counter = Toy_popLiteralArray(&returns);
freeLiteralArray(&arguments);
freeLiteralArray(&returns);
Toy_freeLiteralArray(&arguments);
Toy_freeLiteralArray(&returns);
//call counter repeatedly
{
LiteralArray arguments;
initLiteralArray(&arguments);
LiteralArray returns;
initLiteralArray(&returns);
Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Toy_LiteralArray returns;
Toy_initLiteralArray(&returns);
callLiteralFn(&interpreter, counter, &arguments, &returns);
Toy_callLiteralFn(&interpreter, counter, &arguments, &returns);
//check the results
if (arguments.count != 0) {
@@ -146,21 +146,21 @@ int main() {
error("Returns (1) has the wrong number of members\n");
}
if (!IS_INTEGER(returns.literals[0]) || AS_INTEGER(returns.literals[0]) != 1) {
if (!TOY_IS_INTEGER(returns.literals[0]) || TOY_AS_INTEGER(returns.literals[0]) != 1) {
error("Returned value (1) is incorrect\n");
}
freeLiteralArray(&arguments);
freeLiteralArray(&returns);
Toy_freeLiteralArray(&arguments);
Toy_freeLiteralArray(&returns);
}
{
LiteralArray arguments;
initLiteralArray(&arguments);
LiteralArray returns;
initLiteralArray(&returns);
Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Toy_LiteralArray returns;
Toy_initLiteralArray(&returns);
callLiteralFn(&interpreter, counter, &arguments, &returns);
Toy_callLiteralFn(&interpreter, counter, &arguments, &returns);
//check the results
if (arguments.count != 0) {
@@ -171,21 +171,21 @@ int main() {
error("Returns (2) has the wrong number of members\n");
}
if (!IS_INTEGER(returns.literals[0]) || AS_INTEGER(returns.literals[0]) != 2) {
if (!TOY_IS_INTEGER(returns.literals[0]) || TOY_AS_INTEGER(returns.literals[0]) != 2) {
error("Returned value (2) is incorrect\n");
}
freeLiteralArray(&arguments);
freeLiteralArray(&returns);
Toy_freeLiteralArray(&arguments);
Toy_freeLiteralArray(&returns);
}
{
LiteralArray arguments;
initLiteralArray(&arguments);
LiteralArray returns;
initLiteralArray(&returns);
Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Toy_LiteralArray returns;
Toy_initLiteralArray(&returns);
callLiteralFn(&interpreter, counter, &arguments, &returns);
Toy_callLiteralFn(&interpreter, counter, &arguments, &returns);
//check the results
if (arguments.count != 0) {
@@ -196,36 +196,36 @@ int main() {
error("Returns (3) has the wrong number of members\n");
}
if (!IS_INTEGER(returns.literals[0]) || AS_INTEGER(returns.literals[0]) != 3) {
if (!TOY_IS_INTEGER(returns.literals[0]) || TOY_AS_INTEGER(returns.literals[0]) != 3) {
error("Returned value (3) is incorrect\n");
}
freeLiteralArray(&arguments);
freeLiteralArray(&returns);
Toy_freeLiteralArray(&arguments);
Toy_freeLiteralArray(&returns);
}
freeLiteral(counter);
Toy_freeLiteral(counter);
}
//test assertion failure
{
interpreter.printOutput("Testing assertion failure");
setInterpreterAssert(&interpreter, noPrintFn);
Toy_setInterpreterAssert(&interpreter, noPrintFn);
LiteralArray arguments;
initLiteralArray(&arguments);
LiteralArray returns;
initLiteralArray(&returns);
Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Toy_LiteralArray returns;
Toy_initLiteralArray(&returns);
bool ret = callFn(&interpreter, "fail", &arguments, &returns);
bool ret = Toy_callFn(&interpreter, "fail", &arguments, &returns);
//check the results
if (arguments.count != 0) {
error("Arguments has the wrong number of members\n");
}
if (returns.count != 1 || !IS_NULL(returns.literals[0])) {
if (returns.count != 1 || !TOY_IS_NULL(returns.literals[0])) {
error("Returns has the wrong number of members\n");
}
@@ -233,15 +233,15 @@ int main() {
error("Assertion gives the wrong return value\n");
}
freeLiteralArray(&arguments);
freeLiteralArray(&returns);
Toy_freeLiteralArray(&arguments);
Toy_freeLiteralArray(&returns);
}
//clean up
freeInterpreter(&interpreter);
Toy_freeInterpreter(&interpreter);
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,10 +1,10 @@
#include "lexer.h"
#include "parser.h"
#include "compiler.h"
#include "toy_lexer.h"
#include "toy_parser.h"
#include "toy_compiler.h"
#include "console_colors.h"
#include "toy_console_colors.h"
#include "memory.h"
#include "toy_memory.h"
#include "../repl/repl_tools.h"
@@ -15,9 +15,9 @@
int main() {
{
//test init & free
Compiler compiler;
initCompiler(&compiler);
freeCompiler(&compiler);
Toy_Compiler compiler;
Toy_initCompiler(&compiler);
Toy_freeCompiler(&compiler);
}
{
@@ -25,70 +25,70 @@ int main() {
char* source = "print null;";
//test basic compilation & collation
Lexer lexer;
Parser parser;
Compiler compiler;
Toy_Lexer lexer;
Toy_Parser parser;
Toy_Compiler compiler;
initLexer(&lexer, source);
initParser(&parser, &lexer);
initCompiler(&compiler);
Toy_initLexer(&lexer, source);
Toy_initParser(&parser, &lexer);
Toy_initCompiler(&compiler);
ASTNode* node = scanParser(&parser);
Toy_ASTNode* node = Toy_scanParser(&parser);
//write
writeCompiler(&compiler, node);
Toy_writeCompiler(&compiler, node);
//collate
int size = 0;
unsigned char* bytecode = collateCompiler(&compiler, &size);
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
//cleanup
FREE_ARRAY(unsigned char, bytecode, size);
freeASTNode(node);
freeParser(&parser);
freeCompiler(&compiler);
TOY_FREE_ARRAY(unsigned char, bytecode, size);
Toy_freeASTNode(node);
Toy_freeParser(&parser);
Toy_freeCompiler(&compiler);
}
{
//source
size_t sourceLength = 0;
char* source = readFile("scripts/compiler_sample_code.toy", &sourceLength);
char* source = Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength);
//test basic compilation & collation
Lexer lexer;
Parser parser;
Compiler compiler;
Toy_Lexer lexer;
Toy_Parser parser;
Toy_Compiler compiler;
initLexer(&lexer, source);
initParser(&parser, &lexer);
initCompiler(&compiler);
Toy_initLexer(&lexer, source);
Toy_initParser(&parser, &lexer);
Toy_initCompiler(&compiler);
ASTNode* node = scanParser(&parser);
Toy_ASTNode* node = Toy_scanParser(&parser);
while (node != NULL) {
if (node->type == AST_NODE_ERROR) {
fprintf(stderr, ERROR "ERROR: Error node found" RESET);
if (node->type == TOY_AST_NODE_ERROR) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Error node found" TOY_CC_RESET);
return -1;
}
//write
writeCompiler(&compiler, node);
freeASTNode(node);
Toy_writeCompiler(&compiler, node);
Toy_freeASTNode(node);
node = scanParser(&parser);
node = Toy_scanParser(&parser);
}
//collate
int size = 0;
unsigned char* bytecode = collateCompiler(&compiler, &size);
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
//cleanup
FREE_ARRAY(char, source, sourceLength);
FREE_ARRAY(unsigned char, bytecode, size);
freeParser(&parser);
freeCompiler(&compiler);
TOY_FREE_ARRAY(char, source, sourceLength);
TOY_FREE_ARRAY(unsigned char, bytecode, size);
Toy_freeParser(&parser);
Toy_freeCompiler(&compiler);
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,11 +1,11 @@
#include "lexer.h"
#include "parser.h"
#include "compiler.h"
#include "interpreter.h"
#include "toy_lexer.h"
#include "toy_parser.h"
#include "toy_compiler.h"
#include "toy_interpreter.h"
#include "console_colors.h"
#include "toy_console_colors.h"
#include "memory.h"
#include "toy_memory.h"
#include "../repl/repl_tools.h"
@@ -24,27 +24,27 @@ static void noAssertFn(const char* output) {
ignoredAssertions++;
}
else {
fprintf(stderr, ERROR "Assertion failure: ");
fprintf(stderr, TOY_CC_ERROR "Assertion failure: ");
fprintf(stderr, "%s", output);
fprintf(stderr, "\n" RESET); //default new line
fprintf(stderr, "\n" TOY_CC_RESET); //default new line
}
}
void runBinaryCustom(unsigned char* tb, size_t size) {
Interpreter interpreter;
initInterpreter(&interpreter);
Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter);
//NOTE: suppress print output for testing
setInterpreterPrint(&interpreter, noPrintFn);
setInterpreterAssert(&interpreter, noAssertFn);
Toy_setInterpreterPrint(&interpreter, noPrintFn);
Toy_setInterpreterAssert(&interpreter, noAssertFn);
runInterpreter(&interpreter, tb, size);
freeInterpreter(&interpreter);
Toy_runInterpreter(&interpreter, tb, size);
Toy_freeInterpreter(&interpreter);
}
void runSourceCustom(char* source) {
size_t size = 0;
unsigned char* tb = compileString(source, &size);
unsigned char* tb = Toy_compileString(source, &size);
if (!tb) {
return;
}
@@ -53,7 +53,7 @@ void runSourceCustom(char* source) {
void runSourceFileCustom(char* fname) {
size_t size = 0; //not used
char* source = readFile(fname, &size);
char* source = Toy_readFile(fname, &size);
runSourceCustom(source);
free((void*)source);
}
@@ -61,9 +61,9 @@ void runSourceFileCustom(char* fname) {
int main() {
{
//test init & free
Interpreter interpreter;
initInterpreter(&interpreter);
freeInterpreter(&interpreter);
Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter);
Toy_freeInterpreter(&interpreter);
}
{
@@ -71,37 +71,37 @@ int main() {
char* source = "print null;";
//test basic compilation & collation
Lexer lexer;
Parser parser;
Compiler compiler;
Interpreter interpreter;
Toy_Lexer lexer;
Toy_Parser parser;
Toy_Compiler compiler;
Toy_Interpreter interpreter;
initLexer(&lexer, source);
initParser(&parser, &lexer);
initCompiler(&compiler);
initInterpreter(&interpreter);
Toy_initLexer(&lexer, source);
Toy_initParser(&parser, &lexer);
Toy_initCompiler(&compiler);
Toy_initInterpreter(&interpreter);
ASTNode* node = scanParser(&parser);
Toy_ASTNode* node = Toy_scanParser(&parser);
//write
writeCompiler(&compiler, node);
Toy_writeCompiler(&compiler, node);
//collate
int size = 0;
unsigned char* bytecode = collateCompiler(&compiler, &size);
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
//NOTE: suppress print output for testing
setInterpreterPrint(&interpreter, noPrintFn);
setInterpreterAssert(&interpreter, noAssertFn);
Toy_setInterpreterPrint(&interpreter, noPrintFn);
Toy_setInterpreterAssert(&interpreter, noAssertFn);
//run
runInterpreter(&interpreter, bytecode, size);
Toy_runInterpreter(&interpreter, bytecode, size);
//cleanup
freeASTNode(node);
freeParser(&parser);
freeCompiler(&compiler);
freeInterpreter(&interpreter);
Toy_freeASTNode(node);
Toy_freeParser(&parser);
Toy_freeCompiler(&compiler);
Toy_freeInterpreter(&interpreter);
}
{
@@ -144,11 +144,11 @@ int main() {
//1, to allow for the assertion test
if (ignoredAssertions > 1) {
fprintf(stderr, ERROR "Assertions hidden: %d\n", ignoredAssertions);
fprintf(stderr, TOY_CC_ERROR "Assertions hidden: %d\n", ignoredAssertions);
return -1;
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,6 +1,6 @@
#include "lexer.h"
#include "toy_lexer.h"
#include "console_colors.h"
#include "toy_console_colors.h"
#include <stdio.h>
#include <string.h>
@@ -11,39 +11,39 @@ int main() {
char* source = "print null;";
//test init & quit
Lexer lexer;
initLexer(&lexer, source);
Toy_Lexer lexer;
Toy_initLexer(&lexer, source);
//get each token
Token print = scanLexer(&lexer);
Token null = scanLexer(&lexer);
Token semi = scanLexer(&lexer);
Token eof = scanLexer(&lexer);
Toy_Token print = Toy_scanLexer(&lexer);
Toy_Token null = Toy_scanLexer(&lexer);
Toy_Token semi = Toy_scanLexer(&lexer);
Toy_Token eof = Toy_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);
fprintf(stderr, TOY_CC_ERROR "ERROR: print lexeme is wrong: %s" TOY_CC_RESET, print.lexeme);
return -1;
}
if (strncmp(null.lexeme, "null", null.length)) {
fprintf(stderr, ERROR "ERROR: null lexeme is wrong: %s" RESET, null.lexeme);
fprintf(stderr, TOY_CC_ERROR "ERROR: null lexeme is wrong: %s" TOY_CC_RESET, null.lexeme);
return -1;
}
if (strncmp(semi.lexeme, ";", semi.length)) {
fprintf(stderr, ERROR "ERROR: semicolon lexeme is wrong: %s" RESET, semi.lexeme);
fprintf(stderr, TOY_CC_ERROR "ERROR: semicolon lexeme is wrong: %s" TOY_CC_RESET, semi.lexeme);
return -1;
}
if (eof.type != TOKEN_EOF) {
fprintf(stderr, ERROR "ERROR: Failed to find EOF token" RESET);
if (eof.type != TOY_TOKEN_EOF) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to find EOF token" TOY_CC_RESET);
return -1;
}
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,11 +1,11 @@
#include "lexer.h"
#include "parser.h"
#include "compiler.h"
#include "interpreter.h"
#include "toy_lexer.h"
#include "toy_parser.h"
#include "toy_compiler.h"
#include "toy_interpreter.h"
#include "console_colors.h"
#include "toy_console_colors.h"
#include "memory.h"
#include "toy_memory.h"
#include <stdio.h>
#include <stdlib.h>
@@ -25,57 +25,57 @@ static void noPrintFn(const char* output) {
static int failedAsserts = 0;
static void assertWrapper(const char* output) {
failedAsserts++;
fprintf(stderr, ERROR "Assertion failure: ");
fprintf(stderr, TOY_CC_ERROR "Assertion failure: ");
fprintf(stderr, "%s", output);
fprintf(stderr, "\n" RESET); //default new line
fprintf(stderr, "\n" TOY_CC_RESET); //default new line
}
static void errorWrapper(const char* output) {
failedAsserts++;
fprintf(stderr, ERROR "%s" RESET, output);
fprintf(stderr, TOY_CC_ERROR "%s" TOY_CC_RESET, output);
}
void runBinaryWithLibrary(unsigned char* tb, size_t size, char* library, HookFn hook) {
Interpreter interpreter;
initInterpreter(&interpreter);
void runBinaryWithLibrary(unsigned char* tb, size_t size, char* library, Toy_HookFn hook) {
Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter);
//NOTE: supress print output for testing
setInterpreterPrint(&interpreter, noPrintFn);
setInterpreterAssert(&interpreter, assertWrapper);
setInterpreterError(&interpreter, errorWrapper);
Toy_setInterpreterPrint(&interpreter, noPrintFn);
Toy_setInterpreterAssert(&interpreter, assertWrapper);
Toy_setInterpreterError(&interpreter, errorWrapper);
//inject the standard libraries into this interpreter
injectNativeHook(&interpreter, library, hook);
Toy_injectNativeHook(&interpreter, library, hook);
runInterpreter(&interpreter, tb, size);
freeInterpreter(&interpreter);
Toy_runInterpreter(&interpreter, tb, size);
Toy_freeInterpreter(&interpreter);
}
typedef struct Payload {
char* fname;
char* libname;
HookFn hook;
Toy_HookFn hook;
} Payload;
int main() {
//setup the runner filesystem (hacky)
initDriveDictionary();
Toy_initDriveDictionary();
Literal driveLiteral = TO_STRING_LITERAL(createRefString("scripts"));
Literal pathLiteral = TO_STRING_LITERAL(createRefString("scripts"));
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts"));
Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts"));
setLiteralDictionary(getDriveDictionary(), driveLiteral, pathLiteral);
Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral);
freeLiteral(driveLiteral);
freeLiteral(pathLiteral);
Toy_freeLiteral(driveLiteral);
Toy_freeLiteral(pathLiteral);
{
//run each file in test/scripts
Payload payloads[] = {
{"interactions.toy", "standard", hookStandard}, //interactions needs standard
{"standard.toy", "standard", hookStandard},
{"timer.toy", "timer", hookTimer},
{"runner.toy", "runner", hookRunner},
{"interactions.toy", "standard", Toy_hookStandard}, //interactions needs standard
{"standard.toy", "standard", Toy_hookStandard},
{"timer.toy", "timer", Toy_hookTimer},
{"runner.toy", "runner", Toy_hookRunner},
{NULL, NULL, NULL}
};
@@ -87,18 +87,18 @@ int main() {
//compile the source
size_t size = 0;
char* source = readFile(fname, &size);
char* source = Toy_readFile(fname, &size);
if (!source) {
printf(ERROR "Failed to load file: %s\n" RESET, fname);
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
failedAsserts++;
continue;
}
unsigned char* tb = compileString(source, &size);
unsigned char* tb = Toy_compileString(source, &size);
free((void*)source);
if (!tb) {
printf(ERROR "Failed to compile file: %s\n" RESET, fname);
printf(TOY_CC_ERROR "Failed to compile file: %s\n" TOY_CC_RESET, fname);
failedAsserts++;
continue;
}
@@ -108,13 +108,13 @@ int main() {
}
//lib cleanup
freeDriveDictionary();
Toy_freeDriveDictionary();
if (!failedAsserts) {
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
}
else {
printf(WARN "Problems detected in libraries\n" RESET);
printf(TOY_CC_WARN "Problems detected in libraries\n" TOY_CC_RESET);
}
return failedAsserts;

View File

@@ -1,28 +1,28 @@
#include "literal.h"
#include "toy_literal.h"
#include "memory.h"
#include "console_colors.h"
#include "toy_memory.h"
#include "toy_console_colors.h"
#include <stdio.h>
int main() {
{
//test a single null literal
Literal literal = TO_NULL_LITERAL;
Toy_Literal literal = TOY_TO_NULL_LITERAL;
if (!IS_NULL(literal)) {
fprintf(stderr, ERROR "ERROR: null literal failed\n" RESET);
if (!TOY_IS_NULL(literal)) {
fprintf(stderr, TOY_CC_ERROR "ERROR: null literal failed\n" TOY_CC_RESET);
return -1;
}
}
{
//test boolean literals
Literal t = TO_BOOLEAN_LITERAL(true);
Literal f = TO_BOOLEAN_LITERAL(false);
Toy_Literal t = TOY_TO_BOOLEAN_LITERAL(true);
Toy_Literal f = TOY_TO_BOOLEAN_LITERAL(false);
if (!IS_TRUTHY(t) || IS_TRUTHY(f)) {
fprintf(stderr, ERROR "ERROR: boolean literal failed\n" RESET);
if (!TOY_IS_TRUTHY(t) || TOY_IS_TRUTHY(f)) {
fprintf(stderr, TOY_CC_ERROR "ERROR: boolean literal failed\n" TOY_CC_RESET);
return -1;
}
}
@@ -31,20 +31,20 @@ int main() {
//test string literals
char* buffer = "Hello world";
Literal literal = TO_STRING_LITERAL(createRefString(buffer));
Toy_Literal literal = TOY_TO_STRING_LITERAL(Toy_createRefString(buffer));
freeLiteral(literal);
Toy_freeLiteral(literal);
}
{
//test identifier literals
char buffer[] = "Hello world";
Literal literal = TO_IDENTIFIER_LITERAL(createRefString(buffer));
Toy_Literal literal = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(buffer));
freeLiteral(literal);
Toy_freeLiteral(literal);
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,72 +1,72 @@
#include "literal_array.h"
#include "toy_literal_array.h"
#include "memory.h"
#include "console_colors.h"
#include "toy_memory.h"
#include "toy_console_colors.h"
#include <stdio.h>
int main() {
{
//test init & cleanup
LiteralArray array;
initLiteralArray(&array);
freeLiteralArray(&array);
Toy_LiteralArray array;
Toy_initLiteralArray(&array);
Toy_freeLiteralArray(&array);
}
{
//test pushing and pulling
LiteralArray array;
initLiteralArray(&array);
Toy_LiteralArray array;
Toy_initLiteralArray(&array);
for (int i = 0; i < 100; i++) {
pushLiteralArray(&array, TO_INTEGER_LITERAL(i));
Toy_pushLiteralArray(&array, TOY_TO_INTEGER_LITERAL(i));
}
for (int i = 0; i < 90; i++) {
Literal lit = popLiteralArray(&array);
Toy_Literal lit = Toy_popLiteralArray(&array);
freeLiteral(lit);
Toy_freeLiteral(lit);
}
if (array.count != 10) {
fprintf(stderr, ERROR "ERROR: Array didn't clear the correct number of literal integers\n" RESET);
freeLiteralArray(&array);
fprintf(stderr, TOY_CC_ERROR "ERROR: Array didn't clear the correct number of literal integers\n" TOY_CC_RESET);
Toy_freeLiteralArray(&array);
return -1;
}
freeLiteralArray(&array);
Toy_freeLiteralArray(&array);
}
{
//check string, identifier and compound type behaviours
LiteralArray array;
initLiteralArray(&array);
Toy_LiteralArray array;
Toy_initLiteralArray(&array);
//raw
char* str_raw = "hello world";
char* idn_raw = "foobar";
Literal string = TO_STRING_LITERAL(createRefString(str_raw));
Literal identifier = TO_IDENTIFIER_LITERAL(createRefString(idn_raw));
Toy_Literal string = TOY_TO_STRING_LITERAL(Toy_createRefString(str_raw));
Toy_Literal identifier = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(idn_raw));
//[string : string]
Literal type = TO_TYPE_LITERAL(LITERAL_DICTIONARY, false);
TYPE_PUSH_SUBTYPE(&type, TO_TYPE_LITERAL(LITERAL_STRING, false));
TYPE_PUSH_SUBTYPE(&type, TO_TYPE_LITERAL(LITERAL_STRING, false));
Toy_Literal type = TOY_TO_TYPE_LITERAL(TOY_LITERAL_DICTIONARY, false);
TOY_TYPE_PUSH_SUBTYPE(&type, TOY_TO_TYPE_LITERAL(TOY_LITERAL_STRING, false));
TOY_TYPE_PUSH_SUBTYPE(&type, TOY_TO_TYPE_LITERAL(TOY_LITERAL_STRING, false));
//push
pushLiteralArray(&array, string);
pushLiteralArray(&array, identifier);
pushLiteralArray(&array, type);
Toy_pushLiteralArray(&array, string);
Toy_pushLiteralArray(&array, identifier);
Toy_pushLiteralArray(&array, type);
//free the local literals
freeLiteral(string);
freeLiteral(identifier);
freeLiteral(type);
Toy_freeLiteral(string);
Toy_freeLiteral(identifier);
Toy_freeLiteral(type);
freeLiteralArray(&array);
Toy_freeLiteralArray(&array);
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,16 +1,16 @@
#include "literal_dictionary.h"
#include "toy_literal_dictionary.h"
#include "memory.h"
#include "console_colors.h"
#include "toy_memory.h"
#include "toy_console_colors.h"
#include <stdio.h>
int main() {
{
//test init & cleanup
LiteralDictionary dictionary;
initLiteralDictionary(&dictionary);
freeLiteralDictionary(&dictionary);
Toy_LiteralDictionary dictionary;
Toy_initLiteralDictionary(&dictionary);
Toy_freeLiteralDictionary(&dictionary);
}
{
@@ -18,21 +18,21 @@ int main() {
char* idn_raw = "foobar";
char* str_raw = "hello world";
Literal identifier = TO_IDENTIFIER_LITERAL(createRefString(idn_raw));
Literal string = TO_STRING_LITERAL(createRefString(str_raw));
Toy_Literal identifier = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(idn_raw));
Toy_Literal string = TOY_TO_STRING_LITERAL(Toy_createRefString(str_raw));
LiteralDictionary dictionary;
initLiteralDictionary(&dictionary);
Toy_LiteralDictionary dictionary;
Toy_initLiteralDictionary(&dictionary);
setLiteralDictionary(&dictionary, identifier, string);
Toy_setLiteralDictionary(&dictionary, identifier, string);
freeLiteral(identifier);
freeLiteral(string);
Toy_freeLiteral(identifier);
Toy_freeLiteral(string);
freeLiteralDictionary(&dictionary);
Toy_freeLiteralDictionary(&dictionary);
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,6 +1,6 @@
#include "memory.h"
#include "toy_memory.h"
#include "console_colors.h"
#include "toy_console_colors.h"
#include <stdio.h>
#include <stdlib.h>
@@ -32,29 +32,29 @@ void* allocator(void* pointer, size_t oldSize, size_t newSize) {
void testMemoryAllocation() {
{
//test single pointer
int* integer = ALLOCATE(int, 1);
FREE(int, integer);
int* integer = TOY_ALLOCATE(int, 1);
TOY_FREE(int, integer);
}
{
//test single pointer array
int* array = ALLOCATE(int, 10);
int* array = TOY_ALLOCATE(int, 10);
array[1] = 42; //access the given memory
FREE_ARRAY(int, array, 10);
TOY_FREE_ARRAY(int, array, 10);
}
{
//test multiple pointer arrays
int* array1 = ALLOCATE(int, 10);
int* array2 = ALLOCATE(int, 10);
int* array1 = TOY_ALLOCATE(int, 10);
int* array2 = TOY_ALLOCATE(int, 10);
array1[1] = 42; //access the given memory
array2[1] = 42; //access the given memory
FREE_ARRAY(int, array1, 10);
FREE_ARRAY(int, array2, 10);
TOY_FREE_ARRAY(int, array1, 10);
TOY_FREE_ARRAY(int, array2, 10);
}
}
@@ -63,14 +63,14 @@ int main() {
testMemoryAllocation();
//test the custom allocator
setMemoryAllocator(allocator);
Toy_setMemoryAllocator(allocator);
testMemoryAllocation();
if (callCount != 8) {
fprintf(stderr, ERROR "Unexpected call count for custom allocator; was called %d times" RESET, callCount);
fprintf(stderr, TOY_CC_ERROR "Unexpected call count for custom allocator; was called %d times" TOY_CC_RESET, callCount);
return -1;
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,11 +1,11 @@
#include "lexer.h"
#include "parser.h"
#include "compiler.h"
#include "interpreter.h"
#include "toy_lexer.h"
#include "toy_parser.h"
#include "toy_compiler.h"
#include "toy_interpreter.h"
#include "console_colors.h"
#include "toy_console_colors.h"
#include "memory.h"
#include "toy_memory.h"
#include "../repl/repl_tools.h"
@@ -24,37 +24,37 @@ static void noErrorFn(const char* output) {
}
unsigned char* compileStringCustom(char* source, size_t* size) {
Lexer lexer;
Parser parser;
Compiler compiler;
Toy_Lexer lexer;
Toy_Parser parser;
Toy_Compiler compiler;
initLexer(&lexer, source);
initParser(&parser, &lexer);
initCompiler(&compiler);
Toy_initLexer(&lexer, source);
Toy_initParser(&parser, &lexer);
Toy_initCompiler(&compiler);
//run the parser until the end of the source
ASTNode* node = scanParser(&parser);
Toy_ASTNode* node = Toy_scanParser(&parser);
while(node != NULL) {
//pack up and leave
if (node->type == AST_NODE_ERROR) {
if (node->type == TOY_AST_NODE_ERROR) {
errorsTriggered++; //custom error catch
freeASTNode(node);
freeCompiler(&compiler);
freeParser(&parser);
Toy_freeASTNode(node);
Toy_freeCompiler(&compiler);
Toy_freeParser(&parser);
return NULL;
}
writeCompiler(&compiler, node);
freeASTNode(node);
node = scanParser(&parser);
Toy_writeCompiler(&compiler, node);
Toy_freeASTNode(node);
node = Toy_scanParser(&parser);
}
//get the bytecode dump
unsigned char* tb = collateCompiler(&compiler, (int*)(size));
unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
//cleanup
freeCompiler(&compiler);
freeParser(&parser);
Toy_freeCompiler(&compiler);
Toy_freeParser(&parser);
//no lexer to clean up
//finally
@@ -62,15 +62,15 @@ unsigned char* compileStringCustom(char* source, size_t* size) {
}
void runBinaryCustom(unsigned char* tb, size_t size) {
Interpreter interpreter;
initInterpreter(&interpreter);
Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter);
//NOTE: suppress print output for testing
setInterpreterPrint(&interpreter, noPrintFn);
setInterpreterError(&interpreter, noErrorFn);
Toy_setInterpreterPrint(&interpreter, noPrintFn);
Toy_setInterpreterError(&interpreter, noErrorFn);
runInterpreter(&interpreter, tb, size);
freeInterpreter(&interpreter);
Toy_runInterpreter(&interpreter, tb, size);
Toy_freeInterpreter(&interpreter);
}
void runSourceCustom(char* source) {
@@ -84,7 +84,7 @@ void runSourceCustom(char* source) {
void runSourceFileCustom(char* fname) {
size_t size = 0; //not used
char* source = readFile(fname, &size);
char* source = Toy_readFile(fname, &size);
runSourceCustom(source);
free((void*)source);
}
@@ -114,7 +114,7 @@ int main() {
runSourceFileCustom(buffer);
if (errorsTriggered == 0) {
printf(ERROR "Expected error did not occur in %s\n" RESET, filenames[i]);
printf(TOY_CC_ERROR "Expected error did not occur in %s\n" TOY_CC_RESET, filenames[i]);
success = false;
}
@@ -126,7 +126,7 @@ int main() {
return -1;
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,11 +1,11 @@
#include "lexer.h"
#include "parser.h"
#include "compiler.h"
#include "interpreter.h"
#include "toy_lexer.h"
#include "toy_parser.h"
#include "toy_compiler.h"
#include "toy_interpreter.h"
#include "console_colors.h"
#include "toy_console_colors.h"
#include "memory.h"
#include "toy_memory.h"
#include "../repl/repl_tools.h"
@@ -27,39 +27,39 @@ typedef struct ArbitraryData {
int value;
} ArbitraryData;
static int produce(Interpreter* interpreter, LiteralArray* arguments) {
ArbitraryData* data = ALLOCATE(ArbitraryData, 1);
static int produce(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
ArbitraryData* data = TOY_ALLOCATE(ArbitraryData, 1);
data->value = 42;
Literal o = TO_OPAQUE_LITERAL(data, 0);
Toy_Literal o = TOY_TO_OPAQUE_LITERAL(data, 0);
pushLiteralArray(&interpreter->stack, o);
Toy_pushLiteralArray(&interpreter->stack, o);
freeLiteral(o);
Toy_freeLiteral(o);
return 1;
}
static int consume(Interpreter* interpreter, LiteralArray* arguments) {
Literal o = popLiteralArray(arguments);
static int consume(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
Toy_Literal o = Toy_popLiteralArray(arguments);
Literal idn = o;
Toy_Literal idn = o;
if (parseIdentifierToValue(interpreter, &o)) {
freeLiteral(idn);
if (Toy_parseIdentifierToValue(interpreter, &o)) {
Toy_freeLiteral(idn);
}
if (IS_OPAQUE(o) && ((ArbitraryData*)(AS_OPAQUE(o)))->value == 42) {
ArbitraryData* data = (ArbitraryData*)AS_OPAQUE(o);
if (TOY_IS_OPAQUE(o) && ((ArbitraryData*)(TOY_AS_OPAQUE(o)))->value == 42) {
ArbitraryData* data = (ArbitraryData*)TOY_AS_OPAQUE(o);
FREE(ArbitraryData, data);
TOY_FREE(ArbitraryData, data);
//all went well
freeLiteral(o);
Toy_freeLiteral(o);
return 0;
}
printf(ERROR "opaque failed: %d\n" RESET, IS_OPAQUE(o));
printf(TOY_CC_ERROR "opaque failed: %d\n" TOY_CC_RESET, TOY_IS_OPAQUE(o));
exit(-1);
return -1;
@@ -68,28 +68,28 @@ static int consume(Interpreter* interpreter, LiteralArray* arguments) {
int main() {
{
size_t size = 0;
char* source = readFile("scripts/opaque-data-type.toy", &size);
unsigned char* tb = compileString(source, &size);
char* source = Toy_readFile("scripts/opaque-data-type.toy", &size);
unsigned char* tb = Toy_compileString(source, &size);
free((void*)source);
if (!tb) {
return -1;
}
Interpreter interpreter;
initInterpreter(&interpreter);
Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter);
injectNativeFn(&interpreter, "produce", produce);
injectNativeFn(&interpreter, "consume", consume);
Toy_injectNativeFn(&interpreter, "produce", produce);
Toy_injectNativeFn(&interpreter, "consume", consume);
//run teh script
runInterpreter(&interpreter, tb, size);
Toy_runInterpreter(&interpreter, tb, size);
//clean up
freeInterpreter(&interpreter);
Toy_freeInterpreter(&interpreter);
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,6 +1,6 @@
#include "parser.h"
#include "toy_parser.h"
#include "console_colors.h"
#include "toy_console_colors.h"
#include "../repl/repl_tools.h"
@@ -14,12 +14,12 @@ int main() {
char* source = "print null;";
//test init & quit
Lexer lexer;
Parser parser;
initLexer(&lexer, source);
initParser(&parser, &lexer);
Toy_Lexer lexer;
Toy_Parser parser;
Toy_initLexer(&lexer, source);
Toy_initParser(&parser, &lexer);
freeParser(&parser);
Toy_freeParser(&parser);
}
{
@@ -27,63 +27,63 @@ int main() {
char* source = "print null;";
//test parsing
Lexer lexer;
Parser parser;
initLexer(&lexer, source);
initParser(&parser, &lexer);
Toy_Lexer lexer;
Toy_Parser parser;
Toy_initLexer(&lexer, source);
Toy_initParser(&parser, &lexer);
ASTNode* node = scanParser(&parser);
Toy_ASTNode* node = Toy_scanParser(&parser);
//inspect the node
if (node == NULL) {
fprintf(stderr, ERROR "ERROR: ASTNode is null" RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: ASTNode is null" TOY_CC_RESET);
return -1;
}
if (node->type != AST_NODE_UNARY || node->unary.opcode != OP_PRINT) {
fprintf(stderr, ERROR "ERROR: ASTNode is not a unary print instruction" RESET);
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 != AST_NODE_LITERAL || !IS_NULL(node->unary.child->atomic.literal)) {
fprintf(stderr, ERROR "ERROR: ASTNode to be printed is not a null literal" RESET);
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
freeASTNode(node);
freeParser(&parser);
Toy_freeASTNode(node);
Toy_freeParser(&parser);
}
{
//get the source file
size_t size = 0;
char* source = readFile("scripts/parser_sample_code.toy", &size);
char* source = Toy_readFile("scripts/parser_sample_code.toy", &size);
//test parsing a chunk of junk (valgrind will find leaks)
Lexer lexer;
Parser parser;
initLexer(&lexer, source);
initParser(&parser, &lexer);
Toy_Lexer lexer;
Toy_Parser parser;
Toy_initLexer(&lexer, source);
Toy_initParser(&parser, &lexer);
ASTNode* node = scanParser(&parser);
Toy_ASTNode* node = Toy_scanParser(&parser);
while (node != NULL) {
if (node->type == AST_NODE_ERROR) {
fprintf(stderr, ERROR "ERROR: Error node detected" RESET);
if (node->type == TOY_AST_NODE_ERROR) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Error node detected" TOY_CC_RESET);
return -1;
}
freeASTNode(node);
node = scanParser(&parser);
Toy_freeASTNode(node);
node = Toy_scanParser(&parser);
}
//cleanup
freeParser(&parser);
Toy_freeParser(&parser);
free((void*)source);
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

View File

@@ -1,124 +1,124 @@
#include "scope.h"
#include "toy_scope.h"
#include "memory.h"
#include "console_colors.h"
#include "toy_memory.h"
#include "toy_console_colors.h"
#include <stdio.h>
int main() {
{
//test init & quit
Scope* scope = pushScope(NULL);
scope = popScope(scope);
Toy_Scope* scope = Toy_pushScope(NULL);
scope = Toy_popScope(scope);
}
{
//prerequisites
char* idn_raw = "foobar";
Literal identifier = TO_IDENTIFIER_LITERAL(createRefString(idn_raw));
Literal value = TO_INTEGER_LITERAL(42);
Literal type = TO_TYPE_LITERAL(value.type, false);
Toy_Literal identifier = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(idn_raw));
Toy_Literal value = TOY_TO_INTEGER_LITERAL(42);
Toy_Literal type = TOY_TO_TYPE_LITERAL(value.type, false);
//test declarations & assignments
Scope* scope = pushScope(NULL);
Toy_Scope* scope = Toy_pushScope(NULL);
//declare & assign
if (!declareScopeVariable(scope, identifier, type)) {
printf(ERROR "Failed to declare scope variable" RESET);
if (!Toy_declareScopeVariable(scope, identifier, type)) {
printf(TOY_CC_ERROR "Failed to declare scope variable" TOY_CC_RESET);
return -1;
}
if (!setScopeVariable(scope, identifier, value, true)) {
printf(ERROR "Failed to sete scope variable" RESET);
if (!Toy_setScopeVariable(scope, identifier, value, true)) {
printf(TOY_CC_ERROR "Failed to sete scope variable" TOY_CC_RESET);
return -1;
}
//cleanup
scope = popScope(scope);
scope = Toy_popScope(scope);
freeLiteral(identifier);
freeLiteral(value);
freeLiteral(type);
Toy_freeLiteral(identifier);
Toy_freeLiteral(value);
Toy_freeLiteral(type);
}
{
//prerequisites
char* idn_raw = "foobar";
Literal identifier = TO_IDENTIFIER_LITERAL(createRefString(idn_raw));
Literal type = TO_TYPE_LITERAL(LITERAL_INTEGER, false);
Toy_Literal identifier = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(idn_raw));
Toy_Literal type = TOY_TO_TYPE_LITERAL(TOY_LITERAL_INTEGER, false);
//test declarations & assignments
Scope* scope = pushScope(NULL);
Toy_Scope* scope = Toy_pushScope(NULL);
//declare & assign
if (!declareScopeVariable(scope, identifier, type)) {
printf(ERROR "Failed to declare the scope variable" RESET);
if (!Toy_declareScopeVariable(scope, identifier, type)) {
printf(TOY_CC_ERROR "Failed to declare the scope variable" TOY_CC_RESET);
return -1;
}
if (!setScopeVariable(scope, identifier, TO_INTEGER_LITERAL(42), true)) {
printf(ERROR "Failed to set the scope variable" RESET);
if (!Toy_setScopeVariable(scope, identifier, TOY_TO_INTEGER_LITERAL(42), true)) {
printf(TOY_CC_ERROR "Failed to set the scope variable" TOY_CC_RESET);
return -1;
}
//deeper scope
scope = pushScope(scope);
scope = Toy_pushScope(scope);
//test shadowing
Literal ref;
if (!getScopeVariable(scope, identifier, &ref)) {
printf(ERROR "Failed to get the scope variable" RESET);
Toy_Literal ref;
if (!Toy_getScopeVariable(scope, identifier, &ref)) {
printf(TOY_CC_ERROR "Failed to get the scope variable" TOY_CC_RESET);
return -1;
}
if (AS_INTEGER(ref) != 42) {
printf(ERROR "Failed to retreive the correct variable value" RESET);
if (TOY_AS_INTEGER(ref) != 42) {
printf(TOY_CC_ERROR "Failed to retreive the correct variable value" TOY_CC_RESET);
return -1;
}
if (!declareScopeVariable(scope, identifier, type)) {
printf(ERROR "Failed to declare the scope variable (shadowing)" RESET);
if (!Toy_declareScopeVariable(scope, identifier, type)) {
printf(TOY_CC_ERROR "Failed to declare the scope variable (shadowing)" TOY_CC_RESET);
return -1;
}
if (!setScopeVariable(scope, identifier, TO_INTEGER_LITERAL(69), true)) {
printf(ERROR "Failed to set the scope variable (shadowing)" RESET);
if (!Toy_setScopeVariable(scope, identifier, TOY_TO_INTEGER_LITERAL(69), true)) {
printf(TOY_CC_ERROR "Failed to set the scope variable (shadowing)" TOY_CC_RESET);
return -1;
}
if (!getScopeVariable(scope, identifier, &ref)) {
printf(ERROR "Failed to get the scope variable (shadowing)" RESET);
if (!Toy_getScopeVariable(scope, identifier, &ref)) {
printf(TOY_CC_ERROR "Failed to get the scope variable (shadowing)" TOY_CC_RESET);
return -1;
}
if (AS_INTEGER(ref) != 69) {
printf(ERROR "Failed to retreive the correct variable value (shadowing)" RESET);
if (TOY_AS_INTEGER(ref) != 69) {
printf(TOY_CC_ERROR "Failed to retreive the correct variable value (shadowing)" TOY_CC_RESET);
return -1;
}
//unwind
scope = popScope(scope);
scope = Toy_popScope(scope);
if (!getScopeVariable(scope, identifier, &ref)) {
printf(ERROR "Failed to get the scope variable" RESET);
if (!Toy_getScopeVariable(scope, identifier, &ref)) {
printf(TOY_CC_ERROR "Failed to get the scope variable" TOY_CC_RESET);
return -1;
}
if (AS_INTEGER(ref) != 42) {
printf(ERROR "Failed to retreive the correct variable value" RESET);
if (TOY_AS_INTEGER(ref) != 42) {
printf(TOY_CC_ERROR "Failed to retreive the correct variable value" TOY_CC_RESET);
return -1;
}
//cleanup
scope = popScope(scope);
scope = Toy_popScope(scope);
freeLiteral(identifier);
freeLiteral(type);
Toy_freeLiteral(identifier);
Toy_freeLiteral(type);
}
printf(NOTICE "All good\n" RESET);
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}