Skip to content

Commit ba654c6

Browse files
committed
feat: Introduce core VM, compiler, and runtime components with initial instruction set.
1 parent 40acb71 commit ba654c6

21 files changed

Lines changed: 616 additions & 690 deletions

include/advanced.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#ifndef PROX_ADVANCED_H
1313
#define PROX_ADVANCED_H
1414

15-
#include "chunk.h"
15+
#include "bytecode.h"
1616
#include "common.h"
1717

1818

include/bytecode.h

Lines changed: 62 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,70 @@
1-
// --------------------------------------------------
2-
// Project: ProX Programming Language (ProXPL)
3-
// Author: ProgrammerKR
4-
// Created: 2025-12-16
5-
// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved.
6-
71
#ifndef PROXPL_BYTECODE_H
82
#define PROXPL_BYTECODE_H
9-
#include <stdint.h>
10-
#include <stddef.h>
11-
12-
/* Value representation (simple) */
13-
typedef enum {
14-
VAL_NULL = 0,
15-
VAL_NUMBER,
16-
VAL_BOOL,
17-
VAL_STRING,
18-
VAL_FUNCTION_PROTO
19-
} ValueType;
203

21-
typedef struct {
22-
ValueType type;
23-
union {
24-
double number;
25-
int boolean;
26-
struct {
27-
char *chars;
28-
uint32_t length;
29-
} string;
30-
void *proto; /* placeholder for function proto during runtime */
31-
} as;
32-
} Value;
4+
#include "common.h"
5+
#include "value.h"
336

34-
/* Constant table */
35-
typedef struct {
36-
Value *items;
37-
size_t count;
38-
size_t capacity;
39-
} ConstTable;
40-
41-
/* Chunk (bytecode + constants) */
7+
// ProX-Bytecode Opcodes
8+
typedef enum {
9+
OP_CONSTANT,
10+
OP_NIL,
11+
OP_TRUE,
12+
OP_FALSE,
13+
OP_POP,
14+
OP_GET_LOCAL,
15+
OP_SET_LOCAL,
16+
OP_GET_GLOBAL,
17+
OP_DEFINE_GLOBAL,
18+
OP_SET_GLOBAL,
19+
OP_GET_UPVALUE,
20+
OP_SET_UPVALUE,
21+
OP_GET_PROPERTY,
22+
OP_SET_PROPERTY,
23+
OP_GET_SUPER,
24+
OP_EQUAL,
25+
OP_GREATER,
26+
OP_LESS,
27+
OP_ADD,
28+
OP_SUBTRACT,
29+
OP_MULTIPLY,
30+
OP_DIVIDE,
31+
OP_NOT,
32+
OP_NEGATE,
33+
OP_PRINT,
34+
OP_JUMP,
35+
OP_JUMP_IF_FALSE,
36+
OP_LOOP,
37+
OP_CALL,
38+
OP_INVOKE,
39+
OP_SUPER_INVOKE,
40+
OP_CLOSURE,
41+
OP_CLOSE_UPVALUE,
42+
OP_RETURN,
43+
OP_CLASS,
44+
OP_INHERIT,
45+
OP_METHOD,
46+
47+
// Future enhancements
48+
OP_MATCH,
49+
OP_AWAIT,
50+
OP_YIELD,
51+
52+
OP_HALT = 0xFF
53+
} OpCode;
54+
55+
// Chunk: Sequence of bytecode and metadata
4256
typedef struct {
43-
uint8_t *code;
44-
size_t code_len;
45-
size_t code_cap;
46-
ConstTable constants;
57+
int count;
58+
int capacity;
59+
uint8_t *code;
60+
int *lines; // Line numbers for debugging/errors
61+
ValueArray constants; // Constant pool
4762
} Chunk;
4863

