rename state struct
This commit is contained in:
parent
23e5342cb0
commit
c365caee6b
1 changed files with 49 additions and 49 deletions
98
pila.c
98
pila.c
|
@ -17,12 +17,12 @@
|
||||||
typedef struct UserWord UserWord;
|
typedef struct UserWord UserWord;
|
||||||
typedef struct Word Word;
|
typedef struct Word Word;
|
||||||
typedef struct DictionaryEntry DictionaryEntry;
|
typedef struct DictionaryEntry DictionaryEntry;
|
||||||
typedef struct RpnState RpnState;
|
typedef struct Pila Pila;
|
||||||
typedef struct Str Str;
|
typedef struct Str Str;
|
||||||
typedef struct Value Value;
|
typedef struct Value Value;
|
||||||
typedef enum ValueKind ValueKind;
|
typedef enum ValueKind ValueKind;
|
||||||
typedef enum WordKind WordKind;
|
typedef enum WordKind WordKind;
|
||||||
typedef void (*NativeWord)(RpnState *);
|
typedef void (*NativeWord)(Pila *);
|
||||||
|
|
||||||
struct Str {
|
struct Str {
|
||||||
char *s;
|
char *s;
|
||||||
|
@ -63,7 +63,7 @@ struct Word {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct RpnState {
|
struct Pila {
|
||||||
Value data_stack[DATA_STACK_LEN];
|
Value data_stack[DATA_STACK_LEN];
|
||||||
Word *rts[RETURN_STACK_LEN]; /* "return" stack */
|
Word *rts[RETURN_STACK_LEN]; /* "return" stack */
|
||||||
/* stack, return pointers */
|
/* stack, return pointers */
|
||||||
|
@ -90,11 +90,11 @@ static const Value Nil = (Value){.kind = VAL_NIL};
|
||||||
static const Str StrEmpty = (Str){.s = "", .len = 0};
|
static const Str StrEmpty = (Str){.s = "", .len = 0};
|
||||||
|
|
||||||
void
|
void
|
||||||
signal_error(RpnState *st, const char *err);
|
signal_error(Pila *st, const char *err);
|
||||||
void
|
void
|
||||||
signal_errorf(RpnState *st, const char *err, ...);
|
signal_errorf(Pila *st, const char *err, ...);
|
||||||
Word
|
Word
|
||||||
*search_word(RpnState *st, Str word_name);
|
*search_word(Pila *st, Str word_name);
|
||||||
Str
|
Str
|
||||||
read_word_name(char **pstart, char *pend);
|
read_word_name(char **pstart, char *pend);
|
||||||
bool
|
bool
|
||||||
|
@ -102,11 +102,11 @@ read_number(Str ns, u64 *res);
|
||||||
void
|
void
|
||||||
skip_whitespace(char **pstart, char *pend);
|
skip_whitespace(char **pstart, char *pend);
|
||||||
void
|
void
|
||||||
add_word(RpnState *st, Word *w);
|
add_word(Pila *st, Word *w);
|
||||||
void
|
void
|
||||||
eval_word(RpnState *st, Word *w);
|
eval_word(Pila *st, Word *w);
|
||||||
void
|
void
|
||||||
eval(RpnState *st, Str src);
|
eval(Pila *st, Str src);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ascii_isdigit(char c)
|
ascii_isdigit(char c)
|
||||||
|
@ -151,7 +151,7 @@ Str_new(const char *data, isize len)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
signal_errorf(RpnState *st, const char *err, ...)
|
signal_errorf(Pila *st, const char *err, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, err);
|
va_start(args, err);
|
||||||
|
@ -163,7 +163,7 @@ signal_errorf(RpnState *st, const char *err, ...)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
signal_error(RpnState *st, const char *err)
|
signal_error(Pila *st, const char *err)
|
||||||
{
|
{
|
||||||
fputs("error: ", stderr);
|
fputs("error: ", stderr);
|
||||||
fputs(err, stderr);
|
fputs(err, stderr);
|
||||||
|
@ -172,7 +172,7 @@ signal_error(RpnState *st, const char *err)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
push_val(RpnState *st, Value val)
|
push_val(Pila *st, Value val)
|
||||||
{
|
{
|
||||||
if (st->sp + 1 > DATA_STACK_LEN) {
|
if (st->sp + 1 > DATA_STACK_LEN) {
|
||||||
signal_error(st, "stack overflow");
|
signal_error(st, "stack overflow");
|
||||||
|
@ -182,7 +182,7 @@ push_val(RpnState *st, Value val)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
top_val(RpnState *st, Value *val)
|
top_val(Pila *st, Value *val)
|
||||||
{
|
{
|
||||||
if (st->sp == 0) {
|
if (st->sp == 0) {
|
||||||
signal_error(st, "stack underflow");
|
signal_error(st, "stack underflow");
|
||||||
|
@ -193,7 +193,7 @@ top_val(RpnState *st, Value *val)
|
||||||
}
|
}
|
||||||
|
|
||||||
Value
|
Value
|
||||||
pop_val(RpnState *st)
|
pop_val(Pila *st)
|
||||||
{
|
{
|
||||||
Value val;
|
Value val;
|
||||||
if (!top_val(st, &val))
|
if (!top_val(st, &val))
|
||||||
|
@ -203,7 +203,7 @@ pop_val(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
add_nat(RpnState *st)
|
add_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value y = pop_val(st);
|
Value y = pop_val(st);
|
||||||
Value x = pop_val(st);
|
Value x = pop_val(st);
|
||||||
|
@ -211,7 +211,7 @@ add_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
sub_nat(RpnState *st)
|
sub_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value y = pop_val(st);
|
Value y = pop_val(st);
|
||||||
Value x = pop_val(st);
|
Value x = pop_val(st);
|
||||||
|
@ -219,7 +219,7 @@ sub_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
mul_nat(RpnState *st)
|
mul_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value y = pop_val(st);
|
Value y = pop_val(st);
|
||||||
Value x = pop_val(st);
|
Value x = pop_val(st);
|
||||||
|
@ -227,7 +227,7 @@ mul_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
div_nat(RpnState *st)
|
div_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value y = pop_val(st);
|
Value y = pop_val(st);
|
||||||
Value x = pop_val(st);
|
Value x = pop_val(st);
|
||||||
|
@ -239,7 +239,7 @@ div_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
lt_nat(RpnState *st)
|
lt_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value rhs = pop_val(st);
|
Value rhs = pop_val(st);
|
||||||
Value lhs = pop_val(st);
|
Value lhs = pop_val(st);
|
||||||
|
@ -247,7 +247,7 @@ lt_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
eq_nat(RpnState *st)
|
eq_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value rhs = pop_val(st);
|
Value rhs = pop_val(st);
|
||||||
Value lhs = pop_val(st);
|
Value lhs = pop_val(st);
|
||||||
|
@ -255,14 +255,14 @@ eq_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
not_nat(RpnState *st)
|
not_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value lhs = pop_val(st);
|
Value lhs = pop_val(st);
|
||||||
push_val(st, BOX_INTN(!lhs.intn));
|
push_val(st, BOX_INTN(!lhs.intn));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
dup_nat(RpnState *st)
|
dup_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value val;
|
Value val;
|
||||||
top_val(st, &val);
|
top_val(st, &val);
|
||||||
|
@ -270,7 +270,7 @@ dup_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
swap_nat(RpnState *st)
|
swap_nat(Pila *st)
|
||||||
{
|
{
|
||||||
if (st->sp < 2)
|
if (st->sp < 2)
|
||||||
return;
|
return;
|
||||||
|
@ -281,20 +281,20 @@ swap_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
drop_nat(RpnState *st)
|
drop_nat(Pila *st)
|
||||||
{
|
{
|
||||||
pop_val(st);
|
pop_val(st);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
bye_nat(RpnState *st)
|
bye_nat(Pila *st)
|
||||||
{
|
{
|
||||||
(void)st;
|
(void)st;
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
print_value(RpnState *st, Value val, bool reader_fmt)
|
print_value(Pila *st, Value val, bool reader_fmt)
|
||||||
{
|
{
|
||||||
(void)st;
|
(void)st;
|
||||||
switch (val.kind) {
|
switch (val.kind) {
|
||||||
|
@ -311,7 +311,7 @@ print_value(RpnState *st, Value val, bool reader_fmt)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
stack_contents_nat(RpnState *st)
|
stack_contents_nat(Pila *st)
|
||||||
{
|
{
|
||||||
printf("<%lu> ", st->sp);
|
printf("<%lu> ", st->sp);
|
||||||
if (st->sp == 0) {
|
if (st->sp == 0) {
|
||||||
|
@ -328,7 +328,7 @@ stack_contents_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
print_nat(RpnState *st)
|
print_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value top = pop_val(st);
|
Value top = pop_val(st);
|
||||||
print_value(st, top, false);
|
print_value(st, top, false);
|
||||||
|
@ -336,21 +336,21 @@ print_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
getch_nat(RpnState *st)
|
getch_nat(Pila *st)
|
||||||
{
|
{
|
||||||
char c = getchar();
|
char c = getchar();
|
||||||
push_val(st, BOX_INTN((u64)c));
|
push_val(st, BOX_INTN((u64)c));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
emit_nat(RpnState *st)
|
emit_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value c = pop_val(st);
|
Value c = pop_val(st);
|
||||||
printf("%c", (char)c.intn);
|
printf("%c", (char)c.intn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
compile_start(RpnState *st, Str wn)
|
compile_start(Pila *st, Str wn)
|
||||||
{
|
{
|
||||||
Word *w = malloc(sizeof(*w));
|
Word *w = malloc(sizeof(*w));
|
||||||
w->name = wn;
|
w->name = wn;
|
||||||
|
@ -369,7 +369,7 @@ compile_start(RpnState *st, Str wn)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
compile_start_nat(RpnState *st)
|
compile_start_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Str wn;
|
Str wn;
|
||||||
skip_whitespace(&st->rsp, st->rep);
|
skip_whitespace(&st->rsp, st->rep);
|
||||||
|
@ -380,13 +380,13 @@ compile_start_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
compile_anon_start_nat(RpnState *st)
|
compile_anon_start_nat(Pila *st)
|
||||||
{
|
{
|
||||||
compile_start(st, StrEmpty);
|
compile_start(st, StrEmpty);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
compile_end_nat(RpnState *st)
|
compile_end_nat(Pila *st)
|
||||||
{
|
{
|
||||||
if (!st->compiling) {
|
if (!st->compiling) {
|
||||||
signal_error(st, "not in compiling mode");
|
signal_error(st, "not in compiling mode");
|
||||||
|
@ -398,14 +398,14 @@ compile_end_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
Str
|
Str
|
||||||
next_word(RpnState *st)
|
next_word(Pila *st)
|
||||||
{
|
{
|
||||||
skip_whitespace(&st->rsp, st->rep);
|
skip_whitespace(&st->rsp, st->rep);
|
||||||
return read_word_name(&st->rsp, st->rep);
|
return read_word_name(&st->rsp, st->rep);
|
||||||
}
|
}
|
||||||
|
|
||||||
Word *
|
Word *
|
||||||
next_word_read_get(RpnState *st)
|
next_word_read_get(Pila *st)
|
||||||
{
|
{
|
||||||
Str wn;
|
Str wn;
|
||||||
skip_whitespace(&st->rsp, st->rep);
|
skip_whitespace(&st->rsp, st->rep);
|
||||||
|
@ -418,7 +418,7 @@ next_word_read_get(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
tick_nat(RpnState *st)
|
tick_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Word *w = next_word_read_get(st);
|
Word *w = next_word_read_get(st);
|
||||||
if (w == nil) {
|
if (w == nil) {
|
||||||
|
@ -429,7 +429,7 @@ tick_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
exec_nat(RpnState *st)
|
exec_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value waddr_w = pop_val(st);
|
Value waddr_w = pop_val(st);
|
||||||
if (waddr_w.kind == VAL_NIL) {
|
if (waddr_w.kind == VAL_NIL) {
|
||||||
|
@ -439,7 +439,7 @@ exec_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
eval_nat(RpnState *st)
|
eval_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value pstr = pop_val(st);
|
Value pstr = pop_val(st);
|
||||||
if (pstr.kind == VAL_NIL || pstr.kind != VAL_STR) {
|
if (pstr.kind == VAL_NIL || pstr.kind != VAL_STR) {
|
||||||
|
@ -450,7 +450,7 @@ eval_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
to_number_nat(RpnState *st)
|
to_number_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value ns = pop_val(st);
|
Value ns = pop_val(st);
|
||||||
if (ns.kind == VAL_NIL || ns.kind != VAL_STR) {
|
if (ns.kind == VAL_NIL || ns.kind != VAL_STR) {
|
||||||
|
@ -466,13 +466,13 @@ to_number_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
parse_word_nat(RpnState *st)
|
parse_word_nat(Pila *st)
|
||||||
{
|
{
|
||||||
push_val(st, BOX_STR(next_word(st)));
|
push_val(st, BOX_STR(next_word(st)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
find_word_nat(RpnState *st)
|
find_word_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value wnw = pop_val(st);
|
Value wnw = pop_val(st);
|
||||||
if (wnw.kind != VAL_STR) {
|
if (wnw.kind != VAL_STR) {
|
||||||
|
@ -488,7 +488,7 @@ find_word_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
branch_nat(RpnState *st)
|
branch_nat(Pila *st)
|
||||||
{
|
{
|
||||||
Value predw = pop_val(st);
|
Value predw = pop_val(st);
|
||||||
if (predw.kind != VAL_INT) {
|
if (predw.kind != VAL_INT) {
|
||||||
|
@ -506,7 +506,7 @@ branch_nat(RpnState *st)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
add_word(RpnState *st, Word *w)
|
add_word(Pila *st, Word *w)
|
||||||
{
|
{
|
||||||
if (!Str_empty(w->name)) {
|
if (!Str_empty(w->name)) {
|
||||||
DictionaryEntry *de = malloc(sizeof(*de));
|
DictionaryEntry *de = malloc(sizeof(*de));
|
||||||
|
@ -519,7 +519,7 @@ add_word(RpnState *st, Word *w)
|
||||||
}
|
}
|
||||||
|
|
||||||
Word *
|
Word *
|
||||||
search_word(RpnState *st, Str word_name)
|
search_word(Pila *st, Str word_name)
|
||||||
{
|
{
|
||||||
DictionaryEntry *de = st->dict;
|
DictionaryEntry *de = st->dict;
|
||||||
while (de != nil) {
|
while (de != nil) {
|
||||||
|
@ -602,7 +602,7 @@ read_string_lit(char **pstart, char *pend)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
eval_word(RpnState *st, Word *w)
|
eval_word(Pila *st, Word *w)
|
||||||
{
|
{
|
||||||
/* Here, as an implementation detail, the "return stack" behaves
|
/* Here, as an implementation detail, the "return stack" behaves
|
||||||
* more like a queue. */
|
* more like a queue. */
|
||||||
|
@ -623,7 +623,7 @@ eval_word(RpnState *st, Word *w)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
eval(RpnState *st, Str src)
|
eval(Pila *st, Str src)
|
||||||
{
|
{
|
||||||
st->rsp = src.s;
|
st->rsp = src.s;
|
||||||
st->rep = src.s + src.len;
|
st->rep = src.s + src.len;
|
||||||
|
@ -665,7 +665,7 @@ eval(RpnState *st, Str src)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
repl(RpnState *st)
|
repl(Pila *st)
|
||||||
{
|
{
|
||||||
char buf[REPL_LINE_BUF_LEN] = {0};
|
char buf[REPL_LINE_BUF_LEN] = {0};
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
@ -684,7 +684,7 @@ repl(RpnState *st)
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
RpnState st = {0};
|
Pila st = {0};
|
||||||
Word natws[] = {
|
Word natws[] = {
|
||||||
{.name = Sl("+"), .kind = WORD_NATIVE, .nat = add_nat },
|
{.name = Sl("+"), .kind = WORD_NATIVE, .nat = add_nat },
|
||||||
{.name = Sl("-"), .kind = WORD_NATIVE, .nat = sub_nat },
|
{.name = Sl("-"), .kind = WORD_NATIVE, .nat = sub_nat },
|
||||||
|
|
Loading…
Add table
Reference in a new issue