feat: switch to 64-bit integers and dynamic memory

Co-authored-by: aider (openrouter/deepseek/deepseek-v4-pro) <aider@aider.chat>
This commit is contained in:
2026-05-03 18:45:42 +03:00
parent a882bf6888
commit afd0a80dcb
6 changed files with 551 additions and 660 deletions
+35 -40
View File
@@ -8,81 +8,76 @@
#include <stdint.h>
#include <inttypes.h>
// Configuration
#define DATA_STACK_SIZE 256
#define RET_STACK_SIZE 256
#define DICT_SIZE 256
#define BODY_SIZE 1024
#define COMPILE_BUF_SIZE 1024
#define INPUT_BUF_SIZE 256
// Configuration (all hard limits removed)
#define MAX_NAME_LEN 31
#define COMPILE_STACK_SIZE 256
#define USER_MEMORY_SIZE 1024
// Core types
typedef struct Word Word;
typedef union Cell {
Word* word;
int32_t num;
union Cell* cell_ptr; // Use union tag here, as typedef Cell is not available yet
int64_t num;
void* ptr; // used for return stack (holds Cell*)
} Cell;
struct Word {
Word* prev;
uint8_t flags; // Bit7=immediate, Bit6=hidden, Bits0-5=name length
uint8_t flags; // Bit7=immediate, Bit6=hidden, Bits0-5=name length
char name[MAX_NAME_LEN + 1];
void (*code)(Word*);
Cell* body;
Cell* body; // points to the words body (allocated dynamically)
};
// Globals
extern int32_t data_stack[DATA_STACK_SIZE];
extern int sp;
extern Cell ret_stack[RET_STACK_SIZE];
extern int rp;
extern Cell* ip;
// Globals (all dynamic)
extern int64_t *data_stack;
extern int32_t data_sp; // stack indices are small enough for int32_t
extern int32_t data_cap;
extern Cell *ret_stack;
extern int32_t rp;
extern int32_t ret_cap;
extern Cell* ip; // instruction pointer
extern Word dict[DICT_SIZE];
extern int dict_idx;
extern Word* dict_head;
extern Cell dict_bodies[BODY_SIZE];
extern int body_idx;
extern int state; // 0=interpret, 1=compile
extern Cell compile_buf[COMPILE_BUF_SIZE];
extern int compile_idx;
extern int state;
extern Cell *compile_buf;
extern int32_t compile_idx;
extern int32_t compile_cap;
extern char compiling_name[MAX_NAME_LEN + 1];
extern char input_buf[INPUT_BUF_SIZE];
extern char input_buf[1024]; // fixed for line input
extern char* input_ptr;
extern Cell* compile_stack[COMPILE_STACK_SIZE];
extern int compile_sp;
extern int64_t *compile_stack; // holds indices into compile_buf
extern int32_t compile_sp;
extern int32_t compile_stack_cap;
extern int32_t user_mem[USER_MEMORY_SIZE];
extern int32_t* here;
extern Cell *user_mem;
extern int64_t user_mem_size; // in cells
extern Cell* here;
// Core function prototypes
void data_push(int32_t val);
int32_t data_pop(void);
void data_push(int64_t val);
int64_t data_pop(void);
void ret_push_ip(Cell* val);
Cell* ret_pop_ip(void);
void ret_push_num(int32_t val);
int32_t ret_pop_num(void);
void compile_push(Cell* addr);
Cell* compile_pop(void);
void ret_push_num(int64_t val);
int64_t ret_pop_num(void);
void compile_push(int64_t idx);
int64_t compile_pop(void);
Word* add_primitive(const char* name, void (*code)(Word*), uint8_t flags);
Word* lookup_word(const char* name);
Word* lookup_word_internal(const char* name);
char* next_token(void);
void inner_interpreter(void);
void process_token(const char* token);
void outer_interpreter(void);
// Primitive word prototypes
// Ensure compile_buf has at least 'needed' free cells
void ensure_compile_cap(int32_t needed);
// Stack ops
void do_dup(Word* w);
void do_drop(Word* w);
@@ -171,4 +166,4 @@ void do_until(Word* w);
void do_while(Word* w);
void do_repeat(Word* w);
#endif // FORTH_H
#endif