pforth  0943e5e71114f286dbb2474d55aabf91df02d104
pforth.h File Reference
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include "words.h"

Go to the source code of this file.

Data Structures

struct  _pforth_word
 
struct  _dict_entry
 
struct  dict_s
 

Macros

#define DBG(fmt, ...)   {}
 
#define PRINT(fmt, ...)   fprintf(stdout, fmt, __VA_ARGS__);
 
#define LO(x)   ((unsigned char) ((x) & 0xFF))
 
#define HI(x)   ((unsigned char) (((x) >> 8) & 0xFF))
 
#define FORTH_DICT_SIZE   1000
 
#define MAX_DO_LOOP_DEPTH   3
 
#define MAX_BEGIN_LOOP_DEPTH   5
 
#define FORTH_TYPE   int32_t
 
#define POINTER_TYPE   void *
 
#define INT_TYPE_LIST   FORTH_TYPE
 
#define WORD_IS_VARIABLE   (w) (w.text_code[0] == 0x01)
 
#define WORD_IS_CONSTANT   (w) (w.text_code[1] & 0x01)
 
#define WORD_IS_ARRAY   (w) (w.text_code[1] & 0x02)
 
#define PFORTH_WORD_RUN(w)   { word_function f = *(word->function); f(); }
 

Typedefs

typedef void(* word_function) ()
 
typedef struct _pforth_word pforth_word
 
typedef pforth_wordpforth_word_ptr
 
typedef struct _dict_entry dict_entry
 
typedef struct dict_s dict_t
 

Functions

uint8_t * data_stack_bottom ()
 
uint8_t * return_stack_bottom ()
 
dict_tdict_create (uint32_t size)
 
void dict_free (dict_t *dict, uint32_t size)
 
pforth_word_ptr dict_set (dict_t *dict, const char *key, const pforth_word_ptr value)
 
pforth_word_ptr dict_get (dict_t *dict, const char *key)
 
void pforth_init ()
 
void pforth_deinit ()
 
pforth_word_ptr pforth_word_copy (const pforth_word_ptr dest, const pforth_word_ptr src)
 
pforth_word_ptr pforth_word_dup (const pforth_word_ptr src)
 
pforth_word_ptr pforth_word_alloc ()
 
void pforth_word_free (const pforth_word_ptr word)
 
int preprocess (char *line)
 
void eval (dict_t *dict, const char *line, const char *line_end)
 
void register_native (const char *op, word_function f)
 
void dump_stack ()
 

Variables

uint8_t * data_stack_top
 
void * return_stack_top
 
dict_tforth_dict
 

Macro Definition Documentation

#define DBG (   fmt,
  ... 
)    {}
#define FORTH_DICT_SIZE   1000

Maximal dictionary size

#define FORTH_TYPE   int32_t

Default data stack type definition

#define HI (   x)    ((unsigned char) (((x) >> 8) & 0xFF))
#define INT_TYPE_LIST   FORTH_TYPE

Comma separated list of types to generate math operations for

#define LO (   x)    ((unsigned char) ((x) & 0xFF))
#define MAX_BEGIN_LOOP_DEPTH   5

Maximal BEGIN loop nesting depth

#define MAX_DO_LOOP_DEPTH   3

Maximal DO loop nesting depth

#define PFORTH_WORD_RUN (   w)    { word_function f = *(word->function); f(); }

Call the word function.

#define POINTER_TYPE   void *

Pointer type definition

#define PRINT (   fmt,
  ... 
)    fprintf(stdout, fmt, __VA_ARGS__);
#define WORD_IS_ARRAY   (w) (w.text_code[1] & 0x02)
#define WORD_IS_CONSTANT   (w) (w.text_code[1] & 0x01)
#define WORD_IS_VARIABLE   (w) (w.text_code[0] == 0x01)

Typedef Documentation

typedef struct _dict_entry dict_entry
typedef struct dict_s dict_t
typedef struct _pforth_word pforth_word
typedef void(* word_function) ()

Type of function to be called from the word.

Function Documentation

uint8_t* data_stack_bottom ( )
dict_t* dict_create ( uint32_t  size)

Create a new hash table dictionary.

Parameters
sizenumber of elements
Returns
pointer to created dictionary
void dict_free ( dict_t dict,
uint32_t  size 
)
pforth_word_ptr dict_get ( dict_t dict,
const char *  key 
)

Retrieve the word from the dictionary.

Parameters
dictdictionary to use
keyFORTH word in ASCII form
Returns
pointer to word inside the dictionary. Do not harm it.
pforth_word_ptr dict_set ( dict_t dict,
const char *  key,
const pforth_word_ptr  value 
)

Insert word into dictionary.

The word will be copied into the dictionary. The value must be freed in caller afterwards.

Parameters
dictdictionary to use
keyFORTH word in ASCII form
valuepointer to pforth_word struct with the word.
Returns
pointer to the newly created word
void dump_stack ( )

Debug printer for the whole data stack

void eval ( dict_t dict,
const char *  line,
const char *  line_end 
)

Evaluate the FORTH line

Parameters
dictthe dictionary to use
linethe FORTH line
line_endthe last byte to interpret, if NULL then function evaluates ASCIIZ line
void pforth_deinit ( )

Free the FORTH machine internal structures.

Deallocate stacks, remove dictionary.

void pforth_init ( )

Init the FORTH machine.

Allocate stacks, create dictionary.

pforth_word_ptr pforth_word_alloc ( )

Allocate memory for new word

Returns
pointer to new word
NULL if not enough memory
pforth_word_ptr pforth_word_copy ( const pforth_word_ptr  dest,
const pforth_word_ptr  src 
)

Copy the word from src to dest

The dest must be allocated beforehand.

Parameters
srcsource word
destdestination buffer
Returns
pointer to the buffer (equal to dest)
NULL if not enough memory
pforth_word_ptr pforth_word_dup ( const pforth_word_ptr  src)

Create a duplicate of the word src

The memory is allocated inside and must be freed in caller

Parameters
srcsource word
Returns
pointer to new word
NULL if not enough memory
void pforth_word_free ( const pforth_word_ptr  word)

Free the word structure

Parameters
wordthe word to free
int preprocess ( char *  line)

Preprocess the FORTH line.

The preprocessing is done in-place, comments and newlines are removed by replacing with space characters. There's no space compression pass - it's easier to do that right in eval().

Parameters
linethe line to be preprocessed.
void register_native ( const char *  op,
word_function  f 
)

Register native (compile-time) word

Parameters
opThe FORTH word to add
fFunction to call
uint8_t* return_stack_bottom ( )

Variable Documentation

uint8_t* data_stack_top

The global data stack top pointer for the FORTH machine

dict_t* forth_dict

Storage for the FORTH words

void* return_stack_top