Skip to content

Commit 6fa224f

Browse files
committed
Hooks can't be dict keys, tweaked Toy_readFile
1 parent 8a68d86 commit 6fa224f

13 files changed

+22
-20
lines changed

repl/lib_runner.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
4949

5050
//load and compile the bytecode
5151
size_t fileSize = 0;
52-
const char* source = Toy_readFile(filePath, &fileSize);
52+
const char* source = (const char*)Toy_readFile(filePath, &fileSize);
5353

5454
if (!source) {
5555
interpreter->errorOutput("Failed to load source file\n");

repl/repl_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ int main(int argc, const char* argv[]) {
180180

181181
//compile and save
182182
size_t size = 0;
183-
const char* source = Toy_readFile(Toy_commandLine.compilefile, &size);
183+
const char* source = (const char*)Toy_readFile(Toy_commandLine.compilefile, &size);
184184
if (!source) {
185185
return 1;
186186
}
@@ -220,7 +220,7 @@ int main(int argc, const char* argv[]) {
220220
}
221221

222222
size_t size;
223-
initialSource = Toy_readFile(Toy_commandLine.initialfile, &size);
223+
initialSource = (const char*)Toy_readFile(Toy_commandLine.initialfile, &size);
224224
}
225225

226226
repl(initialSource);

repl/repl_tools.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <stdlib.h>
1515

1616
//IO functions
17-
const char* Toy_readFile(const char* path, size_t* fileSize) {
17+
const unsigned char* Toy_readFile(const char* path, size_t* fileSize) {
1818
FILE* file = fopen(path, "rb");
1919

2020
if (file == NULL) {
@@ -26,14 +26,14 @@ const char* Toy_readFile(const char* path, size_t* fileSize) {
2626
*fileSize = ftell(file);
2727
rewind(file);
2828

29-
char* buffer = (char*)malloc(*fileSize + 1);
29+
unsigned char* buffer = (unsigned char*)malloc(*fileSize + 1);
3030

3131
if (buffer == NULL) {
3232
fprintf(stderr, TOY_CC_ERROR "Not enough memory to read \"%s\"\n" TOY_CC_RESET, path);
3333
return NULL;
3434
}
3535

36-
size_t bytesRead = fread(buffer, sizeof(char), *fileSize, file);
36+
size_t bytesRead = fread(buffer, sizeof(unsigned char), *fileSize, file);
3737

3838
buffer[*fileSize] = '\0'; //NOTE: fread doesn't append this
3939

@@ -120,7 +120,7 @@ void Toy_runBinary(const unsigned char* tb, size_t size) {
120120

121121
void Toy_runBinaryFile(const char* fname) {
122122
size_t size = 0; //not used
123-
const unsigned char* tb = (const unsigned char*)Toy_readFile(fname, &size);
123+
const unsigned char* tb = Toy_readFile(fname, &size);
124124
if (!tb) {
125125
return;
126126
}
@@ -140,7 +140,7 @@ void Toy_runSource(const char* source) {
140140

141141
void Toy_runSourceFile(const char* fname) {
142142
size_t size = 0; //not used
143-
const char* source = Toy_readFile(fname, &size);
143+
const char* source = (const char*)Toy_readFile(fname, &size);
144144
if (!source) {
145145
return;
146146
}

repl/repl_tools.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "toy_common.h"
44

5-
const char* Toy_readFile(const char* path, size_t* fileSize);
5+
const unsigned char* Toy_readFile(const char* path, size_t* fileSize);
66
int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size);
77

88
const unsigned char* Toy_compileString(const char* source, size_t* size);

source/toy_literal_array.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ TOY_API bool Toy_setLiteralArray(Toy_LiteralArray* array, Toy_Literal index, Toy
1818
TOY_API Toy_Literal Toy_getLiteralArray(Toy_LiteralArray* array, Toy_Literal index);
1919

2020
int Toy_findLiteralIndex(Toy_LiteralArray* array, Toy_Literal literal);
21+
22+
//TODO: add a function to get the capacity & count

source/toy_literal_dictionary.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key
141141
}
142142

143143
//BUGFIX: Can't hash a function
144-
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key)) {
144+
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key) || TOY_IS_FUNCTION_HOOK(key)) {
145145
fprintf(stderr, TOY_CC_ERROR "Dictionaries can't have function keys (set)\n" TOY_CC_RESET);
146146
return;
147147
}
@@ -166,7 +166,7 @@ Toy_Literal Toy_getLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Lite
166166
}
167167

168168
//BUGFIX: Can't hash a function
169-
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key)) {
169+
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key) || TOY_IS_FUNCTION_HOOK(key)) {
170170
fprintf(stderr, TOY_CC_ERROR "Dictionaries can't have function keys (get)\n" TOY_CC_RESET);
171171
return TOY_TO_NULL_LITERAL;
172172
}
@@ -193,7 +193,7 @@ void Toy_removeLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal
193193
}
194194

