pforth  0943e5e71114f286dbb2474d55aabf91df02d104
words.c File Reference
#include "pforth.h"
#include "generators_run.h"
#include "core_fs.h"

Macros

#define POP_WORD(TYPE)
 
#define PUSH_WORD(TYPE)
 
#define _MATH_OP(TYPE, NAME, OP)
 
#define _COMPARE_OP(TYPE, NAME, OP)
 
#define _TRUE_OP(TYPE)
 
#define _TYPED_GENERIC_WORD(TYPE, NAME, BODY)
 
#define _GENERIC_WORD(NAME, BODY)   _TYPED_GENERIC_WORD(FORTH_TYPE, NAME, BODY)
 
#define _TYPED_POP_NUM(TYPE, NAME)   TYPE NAME = M_CONC(pop, M_CONC(_, TYPE))();
 
#define _POP_NUM(NAME)   _TYPED_POP_NUM(FORTH_TYPE, NAME)
 
#define _PUSH_NUM(value)   M_CONC(push_, FORTH_TYPE) (value);
 
#define _MATH_OP(TYPE, NAME, OP)    register_native(#OP, &M_CONC(_, M_CONC(NAME, M_CONC(_, FORTH_TYPE))));
 
#define _COMPARE_OP(TYPE, NAME, OP)   register_native(#OP, &M_CONC(_, M_CONC(NAME, M_CONC(_, FORTH_TYPE))));
 
#define POP_WORD(TYPE)
 
#define PUSH_WORD(TYPE)
 
#define _TRUE_OP(TYPE)
 
#define _DEF_TYPE_OP(op)   M_CONC(_, M_CONC(op, M_CONC(_, FORTH_TYPE)))
 

Functions

void _push (void *data, size_t size)
 
FORTH_TYPE _top ()
 
FORTH_TYPE _depth ()
 
void _drop (size_t size)
 
 _GENERIC_WORD (emit, _POP_NUM(number) char c=LO(number);DBG("EMIT %c\n", c);PRINT("%c", c)) _GENERIC_WORD(dot
 
 _POP_NUM (number)
 
 _GENERIC_WORD (over, FORTH_TYPE val;memcpy(&val, data_stack_top-2 *sizeof(FORTH_TYPE), sizeof(FORTH_TYPE));_PUSH_NUM(val) DBG("OVER %d\n", val)) _GENERIC_WORD(drop
 
 _drop (sizeof(FORTH_TYPE))
 
 DBG ("%s\n","DROP")
 
void _EMBED_DECORATE (set_variable_value())
 
void register_precompiled ()
 
pforth_word_ptr create_variable (const char *name)
 

Macro Definition Documentation

#define _COMPARE_OP (   TYPE,
  NAME,
  OP 
)
Value:
_EMBED_DECORATE(void M_CONC(_, M_CONC(NAME, M_CONC(_, TYPE))) ()); \
void M_CONC(_, M_CONC(NAME, M_CONC(_, TYPE))) () { \
TYPE b = pop_ ## TYPE(); \
TYPE a = pop_ ## TYPE(); \
DBG("COMP_OP %d %s %d\n", a, #OP, b); \
push_ ## TYPE(a OP b ? -1 : 0); \
}
#define M_CONC(A, B)
Definition: words.h:76
void _EMBED_DECORATE(set_variable_value())
Definition: words.c:306

Generator for comparison words

Creates function named NAME for operation OP with arguments of TYPE The result for int32_t, ge, >= will be

1 void _ge_int32_t() {
2  int32_t a = pop_int32();
3  int32_t b = pop_int32();
4  push_int32_t (a >= b ? -1 : 0);
5 }

The resulting function will use push_TYPE for data placement

Parameters
TYPEdata type for generated function
NAMEresulting function name prefix
OPoperation to compile into function
#define _COMPARE_OP (   TYPE,
  NAME,
  OP 
)    register_native(#OP, &M_CONC(_, M_CONC(NAME, M_CONC(_, FORTH_TYPE))));

Generator for comparison words

Creates function named NAME for operation OP with arguments of TYPE The result for int32_t, ge, >= will be

1 void _ge_int32_t() {
2  int32_t a = pop_int32();
3  int32_t b = pop_int32();
4  push_int32_t (a >= b ? -1 : 0);
5 }

The resulting function will use push_TYPE for data placement

Parameters
TYPEdata type for generated function
NAMEresulting function name prefix
OPoperation to compile into function
#define _DEF_TYPE_OP (   op)    M_CONC(_, M_CONC(op, M_CONC(_, FORTH_TYPE)))
#define _GENERIC_WORD (   NAME,
  BODY 
)    _TYPED_GENERIC_WORD(FORTH_TYPE, NAME, BODY)
#define _MATH_OP (   TYPE,
  NAME,
  OP 
)
Value:
void M_CONC(_, M_CONC(NAME, M_CONC(_, TYPE))) () { \
TYPE b = pop_ ## TYPE(); \
TYPE a = pop_ ## TYPE(); \
DBG("MATH_OP %d %s %d\n", a, #OP, b); \
push_ ## TYPE(a OP b); \
}
#define M_CONC(A, B)
Definition: words.h:76

Generator for native math words

Creates function named NAME for operation OP with arguments of TYPE The result for int32_t, add, + will be

1 void _add_int32_t() {
2  int32_t a = pop_int32();
3  int32_t b = pop_int32();
4  push_int32_t (a >= b ? -1 : 0);
5 }

The resulting function will use push_TYPE for data placement

Parameters
TYPEdata type for generated function
NAMEresulting function name prefix
OPoperation to compile into function
#define _MATH_OP (   TYPE,
  NAME,
  OP 
)    register_native(#OP, &M_CONC(_, M_CONC(NAME, M_CONC(_, FORTH_TYPE))));

Generator for native math words

Creates function named NAME for operation OP with arguments of TYPE The result for int32_t, add, + will be

1 void _add_int32_t() {
2  int32_t a = pop_int32();
3  int32_t b = pop_int32();
4  push_int32_t (a >= b ? -1 : 0);
5 }

The resulting function will use push_TYPE for data placement

Parameters
TYPEdata type for generated function
NAMEresulting function name prefix
OPoperation to compile into function
#define _POP_NUM (   NAME)    _TYPED_POP_NUM(FORTH_TYPE, NAME)
#define _PUSH_NUM (   value)    M_CONC(push_, FORTH_TYPE) (value);
#define _TRUE_OP (   TYPE)
Value:
int M_CONC(_, M_CONC(true, M_CONC(_, TYPE))) () { \
return pop_ ## TYPE() == -1 ? 1 : 0; \
}
#define M_CONC(A, B)
Definition: words.h:76
#define _TRUE_OP (   TYPE)
#define _TYPED_GENERIC_WORD (   TYPE,
  NAME,
  BODY 
)
Value:
void M_CONC(_, M_CONC(NAME, M_CONC(_, TYPE))) () { \
BODY \
}
#define M_CONC(A, B)
Definition: words.h:76
#define _TYPED_POP_NUM (   TYPE,
  NAME 
)    TYPE NAME = M_CONC(pop, M_CONC(_, TYPE))();
#define POP_WORD (   TYPE)
Value:
TYPE pop_ ## TYPE() { \
DBG("The data stack is empty. %s\n", "DNIWE is here!"); \
return 0; \
}; \
TYPE rv = 0; \
memcpy(&rv, data_stack_top - sizeof(TYPE), sizeof(TYPE)); \
_drop(sizeof(TYPE)); \
DBG("poping %d of %s\n", rv, #TYPE); \
return rv; \
}
uint8_t * data_stack_bottom()
Definition: pforth.c:9
uint8_t * data_stack_top
Definition: pforth.c:4

