Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wordlists #5

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*~
6 changes: 6 additions & 0 deletions build/unix/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.eo
*.o
pfdicdat.h
pforth
pforth.dic
pforth_standalone
10 changes: 7 additions & 3 deletions build/unix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ EMBCCOPTS = -DPF_STATIC_DIC
PFINCLUDES = pf_all.h pf_cglue.h pf_clib.h pf_core.h pf_float.h \
pf_guts.h pf_host.h pf_inc1.h pf_io.h pf_mem.h pf_save.h \
pf_text.h pf_types.h pf_win32.h pf_words.h pfcompfp.h \
pfcompil.h pfdicdat_arm.h pfinnrfp.h pforth.h
pfcompil.h pfdicdat_arm.h pfinnrfp.h pforth.h pf_search.h
PFBASESOURCE = pf_cglue.c pf_clib.c pf_core.c pf_inner.c \
pf_io.c pf_io_none.c pf_main.c pf_mem.c pf_save.c \
pf_text.c pf_words.c pfcompil.c pfcustom.c
pf_text.c pf_words.c pfcompil.c pfcustom.c pf_search.c
PFSOURCE = $(PFBASESOURCE) $(IO_SOURCE)

VPATH = .:$(CSRCDIR):$(CSRCDIR)/posix:$(CSRCDIR)/stdio:$(CSRCDIR)/win32_console:$(CSRCDIR)/win32

XCFLAGS = $(CCOPTS)
XCPPFLAGS = -DPF_SUPPORT_FP -D_DEFAULT_SOURCE

XCPPFLAGS = -DPF_SUPPORT_FP -D_DEFAULT_SOURCE -DPF_SUPPORT_WORDLIST
#XCPPFLAGS = -DPF_SUPPORT_FP -D_DEFAULT_SOURCE

XLDFLAGS = $(WIDTHOPT)

CPPFLAGS = -I. $(XCPPFLAGS)
Expand Down Expand Up @@ -136,6 +139,7 @@ test: $(PFORTHAPP)
wd=$$(pwd); (cd $(FTHDIR); $${wd}/$(PFORTHAPP) -q t_locals.fth)
wd=$$(pwd); (cd $(FTHDIR); $${wd}/$(PFORTHAPP) -q t_alloc.fth)
wd=$$(pwd); (cd $(FTHDIR); $${wd}/$(PFORTHAPP) -q t_floats.fth)
wd=$$(pwd); (cd $(FTHDIR); $${wd}/$(PFORTHAPP) -q t_wordlist.fth)

clean:
rm -f $(PFOBJS) $(PFEMBOBJS)
Expand Down
2 changes: 1 addition & 1 deletion csrc/pf_all.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#include "pf_mem.h"
#include "pf_cglue.h"
#include "pf_core.h"

#include "pf_search.h"
#ifdef PF_USER_INC2
/* This could be used to undef and redefine macros. */
#include PF_USER_INC2
Expand Down
2 changes: 2 additions & 0 deletions csrc/pf_guts.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ enum cforth_primitive_ids
#define THROW_EXECUTING (-14)
#define THROW_PAIRS (-22)
#define THROW_FLOAT_STACK_UNDERFLOW ( -45)
#define THROW_SEARCH_OVERFLOW (-49)
#define THROW_SEARCH_UNDERFLOW (-50)
#define THROW_QUIT (-56)

