Functions are WIP, read more

They're no-ops in compilation for now, and param types aren't parsed.

'return' keyword needs to be implemented.

Was distracted by bugfixes in v2 and v2-docs.
This commit is contained in:
2025-02-02 17:26:47 +11:00
parent 336616a1bf
commit b93ea5006c
6 changed files with 76 additions and 9 deletions

View File

@@ -890,7 +890,7 @@ static void makeVariableDeclarationStmt(Toy_Bucket** bucketHandle, Toy_Parser* p
}
}
//build the string
//build the name string
Toy_String* nameStr = Toy_createNameStringLength(bucketHandle, nameToken.lexeme, nameToken.length, varType, constant);
//if there's an assignment, read it, or default to null
@@ -908,6 +908,31 @@ static void makeVariableDeclarationStmt(Toy_Bucket** bucketHandle, Toy_Parser* p
consume(parser, TOY_TOKEN_OPERATOR_SEMICOLON, "Expected ';' at the end of var statement");
}
static void makeFunctionDeclarationStmt(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle) {
consume(parser, TOY_TOKEN_NAME, "Expected function name after 'fn' keyword");
if (parser->previous.length > 255) {
printError(parser, parser->previous, "Can't have a function name longer than 255 characters");
Toy_private_emitAstError(bucketHandle, rootHandle);
return;
}
//build the name string
Toy_Token nameToken = parser->previous;
Toy_String* nameStr = Toy_createNameStringLength(bucketHandle, nameToken.lexeme, nameToken.length, TOY_VALUE_FUNCTION, true);
//read the function parameters, as a grouping
Toy_Ast* params = NULL;
parsePrecedence(bucketHandle, parser, &params, PREC_GROUP);
//read the body
Toy_Ast* body = NULL;
makeBlockStmt(bucketHandle, parser, &body);
//finally, emit the declaration as an Ast
Toy_private_emitAstFunctionDeclaration(bucketHandle, rootHandle, nameStr, params, body);
}
static void makeStmt(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle) {
//inner scope
if (match(parser, TOY_TOKEN_OPERATOR_BRACE_LEFT)) {
@@ -984,10 +1009,10 @@ static void makeDeclarationStmt(Toy_Bucket** bucketHandle, Toy_Parser* parser, T
makeVariableDeclarationStmt(bucketHandle, parser, rootHandle);
}
// //function declarations
// else if (match(parser, TOY_TOKEN_KEYWORD_FUNCTION)) {
// makeFunctionDeclarationStmt(bucketHandle, parser, rootHandle);
// }
//function declarations
else if (match(parser, TOY_TOKEN_KEYWORD_FUNCTION)) {
makeFunctionDeclarationStmt(bucketHandle, parser, rootHandle);
}
//otherwise
else {