mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Fixed unary negation bug, removed newline from print
This commit is contained in:
@@ -24,7 +24,7 @@ fn getY(node: opaque) {
|
|||||||
|
|
||||||
//lifecycle functions
|
//lifecycle functions
|
||||||
fn onInit(node: opaque) {
|
fn onInit(node: opaque) {
|
||||||
print "render.toy:onInit() called";
|
print "render.toy:onInit() called\n";
|
||||||
|
|
||||||
node.loadTexture("sprites:/character.png");
|
node.loadTexture("sprites:/character.png");
|
||||||
parent = node.getNodeParent();
|
parent = node.getNodeParent();
|
||||||
@@ -36,13 +36,13 @@ fn onStep(node: opaque) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn onFree(node: opaque) {
|
fn onFree(node: opaque) {
|
||||||
print "render.toy:onFree() called";
|
print "render.toy:onFree() called\n";
|
||||||
|
|
||||||
node.freeTexture();
|
node.freeTexture();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onDraw(node: opaque) {
|
fn onDraw(node: opaque) {
|
||||||
// print "render.toy:onDraw() called";
|
// print "render.toy:onDraw() called\n";
|
||||||
|
|
||||||
var px = parent.callNode("getX");
|
var px = parent.callNode("getX");
|
||||||
var py = parent.callNode("getY");
|
var py = parent.callNode("getY");
|
||||||
@@ -104,11 +104,11 @@ fn onKeyUp(node: opaque, event: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn onMouseMotion(node: opaque, x: int, y: int, xrel: int, yrel: int) {
|
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) {
|
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
|
//jump to pos
|
||||||
posX = x - WIDTH / 2;
|
posX = x - WIDTH / 2;
|
||||||
@@ -116,10 +116,10 @@ fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) {
|
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) {
|
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";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,5 +17,5 @@ fn fib(n : int) {
|
|||||||
|
|
||||||
for (var i = 0; i < 40; i++) {
|
for (var i = 0; i < 40; i++) {
|
||||||
var res = fib(i);
|
var res = fib(i);
|
||||||
print string i + ": " + string res;
|
print string i + ": " + string res + "\n";
|
||||||
}
|
}
|
||||||
@@ -5,5 +5,5 @@ fn fib(n : int) {
|
|||||||
|
|
||||||
for (var i = 0; i < 20; i++) {
|
for (var i = 0; i < 20; i++) {
|
||||||
var res = fib(i);
|
var res = fib(i);
|
||||||
print string i + ": " + string res;
|
print string i + ": " + string res + "\n";
|
||||||
}
|
}
|
||||||
61
scripts/level.toy
Normal file
61
scripts/level.toy
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ for (var i = 0; i < SIZE -1; i++) {
|
|||||||
prev += " ";
|
prev += " ";
|
||||||
}
|
}
|
||||||
prev += "*"; //initial
|
prev += "*"; //initial
|
||||||
print prev;
|
print prev + "\n";
|
||||||
|
|
||||||
//run
|
//run
|
||||||
for (var iteration = 0; iteration < SIZE -1; iteration++) {
|
for (var iteration = 0; iteration < SIZE -1; iteration++) {
|
||||||
@@ -44,6 +44,6 @@ for (var iteration = 0; iteration < SIZE -1; iteration++) {
|
|||||||
//right
|
//right
|
||||||
output += (lookup[prev[SIZE-2]][prev[SIZE-1]][" "]);
|
output += (lookup[prev[SIZE-2]][prev[SIZE-1]][" "]);
|
||||||
|
|
||||||
print output;
|
print output + "\n";
|
||||||
prev = output;
|
prev = output;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import compound;
|
|
||||||
|
|
||||||
var a = [false, false, false];
|
var xrel: int = 0;
|
||||||
var d = ["one": false, "two": false];
|
var yrel: int = 0;
|
||||||
|
|
||||||
print d.containsKey("one");
|
|
||||||
print d.containsKey("three");
|
|
||||||
|
|
||||||
|
if (xrel > 1 || xrel < -1 || yrel > 1 || yrel < -1) {
|
||||||
|
print "outside";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print "inside";
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
static void printWrapper(const char* output) {
|
static void printWrapper(const char* output) {
|
||||||
printf("%s", output);
|
printf("%s", output);
|
||||||
printf("\n"); //default new line
|
// printf("\n"); //default new line
|
||||||
}
|
}
|
||||||
|
|
||||||
static void assertWrapper(const char* output) {
|
static void assertWrapper(const char* output) {
|
||||||
|
|||||||
@@ -428,12 +428,12 @@ static Toy_Opcode binary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case TOY_TOKEN_AND: {
|
case TOY_TOKEN_AND: {
|
||||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
|
parsePrecedence(parser, nodeHandle, PREC_AND);
|
||||||
return TOY_OP_AND;
|
return TOY_OP_AND;
|
||||||
}
|
}
|
||||||
|
|
||||||
case TOY_TOKEN_OR: {
|
case TOY_TOKEN_OR: {
|
||||||
parsePrecedence(parser, nodeHandle, PREC_COMPARISON);
|
parsePrecedence(parser, nodeHandle, PREC_OR);
|
||||||
return TOY_OP_OR;
|
return TOY_OP_OR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -448,7 +448,7 @@ static Toy_Opcode unary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
|||||||
|
|
||||||
if (parser->previous.type == TOY_TOKEN_MINUS) {
|
if (parser->previous.type == TOY_TOKEN_MINUS) {
|
||||||
//temp handle to potentially negate values
|
//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
|
//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))) {
|
if (tmpNode != NULL && tmpNode->type == TOY_AST_NODE_LITERAL && (TOY_IS_INTEGER(tmpNode->atomic.literal) || TOY_IS_FLOAT(tmpNode->atomic.literal))) {
|
||||||
|
|||||||
7
test/scripts/or-chaining-bugfix.toy
Normal file
7
test/scripts/or-chaining-bugfix.toy
Normal file
@@ -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";
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ int main() {
|
|||||||
|
|
||||||
//test answer
|
//test answer
|
||||||
{
|
{
|
||||||
interpreter.printOutput("Testing answer");
|
interpreter.printOutput("Testing answer\n");
|
||||||
|
|
||||||
Toy_LiteralArray arguments;
|
Toy_LiteralArray arguments;
|
||||||
Toy_initLiteralArray(&arguments);
|
Toy_initLiteralArray(&arguments);
|
||||||
@@ -69,7 +69,7 @@ int main() {
|
|||||||
|
|
||||||
//test identity
|
//test identity
|
||||||
{
|
{
|
||||||
interpreter.printOutput("Testing identity");
|
interpreter.printOutput("Testing identity\n");
|
||||||
|
|
||||||
Toy_LiteralArray arguments;
|
Toy_LiteralArray arguments;
|
||||||
Toy_initLiteralArray(&arguments);
|
Toy_initLiteralArray(&arguments);
|
||||||
@@ -104,7 +104,7 @@ int main() {
|
|||||||
|
|
||||||
//test makeCounter (closures)
|
//test makeCounter (closures)
|
||||||
{
|
{
|
||||||
interpreter.printOutput("Testing makeCounter (closures)");
|
interpreter.printOutput("Testing makeCounter (closures)\n");
|
||||||
|
|
||||||
Toy_LiteralArray arguments;
|
Toy_LiteralArray arguments;
|
||||||
Toy_initLiteralArray(&arguments);
|
Toy_initLiteralArray(&arguments);
|
||||||
@@ -209,7 +209,7 @@ int main() {
|
|||||||
|
|
||||||
//test assertion failure
|
//test assertion failure
|
||||||
{
|
{
|
||||||
interpreter.printOutput("Testing assertion failure");
|
interpreter.printOutput("Testing assertion failure\n");
|
||||||
|
|
||||||
Toy_setInterpreterAssert(&interpreter, noPrintFn);
|
Toy_setInterpreterAssert(&interpreter, noPrintFn);
|
||||||
|
|
||||||
|
|||||||
@@ -126,6 +126,7 @@ int main() {
|
|||||||
"long-dictionary.toy",
|
"long-dictionary.toy",
|
||||||
"long-literals.toy",
|
"long-literals.toy",
|
||||||
"native-functions.toy",
|
"native-functions.toy",
|
||||||
|
"or-chaining-bugfix.toy",
|
||||||
"panic-within-functions.toy",
|
"panic-within-functions.toy",
|
||||||
"ternary-expressions.toy",
|
"ternary-expressions.toy",
|
||||||
"types.toy",
|
"types.toy",
|
||||||
|
|||||||
Reference in New Issue
Block a user