This commit is contained in:
Dominic Grimm 2022-12-22 16:02:45 +01:00
parent 2a9968c4ce
commit d8cff2488a
No known key found for this signature in database
GPG key ID: 6F294212DEAAC530
7 changed files with 230 additions and 21 deletions

View file

@ -1,9 +1,28 @@
#include <stdbool.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "hydroforth/hydroforth.h"
bool hydroforth__step(HYDROFORTH__INTERPRETER *interpreter)
unsigned int hydroforth__hash_string(const char *const key, unsigned char len)
{
unsigned char i = 0;
unsigned int hash = 0;
while (i != len)
{
hash += key[i++];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
bool hydroforth__parse(HYDROFORTH__INTERPRETER *interpreter)
{
const unsigned long start = interpreter->pos;
unsigned char len = 0;
@ -12,7 +31,6 @@ bool hydroforth__step(HYDROFORTH__INTERPRETER *interpreter)
len++;
interpreter->pos++;
}
printf("START: %lu\nLEN: %u\n", start, len);
HYDROFORTH__WORD word;
if (len == 1)
{
@ -33,22 +51,89 @@ bool hydroforth__step(HYDROFORTH__INTERPRETER *interpreter)
}
else
{
if ((interpreter->src[start] == '-' || interpreter->src[start] == '+') &&
hydroforth__number__is_digit(interpreter->src[start + 1]))
const bool next_is_digit = hydroforth__number__is_digit(interpreter->src[start + 1]);
if (next_is_digit && interpreter->src[start] == '-')
{
int n;
if (!hydroforth__number__parse_number(interpreter->src + start + 1, len - 1, &n))
{
fputs("Error parsing number!", stderr);
return false;
}
word = (HYDROFORTH__WORD){
.type = PUSH,
.data = {.number = -n},
};
}
else if (next_is_digit && interpreter->src[start] == '+')
{
int n;
if (!hydroforth__number__parse_number(interpreter->src + start + 1, len - 1, &n))
{
fputs("Error parsing number!", stderr);
return false;
}
word = (HYDROFORTH__WORD){
.type = PUSH,
.data = {.number = n},
};
}
else if (hydroforth__number__is_digit(interpreter->src[start]))
{
int n;
if (!hydroforth__number__parse_number(interpreter->src + start, len, &n))
{
printf("RESULT IS FALSE!!!\nNUMBER: %i\n", n);
fputs("Error parsing number!", stderr);
return false;
}
printf("NUMBER: %i\n", n);
word = (HYDROFORTH__WORD){
.type = PUSH,
.data = {.number = n},
};
}
else
{
char *const s = malloc(sizeof(char) * (len + 1));
strncpy(s, interpreter->src + start, len);
s[len] = '\0';
word = (HYDROFORTH__WORD){
.type = WORD,
.data = {.word = s},
};
}
}
interpreter->call_stack[interpreter->call_stack_len++] = word;
return true;
}
bool hydroforth__run_call_stack(HYDROFORTH__INTERPRETER *interpreter)
{
printf("CALL STACK LEN: %u\n", interpreter->call_stack_len);
for (unsigned char i = interpreter->call_stack_len; i > 0; interpreter->call_stack_len--, i--)
{
printf("TYPE: %u\n", interpreter->call_stack[i - 1].type);
const HYDROFORTH__WORD *word = interpreter->call_stack + i - 1;
switch (word->type)
{
case PUSH:
interpreter->stack[interpreter->stack_len++] = word->data.number;
break;
case CHAR_WORD:
switch (word->data.char_word)
{
case '-':
break;
default:
break;
}
break;
case WORD:
break;
}
}
@ -65,10 +150,13 @@ bool hydroforth__run(HYDROFORTH__INTERPRETER *interpreter)
}
if (!interpreter->src[interpreter->pos])
{
puts("END REACHED!");
return true;
}
if (!hydroforth__step(interpreter))
if (!hydroforth__parse(interpreter))
{
return false;
}
if (!hydroforth__run_call_stack(interpreter))
{
return false;
}

View file

@ -8,9 +8,73 @@ bool hydroforth__number__is_digit(char c)
return '0' <= c && c <= '9';
}
bool hydroforth__number__convert_hex_digit(char c, unsigned char *const val)
{
if (hydroforth__number__is_digit(c))
{
*val = c - '0';
}
else
{
switch (c)
{
case 'A':
case 'a':
*val = 0xa;
break;
case 'B':
case 'b':
*val = 0xb;
break;
case 'C':
case 'c':
*val = 0xc;
break;
case 'D':
case 'd':
*val = 0xd;
break;
case 'E':
case 'e':
*val = 0xe;
break;
case 'F':
case 'f':
*val = 0xf;
break;
default:
return false;
}
}
return true;
}
bool hydroforth__number__parse_number_hex(const char *const start, unsigned char len, int *const val)
{
int n;
for (unsigned char i = 0; i < len; i++)
{
unsigned char m;
if (!hydroforth__number__convert_hex_digit(start[i], &m))
{
return false;
}
n *= 16;
n += m;
}
return true;
}
bool hydroforth__number__parse_number(const char *const start, unsigned char len, int *const val)
{
int num;
if (start[0] == '0')
{
if (len > 1)
@ -19,7 +83,7 @@ bool hydroforth__number__parse_number(const char *const start, unsigned char len
{
case 'X':
case 'x':
return false;
return hydroforth__number__parse_number_hex(start + 2, len - 2, val);
default:
return hydroforth__number__parse_number(start + 1, len - 1, val);
@ -43,7 +107,3 @@ bool hydroforth__number__parse_number(const char *const start, unsigned char len
return true;
}
}
bool hydroforth__number__parse_number_with_sign(const char *const start, unsigned char len, int *const val)
{
}

View file

@ -71,6 +71,9 @@ int main(int argc, char *argv[])
HYDROFORTH__INTERPRETER interpreter = {
.src = res.src,
.pos = 0,
.single_char_word_keys_len = 0,
.word_keys_len = 0,
.word_keys_max_len = 0,
};
const bool run_res = hydroforth.run(&interpreter);
free(res.src);