49-
/* Function prototype (serialized form) */
50-
typedef struct {
51-
uint32_t arity;
52-
uint16_t max_regs;
53-
Chunk chunk;
54-
uint16_t upvalue_count;
55-
} FunctionProto;
56-
57-
/* Opcodes */
58-
typedef enum {
59-
OP_NOP = 0x00,
60-
OP_ADD = 0x01,
61-
OP_SUB = 0x02,
62-
OP_MUL = 0x03,
63-
OP_DIV = 0x04,
64-
OP_MOD = 0x05,
65-
OP_NEG = 0x06,
66-
67-
OP_EQ = 0x10,
68-
OP_NEQ = 0x11,
69-
OP_LT = 0x12,
70-
OP_LTE = 0x13,
71-
OP_GT = 0x14,
72-
OP_GTE = 0x15,
73-
74-
OP_NOT = 0x20,
75-
OP_AND = 0x21,
76-
OP_OR = 0x22,
77-
78-
OP_PUSH_CONST = 0x30,
79-
OP_LOAD_REG = 0x31,
80-
OP_STORE_REG = 0x32,
81-
OP_POP = 0x33,
82-
OP_DUP = 0x34,
83-
84-
OP_JMP = 0x40,
85-
OP_JMP_IF_TRUE = 0x41,
86-
OP_JMP_IF_FALSE= 0x42,
87-
88-
OP_CALL = 0x50,
89-
OP_RETURN = 0x51,
90-
OP_TAIL_CALL = 0x52,
91-
92-
OP_MAKE_FUNCTION = 0x60,
93-
OP_CLOSURE = 0x61,
94-
OP_CLOSE_UPVALUE = 0x62,
95-
OP_LOAD_UPVALUE = 0x63,
96-
OP_STORE_UPVALUE = 0x64,
97-
98-
OP_NEW_ARRAY = 0x70,
99-
OP_INDEX_GET = 0x71,
100-
OP_INDEX_SET = 0x72,
101-
OP_GET_FIELD = 0x73,
102-
OP_SET_FIELD = 0x74,
103-
104-
OP_DBG_LINE = 0x90,
105-
OP_DBG_LOC = 0x91,
106-
107-
OP_HALT = 0xFF
108-
} Opcode;
109-
110-
/* Addressing modes for CALL / similar */
111-
typedef enum {
112-
AM_CONST = 0x01,
113-
AM_REG = 0x02,
114-
AM_STACK = 0x03
115-
} AddrMode;
116-
117-
/* Chunk API */
118-
void chunk_init(Chunk *chunk);
119-
void chunk_free(Chunk *chunk);
120-
void chunk_write_byte(Chunk *chunk, uint8_t byte);
121-
void chunk_write_bytes(Chunk *chunk, const uint8_t *bytes, size_t len);
122-
123-
/* emission helpers */
124-
void emit_opcode(Chunk *chunk, Opcode op);
125-
void emit_u8(Chunk *chunk, uint8_t x);
126-
void emit_u16_le(Chunk *chunk, uint16_t x);
127-
void emit_u32_le(Chunk *chunk, uint32_t x);
128-
void emit_uleb128(Chunk *chunk, uint64_t value);
129-
void emit_sleb128(Chunk *chunk, int64_t value);
130-
131-
/* constant table */
132-
void consttable_init(ConstTable *tbl);
133-
void consttable_free(ConstTable *tbl);
134-
size_t consttable_add(ConstTable *tbl, Value v);
135-
Value consttable_get(const ConstTable *tbl, size_t idx);
136-
137-
/* serialization */
138-
int write_chunk_to_file(const char *path, const Chunk *chunk);
139-
int read_chunk_from_file(const char *path, Chunk *out);
140-
141-
/* utility */
142-
uint64_t read_uleb128_from(const uint8_t *buf, size_t buf_len, size_t *out_read);
143-
int64_t read_sleb128_from(const uint8_t *buf, size_t buf_len, size_t *out_read);
64+
// Chunk API
65+
void initChunk(Chunk *chunk);
66+
void freeChunk(Chunk *chunk);
67+
void writeChunk(Chunk *chunk, uint8_t byte, int line);
68+
int addConstant(Chunk *chunk, Value value);
14469

145-
#endif /* PROXPL_BYTECODE_H */
70+
#endif // PROXPL_BYTECODE_H

include/chunk.h

Lines changed: 0 additions & 68 deletions
This file was deleted.

include/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ typedef int16_t i16;
2323
typedef int32_t i32;
2424
typedef int64_t i64;
2525

26+
#define UINT8_MAX 255
27+
#define UINT8_COUNT (UINT8_MAX + 1)
28+
2629
// Debugging
2730
#define DEBUG_TRACE_EXECUTION
2831
#define DEBUG_PRINT_CODE

include/compiler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#ifndef PROX_COMPILER_H
88
#define PROX_COMPILER_H
99

10-
#include "scanner.h"
10+
#include "bytecode.h"
1111
#include "vm.h"
1212

1313

@@ -35,7 +35,7 @@ typedef struct {
3535
Precedence precedence;
3636
} ParseRule;
3737

38-
bool compile(const char *source, Chunk *chunk);
38+
ObjFunction* compile(const char *source);
3939

4040
#include "ast.h"
4141
void generateBytecode(StmtList* statements, Chunk* chunk);

include/debug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#ifndef PROX_DEBUG_H
88
#define PROX_DEBUG_H
99

10-
#include "chunk.h"
10+
#include "bytecode.h"
1111

1212
void disassembleChunk(Chunk *chunk, const char *name);
1313
int disassembleInstruction(Chunk *chunk, int offset);

include/error_report.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef PROXPL_ERROR_REPORT_H
2+
#define PROXPL_ERROR_REPORT_H
3+
4+
#include "common.h"
5+
#include "scanner.h"
6+
7+
// Report a compile-time error with code context
8+
void reportCompileError(const char* source, Token token, const char* message);
9+
10+
// Report a runtime error with code context
11+
void reportRuntimeError(const char* source, int line, const char* message);
12+
13+
#endif // PROXPL_ERROR_REPORT_H

include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include "common.h"
1111
#include "value.h"
12-
#include "chunk.h" // Required because ObjFunction contains a 'Chunk' struct
12+
#include "bytecode.h" // Required because ObjFunction contains a 'Chunk' struct
1313

1414
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
1515

include/proxpl_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define PROXPL_API_H
99

1010
#include "vm.h"
11-
#include "chunk.h"
11+
#include "bytecode.h"
1212

1313
/* Public API: Initialize and shutdown the VM */
1414
void proxpl_vm_init(VM *vm);

0 commit comments

Comments
 (0)