Skip to content

Commit

Permalink
Fixed unary negation bug, removed newline from print
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Feb 10, 2023
1 parent 53012db commit aeb008c
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 25 deletions.
14 changes: 7 additions & 7 deletions scripts/example-entity.toy
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn getY(node: opaque) {

//lifecycle functions
fn onInit(node: opaque) {
print "render.toy:onInit() called";
print "render.toy:onInit() called\n";

node.loadTexture("sprites:/character.png");
parent = node.getNodeParent();
Expand All @@ -36,13 +36,13 @@ fn onStep(node: opaque) {
}

fn onFree(node: opaque) {
print "render.toy:onFree() called";
print "render.toy:onFree() called\n";

node.freeTexture();
}

fn onDraw(node: opaque) {
// print "render.toy:onDraw() called";
// print "render.toy:onDraw() called\n";

var px = parent.callNode("getX");
var py = parent.callNode("getY");
Expand Down Expand Up @@ -104,22 +104,22 @@ fn onKeyUp(node: opaque, event: string) {
}

fn onMouseMotion(node: opaque, x: int, y: int, xrel: int, yrel: int) {
print "entity.toy:onMouseMotion(" + string x + ", " + string y + ", " + string xrel + ", " + string yrel + ")";
// print "entity.toy:onMouseMotion(" + string x + ", " + string y + ", " + string xrel + ", " + string yrel + ")\n";
}

fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
print "entity.toy:onMouseButtonDown(" + string x + ", " + string y + ", " + button + ")";
// print "entity.toy:onMouseButtonDown(" + string x + ", " + string y + ", " + button + ")\n";

//jump to pos
posX = x - WIDTH / 2;
posY = y - HEIGHT / 2;
}

fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) {
print "entity.toy:onMouseButtonUp(" + string x + ", " + string y + ", " + button + ")";
// print "entity.toy:onMouseButtonUp(" + string x + ", " + string y + ", " + button + ")\n";
}

fn onMouseWheel(node: opaque, xrel: int, yrel: int) {
print "entity.toy:onMouseWheel(" + string xrel + ", " + string yrel + ")";
// print "entity.toy:onMouseWheel(" + string xrel + ", " + string yrel + ")\n";
}

2 changes: 1 addition & 1 deletion scripts/fib-memo.toy
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ fn fib(n : int) {

for (var i = 0; i < 40; i++) {
var res = fib(i);
print string i + ": " + string res;
print string i + ": " + string res + "\n";
}
2 changes: 1 addition & 1 deletion scripts/fib.toy
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ fn fib(n : int) {

for (var i = 0; i < 20; i++) {
var res = fib(i);
print string i + ": " + string res;
print string i + ": " + string res + "\n";
}
61 changes: 61 additions & 0 deletions scripts/level.toy
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//constants
var WIDTH: int const = 10;
var HEIGHT: int const = 10;

//WIDTH * HEIGHT in size
var tiles: [[int]] const = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];

var tileset: [int: string] const = [
0: " ",
1: " X "
];

//variables
var posX: int = 5;
var posY: int = 5;

//functions
fn draw() {
for (var j: int = 0; j < HEIGHT; j++) {
for (var i: int = 0; i < WIDTH; i++) {
//draw the player pos
if (i == posX && j == posY) {
print " O ";
continue;
}

print tileset[ tiles[i][j] ];
}
print "\n";
}
print "\n";
}

fn move(xrel: int, yrel: int) {
if (xrel > 1 || xrel < -1 || yrel > 1 || yrel < -1 || (xrel != 0 && yrel != 0)) {
print "too fast!\n";
return;
}

if (tiles[posX + xrel][posY + yrel] > 0) {
print "Can't move that way\n";
return;
}

posX += xrel;
posY += yrel;

draw();
}

4 changes: 2 additions & 2 deletions scripts/rule110.toy
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ for (var i = 0; i < SIZE -1; i++) {
prev += " ";
}
prev += "*"; //initial
print prev;
print prev + "\n";

//run
for (var iteration = 0; iteration < SIZE -1; iteration++) {
Expand All @@ -44,6 +44,6 @@ for (var iteration = 0; iteration < SIZE -1; iteration++) {
//right
output += (lookup[prev[SIZE-2]][prev[SIZE-1]][" "]);

print output;
print output + "\n";
prev = output;
}
14 changes: 8 additions & 6 deletions scripts/small.toy
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import compound;

var a = [false, false, false];
var d = ["one": false, "two": false];

print d.containsKey("one");
print d.containsKey("three");
var xrel: int = 0;
var yrel: int = 0;

if (xrel > 1 || xrel < -1 || yrel > 1 || yrel < -1) {
print "outside";
}
else {
print "inside";
}
2 changes: 1 addition & 1 deletion source/toy_interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

static void printWrapper(const char* output) {
printf("%s", output);
printf("\n"); //default new line
// printf("\n"); //default new line
}

static void assertWrapper(const char* output) {
Expand Down
6 changes: 3 additions & 3 deletions source/toy_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,12 @@ static Toy_Opcode binary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
}

case TOY_TOKEN_AND: {
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
parsePrecedence(parser, nodeHandle, PREC_AND);
return TOY_OP_AND;
}

case TOY_TOKEN_OR: {
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
parsePrecedence(parser, nodeHandle, PREC_OR);
return TOY_OP_OR;
}

Expand All @@ -448,7 +448,7 @@ static Toy_Opcode unary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {

if (parser->previous.type == TOY_TOKEN_MINUS) {
//temp handle to potentially negate values
parsePrecedence(parser, &tmpNode, PREC_TERNARY); //can be a literal
parsePrecedence(parser, &tmpNode, PREC_TERM); //can be a literal

//optimisation: check for negative literals
if (tmpNode != NULL && tmpNode->type == TOY_AST_NODE_LITERAL && (TOY_IS_INTEGER(tmpNode->atomic.literal) || TOY_IS_FLOAT(tmpNode->atomic.literal))) {
Expand Down
7 changes: 7 additions & 0 deletions test/scripts/or-chaining-bugfix.toy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//This is just a check to ensure that unary minus doesn't screw up the AST

var xrel: int = 0;
var yrel: int = 0;

assert (xrel > 1 || xrel < -1 || yrel > 1 || yrel < -1) == false, "or-chaining bugfix failed";

8 changes: 4 additions & 4 deletions test/test_call_from_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main() {

//test answer
{
interpreter.printOutput("Testing answer");
interpreter.printOutput("Testing answer\n");

Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Expand Down Expand Up @@ -69,7 +69,7 @@ int main() {

//test identity
{
interpreter.printOutput("Testing identity");
interpreter.printOutput("Testing identity\n");

Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Expand Down Expand Up @@ -104,7 +104,7 @@ int main() {

//test makeCounter (closures)
{
interpreter.printOutput("Testing makeCounter (closures)");
interpreter.printOutput("Testing makeCounter (closures)\n");

Toy_LiteralArray arguments;
Toy_initLiteralArray(&arguments);
Expand Down Expand Up @@ -209,7 +209,7 @@ int main() {

//test assertion failure
{
interpreter.printOutput("Testing assertion failure");
interpreter.printOutput("Testing assertion failure\n");

Toy_setInterpreterAssert(&interpreter, noPrintFn);

Expand Down
1 change: 1 addition & 0 deletions test/test_interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ int main() {
"long-dictionary.toy",
"long-literals.toy",
"native-functions.toy",
"or-chaining-bugfix.toy",
"panic-within-functions.toy",
"ternary-expressions.toy",
"types.toy",
Expand Down

0 comments on commit aeb008c

Please sign in to comment.