#include <stdint.h>
#include <string.h>
#include <ao_lisp_os.h>
+#ifndef __BYTE_ORDER
+#include <endian.h>
+#endif
typedef uint16_t ao_poly;
typedef int16_t ao_signed_poly;
#define AO_LISP_ATOM 4
#define AO_LISP_BUILTIN 5
#define AO_LISP_FRAME 6
-#define AO_LISP_LAMBDA 7
-#define AO_LISP_STACK 8
-#define AO_LISP_BOOL 9
-#define AO_LISP_NUM_TYPE 10
+#define AO_LISP_FRAME_VALS 7
+#define AO_LISP_LAMBDA 8
+#define AO_LISP_STACK 9
+#define AO_LISP_BOOL 10
+#define AO_LISP_BIGINT 11
+#define AO_LISP_FLOAT 12
+#define AO_LISP_NUM_TYPE 13
/* Leave two bits for types to use as they please */
#define AO_LISP_OTHER_TYPE_MASK 0x3f
#define AO_LISP_DIVIDE_BY_ZERO 0x02
#define AO_LISP_INVALID 0x04
#define AO_LISP_UNDEFINED 0x08
-#define AO_LISP_EOF 0x10
-#define AO_LISP_EXIT 0x20
+#define AO_LISP_REDEFINED 0x10
+#define AO_LISP_EOF 0x20
+#define AO_LISP_EXIT 0x40
extern uint8_t ao_lisp_exception;
ao_poly val;
};
+struct ao_lisp_frame_vals {
+ uint8_t type;
+ uint8_t size;
+ struct ao_lisp_val vals[];
+};
+
struct ao_lisp_frame {
uint8_t type;
uint8_t num;
ao_poly prev;
- struct ao_lisp_val vals[];
+ ao_poly vals;
};
struct ao_lisp_bool {
uint16_t pad;
};
+struct ao_lisp_bigint {
+ uint32_t value;
+};
+
+struct ao_lisp_float {
+ uint8_t type;
+ uint8_t pad1;
+ uint16_t pad2;
+ float value;
+};
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+static inline uint32_t
+ao_lisp_int_bigint(int32_t i) {
+ return AO_LISP_BIGINT | (i << 8);
+}
+static inline int32_t
+ao_lisp_bigint_int(uint32_t bi) {
+ return (int32_t) bi >> 8;
+}
+#else
+static inline uint32_t
+ao_lisp_int_bigint(int32_t i) {
+ return (uint32_t) (i & 0xffffff) | (AO_LISP_BIGINT << 24);
+}
+static inlint int32_t
+ao_lisp_bigint_int(uint32_t bi) {
+ return (int32_t) (bi << 8) >> 8;
+}
+#endif
+
+#define AO_LISP_MIN_INT (-(1 << (15 - AO_LISP_TYPE_SHIFT)))
+#define AO_LISP_MAX_INT ((1 << (15 - AO_LISP_TYPE_SHIFT)) - 1)
+#define AO_LISP_MIN_BIGINT (-(1 << 24))
+#define AO_LISP_MAX_BIGINT ((1 << 24) - 1)
+
+#define AO_LISP_NOT_INTEGER 0x7fffffff
+
/* Set on type when the frame escapes the lambda */
#define AO_LISP_FRAME_MARK 0x80
#define AO_LISP_FRAME_PRINT 0x40
return ao_lisp_poly(frame, AO_LISP_OTHER);
}
+static inline struct ao_lisp_frame_vals *
+ao_lisp_poly_frame_vals(ao_poly poly) {
+ return ao_lisp_ref(poly);
+}
+
+static inline ao_poly
+ao_lisp_frame_vals_poly(struct ao_lisp_frame_vals *vals) {
+ return ao_lisp_poly(vals, AO_LISP_OTHER);
+}
+
enum eval_state {
eval_sexpr, /* Evaluate an sexpr */
eval_val, /* Value computed */
eval_apply, /* Execute apply */
eval_cond, /* Start next cond clause */
eval_cond_test, /* Check cond condition */
- eval_progn, /* Start next progn entry */
+ eval_begin, /* Start next begin entry */
eval_while, /* Start while condition */
eval_while_test, /* Check while condition */
eval_macro, /* Finished with macro generation */
return ao_lisp_poly(cons, AO_LISP_CONS);
}
-static inline int
+static inline int32_t
ao_lisp_poly_int(ao_poly poly)
{
- return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
+ return (int32_t) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
}
static inline ao_poly
-ao_lisp_int_poly(int i)
+ao_lisp_int_poly(int32_t i)
{
return ((ao_poly) i << 2) | AO_LISP_INT;
}
+static inline struct ao_lisp_bigint *
+ao_lisp_poly_bigint(ao_poly poly)
+{
+ return ao_lisp_ref(poly);
+}
+
+static inline ao_poly
+ao_lisp_bigint_poly(struct ao_lisp_bigint *bi)
+{
+ return ao_lisp_poly(bi, AO_LISP_OTHER);
+}
+
static inline char *
ao_lisp_poly_string(ao_poly poly)
{
{
return ao_lisp_ref(poly);
}
+
+static inline ao_poly
+ao_lisp_float_poly(struct ao_lisp_float *f)
+{
+ return ao_lisp_poly(f, AO_LISP_OTHER);
+}
+
+static inline struct ao_lisp_float *
+ao_lisp_poly_float(ao_poly poly)
+{
+ return ao_lisp_ref(poly);
+}
+
+float
+ao_lisp_poly_number(ao_poly p);
+
/* memory functions */
extern int ao_lisp_collects[2];
return ao_lisp_poly_stack(ao_lisp_poly_fetch(id));
}
+void
+ao_lisp_frame_stash(int id, struct ao_lisp_frame *frame);
+
+struct ao_lisp_frame *
+ao_lisp_frame_fetch(int id);
+
/* bool */
extern const struct ao_lisp_type ao_lisp_bool_type;
struct ao_lisp_cons *
ao_lisp_cons_cons(ao_poly car, ao_poly cdr);
+/* Return a cons or NULL for a proper list, else error */
+struct ao_lisp_cons *
+ao_lisp_cons_cdr(struct ao_lisp_cons *cons);
+
ao_poly
ao_lisp__cons(ao_poly car, ao_poly cdr);
ao_lisp_atom_intern(char *name);
ao_poly *
-ao_lisp_atom_ref(struct ao_lisp_frame *frame, ao_poly atom);
+ao_lisp_atom_ref(ao_poly atom);
ao_poly
ao_lisp_atom_get(ao_poly atom);
ao_poly
ao_lisp_atom_set(ao_poly atom, ao_poly val);
+ao_poly
+ao_lisp_atom_def(ao_poly atom, ao_poly val);
+
/* int */
void
ao_lisp_int_write(ao_poly i);
+int32_t
+ao_lisp_poly_integer(ao_poly p);
+
+ao_poly
+ao_lisp_integer_poly(int32_t i);
+
+static inline int
+ao_lisp_integer_typep(uint8_t t)
+{
+ return (t == AO_LISP_INT) || (t == AO_LISP_BIGINT);
+}
+
+void
+ao_lisp_bigint_write(ao_poly i);
+
+extern const struct ao_lisp_type ao_lisp_bigint_type;
/* prim */
void
ao_lisp_poly_write(ao_poly p);
ao_poly
ao_lisp_set_cond(struct ao_lisp_cons *cons);
+/* float */
+extern const struct ao_lisp_type ao_lisp_float_type;
+
+void
+ao_lisp_float_write(ao_poly p);
+
+ao_poly
+ao_lisp_float_get(float value);
+
+static inline uint8_t
+ao_lisp_number_typep(uint8_t t)
+{
+ return ao_lisp_integer_typep(t) || (t == AO_LISP_FLOAT);
+}
+
+float
+ao_lisp_poly_number(ao_poly p);
+
/* builtin */
void
ao_lisp_builtin_write(ao_poly b);
/* frame */
extern const struct ao_lisp_type ao_lisp_frame_type;
+extern const struct ao_lisp_type ao_lisp_frame_vals_type;
#define AO_LISP_FRAME_FREE 6
void
ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
-int
-ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
+ao_poly
+ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
void
ao_lisp_frame_write(ao_poly p);
+void
+ao_lisp_frame_init(void);
+
/* lambda */
extern const struct ao_lisp_type ao_lisp_lambda_type;
#include <assert.h>
extern int dbg_move_depth;
#define MDBG_DUMP 1
-#define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
+#define MDBG_OFFSET(a) ((a) ? (int) ((uint8_t *) (a) - ao_lisp_pool) : -1)
extern int dbg_mem;