/* THROW codes unique to pForth */
Expand Down
119 changes: 119 additions & 0 deletions csrc/pf_search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* @(#) search.c 15/04/09 1.0 */
/***************************************************************
** search order for PForth based on 'C'
**
** Author: Hannu Vuolasaho
** Copyright 2015 3DO, Phil Burk, Larry Polansky, David Rosenboom
**
** The pForth software code is dedicated to the public domain,
** and any third party may reproduce, distribute and modify
** the pForth software code or any derivative works thereof
** without any compensation or license. The pForth software
** code is provided on an "as is" basis without any warranty
** of any kind, including, without limitation, the implied
** warranties of merchantability and fitness for a particular
** purpose and their equivalents under the laws of any jurisdiction.
**/
#include "pf_all.h"
#ifdef PF_SUPPORT_WORDLIST

/* Search order and word list arrays */
cell_t arrSearchOrder;

/* global search order start index. points wl.order.first on forth. */
cell_t gVarWlOrderFirst;
cell_t gVarWordLists;
/* gVarWlCompileIndex is gVarWordLists[compilationIdnex], head of comp. list. */
cell_t gVarWlCompileIndex;



/* (init-wordlists) ( search_addr search_index wl_addr comp_index -- ) */
void ffInitWordLists( cell_t search_addr, cell_t search_index,
cell_t wl_addr, cell_t comp_index )
{
gVarWlCompileIndex = comp_index;
gVarWordLists = wl_addr;
gVarWlOrderFirst = search_index;
arrSearchOrder = search_addr;
/* Debug Stuff. remove.
MSG_NUM_D("comp ind ", gVarWlCompileIndex);
MSG_NUM_D("comp ind * ", *(cell_t *)gVarWlCompileIndex);
MSG_NUM_D("wl ", gVarWordLists );
MSG_NUM_D("wl * ", *(cell_t *)gVarWordLists );
MSG_NUM_D("wl * * ", *(cell_t *)(*(cell_t *)gVarWordLists));
MSG_NUM_D("wl+1 * * ", *(cell_t *)(*(cell_t *)gVarWordLists+1));
MSG_NUM_D("first ", gVarWlOrderFirst);
MSG_NUM_D("first * ", *(cell_t *) gVarWlOrderFirst);
MSG_NUM_D("order ", arrSearchOrder);
MSG_NUM_D("order * ", *(cell_t *)arrSearchOrder);
MSG_NUM_D("order name * ", NAMEREL_TO_ABS((*(cell_t *)arrSearchOrder)));
MSG_NUM_D("order code * ", CODEREL_TO_ABS((*(cell_t *)arrSearchOrder)));
MSG_NUM_D("order code * * ", *(cell_t *)(CODEREL_TO_ABS((*(cell_t *)arrSearchOrder))));
*/
}
cell_t getWordList( cell_t index )
{
cell_t temp_addr, *tmp_arr;
if(gVarWordLists)
{
/* Don't underflow search */
if( index < 0 ) return (cell_t) NULL;
/* Address to wordlist */
tmp_arr = (cell_t *) arrSearchOrder;
temp_addr = tmp_arr[index];
if(temp_addr)
{
temp_addr = CODEREL_TO_ABS(temp_addr);
return *(cell_t *)temp_addr;
}
else
{
/* Empty wordlist in search order */
return (cell_t) NULL;
}
}
else
{
return gVarContext;
}
}

/* This should be written in forth */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be written in Forth? Is something in pForth preventing that?

/* search-wordlist ( c-addr u wid -- 0 | xt 1 | xt -1 ) */
cell_t ffSearchWordList( cell_t c_addr, cell_t u, cell_t wid)
{
cell_t Searching = TRUE;
cell_t Result = 0;
uint8_t NameLen;
const char *NameField;

if( wid == 0 || !(*((cell_t *) (CODEREL_TO_ABS(wid))) )) return 0;
/* wid is code relative address of wordlists
* referencing give content of gVarContext of
* compilation time of last word in word list*/
NameField = (ForthString *) *((cell_t *) (CODEREL_TO_ABS(wid)) );
do
{
NameLen = (uint8_t) ((ucell_t)(*NameField) & MASK_NAME_SIZE);
if( ((*NameField & FLAG_SMUDGE) == 0) &&
(NameLen == u) &&
ffCompareTextCaseN( NameField +1, (const char *) c_addr, u ) )
{
PUSH_DATA_STACK(NameToToken(NameField)); /* XT to stack */
Result = ((*NameField) & FLAG_IMMEDIATE) ? 1 : -1;
Searching = FALSE;
}
else
{
NameField = NameToPrevious( NameField );
if( NameField == NULL )
{
Searching = FALSE;
}
}
}while(Searching);
return Result;
}

#endif
44 changes: 44 additions & 0 deletions csrc/pf_search.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* @(#) search.h 15/04/09 1.0 */
#ifndef _pf_search_h
#define _pf_search_h
/***************************************************************
** search order for PForth based on 'C'
**
** Author: Hannu Vuolasaho
** Copyright 2015 3DO, Phil Burk, Larry Polansky, David Rosenboom
**
** The pForth software code is dedicated to the public domain,
** and any third party may reproduce, distribute and modify
** the pForth software code or any derivative works thereof
** without any compensation or license. The pForth software
** code is provided on an "as is" basis without any warranty
** of any kind, including, without limitation, the implied
** warranties of merchantability and fitness for a particular
** purpose and their equivalents under the laws of any jurisdiction.
**/

#ifdef PF_SUPPORT_WORDLIST
#define PF_WORDLIST_EXPORT_FUNCTIONS (2)

/* Search order and word list arrays */

extern cell_t gVarWlOrderFirst;
extern cell_t gVarWordLists;

/* compilationIndex is gVarWordLists[compilationIdnex], head of comp. list. */
extern cell_t gVarWlCompileIndex;

/* (init-wordlists) ( search_addr search_index wl_addr comp_index -- ) */
void ffInitWordLists( cell_t search_addr, cell_t search_index,
cell_t wl_addr, cell_t comp_index );
/* search-wordlist ( c-addr u wid -- 0 | xt 1 | xt -1 ) */
cell_t ffSearchWordList( cell_t c_addr, cell_t u, cell_t wid);

/* Helper function.
* Get the head of wordlist in search order index 'index' */
cell_t getWordList( cell_t index );

#else
#define PF_WORDLIST_EXPORT_FUNCTIONS (0)
#endif
#endif
46 changes: 26 additions & 20 deletions csrc/pf_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
void pfReportError( const char *FunctionName, Err ErrCode )
{
const char *s;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These white space errors have all been fixed.
So the code would need to be rebased and all tabs converted to 4 spaces.

MSG("Error in ");
MSG(FunctionName);
MSG(" - ");

switch(ErrCode & 0xFF)
{
case PF_ERR_NO_MEM & 0xFF:
Expand Down Expand Up @@ -107,6 +107,12 @@ void pfReportThrow( ThrowCode code )
s = "Executing a compile-only word!"; break;
case THROW_FLOAT_STACK_UNDERFLOW:
s = "Float Stack underflow!"; break;
#ifdef PF_SUPPORT_WORDLIST
case THROW_SEARCH_OVERFLOW:
s = "Search order or wordlist owerflow!"; break;
case THROW_SEARCH_UNDERFLOW:
s = "Search order or wordlist underflow!"; break;
#endif
case THROW_UNDEFINED_WORD:
s = "Undefined word!"; break;
case THROW_PAIRS:
Expand All @@ -121,7 +127,7 @@ void pfReportThrow( ThrowCode code )
default:
s = "Unrecognized throw code!"; break;
}

if( s )
{
MSG_NUM_D("THROW code = ", code );
Expand Down Expand Up @@ -178,7 +184,7 @@ char *CStringToForth( char *dst, const char *CString, cell_t dstSize )
cell_t ffCompareText( const char *s1, const char *s2, cell_t len )
{
cell_t i, Result;

Result = TRUE;
for( i=0; i<len; i++ )
{
Expand All @@ -201,7 +207,7 @@ cell_t ffCompareTextCaseN( const char *s1, const char *s2, cell_t len )
{
cell_t i, Result;
char c1,c2;

Result = TRUE;
for( i=0; i<len; i++ )
{
Expand All @@ -225,7 +231,7 @@ DBUGX(("ffCompareText: return 0x%x\n", Result ));
cell_t ffCompare( const char *s1, cell_t len1, const char *s2, int32_t len2 )
{
cell_t i, result, n, diff;

result = 0;
n = MIN(len1,len2);
for( i=0; i<n; i++ )
Expand Down Expand Up @@ -262,7 +268,7 @@ char *ConvertNumberToText( cell_t Num, cell_t Base, int32_t IfSigned, int32_t Mi
char *p,c;
ucell_t NewNum, Rem, uNum;
cell_t i = 0;

uNum = Num;
if( IfSigned )
{
Expand All @@ -273,11 +279,11 @@ char *ConvertNumberToText( cell_t Num, cell_t Base, int32_t IfSigned, int32_t Mi
uNum = -Num;
}
}

/* Point past end of Pad */
p = cnttPad + CNTT_PAD_SIZE;
*(--p) = (char) 0; /* NUL terminate */

while( (i++<MinChars) || (uNum != 0) )
{
NewNum = uNum / Base;
Expand All @@ -286,7 +292,7 @@ char *ConvertNumberToText( cell_t Num, cell_t Base, int32_t IfSigned, int32_t Mi
*(--p) = c;
uNum = NewNum;
}

if( IfSigned )
{
if( IfNegative ) *(--p) = '-';
Expand All @@ -307,7 +313,7 @@ void DumpMemory( void *addr, cell_t cnt)
ptr = (unsigned char *) addr;

EMIT_CR;

for (ln=0; ln<nlines; ln++)
{
MSG( ConvertNumberToText( (cell_t) ptr, 16, FALSE, 8 ) );
Expand Down Expand Up @@ -335,10 +341,10 @@ void TypeName( const char *Name )
{
const char *FirstChar;
cell_t Len;

FirstChar = Name+1;
Len = *Name & 0x1F;

ioType( FirstChar, Len );
}

Expand All @@ -365,43 +371,43 @@ cell_t pfUnitTestText( void )
ASSERT_PAD_IS( 0, 4, "CS len 6" );
ASSERT_PAD_IS( 4, 'g', "CS end 6" );
ASSERT_PAD_IS( 5, 0xA5, "CS past 6" );

pfSetMemory(pad,0xA5,sizeof(pad));
CStringToForth( pad, "frog", 5 );
ASSERT_PAD_IS( 0, 4, "CS len 5" );
ASSERT_PAD_IS( 4, 'g', "CS end 5" );
ASSERT_PAD_IS( 5, 0xA5, "CS past 5" );

pfSetMemory(pad,0xA5,sizeof(pad));
CStringToForth( pad, "frog", 4 );
ASSERT_PAD_IS( 0, 3, "CS len 4" );
ASSERT_PAD_IS( 3, 'o', "CS end 4" );
ASSERT_PAD_IS( 4, 0xA5, "CS past 4" );

/* Make a Forth string for testing ForthStringToC. */
CStringToForth( fpad, "frog", sizeof(fpad) );

pfSetMemory(pad,0xA5,sizeof(pad));
ForthStringToC( pad, fpad, 6 );
ASSERT_PAD_IS( 0, 'f', "FS len 6" );
ASSERT_PAD_IS( 3, 'g', "FS end 6" );
ASSERT_PAD_IS( 4, 0, "FS nul 6" );
ASSERT_PAD_IS( 5, 0xA5, "FS past 6" );

pfSetMemory(pad,0xA5,sizeof(pad));
ForthStringToC( pad, fpad, 5 );
ASSERT_PAD_IS( 0, 'f', "FS len 5" );
ASSERT_PAD_IS( 3, 'g', "FS end 5" );
ASSERT_PAD_IS( 4, 0, "FS nul 5" );
ASSERT_PAD_IS( 5, 0xA5, "FS past 5" );

pfSetMemory(pad,0xA5,sizeof(pad));
ForthStringToC( pad, fpad, 4 );
ASSERT_PAD_IS( 0, 'f', "FS len 4" );
ASSERT_PAD_IS( 2, 'o', "FS end 4" );
ASSERT_PAD_IS( 3, 0, "FS nul 4" );
ASSERT_PAD_IS( 4, 0xA5, "FS past 4" );

return numErrors;
}
#endif