Generator of the stack pop functions for desired TYPE

Creates function named pop_TYPE with retval of TYPE. The result for int32_t will be

1 int32_t pop_int32_t();

The resulting function will use _drop for moving the data_stack_top

Parameters
TYPEthe retval type for generated function
#define POP_WORD (   TYPE)

Generator of the stack pop functions for desired TYPE

Creates function named pop_TYPE with retval of TYPE. The result for int32_t will be

1 int32_t pop_int32_t();

The resulting function will use _drop for moving the data_stack_top

Parameters
TYPEthe retval type for generated function
#define PUSH_WORD (   TYPE)
Value:
void push_ ## TYPE(TYPE value) { \
DBG("pushing %d of %s\n", value, #TYPE); \
_push(&value, sizeof(TYPE)); \
}

Generator of the stack push functions for desired TYPE

Creates function named push_TYPE with argument of TYPE The result for int32_t will be

1 void push_int32_t (int32_t);

The resulting function will use _push for data placement

Parameters
TYPEthe retval type for generated function
#define PUSH_WORD (   TYPE)

Generator of the stack push functions for desired TYPE

Creates function named push_TYPE with argument of TYPE The result for int32_t will be

1 void push_int32_t (int32_t);

The resulting function will use _push for data placement

Parameters
TYPEthe retval type for generated function

Function Documentation

FORTH_TYPE _depth ( )
void _drop ( size_t  size)

Drops the size bytes data_stack.

Memory is not freed, just data_stack_top is moved

Parameters
sizesize of data in bytes
_drop ( sizeof(FORTH_TYPE )
void _EMBED_DECORATE ( set_variable_value()   )
_GENERIC_WORD ( emit  ,
_POP_NUM(number) char  c = LO(number); DBG("EMIT %c\n", c); PRINT("%c", c) 
)
_GENERIC_WORD ( over  ,
FORTH_TYPE val;memcpy(&, -2 *  FORTH_TYPE,
  sizeofFORTH_TYPE 
)
_POP_NUM ( number  )
void _push ( void *  data,
size_t  size 
)

Push the data to global data stack and move the data_stack_top forward

The data must be valid pointer with at least size bytes inside

Parameters
datadata to push
sizesize of data in bytes
FORTH_TYPE _top ( )
pforth_word_ptr create_variable ( const char *  name)
DBG ( "%s\n"  ,
"DROP"   
)
void register_precompiled ( )