mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Added dot chaining for functions
Well, it should work without issues...
This commit is contained in:
@@ -1,5 +1,56 @@
|
||||
|
||||
|
||||
import standard as std;
|
||||
fn _b(self) {
|
||||
print "running _b";
|
||||
print self;
|
||||
return self;
|
||||
}
|
||||
|
||||
print std;
|
||||
fn _c(self) {
|
||||
print "running _c";
|
||||
print self;
|
||||
return self;
|
||||
}
|
||||
|
||||
fn _d(self) {
|
||||
print "running _d";
|
||||
print self;
|
||||
return self;
|
||||
}
|
||||
|
||||
fn _e(self) {
|
||||
print "running _e";
|
||||
print self;
|
||||
return self;
|
||||
}
|
||||
|
||||
fn _f(self) {
|
||||
print "running _f";
|
||||
print self;
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
fn b() {
|
||||
print "running b";
|
||||
}
|
||||
|
||||
fn c() {
|
||||
print "running c";
|
||||
}
|
||||
|
||||
fn d() {
|
||||
print "running d";
|
||||
}
|
||||
|
||||
fn e() {
|
||||
print "running e";
|
||||
}
|
||||
|
||||
fn f() {
|
||||
print "running f";
|
||||
}
|
||||
|
||||
var a = 42;
|
||||
|
||||
print a.b().c().d().e().f();
|
||||
22
scripts/test/dot-chaining.toy
Normal file
22
scripts/test/dot-chaining.toy
Normal file
@@ -0,0 +1,22 @@
|
||||
//test function chaining with the dot operator
|
||||
|
||||
fn _identity(self) {
|
||||
return self;
|
||||
}
|
||||
|
||||
fn _check(self) {
|
||||
assert self == 42, "dot chaining failed";
|
||||
return self;
|
||||
}
|
||||
|
||||
var val = 42;
|
||||
|
||||
val
|
||||
.identity()
|
||||
.check()
|
||||
.identity()
|
||||
.check()
|
||||
;
|
||||
|
||||
|
||||
print "All good";
|
||||
@@ -799,8 +799,10 @@ static Opcode dot(Parser* parser, ASTNode** nodeHandle) {
|
||||
}
|
||||
|
||||
//hijack the function call, and hack in an extra parameter
|
||||
if (node->binary.opcode == OP_FN_CALL) {
|
||||
node->binary.right->fnCall.argumentCount++;
|
||||
node->binary.opcode = OP_DOT;
|
||||
}
|
||||
|
||||
(*nodeHandle) = node;
|
||||
return OP_DOT; //signal that the function name and arguments are in the wrong order
|
||||
@@ -1083,6 +1085,17 @@ static bool calcStaticBinaryArithmetic(Parser* parser, ASTNode** nodeHandle) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static void dottify(Parser* parser, ASTNode** nodeHandle) {
|
||||
//only if this is chained from a higher binary "fn_call"
|
||||
if ((*nodeHandle)->type == AST_NODEBINARY) {
|
||||
if ((*nodeHandle)->binary.opcode == OP_FN_CALL) {
|
||||
(*nodeHandle)->binary.opcode = OP_DOT;
|
||||
}
|
||||
dottify(parser, &(*nodeHandle)->binary.left);
|
||||
dottify(parser, &(*nodeHandle)->binary.right);
|
||||
}
|
||||
}
|
||||
|
||||
static void parsePrecedence(Parser* parser, ASTNode** nodeHandle, PrecedenceRule rule) {
|
||||
//every valid expression has a prefix rule
|
||||
advance(parser);
|
||||
@@ -1116,6 +1129,11 @@ static void parsePrecedence(Parser* parser, ASTNode** nodeHandle, PrecedenceRule
|
||||
return; //we're done here
|
||||
}
|
||||
|
||||
//BUGFIX: dot-chaining
|
||||
if (opcode == OP_DOT) {
|
||||
dottify(parser, &rhsNode);
|
||||
}
|
||||
|
||||
emitASTNodeBinary(nodeHandle, rhsNode, opcode);
|
||||
|
||||
if (!calcStaticBinaryArithmetic(parser, nodeHandle)) {
|
||||
|
||||
@@ -168,6 +168,7 @@ int main() {
|
||||
"coercions.toy",
|
||||
"comparisons.toy",
|
||||
"dot-and-matrix.toy",
|
||||
"dot-chaining.toy",
|
||||
"functions.toy",
|
||||
"imports-and-exports.toy",
|
||||
"index-arrays.toy",
|
||||
|
||||
Reference in New Issue
Block a user