pforth  0943e5e71114f286dbb2474d55aabf91df02d104
pforth.h
Go to the documentation of this file.
1 #pragma once
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <stdio.h>
7 
8 
9 #ifdef DEBUG
10 #define DBG(fmt, ...) fprintf(stderr, "%s:%d: " fmt, __FILE__, __LINE__, __VA_ARGS__);
11 #else
12 #define DBG(fmt, ...) {}
13 #endif
14 
15 #define PRINT(fmt, ...) fprintf(stdout, fmt, __VA_ARGS__);
16 
17 #define LO(x) ((unsigned char) ((x) & 0xFF))
18 #define HI(x) ((unsigned char) (((x) >> 8) & 0xFF))
19 
23 #define FORTH_DICT_SIZE 1000
24 
28 #define MAX_DO_LOOP_DEPTH 3
29 
33 #define MAX_BEGIN_LOOP_DEPTH 5
34 
38 #define FORTH_TYPE int32_t
39 
43 #define POINTER_TYPE void *
44 
48 #define INT_TYPE_LIST FORTH_TYPE
49 
53 typedef void (* word_function)();
54 
58 struct _pforth_word {
76  char* text_code;
77  uint8_t size;
78  union {
79  void* location;
80  word_function function;
81  };
82 };
83 
84 typedef struct _pforth_word pforth_word;
86 
87 #define WORD_IS_VARIABLE (w) (w.text_code[0] == 0x01)
88 #define WORD_IS_CONSTANT (w) (w.text_code[1] & 0x01)
89 #define WORD_IS_ARRAY (w) (w.text_code[1] & 0x02)
90 
91 extern uint8_t* data_stack_top;
92 extern void* return_stack_top;
93 uint8_t* data_stack_bottom();
94 uint8_t* return_stack_bottom();
95 
96 struct _dict_entry {
97  char* key;
99  struct _dict_entry* next;
100 };
101 
102 typedef struct _dict_entry dict_entry;
103 
104 struct dict_s {
105  int size;
106  struct _dict_entry** table;
107 };
108 
109 typedef struct dict_s dict_t;
110 
111 dict_t* dict_create(uint32_t size);
112 void dict_free(dict_t* dict, uint32_t size);
113 pforth_word_ptr dict_set(dict_t* dict, const char* key, const pforth_word_ptr value);
114 pforth_word_ptr dict_get(dict_t* dict, const char* key);
115 
119 extern dict_t* forth_dict;
120 
126 void pforth_init();
127 
133 void pforth_deinit();
134 
146 
157 
165 
171 void pforth_word_free(const pforth_word_ptr word);
172 
176 #define PFORTH_WORD_RUN(w) { word_function f = *(word->function); f(); }
177 
187 int preprocess(char* line);
188 
197 void eval(dict_t* dict, const char* line, const char* line_end);
198 
205 void register_native(const char* op, word_function f);
206 
210 void dump_stack();
211 
212 #include "words.h"
dict_t * forth_dict
Definition: pforth.c:6
void eval(dict_t *dict, const char *line, const char *line_end)
Definition: interp.c:67
pforth_word_ptr word
Definition: pforth.h:98
pforth_word_ptr dict_get(dict_t *dict, const char *key)
Definition: dict.c:171
void dump_stack()
Definition: pforth.c:72
void register_native(const char *op, word_function f)
Definition: pforth.c:80
pforth_word_ptr dict_set(dict_t *dict, const char *key, const pforth_word_ptr value)
Definition: dict.c:123
void * return_stack_top
Definition: pforth.c:5
pforth_word_ptr pforth_word_dup(const pforth_word_ptr src)
int preprocess(char *line)
Definition: interp.c:16
void * location
Definition: pforth.h:79
void pforth_deinit()
Definition: pforth.c:39
void dict_free(dict_t *dict, uint32_t size)
Definition: dict.c:39
int size
Definition: pforth.h:105
struct _dict_entry ** table
Definition: pforth.h:106
uint8_t size
Definition: pforth.h:77
uint8_t * data_stack_top
Definition: pforth.c:4
uint8_t * return_stack_bottom()
Definition: pforth.c:12
char * text_code
Definition: pforth.h:76
Definition: pforth.h:96
char * key
Definition: pforth.h:97
pforth_word * pforth_word_ptr
Definition: pforth.h:85
uint8_t * data_stack_bottom()
Definition: pforth.c:9
pforth_word_ptr pforth_word_alloc()
Definition: pforth.c:15
dict_t * dict_create(uint32_t size)
Definition: dict.c:13
void(* word_function)()
Definition: pforth.h:53
Definition: pforth.h:58
Definition: pforth.h:104
struct _dict_entry * next
Definition: pforth.h:99
void pforth_word_free(const pforth_word_ptr word)
Definition: pforth.c:23
pforth_word_ptr pforth_word_copy(const pforth_word_ptr dest, const pforth_word_ptr src)
Definition: pforth.c:46
void pforth_init()
Definition: pforth.c:30