This commit is contained in:
Dominic Grimm 2022-12-23 15:02:29 +01:00
parent 0019e3278e
commit 4e8c187050
No known key found for this signature in database
GPG key ID: 6F294212DEAAC530
8 changed files with 228 additions and 54 deletions

View file

@ -3,6 +3,7 @@
#include <stdbool.h>
#include "result.h"
#include "number.h"
extern unsigned int hydroforth__hash_string(const char *const key, unsigned char len);
@ -67,6 +68,14 @@ typedef struct HYDROFORTH__INTERPRETER
unsigned char stack_len;
} HYDROFORTH__INTERPRETER;
typedef struct HYDROFORTH__SCAN_NEXT_WORD_RESULT
{
unsigned long start;
unsigned char len;
} HYDROFORTH__SCAN_NEXT_WORD_RESULT;
extern HYDROFORTH__SCAN_NEXT_WORD_RESULT hydroforth__scan_next_word(HYDROFORTH__INTERPRETER *interpreter);
extern bool hydroforth__parse(HYDROFORTH__INTERPRETER *interpreter);
extern bool hydroforth__run_call_stack(HYDROFORTH__INTERPRETER *interpreter);
@ -75,23 +84,32 @@ extern bool hydroforth__run(HYDROFORTH__INTERPRETER *interpreter);
typedef struct __HYDROFORTH
{
__HYDROFORTH__RESULT result;
__HYDROFORTH__NUMBER number;
unsigned int (*hash_string)(const char *const key, unsigned char len);
bool (*is_space)(char c);
HYDROFORTH__SCAN_NEXT_WORD_RESULT(*scan_next_word)
(HYDROFORTH__INTERPRETER *interpreter);
bool (*parse)(HYDROFORTH__INTERPRETER *interpreter);
bool (*run_call_stack)(HYDROFORTH__INTERPRETER *interpreter);
bool (*run)(HYDROFORTH__INTERPRETER *interpreter);
} __HYDROFORTH;
static const __HYDROFORTH hydroforth = {
.result = {
.add_backtrace = hydroforth__result__add_backtrace,
.get_error_message = hydroforth__result__get_error_message,
},
.number = {
.is_digit = hydroforth__number__is_digit,
.convert_hex_digit = hydroforth__number__convert_hex_digit,
.parse_number_hex = hydroforth__number__parse_number_hex,
.parse_number = hydroforth__number__parse_number,
.count_digits = hydroforth__number__count_digits,
},
.hash_string = hydroforth__hash_string,
.is_space = hydroforth__is_space,
.scan_next_word = hydroforth__scan_next_word,
.parse = hydroforth__parse,
.run_call_stack = hydroforth__run_call_stack,
.run = hydroforth__run,

View file

@ -1,22 +1,23 @@
#ifndef __HYDROFORTH__NUMBER_H__
#define __HYDROFORTH__NUMBER_H__
#include <stdbool.h>
extern bool hydroforth__number__is_digit(char c);
extern bool hydroforth__number__convert_hex_digit(char c, unsigned char *const val);
extern void hydroforth__number__convert_hex_digit(HYDROFORTH__RESULT *const result, char c, unsigned char *const val);
extern bool hydroforth__number__parse_number_hex(const char *const start, unsigned char len, int *const val);
extern void hydroforth__number__parse_number_hex(HYDROFORTH__RESULT *const result, const char *const start, unsigned char len, int *const val);
extern bool hydroforth__number__parse_number(const char *const start, unsigned char len, int *const val);
extern void hydroforth__number__parse_number(HYDROFORTH__RESULT *const result, const char *const start, unsigned char len, int *const val);
extern unsigned char hydroforth__number__count_digits(int n);
typedef struct __HYDROFORTH__NUMBER
{
bool (*is_digit)(char c);
bool (*convert_hex_digit)(char c, unsigned char *const val);
bool (*parse_number_hex)(const char *const start, unsigned char len, int *const val);
bool (*parse_number)(const char *const start, unsigned char len, int *const val);
void (*convert_hex_digit)(HYDROFORTH__RESULT *const result, char c, unsigned char *const val);
void (*parse_number_hex)(HYDROFORTH__RESULT *const result, const char *const start, unsigned char len, int *const val);
void (*parse_number)(HYDROFORTH__RESULT *const result, const char *const start, unsigned char len, int *const val);
unsigned char (*count_digits)(int n);
} __HYDROFORTH__NUMBER;
#endif

View file

@ -0,0 +1,40 @@
#ifndef __HYDROFORTH__RESULT_H__
#define __HYDROFORTH__RESULT_H__
typedef enum HYDROFORTH__RESULT__ERROR
{
OK,
ERR_UNKNOWN,
ERR_INVALID_HEX_CHAR,
} HYDROFORTH__RESULT__ERROR;
typedef HYDROFORTH__RESULT__ERROR HYDROFORTH__ERROR;
typedef struct HYDROFORTH__RESULT__RESULT
{
HYDROFORTH__ERROR error;
char **const backtrace;
unsigned backtrace_len;
} HYDROFORTH__RESULT__RESULT;
typedef HYDROFORTH__RESULT__RESULT HYDROFORTH__RESULT;
extern void hydroforth__result__add_backtrace(HYDROFORTH__RESULT *const result, const char *const s);
extern void hydroforth__result__set(HYDROFORTH__RESULT *const result, HYDROFORTH__ERROR error, const char *const s);
extern const char *hydroforth__result__get_error_message(HYDROFORTH__ERROR error);
#define hydroforth__set_func_result(result, error) hydroforth__result__set(result, error, __func__);
#define hydroforth__add_func_backtrace(result) hydroforth__result__add_backtrace(result, __func__);
typedef struct __HYDROFORTH__RESULT
{
void (*add_backtrace)(HYDROFORTH__RESULT *const result, const char *const s);
void (*set)(HYDROFORTH__RESULT *const result, HYDROFORTH__ERROR error, const char *const s);
const char *(*get_error_message)(HYDROFORTH__ERROR error);
} __HYDROFORTH__RESULT;
#endif