feat: abstract I/O and strings for freestanding compilation
Co-authored-by: aider (openrouter/moonshotai/kimi-k2.6) <aider@aider.chat>
This commit is contained in:
@@ -1,29 +1,46 @@
|
||||
#ifndef FORTH_H
|
||||
#define FORTH_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
// Configuration (all hard limits removed)
|
||||
#define MAX_NAME_LEN 31
|
||||
#define FORTH_EOF (-1)
|
||||
|
||||
/* Portable panic hook. Override for bare-metal reset/handling. */
|
||||
#ifndef forth_panic
|
||||
#define forth_panic() do { for (;;) ; } while (0)
|
||||
#endif
|
||||
/* Platform interface -- must be provided by the target platform layer. */
|
||||
void forth_putchar(char c);
|
||||
int forth_getchar(void);
|
||||
void forth_printf(const char* fmt, ...);
|
||||
void forth_fflush(void);
|
||||
void forth_panic(void);
|
||||
|
||||
/* Portable I/O hooks. Define FORTH_CUSTOM_IO before including this header
|
||||
* to provide bare-metal UART (or other) implementations. */
|
||||
#ifndef FORTH_CUSTOM_IO
|
||||
#define forth_putchar(c) putchar(c)
|
||||
#define forth_getchar() getchar()
|
||||
#define forth_printf(...) printf(__VA_ARGS__)
|
||||
#define forth_fflush() fflush(stdout)
|
||||
#endif
|
||||
/* Platform string-to-number (base 10 is required). */
|
||||
int64_t forth_strtoll(const char* str, char** endptr, int base);
|
||||
|
||||
/* Portable character classification. */
|
||||
static inline int forth_isspace(int c) {
|
||||
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v';
|
||||
}
|
||||
|
||||
/* Portable string operations. */
|
||||
static inline size_t forth_strlen(const char* s) {
|
||||
size_t n = 0;
|
||||
while (s[n]) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline void forth_memcpy(void* dst, const void* src, size_t n) {
|
||||
char* d = dst;
|
||||
const char* s = src;
|
||||
while (n--) *d++ = *s++;
|
||||
}
|
||||
|
||||
static inline int forth_strcmp(const char* a, const char* b) {
|
||||
while (*a && *a == *b) { a++; b++; }
|
||||
return (unsigned char)*a - (unsigned char)*b;
|
||||
}
|
||||
|
||||
/* Static memory sizes (override before including forth.h) */
|
||||
#ifndef FORTH_DATA_STACK_SIZE
|
||||
@@ -105,6 +122,9 @@ extern Word* w_branch;
|
||||
extern Word* w_zbranch;
|
||||
extern Word* w_dot_quote_inner;
|
||||
|
||||
// Entry point
|
||||
void forth_run(void);
|
||||
|
||||
// Core function prototypes
|
||||
void data_push(int64_t val);
|
||||
int64_t data_pop(void);
|
||||
|
||||
Reference in New Issue
Block a user