Phase 2: variables, arrays, program execution, and control flow
Add variable storage (scalar and array), program line storage with RUN/LIST/NEW, and full control flow: FOR/NEXT, GOTO, GOSUB/RETURN, IF/THEN/ELSE, WHILE/WEND, ON GOTO/GOSUB, DATA/READ/RESTORE. New modules: interp.c (1445 lines - execution loop and statement dispatcher), vars.c, arrays.c, input.c. Version bumped to 0.2.0. 16 test programs pass including Leibniz pi and prime sieve.
This commit is contained in:
+26
-1
@@ -10,7 +10,7 @@
|
||||
#include "gw_math.h"
|
||||
#include "strings.h"
|
||||
|
||||
#define GW_VERSION "0.1.0"
|
||||
#define GW_VERSION "0.2.0"
|
||||
#define GW_BANNER "GW-BASIC " GW_VERSION
|
||||
|
||||
/* Tokenizer */
|
||||
@@ -22,4 +22,29 @@ void gw_stmt_print(void);
|
||||
void gw_print_value(gw_value_t *v);
|
||||
void gw_print_newline(void);
|
||||
|
||||
/* Variables (vars.c) */
|
||||
gw_valtype_t gw_parse_varname(char name_out[2]);
|
||||
var_entry_t *gw_var_find_or_create(const char name[2], gw_valtype_t type);
|
||||
void gw_var_assign(var_entry_t *var, gw_value_t *val);
|
||||
void gw_vars_clear(void);
|
||||
void gw_stmt_deftype(gw_valtype_t type);
|
||||
void gw_stmt_swap(void);
|
||||
|
||||
/* Arrays (arrays.c) */
|
||||
void gw_stmt_dim(void);
|
||||
gw_value_t *gw_array_element(const char name[2], gw_valtype_t type);
|
||||
void gw_stmt_erase(void);
|
||||
void gw_stmt_option(void);
|
||||
void gw_arrays_clear(void);
|
||||
|
||||
/* Input (input.c) */
|
||||
void gw_stmt_input(void);
|
||||
void gw_stmt_line_input(void);
|
||||
|
||||
/* DEF FN evaluation (interp.c) */
|
||||
gw_value_t gw_eval_fn_call(void);
|
||||
|
||||
/* Error recovery for run loop */
|
||||
extern jmp_buf gw_run_jmp;
|
||||
|
||||
#endif
|
||||
|
||||
+42
-1
@@ -24,9 +24,43 @@ typedef struct {
|
||||
/* Default variable types for A-Z (DEFTBL) */
|
||||
gw_valtype_t def_type[26];
|
||||
|
||||
/* Variable storage */
|
||||
var_entry_t vars[256];
|
||||
int var_count;
|
||||
array_entry_t arrays[64];
|
||||
int array_count;
|
||||
int option_base;
|
||||
|
||||
/* Control flow stacks */
|
||||
for_entry_t for_stack[16];
|
||||
int for_sp;
|
||||
gosub_entry_t gosub_stack[24];
|
||||
int gosub_sp;
|
||||
while_entry_t while_stack[16];
|
||||
int while_sp;
|
||||
|
||||
/* DEF FN */
|
||||
fn_def_t fn_defs[26];
|
||||
|
||||
/* Run state */
|
||||
program_line_t *cur_line;
|
||||
bool running;
|
||||
|
||||
/* CONT state */
|
||||
uint8_t *cont_text;
|
||||
program_line_t *cont_line;
|
||||
|
||||
/* DATA pointer */
|
||||
uint8_t *data_ptr;
|
||||
program_line_t *data_line_ptr;
|
||||
uint16_t data_line;
|
||||
|
||||
/* Error trapping */
|
||||
uint16_t on_error_line;
|
||||
bool in_error_handler;
|
||||
uint8_t *err_resume_text;
|
||||
program_line_t *err_resume_line;
|
||||
uint16_t err_line_num;
|
||||
} interp_state_t;
|
||||
|
||||
extern interp_state_t gw;
|
||||
@@ -35,8 +69,15 @@ extern interp_state_t gw;
|
||||
#define LINE_DIRECT 65535
|
||||
|
||||
void gw_init(void);
|
||||
void gw_exec_line(uint8_t *tokens);
|
||||
void gw_exec_direct(const char *line);
|
||||
void gw_exec_stmt(void);
|
||||
void gw_run_loop(void);
|
||||
|
||||
/* Program storage */
|
||||
void gw_store_line(uint16_t num, uint8_t *tokens, int len);
|
||||
void gw_delete_line(uint16_t num);
|
||||
program_line_t *gw_find_line(uint16_t num);
|
||||
void gw_free_program(void);
|
||||
|
||||
/* CHRGET/CHRGOT - advance/peek the text pointer */
|
||||
uint8_t gw_chrget(void);
|
||||
|
||||
@@ -44,6 +44,48 @@ typedef struct {
|
||||
gw_value_t val;
|
||||
} var_entry_t;
|
||||
|
||||
/* Array entry */
|
||||
typedef struct {
|
||||
char name[2];
|
||||
gw_valtype_t type;
|
||||
int ndims;
|
||||
int dims[8]; /* max dimensions per DIM */
|
||||
int total_elements;
|
||||
gw_value_t *data; /* malloc'd flat array */
|
||||
} array_entry_t;
|
||||
|
||||
/* FOR stack entry */
|
||||
typedef struct {
|
||||
var_entry_t *var;
|
||||
gw_value_t limit, step;
|
||||
uint8_t *loop_text;
|
||||
struct program_line *loop_line;
|
||||
uint16_t line_num;
|
||||
} for_entry_t;
|
||||
|
||||
/* GOSUB stack entry */
|
||||
typedef struct {
|
||||
uint8_t *ret_text;
|
||||
struct program_line *ret_line;
|
||||
uint16_t line_num;
|
||||
} gosub_entry_t;
|
||||
|
||||
/* WHILE stack entry */
|
||||
typedef struct {
|
||||
uint8_t *while_text;
|
||||
struct program_line *while_line;
|
||||
uint16_t line_num;
|
||||
} while_entry_t;
|
||||
|
||||
/* DEF FN entry */
|
||||
typedef struct {
|
||||
bool defined;
|
||||
char param_name[2];
|
||||
gw_valtype_t param_type, ret_type;
|
||||
uint8_t *body_text;
|
||||
struct program_line *body_line;
|
||||
} fn_def_t;
|
||||
|
||||
/* MBF (Microsoft Binary Format) types for file compatibility */
|
||||
typedef struct {
|
||||
uint8_t mantissa[3];
|
||||
|
||||
Reference in New Issue
Block a user