Compare commits

..

8 Commits

Author SHA1 Message Date
Kayne Ruse abba7e0534 Updated cheatsheet.md 2026-05-27 12:35:43 +10:00
Kayne Ruse b312c0d8a3 Added floor, ceil and sqrt to the standard library 2026-05-27 12:24:20 +10:00
Kayne Ruse 3e115095d6 Added 'min' and 'max' as standard functions
Also fixed floating point printing with sprintf
2026-05-27 10:53:01 +10:00
Kayne Ruse f2b714baaa Fixed an extra return opcode in empty functions 2026-05-27 09:52:21 +10:00
Kayne Ruse 92e4a41662 Renamed 'scripts' directory to 'examples'
Also allowed assignment within conditionals
2026-05-26 23:42:33 +10:00
Kayne Ruse bbb1e38649 Fixed parameter strings in data section
Fucking finally.
2026-05-26 18:51:24 +10:00
Kayne Ruse 190477add8 Fixed negative variables handled incorrectly 2026-05-26 18:01:49 +10:00
Kayne Ruse 69175e801a Fixed a hash collision causing null variables to be overwritten
Andrew, don't you dare run my code through a clanker again or I'll hunt
your Canuck ass down and beat you with a hockey stick.
2026-05-26 17:52:07 +10:00
37 changed files with 299 additions and 187 deletions
+3 -4
View File
@@ -33,10 +33,9 @@ fn makeCounter() {
var tally = makeCounter();
while (true) {
var result = tally();
print result; //prints 1 to 10
var result = 0;
while (result = tally()) {
print result;
if (result >= 10) {
break;
+3 -4
View File
@@ -29,10 +29,9 @@ fn makeCounter() {
var tally = makeCounter();
while (true) {
var result = tally();
print result; //prints 1 to 10
var result = 0;
while (result = tally()) {
print result;
if (result >= 10) {
break;
+15
View File
@@ -142,3 +142,18 @@ TOY_API unsigned int Toy_runVM(Toy_VM* vm);
TOY_API void Toy_freeVM(Toy_VM* vm);
TOY_API Toy_Value Toy_getReturnValueFromVM(Toy_VM* parentVM, Toy_VM* subVM);
```
## Standard Library
The tools in `repl/` includes a standard library, which can be added to the root VM by calling `initStandardLibrary` just before `Toy_runVM`. It provides the following general purpose functions:
* `min(x, y)`
* `max(x, y)`
* `floor(x)`
* `ceil(x)`
* `sqrt(x)`
* `range(x)`
It also offers an example of how to write an API.
*Note: Range returns a closure intended for use in the `for` keyword, but the that keyword isn't done yet.*
@@ -1,5 +1,3 @@
//tentatively functional
//fibonacci sequence
fn fib(n) {
if (n < 2) return n;
+2
View File
@@ -0,0 +1,2 @@
//the semicolon is super important
print "Hello world!";
@@ -1,11 +1,10 @@
//find the leap years
//is 'n' a leap year
fn isLeapYear(n: Int) {
if (n % 400 == 0) return true;
if (n % 100 == 0) return false;
return n % 4 == 0;
}
//check for string reuse
{
print isLeapYear(1999);
}
+10
View File
@@ -0,0 +1,10 @@
var randi: Int = 69420;
fn rand() {
//a quick and dirty random number generator
return randi = randi * 1664525 + 1013904223;
}
print rand();
print rand();
print rand();
+3 -3
View File
@@ -8,11 +8,11 @@ fn makeCounter() {
return increment;
}
//'tally' becomes a closure
var tally = makeCounter();
while (true) {
var result = tally();
var result = 0;
while (result = tally()) {
print result;
if (result >= 10) {
+1 -1
View File
@@ -13,7 +13,7 @@ int inspect_bucket(Toy_Bucket** bucketHandle) {
unsigned char* ptr = iter->data;
while ((ptr - iter->data < iter->count) && *((int*)ptr) != 0) { //for each partition
while ((ptr >= iter->data) && (ptr < iter->data + iter->count) && *((int*)ptr) != 0) { //for each partition
if ( ( *((int*)ptr) & 1) == 0) { //is this partition still in use?
occupied++;
+4
View File
@@ -233,6 +233,10 @@ int inspect_instruction(unsigned char* bytecode, unsigned int pc, unsigned int j
printf(MARKER "MODULO %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] == TOY_OPCODE_ASSIGN ? "and ASSIGN" : "");
return 4;
case TOY_OPCODE_INVERT:
printf(MARKER "INVERT\n", MARKER_VALUE(pc, unsigned char));
return 4;
case TOY_OPCODE_COMPARE_EQUAL:
printf(MARKER "COMPARE %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] != TOY_OPCODE_NEGATE ? "==" : "!=");
return 4;
+3 -4
View File
@@ -9,7 +9,6 @@
#include "toy_compiler.h"
#include "toy_vm.h"
//NOTE: for testing
#include "standard_library.h"
#include <stdio.h>
@@ -26,7 +25,7 @@ unsigned char* readFile(char* path, int* size) {
//determine the file's length
fseek(file, 0L, SEEK_END);
*size = ftell(file);
*size = (int)ftell(file);
rewind(file);
//make some space
@@ -44,7 +43,7 @@ unsigned char* readFile(char* path, int* size) {
return NULL;
}
buffer[(*size)++] = '\0';
buffer[(*size)] = '\0';
//clean up and return
fclose(file);
@@ -334,7 +333,7 @@ int repl(const char* filepath, bool verbose) {
inputBuffer[--length] = '\0';
}
if (length == 0 || !inputBuffer[ strspn(inputBuffer, " \r\n\t") ]) {
if (length == 0 || inputBuffer[ strspn(inputBuffer, " \r\n\t") ] == '\0') {
printf("%s> ", prompt); //shows the terminal prompt and restart
continue;
}
+160 -23
View File
@@ -1,49 +1,185 @@
#include "standard_library.h"
#include "toy_console_colors.h"
#include "toy_print.h"
#include "toy_scope.h"
#include "toy_stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct CallbackPairs {
const char* name;
Toy_nativeCallback callback;
} CallbackPairs;
//example callbacks
static void answer(Toy_VM* vm, Toy_FunctionNative* self) {
(void)vm;
//example of how to write and use C bindings
static void std_min(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
Toy_print(TOY_CC_DEBUG "This function returns the integer '42' to the calling scope." TOY_CC_RESET);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_INTEGER(42));
//return the lesser of two values, or null on error
Toy_Value first = Toy_popStack(&vm->stack);
Toy_Value second = Toy_popStack(&vm->stack);
//check types
if ((!TOY_VALUE_IS_INTEGER(first) && !TOY_VALUE_IS_FLOAT(first)) || (!TOY_VALUE_IS_INTEGER(second) && !TOY_VALUE_IS_FLOAT(second))) {
char buffer[256];
snprintf(buffer, 256, "Invalid types '%s' and '%s' found in 'min()'", Toy_getValueTypeAsCString(Toy_unwrapValue(first).type), Toy_getValueTypeAsCString(Toy_unwrapValue(second).type));
Toy_error(buffer);
Toy_freeValue(first);
Toy_freeValue(second);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return;
}
//compare ints, or coerce ints into floats if needed
if (TOY_VALUE_IS_INTEGER(first) && TOY_VALUE_IS_INTEGER(second)) {
Toy_Value result = TOY_VALUE_FROM_INTEGER(TOY_VALUE_AS_INTEGER(first) < TOY_VALUE_AS_INTEGER(second) ? TOY_VALUE_AS_INTEGER(first) : TOY_VALUE_AS_INTEGER(second));
Toy_pushStack(&vm->stack, result);
return;
}
else if (TOY_VALUE_IS_INTEGER(first) && TOY_VALUE_IS_FLOAT(second)) {
first = TOY_VALUE_FROM_FLOAT( (float)TOY_VALUE_AS_INTEGER(first) );
}
else if (TOY_VALUE_IS_FLOAT(first) && TOY_VALUE_IS_INTEGER(second)) {
second = TOY_VALUE_FROM_FLOAT( (float)TOY_VALUE_AS_INTEGER(second) );
}
//finally, do the comparison on floats
Toy_Value result = TOY_VALUE_FROM_FLOAT(TOY_VALUE_AS_FLOAT(first) < TOY_VALUE_AS_FLOAT(second) ? TOY_VALUE_AS_FLOAT(first) : TOY_VALUE_AS_FLOAT(second));
Toy_pushStack(&vm->stack, result);
//NOTE: not freeing scalar values does work, but only in narrow cases
}
static void identity(Toy_VM* vm, Toy_FunctionNative* self) {
//does nothing, but any arguements are left on the stack as results
(void)vm;
static void std_max(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
//return the lesser of two values, or null on error
Toy_Value first = Toy_popStack(&vm->stack);
Toy_Value second = Toy_popStack(&vm->stack);
//check types
if ((!TOY_VALUE_IS_INTEGER(first) && !TOY_VALUE_IS_FLOAT(first)) || (!TOY_VALUE_IS_INTEGER(second) && !TOY_VALUE_IS_FLOAT(second))) {
char buffer[256];
snprintf(buffer, 256, "Invalid types '%s' and '%s' found in 'max()'", Toy_getValueTypeAsCString(Toy_unwrapValue(first).type), Toy_getValueTypeAsCString(Toy_unwrapValue(second).type));
Toy_error(buffer);
Toy_freeValue(first);
Toy_freeValue(second);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return;
}
//compare ints, or coerce ints into floats if needed
if (TOY_VALUE_IS_INTEGER(first) && TOY_VALUE_IS_INTEGER(second)) {
Toy_Value result = TOY_VALUE_FROM_INTEGER(TOY_VALUE_AS_INTEGER(first) > TOY_VALUE_AS_INTEGER(second) ? TOY_VALUE_AS_INTEGER(first) : TOY_VALUE_AS_INTEGER(second));
Toy_pushStack(&vm->stack, result);
return;
}
else if (TOY_VALUE_IS_INTEGER(first) && TOY_VALUE_IS_FLOAT(second)) {
first = TOY_VALUE_FROM_FLOAT( (float)TOY_VALUE_AS_INTEGER(first) );
}
else if (TOY_VALUE_IS_FLOAT(first) && TOY_VALUE_IS_INTEGER(second)) {
second = TOY_VALUE_FROM_FLOAT( (float)TOY_VALUE_AS_INTEGER(second) );
}
//finally, do the comparison on floats
Toy_Value result = TOY_VALUE_FROM_FLOAT(TOY_VALUE_AS_FLOAT(first) > TOY_VALUE_AS_FLOAT(second) ? TOY_VALUE_AS_FLOAT(first) : TOY_VALUE_AS_FLOAT(second));
Toy_pushStack(&vm->stack, result);
//NOTE: not freeing scalar values does work, but only in narrow cases
}
static void echo(Toy_VM* vm, Toy_FunctionNative* self) {
static void std_floor(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
//pops one argument, and prints it
Toy_Value value = Toy_popStack(&vm->stack);
Toy_String* string = Toy_stringifyValue(&vm->memoryBucket, value);
char* cstr = Toy_getStringRaw(string);
Toy_print(cstr);
if (!TOY_VALUE_IS_INTEGER(value) && !TOY_VALUE_IS_FLOAT(value)) {
char buffer[256];
snprintf(buffer, 256, "Invalid type '%s' found in 'floor()'", Toy_getValueTypeAsCString(Toy_unwrapValue(value).type));
Toy_error(buffer);
free(cstr);
Toy_freeString(string);
Toy_freeValue(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return;
}
//this only runs on floats, but converts them to integers
if (TOY_VALUE_IS_FLOAT(value)) {
double d = (double)TOY_VALUE_AS_FLOAT(value);
d = floor(d);
value = TOY_VALUE_FROM_INTEGER((int)d);
}
Toy_pushStack(&vm->stack, value);
}
static void std_ceil(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
Toy_Value value = Toy_popStack(&vm->stack);
if (!TOY_VALUE_IS_INTEGER(value) && !TOY_VALUE_IS_FLOAT(value)) {
char buffer[256];
snprintf(buffer, 256, "Invalid type '%s' found in 'ceil()'", Toy_getValueTypeAsCString(Toy_unwrapValue(value).type));
Toy_error(buffer);
Toy_freeValue(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return;
}
//this only runs on floats, but converts them to integers
if (TOY_VALUE_IS_FLOAT(value)) {
double d = (double)TOY_VALUE_AS_FLOAT(value);
d = ceil(d);
value = TOY_VALUE_FROM_INTEGER((int)d);
}
Toy_pushStack(&vm->stack, value);
}
static void std_sqrt(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
Toy_Value value = Toy_popStack(&vm->stack);
if (!TOY_VALUE_IS_INTEGER(value) && !TOY_VALUE_IS_FLOAT(value)) {
char buffer[256];
snprintf(buffer, 256, "Invalid type '%s' found in 'sqrt()'", Toy_getValueTypeAsCString(Toy_unwrapValue(value).type));
Toy_error(buffer);
Toy_freeValue(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return;
}
double d = 0;
if (TOY_VALUE_IS_INTEGER(value)) d = (double)TOY_VALUE_AS_INTEGER(value);
else if (TOY_VALUE_IS_FLOAT(value)) d = (double)TOY_VALUE_AS_FLOAT(value);
//only works on non-negative values
if (d < 0) {
char buffer[256];
snprintf(buffer, 256, "Can't get the sqrt of a negative number (found '%g')", d);
Toy_error(buffer);
Toy_freeValue(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return;
}
//do the thing and push the result
d = sqrt(d);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_FLOAT((float)d));
}
static void next(Toy_VM* vm, Toy_FunctionNative* self) {
//used by 'range'
//used by 'std_range'
if (self->meta2 < self->meta1) {
Toy_Value result = TOY_VALUE_FROM_INTEGER(self->meta2);
Toy_pushStack(&vm->stack, result);
@@ -54,7 +190,7 @@ static void next(Toy_VM* vm, Toy_FunctionNative* self) {
}
}
static void range(Toy_VM* vm, Toy_FunctionNative* self) {
static void std_range(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
//one arg to represent the number of iterations
@@ -79,12 +215,13 @@ static void range(Toy_VM* vm, Toy_FunctionNative* self) {
Toy_pushStack(&vm->stack, result);
}
CallbackPairs callbackPairs[] = {
{"dbg_answer", answer},
{"dbg_identity", identity},
{"dbg_echo", echo},
{"range", range},
{"min", std_min},
{"max", std_max},
{"floor", std_floor},
{"ceil", std_ceil},
{"sqrt", std_sqrt},
{"range", std_range},
{NULL, NULL},
};
-16
View File
@@ -1,16 +0,0 @@
//calculate the nth fibonacci number, and print it
var counter: Int = 0;
var first: Int = 1;
var second: Int = 0;
while (counter < 100_000) {
var third: Int = first + second;
first = second;
second = third;
print third;
++counter;
}
-11
View File
@@ -1,11 +0,0 @@
fn output(arg) {
print arg;
}
var array = ["alpha", "bravo", "charlie"];
array.forEach(echo);
array.forEach(output);
-35
View File
@@ -1,35 +0,0 @@
//WARN: This is just a scratch pad, don't use it
//TODO: table.hasValue or table.getKeyFromValue?
//for (var i in array) print i;
//for (var i in table) print i;
//for (var i in range(10)) print i;
//for (range(10)) print "ha";
//example of a `range`-like function
fn range(limit: Int) {
var counter: Int = 0;
fn next() {
if (counter >= limit) {
return null;
}
else return counter++;
}
return next;
}
var next = range(10);
fn log(x) {
if (x == null) return;
print x;
}
while (true) {
log(next());
}
-21
View File
@@ -1,21 +0,0 @@
var table = [
"Alpha": 1,
"Bravo": 2,
"Charlie": 3,
"Delta": 4,
"Echo": 5,
"Foxtrot": 6,
"Golf": 7,
"Hotel": 8,
"India": 9,
"Juliett": 10,
"Kilo": 11,
"Lima": 12,
"Mike": 13,
];
for (var i in table) {
print i;
}
-12
View File
@@ -1,12 +0,0 @@
var randi: Int = 69420;
fn rand() {
return randi = randi * 1664525 + 1013904223;
}
var a = rand();
+3 -2
View File
@@ -86,9 +86,10 @@ typedef enum Toy_AstFlag {
TOY_AST_FLAG_PREFIX_DECREMENT = 42,
TOY_AST_FLAG_POSTFIX_INCREMENT = 43,
TOY_AST_FLAG_POSTFIX_DECREMENT = 44,
TOY_AST_FLAG_INVERT = 45,
TOY_AST_FLAG_INVOKATION = 45,
TOY_AST_FLAG_ATTRIBUTE = 46,
TOY_AST_FLAG_INVOKATION = 46,
TOY_AST_FLAG_ATTRIBUTE = 47,
// TOY_AST_FLAG_TERNARY,
} Toy_AstFlag;
+1 -1
View File
@@ -172,7 +172,7 @@ static void attr_tableRemove(Toy_VM* vm, Toy_FunctionNative* self) {
Toy_Value Toy_private_handleTableAttributes(Toy_VM* vm, Toy_Value compound, Toy_Value attribute) {
if (MATCH_VALUE_AND_CSTRING(attribute, "length")) {
return TOY_VALUE_FROM_INTEGER(TOY_VALUE_AS_ARRAY(compound)->count);
return TOY_VALUE_FROM_INTEGER(TOY_VALUE_AS_TABLE(compound)->count);
}
else if (MATCH_VALUE_AND_CSTRING(attribute, "insert")) {
Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_tableInsert);
-2
View File
@@ -56,5 +56,3 @@ TOY_API void Toy_collectBucketGarbage(Toy_Bucket** bucketHandle);
#ifndef TOY_BUCKET_IDEAL
#define TOY_BUCKET_IDEAL (TOY_BUCKET_64KB - sizeof(Toy_Bucket))
#endif
//TODO: check for leaks when freeBucket is called, for debugging
+32 -8
View File
@@ -78,6 +78,7 @@ static void emitByte(unsigned char** handle, unsigned int* capacity, unsigned in
((unsigned char*)(*handle))[(*count)++] = byte;
}
//BUG: There might be issues here when compiled on big-endian platforms
static void emitInt(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned int bytes) {
char* ptr = (char*)&bytes;
emitByte(handle, capacity, count, *(ptr++));
@@ -168,8 +169,8 @@ static unsigned int emitString(Toy_Bytecode** mb, Toy_String* str) {
}
//mark the position within the jump index, reusing an existing entry if it exists
for (unsigned int i = 0; i < (*mb)->jumpsCount; i++) {
if ((*mb)->jumps[i] == dataAddr) {
for (unsigned int i = 0; i < (*mb)->jumpsCount; i += 4) {
if (*(unsigned int*)((*mb)->jumps + i) == dataAddr) {
//reuse, and finish
EMIT_INT(mb, code, i);
return 1;
@@ -200,13 +201,13 @@ static unsigned int emitParameters(Toy_Bytecode* mb, Toy_Ast* ast) {
}
//the address within the data section
char buffer[128];
snprintf(buffer, 128, "%.*s", ast->varDeclare.name->info.length, ast->varDeclare.name->leaf.data);
char buffer[256];
snprintf(buffer, 256, "%.*s", ast->varDeclare.name->info.length, ast->varDeclare.name->leaf.data);
unsigned int dataAddr = emitCStringToData(&(mb->data), &(mb->dataCapacity), &(mb->dataCount), buffer);
//check the param index for that entry i.e. don't reuse parameter names
for (unsigned int i = 0; i < mb->paramCount; i++) {
if (mb->param[i] == dataAddr) {
for (unsigned int i = 0; i < mb->paramCount; i+=4) {
if (*(unsigned int*)(mb->param + i) == dataAddr) {
//not allowed
fprintf(stderr, TOY_CC_ERROR "COMPILER ERROR: Function parameters must have unique names\n" TOY_CC_RESET);
mb->panic = true;
@@ -376,6 +377,17 @@ static unsigned int writeInstructionUnary(Toy_Bytecode** mb, Toy_AstUnary ast) {
result = 1;
}
else if (ast.flag == TOY_AST_FLAG_INVERT) {
result = writeBytecodeFromAst(mb, ast.child);
EMIT_BYTE(mb, code, TOY_OPCODE_INVERT);
//4-byte alignment
EMIT_BYTE(mb, code, 0);
EMIT_BYTE(mb, code, 0);
EMIT_BYTE(mb, code, 0);
}
else {
fprintf(stderr, TOY_CC_ERROR "ERROR: Invalid AST unary flag found\n" TOY_CC_RESET);
exit(-1);
@@ -620,6 +632,12 @@ static unsigned int writeInstructionIfThenElse(Toy_Bytecode** mb, Toy_AstIfThenE
//cond-branch
writeBytecodeFromAst(mb, ast.condBranch);
//leave the assigned value on the stack when inside a condition
if (checkForChainedAssign(ast.condBranch)) {
Toy_AstVarAccess access = { .type = TOY_AST_VAR_ACCESS, .child = ast.condBranch->varAssign.target };
writeInstructionAccess(mb, access);
}
//emit the jump word (opcode, type, condition, padding)
EMIT_BYTE(mb, code, TOY_OPCODE_JUMP);
EMIT_BYTE(mb, code, TOY_OP_PARAM_JUMP_RELATIVE);
@@ -665,6 +683,12 @@ static unsigned int writeInstructionWhileThen(Toy_Bytecode** mb, Toy_AstWhileThe
//cond-branch
writeBytecodeFromAst(mb, ast.condBranch);
//leave the assigned value on the stack when inside a condition
if (checkForChainedAssign(ast.condBranch)) {
Toy_AstVarAccess access = { .type = TOY_AST_VAR_ACCESS, .child = ast.condBranch->varAssign.target };
writeInstructionAccess(mb, access);
}
//emit the jump word (opcode, type, condition, padding)
EMIT_BYTE(mb, code, TOY_OPCODE_JUMP);
EMIT_BYTE(mb, code, TOY_OP_PARAM_JUMP_RELATIVE);
@@ -763,7 +787,7 @@ static unsigned int writeInstructionForThen(Toy_Bytecode** mb, Toy_AstForThen as
emitString(mb, ast.condBranch->iterable.left->varDeclare.name);
//TODO: use a different approach
//TODO: use a different approach?
//BUGFIX: shadow the iterable's name
EMIT_BYTE(mb, code, TOY_OPCODE_READ);
EMIT_BYTE(mb, code, TOY_VALUE_NULL);
@@ -1440,7 +1464,7 @@ static void writeBytecodeBody(Toy_Bytecode* mb, Toy_Ast* ast) {
writeBytecodeFromAst(&mb, ast);
//append an extra return if needed
if (mb->codeCount <= 4 || mb->code[mb->codeCount - 4] != TOY_OPCODE_RETURN) { //if empty or no return statement
if (mb->codeCount < 4 || mb->code[mb->codeCount - 4] != TOY_OPCODE_RETURN) { //if empty or no return statement
EMIT_BYTE(&mb, code, TOY_OPCODE_RETURN); //end terminator
EMIT_BYTE(&mb, code, 0); //4-byte alignment
EMIT_BYTE(&mb, code, 0);
+1
View File
@@ -22,6 +22,7 @@ typedef enum Toy_OpcodeType {
TOY_OPCODE_MULTIPLY,
TOY_OPCODE_DIVIDE,
TOY_OPCODE_MODULO,
TOY_OPCODE_INVERT, //negative numbers
//comparison instructions
TOY_OPCODE_COMPARE_EQUAL,
+3 -3
View File
@@ -324,7 +324,7 @@ static Toy_AstFlag literal(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_As
case TOY_TOKEN_LITERAL_INTEGER: {
//filter the '_' character
char buffer[parser->previous.length];
char buffer[parser->previous.length + 1];
unsigned int i = 0, o = 0;
do {
@@ -341,7 +341,7 @@ static Toy_AstFlag literal(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_As
case TOY_TOKEN_LITERAL_FLOAT: {
//filter the '_' character
char buffer[parser->previous.length];
char buffer[parser->previous.length + 1];
unsigned int i = 0, o = 0;
do {
@@ -417,7 +417,7 @@ static Toy_AstFlag unary(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast*
}
else {
//actually emit the negation node
Toy_private_emitAstUnary(bucketHandle, rootHandle, TOY_AST_FLAG_NEGATE);
Toy_private_emitAstUnary(bucketHandle, rootHandle, TOY_AST_FLAG_INVERT);
}
}
+4
View File
@@ -1,5 +1,6 @@
#include "toy_print.h"
#include <assert.h>
#include <stdio.h>
static Toy_callbackType printCallback = puts;
@@ -19,14 +20,17 @@ void Toy_assertFailure(const char* msg) {
}
void Toy_setPrintCallback(Toy_callbackType cb) {
assert(cb);
printCallback = cb;
}
void Toy_setErrorCallback(Toy_callbackType cb) {
assert(cb);
errorCallback = cb;
}
void Toy_setAssertFailureCallback(Toy_callbackType cb) {
assert(cb);
assertCallback = cb;
}
+1 -1
View File
@@ -48,7 +48,7 @@ static void probeAndInsert(Toy_Scope* scope, Toy_String* key, Toy_Value value, T
}
//if this spot is free, insert and return
if (TOY_VALUE_IS_NULL(scope->data[probe].value)) {
if (scope->data[probe].key == NULL) { //fuck my life
scope->data[probe] = entry;
scope->count++;
scope->maxPsl = entry.psl > scope->maxPsl ? entry.psl : scope->maxPsl;
+3 -17
View File
@@ -464,29 +464,15 @@ Toy_String* Toy_stringifyValue(Toy_Bucket** bucketHandle, Toy_Value value) {
}
case TOY_VALUE_INTEGER: {
char buffer[16];
char buffer[128];
sprintf(buffer, "%d", value.as.integer);
return Toy_createStringLength(bucketHandle, buffer, strlen(buffer));
}
case TOY_VALUE_FLOAT: {
//using printf
char buffer[16];
sprintf(buffer, "%f", value.as.number);
//BUGFIX: printf format specificer '%f' will set the precision to 6 decimal places, which means there's trailing zeroes
char buffer[128];
sprintf(buffer, "%g", value.as.number);
unsigned int length = strlen(buffer);
//find the decimal, if it exists
unsigned int decimal = 0;
while (decimal != length && buffer[decimal] != '.' && buffer[decimal] != ',') decimal++; //'.' and ',' supports more locales
//locales are hard, sorry!
if (decimal != length && buffer[decimal] == ',') buffer[decimal] = '.';
//wipe the trailing zeros
while(decimal != length && buffer[length-1] == '0') buffer[--length] = '\0';
return Toy_createStringLength(bucketHandle, buffer, length);
}
+24 -2
View File
@@ -73,6 +73,8 @@ static void processRead(Toy_VM* vm) {
//grab the jump as an integer
unsigned int jump = *((int*)(vm->code + vm->jumpsAddr + READ_INT(vm)));
//BUG: the jump parameter could cause an out of bounds read if it's malformed
//jumps are relative to the data address
char* cstring = (char*)(vm->code + vm->dataAddr + jump);
@@ -544,7 +546,7 @@ static void processIterate(Toy_VM* vm) {
processJump(vm);
}
}
//TODO: support closures as a parameter
//TODO: support closures in for-loops
else {
fprintf(stderr, TOY_CC_ERROR "ERROR: Unknown iterable type '%s' found in for loop, exiting\n" TOY_CC_RESET, Toy_getValueTypeAsCString(Toy_unwrapValue(compound).type));
exit(-1);
@@ -552,6 +554,25 @@ static void processIterate(Toy_VM* vm) {
}
static void processArithmetic(Toy_VM* vm, Toy_OpcodeType opcode) {
//BUGFIX: handle negative variables
if (opcode == TOY_OPCODE_INVERT) {
Toy_Value value = Toy_popStack(&vm->stack);
if (TOY_VALUE_IS_INTEGER(value)) {
int intermediate = TOY_VALUE_AS_INTEGER(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_INTEGER(-intermediate));
}
else if (TOY_VALUE_IS_FLOAT(value)) {
float intermediate = TOY_VALUE_AS_FLOAT(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_FLOAT(-intermediate));
}
else {
Toy_error("Can't negate a non-number");
Toy_freeValue(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
}
return;
}
Toy_Value right = Toy_popStack(&vm->stack);
Toy_Value left = Toy_popStack(&vm->stack);
@@ -805,7 +826,7 @@ static void processAssert(Toy_VM* vm) {
//determine the args
if (count == 1) {
message = TOY_VALUE_FROM_STRING(Toy_toString(&vm->memoryBucket, "assertion failed")); //TODO: better default error message
message = TOY_VALUE_FROM_STRING(Toy_toString(&vm->memoryBucket, "assertion failed")); //TODO: better default assert message
value = Toy_popStack(&vm->stack);
}
else if (count == 2) {
@@ -1094,6 +1115,7 @@ static unsigned int process(Toy_VM* vm) {
case TOY_OPCODE_MULTIPLY:
case TOY_OPCODE_DIVIDE:
case TOY_OPCODE_MODULO:
case TOY_OPCODE_INVERT:
processArithmetic(vm, opcode);
break;
-2
View File
@@ -7,5 +7,3 @@ all:
gdb:
$(MAKE) -C units -k gdb
$(MAKE) -C scripts -k gdb
#TODO: valgrind
+1 -1
View File
@@ -1 +1 @@
//TODO: empty test script
//URGENT: empty test script
+1 -1
View File
@@ -1 +1 @@
//TODO: empty test script
//URGENT: empty test script
+1 -1
View File
@@ -1,4 +1,4 @@
//TODO: empty test script
//URGENT: empty test script
//TODO: test iteration on arrays, tables, closures
+6 -2
View File
@@ -1,8 +1,12 @@
fn empty() { //BUG: there's an extra return in the bytecode
fn empty() {
//
}
fn almostEmpty() {
return;
}
fn full() {
fn notEmpty() {
return 42;
}
+7
View File
@@ -157,4 +157,11 @@ print !false; //true
print a;
}
{
//Yet Another Cosmic Joke
var a = null;
var i = 42;
print a;
}
//TODO: type casting
+1 -1
View File
@@ -4,7 +4,7 @@
#include <string.h>
int main(void) {
//TODO: Test not yet implemented
//URGENT: Test not yet implemented
printf(TOY_CC_WARN "Test not yet implemented: %s\n" TOY_CC_RESET, __FILE__);
return 0;
}
+1 -1
View File
@@ -117,7 +117,7 @@ int test_functions_from_bytecodes(void) {
}
int test_functions_from_callbacks(void) {
//TODO: Test not yet implemented
//URGENT: Test not yet implemented
printf(TOY_CC_WARN "WIP test not yet implemented: %s\n" TOY_CC_RESET, __FILE__);
return 0;
}
+2 -2
View File
@@ -607,8 +607,8 @@ int main(void) {
total += res;
}
//TODO: references
//TODO: type coersions
//TODO: references?
//TODO: type coersions?
//TODO: opaques?
return total;