|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include "../../include/ast.h" |
| 6 | +#include "../../include/chunk.h" |
| 7 | +#include "../../include/common.h" |
| 8 | +#include "../../include/value.h" |
| 9 | +#include "../../include/object.h" |
| 10 | + |
| 11 | +typedef struct { |
| 12 | + Chunk* chunk; |
| 13 | + // Local scope management (simplified for now, will need full symbol table later) |
| 14 | + // For now, we assume global scope if not handled. |
| 15 | +} BytecodeGen; |
| 16 | + |
| 17 | +static void emitByte(BytecodeGen* gen, uint8_t byte, int line) { |
| 18 | + writeChunk(gen->chunk, byte, line); |
| 19 | +} |
| 20 | + |
| 21 | +static void emitBytes(BytecodeGen* gen, uint8_t byte1, uint8_t byte2, int line) { |
| 22 | + emitByte(gen, byte1, line); |
| 23 | + emitByte(gen, byte2, line); |
| 24 | +} |
| 25 | + |
| 26 | +static void emitConstant(BytecodeGen* gen, Value value, int line) { |
| 27 | + int constant = addConstant(gen->chunk, value); |
| 28 | + if (constant > 255) { |
| 29 | + // Handle long constants if needed |
| 30 | + fprintf(stderr, "Too many constants in one chunk\n"); |
| 31 | + return; |
| 32 | + } |
| 33 | + emitBytes(gen, OP_CONSTANT, (uint8_t)constant, line); |
| 34 | +} |
| 35 | + |
| 36 | +static int emitJump(BytecodeGen *gen, uint8_t instruction, int line) { |
| 37 | + emitByte(gen, instruction, line); |
| 38 | + emitByte(gen, 0xff, line); |
| 39 | + emitByte(gen, 0xff, line); |
| 40 | + return gen->chunk->count - 2; |
| 41 | +} |
| 42 | + |
| 43 | +static void patchJump(BytecodeGen *gen, int offset) { |
| 44 | + // -2 to adjust for the bytecode for the jump offset itself. |
| 45 | + int jump = gen->chunk->count - offset - 2; |
| 46 | + |
| 47 | + if (jump > UINT16_MAX) { |
| 48 | + fprintf(stderr, "Too much code to jump over.\n"); |
| 49 | + } |
| 50 | + |
| 51 | + gen->chunk->code[offset] = (jump >> 8) & 0xff; |
| 52 | + gen->chunk->code[offset + 1] = jump & 0xff; |
| 53 | +} |
| 54 | + |
| 55 | +static void emitLoop(BytecodeGen *gen, int loopStart, int line) { |
| 56 | + emitByte(gen, OP_LOOP, line); |
| 57 | + |
| 58 | + int offset = gen->chunk->count - loopStart + 2; |
| 59 | + if (offset > UINT16_MAX) fprintf(stderr, "Loop body too large.\n"); |
| 60 | + |
| 61 | + emitByte(gen, (offset >> 8) & 0xff, line); |
| 62 | + emitByte(gen, offset & 0xff, line); |
| 63 | +} |
| 64 | + |
| 65 | +// Forward declarations for recursive traversal |
| 66 | +static void genExpr(BytecodeGen* gen, Expr* expr); |
| 67 | +static void genStmt(BytecodeGen* gen, Stmt* stmt); |
| 68 | + |
| 69 | +static void genExpr(BytecodeGen* gen, Expr* expr) { |
| 70 | + if (expr == NULL) return; |
| 71 | + |
| 72 | + switch (expr->type) { |
| 73 | + case EXPR_LITERAL: |
| 74 | + if (IS_NUMBER(expr->as.literal.value)) { |
| 75 | + emitConstant(gen, expr->as.literal.value, expr->line); |
| 76 | + } else if (IS_BOOL(expr->as.literal.value)) { |
| 77 | + emitByte(gen, AS_BOOL(expr->as.literal.value) ? OP_TRUE : OP_FALSE, expr->line); |
| 78 | + } else if (IS_NULL(expr->as.literal.value)) { |
| 79 | + emitByte(gen, OP_NIL, expr->line); |
| 80 | + } |
| 81 | + break; |
| 82 | + |
| 83 | + case EXPR_BINARY: |
| 84 | + genExpr(gen, expr->as.binary.left); |
| 85 | + genExpr(gen, expr->as.binary.right); |
| 86 | + |
| 87 | + if (strcmp(expr->as.binary.operator, "+") == 0) emitByte(gen, OP_ADD, expr->line); |
| 88 | + else if (strcmp(expr->as.binary.operator, "-") == 0) emitByte(gen, OP_SUBTRACT, expr->line); |
| 89 | + else if (strcmp(expr->as.binary.operator, "*") == 0) emitByte(gen, OP_MULTIPLY, expr->line); |
| 90 | + else if (strcmp(expr->as.binary.operator, "/") == 0) emitByte(gen, OP_DIVIDE, expr->line); |
| 91 | + else if (strcmp(expr->as.binary.operator, "==") == 0) emitByte(gen, OP_EQUAL, expr->line); |
| 92 | + else if (strcmp(expr->as.binary.operator, "!=") == 0) { |
| 93 | + emitBytes(gen, OP_EQUAL, OP_NOT, expr->line); |
| 94 | + } |
| 95 | + else if (strcmp(expr->as.binary.operator, ">") == 0) emitByte(gen, OP_GREATER, expr->line); |
| 96 | + else if (strcmp(expr->as.binary.operator, "<") == 0) emitByte(gen, OP_LESS, expr->line); |
| 97 | + else if (strcmp(expr->as.binary.operator, ">=") == 0) { |
| 98 | + emitBytes(gen, OP_LESS, OP_NOT, expr->line); |
| 99 | + } |
| 100 | + else if (strcmp(expr->as.binary.operator, "<=") == 0) { |
| 101 | + emitBytes(gen, OP_GREATER, OP_NOT, expr->line); |
| 102 | + } |
| 103 | + break; |
| 104 | + |
| 105 | + case EXPR_UNARY: |
| 106 | + genExpr(gen, expr->as.unary.right); |
| 107 | + if (strcmp(expr->as.unary.operator, "!") == 0) emitByte(gen, OP_NOT, expr->line); |
| 108 | + else if (strcmp(expr->as.unary.operator, "-") == 0) emitByte(gen, OP_NEGATE, expr->line); |
| 109 | + break; |
| 110 | + |
| 111 | + case EXPR_GROUPING: |
| 112 | + genExpr(gen, expr->as.grouping.expression); |
| 113 | + break; |
| 114 | + |
| 115 | + case EXPR_VARIABLE: { |
| 116 | + // Placeholder: Assume global for now |
| 117 | + // In a real implementation, we'd lookup in scope and emit OP_GET_LOCAL if found |
| 118 | + // For now, we'll need to store the variable name as a constant |
| 119 | + Value nameVal = OBJ_VAL(copyString(expr->as.variable.name, strlen(expr->as.variable.name))); |
| 120 | + int nameConst = addConstant(gen->chunk, nameVal); |
| 121 | + emitBytes(gen, OP_GET_GLOBAL, (uint8_t)nameConst, expr->line); |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + case EXPR_ASSIGN: { |
| 126 | + genExpr(gen, expr->as.assign.value); |
| 127 | + Value nameVal = OBJ_VAL(copyString(expr->as.assign.name, strlen(expr->as.assign.name))); |
| 128 | + int nameConst = addConstant(gen->chunk, nameVal); |
| 129 | + emitBytes(gen, OP_SET_GLOBAL, (uint8_t)nameConst, expr->line); |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + case EXPR_LOGICAL: { |
| 134 | + if (strcmp(expr->as.logical.operator, "&&") == 0) { |
| 135 | + genExpr(gen, expr->as.logical.left); |
| 136 | + int endJump = emitJump(gen, OP_JUMP_IF_FALSE, expr->line); |
| 137 | + emitByte(gen, OP_POP, expr->line); |
| 138 | + genExpr(gen, expr->as.logical.right); |
| 139 | + patchJump(gen, endJump); |
| 140 | + } else { // || |
| 141 | + genExpr(gen, expr->as.logical.left); |
| 142 | + int elseJump = emitJump(gen, OP_JUMP_IF_FALSE, expr->line); |
| 143 | + int endJump = emitJump(gen, OP_JUMP, expr->line); |
| 144 | + patchJump(gen, elseJump); |
| 145 | + emitByte(gen, OP_POP, expr->line); |
| 146 | + genExpr(gen, expr->as.logical.right); |
| 147 | + patchJump(gen, endJump); |
| 148 | + } |
| 149 | + break; |
| 150 | + } |
| 151 | + |
| 152 | + default: |
| 153 | + fprintf(stderr, "Unimplemented expression type in BytecodeGen: %d\n", expr->type); |
| 154 | + break; |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +static void genStmt(BytecodeGen* gen, Stmt* stmt) { |
| 159 | + if (stmt == NULL) return; |
| 160 | + |
| 161 | + switch (stmt->type) { |
| 162 | + case STMT_EXPRESSION: |
| 163 | + genExpr(gen, stmt->as.expression.expression); |
| 164 | + emitByte(gen, OP_POP, stmt->line); |
| 165 | + break; |
| 166 | + |
| 167 | + case STMT_PRINT: |
| 168 | + genExpr(gen, stmt->as.print.expression); |
| 169 | + emitByte(gen, OP_PRINT, stmt->line); |
| 170 | + break; |
| 171 | + |
| 172 | + case STMT_RETURN: |
| 173 | + if (stmt->as.return_stmt.value != NULL) { |
| 174 | + genExpr(gen, stmt->as.return_stmt.value); |
| 175 | + } else { |
| 176 | + emitByte(gen, OP_NIL, stmt->line); |
| 177 | + } |
| 178 | + emitByte(gen, OP_RETURN, stmt->line); |
| 179 | + break; |
| 180 | + |
| 181 | + case STMT_BLOCK: |
| 182 | + for (int i = 0; i < stmt->as.block.statements->count; i++) { |
| 183 | + genStmt(gen, stmt->as.block.statements->items[i]); |
| 184 | + } |
| 185 | + break; |
| 186 | + |
| 187 | + case STMT_IF: { |
| 188 | + genExpr(gen, stmt->as.if_stmt.condition); |
| 189 | + int thenJump = emitJump(gen, OP_JUMP_IF_FALSE, stmt->line); |
| 190 | + emitByte(gen, OP_POP, stmt->line); |
| 191 | + genStmt(gen, stmt->as.if_stmt.then_branch); |
| 192 | + |
| 193 | + int elseJump = emitJump(gen, OP_JUMP, stmt->line); |
| 194 | + patchJump(gen, thenJump); |
| 195 | + emitByte(gen, OP_POP, stmt->line); |
| 196 | + |
| 197 | + if (stmt->as.if_stmt.else_branch != NULL) { |
| 198 | + genStmt(gen, stmt->as.if_stmt.else_branch); |
| 199 | + } |
| 200 | + patchJump(gen, elseJump); |
| 201 | + break; |
| 202 | + } |
| 203 | + |
| 204 | + case STMT_WHILE: { |
| 205 | + int loopStart = gen->chunk->count; |
| 206 | + genExpr(gen, stmt->as.while_stmt.condition); |
| 207 | + |
| 208 | + int exitJump = emitJump(gen, OP_JUMP_IF_FALSE, stmt->line); |
| 209 | + emitByte(gen, OP_POP, stmt->line); |
| 210 | + genStmt(gen, stmt->as.while_stmt.body); |
| 211 | + emitLoop(gen, loopStart, stmt->line); |
| 212 | + |
| 213 | + patchJump(gen, exitJump); |
| 214 | + emitByte(gen, OP_POP, stmt->line); |
| 215 | + break; |
| 216 | + } |
| 217 | + |
| 218 | + case STMT_VAR_DECL: { |
| 219 | + if (stmt->as.var_decl.initializer != NULL) { |
| 220 | + genExpr(gen, stmt->as.var_decl.initializer); |
| 221 | + } else { |
| 222 | + emitByte(gen, OP_NIL, stmt->line); |
| 223 | + } |
| 224 | + Value nameVal = OBJ_VAL(copyString(stmt->as.var_decl.name, strlen(stmt->as.var_decl.name))); |
| 225 | + int nameConst = addConstant(gen->chunk, nameVal); |
| 226 | + emitBytes(gen, OP_DEFINE_GLOBAL, (uint8_t)nameConst, stmt->line); |
| 227 | + break; |
| 228 | + } |
| 229 | + |
| 230 | + default: |
| 231 | + fprintf(stderr, "Unimplemented statement type in BytecodeGen: %d\n", stmt->type); |
| 232 | + break; |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +void generateBytecode(StmtList* statements, Chunk* chunk) { |
| 237 | + BytecodeGen gen; |
| 238 | + gen.chunk = chunk; |
| 239 | + |
| 240 | + for (int i = 0; i < statements->count; i++) { |
| 241 | + genStmt(&gen, statements->items[i]); |
| 242 | + } |
| 243 | +} |
0 commit comments