195195
//BUGFIX: Can't hash a function
196-
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key)) {
196+
if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key) || TOY_IS_FUNCTION_HOOK(key)) {
197197
fprintf(stderr, TOY_CC_ERROR "Dictionaries can't have function keys (remove)\n" TOY_CC_RESET);
198198
return;
199199
}

test/test_call_from_host.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void error(char* msg) {
2727
int main() {
2828
{
2929
size_t size = 0;
30-
const char* source = Toy_readFile("scripts/call-from-host.toy", &size);
30+
const char* source = (const char*)Toy_readFile("scripts/call-from-host.toy", &size);
3131
const unsigned char* tb = Toy_compileString(source, &size);
3232
free((void*)source);
3333

test/test_compiler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int main() {
5252
{
5353
//source
5454
size_t sourceLength = 0;
55-
const char* source = Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength);
55+
const char* source = (const char*)Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength);
5656

5757
//test basic compilation & collation
5858
Toy_Lexer lexer;

test/test_interpreter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void runSourceCustom(const char* source) {
5353

5454
void runSourceFileCustom(const char* fname) {
5555
size_t size = 0; //not used
56-
const char* source = Toy_readFile(fname, &size);
56+
const char* source = (const char*)Toy_readFile(fname, &size);
5757
runSourceCustom(source);
5858
free((void*)source);
5959
}

test/test_libraries.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int main() {
105105

106106
//compile the source
107107
size_t size = 0;
108-
const char* source = Toy_readFile(fname, &size);
108+
const char* source = (const char*)Toy_readFile(fname, &size);
109109
if (!source) {
110110
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
111111
failedAsserts++;
@@ -140,7 +140,7 @@ int main() {
140140

141141
//compile the source
142142
size_t size = 0;
143-
const char* source = Toy_readFile(fname, &size);
143+
const char* source = (const char*)Toy_readFile(fname, &size);
144144
if (!source) {
145145
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
146146
failedAsserts++;

test/test_mustfail.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void runSourceCustom(const char* source) {
8484

8585
void runSourceFileCustom(const char* fname) {
8686
size_t size = 0; //not used
87-
const char* source = Toy_readFile(fname, &size);
87+
const char* source = (const char*)Toy_readFile(fname, &size);
8888
runSourceCustom(source);
8989
free((void*)source);
9090
}

test/test_opaque_data_type.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static int consume(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
6868
int main() {
6969
{
7070
size_t size = 0;
71-
const char* source = Toy_readFile("scripts/opaque-data-type.toy", &size);
71+
const char* source = (const char*)Toy_readFile("scripts/opaque-data-type.toy", &size);
7272
const unsigned char* tb = Toy_compileString(source, &size);
7373
free((void*)source);
7474

test/test_parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int main() {
5858
{
5959
//get the source file
6060
size_t size = 0;
61-
const char* source = Toy_readFile("scripts/parser_sample_code.toy", &size);
61+
const char* source = (const char*)Toy_readFile("scripts/parser_sample_code.toy", &size);
6262

6363
//test parsing a chunk of junk (valgrind will find leaks)
6464
Toy_Lexer lexer;

0 commit comments

Comments
 (0)