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