読者です 読者をやめる 読者になる 読者になる

Let's write β

趣味で書いたこととか、RustとLispが好き

WORD DICTIONARYテスト

C/C++

とりあえずForth風のWORDディクショナリのテストをする為に適当に習作プログラムを記述してます。

// main.c
#include <stdio.h>
#include <stdlib.h>
#include "word.h"

void print(void);

int main(int argc, char** argv)
{
	struct word* res;
	addfword("test1",NULL);
	addcword("test2",&print);
	addfword("test3",NULL);
	set_immediate();
	set_compile_only();
	res = findword("test1");
	if(res != NULL) {
		printf("FIND: %s\n",res->word_pointer);
	}
	else {
		printf("CANT FIND ANY DATA\n");
	}
	return 0;
}

void print(void)
{
	printf("\tTHIS IS CWORD\n");
}
//word.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "word.h"

//initialize word_dict header
struct word* word_dict = NULL;

//register new forth word
void addfword(char* wp, char* pp)
{
	struct word* newword;
	printf("ADD FORTH WORD\n");
	printf("\tNAME: %s\n",wp);
	newword = (struct word*)malloc(sizeof(struct word));
	//add data
	newword->word_pointer = wp;
	newword->prog_pointer = pp;
	newword->type = FWORD;
	newword->function = NULL;
	newword->immediate = 0;
	newword->compile_only = 0;
	//if first word
	if(word_dict == NULL) {
		//register as first word
		newword->next = NULL;
		word_dict = newword;
	}
	else {
		//add top
		newword->next = word_dict;
		//refresh word_head
		word_dict = newword;
	}
}

void addcword(char* wp, void (*cfunction)(void))
{
	struct word* newword;
	printf("ADD C WORD\n");
	printf("\tNAME: %s\n",wp);
	newword = (struct word*)malloc(sizeof(struct word));
	//add data
	newword->word_pointer = wp;
	newword->prog_pointer = NULL;
	newword->type = CWORD;
	newword->function = cfunction;
	newword->immediate = 0;
	newword->compile_only = 0;
	//if first word
	if(word_dict == NULL) {
		//register as first word
		newword->next = NULL;
		word_dict = newword;
	}
	else {
		//add top
		newword->next = word_dict;
		//refresh word_head
		word_dict = newword;
	}
}

struct word* findword(char* name)
{
	struct word* curr_word;
	printf("FIND WORD\n");
	curr_word = word_dict;
	while(curr_word != NULL) {
		printf("\tCURRENT WORD NAME IS: %s\n",curr_word->word_pointer);
		if(curr_word->type == FWORD) {
			printf("\tTHIS IS FWORD\n");
		}
		else if(curr_word->type == CWORD) {
			curr_word->function();
		}
		if(strcmp(curr_word->word_pointer, name) == 0) {
			return curr_word;
		}
		curr_word = curr_word->next;
	}
	return NULL;
}

void set_immediate(void)
{
	printf("SET IMMEDIATE\n");
	printf("\tWORD: %s\n",word_dict->word_pointer);
	word_dict->immediate = 1;
}

void set_compile_only(void)
{
	printf("SET COMPILE ONLY\n");
	printf("\tWORD: %s\n",word_dict->word_pointer);
	word_dict->compile_only = 1;
}
//word.h

#ifndef _WORD_H_
#define _WORD_H_

typedef void (*FUNCPTR)(void);

enum WORDTYPE {
	CWORD,
	FWORD,
};

struct word {
	//Pointer to word name
	char* word_pointer;
	//Pointer to program code (enabled if word is FWORD)
	char* prog_pointer;
	//Pointer for c function
	FUNCPTR function;
	//flag of function type
	int type;
	//flag of immediate word
	int immediate;
	//flag of compile-only word
	int compile_only;
	//Pointer to next word
	struct word* next;
};

//word_dictionary head pointer
struct word* word_dict; 

//add new forth word to dictionary
void addfword(char*, char*);
//add new c word to dictionary
void addcword(char*, void (*cfunction)(void));
//find registerd word with name
struct word* findword(char*);
//set immediate latest word
void set_immediate(void);
//set compile only latest word
void set_compile_only(void);

#endif

単純にCWORDでCで定義された関数,FWORDでforthで定義されているワードという区別を付けて、
最新に登録されたワードから検索、先頭に追加、そしてCWORDならば登録された関数の実行のテストをしています。
こんな感じでWORDのディクショナリは実装できそうですね。

僕が働いているAzit.incでは一緒に働けるエンジニアを募集しています!
採用情報 — 株式会社アジット|Azit Inc.