Implemented and tested variable declaration

Assignment, etc. is still to come, as are types.
This commit is contained in:
2024-10-13 15:02:42 +11:00
parent 1ad6bdff70
commit 80734563b9
18 changed files with 498 additions and 99 deletions

View File

@@ -266,6 +266,19 @@ static void writeInstructionPrint(Toy_Routine** rt, Toy_AstPrint ast) {
EMIT_BYTE(rt, code,0);
}
static void writeInstructionVarDeclare(Toy_Routine** rt, Toy_AstVarDeclare ast) {
//initial value
writeRoutineCode(rt, ast.expr);
//delcare with the given name string
EMIT_BYTE(rt, code, TOY_OPCODE_DECLARE);
EMIT_BYTE(rt, code, Toy_getNameStringType(ast.name));
EMIT_BYTE(rt, code, ast.name->length); //quick optimisation to skip a 'strlen()' call
EMIT_BYTE(rt, code, 0);
emitString(rt, ast.name);
}
//routine structure
// static void writeRoutineParam(Toy_Routine* rt) {
// //
@@ -303,6 +316,10 @@ static void writeRoutineCode(Toy_Routine** rt, Toy_Ast* ast) {
writeInstructionPrint(rt, ast->print);
break;
case TOY_AST_VAR_DECLARE:
writeInstructionVarDeclare(rt, ast->varDeclare);
break;
//meta instructions are disallowed
case TOY_AST_PASS:
//NOTE: this should be disallowed, but for now it's required for testing