Skip to content

Commit

Permalink
Purge most unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-byers committed Aug 28, 2024
1 parent 0cd8adb commit f8c9e92
Show file tree
Hide file tree
Showing 34 changed files with 583 additions and 1,149 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ target_sources(paw
${PAW_SOURCE_DIR}/env.c
${PAW_SOURCE_DIR}/gc.c
${PAW_SOURCE_DIR}/hir.c
#${PAW_SOURCE_DIR}/iolib.c
${PAW_SOURCE_DIR}/lex.c
${PAW_SOURCE_DIR}/list.c
${PAW_SOURCE_DIR}/lib.c
#${PAW_SOURCE_DIR}/mathlib.c
${PAW_SOURCE_DIR}/map.c
${PAW_SOURCE_DIR}/mem.c
${PAW_SOURCE_DIR}/os.c
Expand Down
22 changes: 0 additions & 22 deletions src/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,28 +226,6 @@ void *paw_userdata(paw_Env *P, int index)
return f->data;
}

size_t paw_length(paw_Env *P, int index)
{
size_t result;
paw_push_value(P, index);
const Value v = P->top.p[-1];
switch (v.o->gc_kind) {
case VSTRING:
result = pawS_length(V_STRING(v));
break;
case VLIST:
result = pawV_list_length(V_LIST(v));
break;
case VMAP:
result = pawH_length(V_MAP(v));
break;
default:
result = 0;
}
paw_pop(P, 1);
return result;
}

void paw_pop(paw_Env *P, int n)
{
paw_assert(n <= pawC_stklen(P));
Expand Down
64 changes: 29 additions & 35 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "map.h"
#include "mem.h"
#include "parse.h"
#include <inttypes.h>

#define FIRST_ARENA_SIZE 4096
#define LARGE_ARENA_MIN 32
Expand Down Expand Up @@ -52,23 +51,27 @@ struct AstSegment *pawAst_segment_new(struct Ast *ast)
}

typedef struct Printer {
FILE *out;
Buffer *buf;
paw_Env *P;
int indent;
} Printer;

#define DUMP_FMT(P, fmt, ...) (indent_line(P), fprintf((P)->out, fmt, __VA_ARGS__))
#define DUMP_MSG(P, msg) (indent_line(P), fprintf((P)->out, msg))

#define DUMP_BLOCK(P, b) CHECK_EXP(AstIsBlock(AST_CAST_STMT(b)), dump_stmt(P, AST_CAST_STMT(b)))
#define DUMP_NAME(P, s) DUMP_FMT(P, "name: %s\n", s ? s->text : NULL)
#define DUMP_LITERAL(P, lit) L_ADD_LITERAL(ENV(P), (P)->buf, lit)
#define DUMP_STRING(P, str) pawL_add_string(ENV(P), (P)->buf, str)
#define DUMP_FSTRING(P, ...) pawL_add_fstring(ENV(P), (P)->buf, __VA_ARGS__)

static void indent_line(Printer *P)
{
for (int i = 0; i < P->indent; ++i) {
fprintf(P->out, " ");
DUMP_LITERAL(P, " ");
}
}

#define DUMP_FMT(P, ...) (indent_line(P), pawL_add_fstring(ENV(P), (P)->buf, __VA_ARGS__))
#define DUMP_MSG(P, msg) (indent_line(P), pawL_add_string(ENV(P), (P)->buf, msg))
#define DUMP_BLOCK(P, b) CHECK_EXP(AstIsBlock(AST_CAST_STMT(b)), dump_stmt(P, AST_CAST_STMT(b)))
#define DUMP_NAME(P, s) DUMP_FMT(P, "name: %s\n", s ? s->text : "(null)")

static void dump_expr(Printer *P, struct AstExpr *e);
static void dump_decl(Printer *P, struct AstDecl *d);
static void dump_stmt(Printer *P, struct AstStmt *s);
Expand Down Expand Up @@ -97,7 +100,7 @@ DEFINE_LIST_PRINTER(stmt, AstStmt)
{ \
if (node != NULL) { \
struct T *typed = node; \
printf("%s {\n", k##T##Names[AST_KINDOF(typed)]); \
DUMP_FSTRING(P, "%s {\n", k##T##Names[AST_KINDOF(typed)]); \
return 0; \
} \
return -1; \
Expand All @@ -118,7 +121,7 @@ static void dump_path(Printer *P, struct AstPath *p)
static void dump_decl(Printer *P, struct AstDecl *d)
{
if (print_decl_kind(P, d)) {
fprintf(P->out, "(null)\n");
DUMP_LITERAL(P, "(null)\n");
return;
}
++P->indent;
Expand Down Expand Up @@ -175,7 +178,7 @@ static void dump_decl(Printer *P, struct AstDecl *d)
static void dump_stmt(Printer *P, struct AstStmt *s)
{
if (print_stmt_kind(P, s)) {
fprintf(P->out, "(null)\n");
DUMP_LITERAL(P, "(null)\n");
return;
}
++P->indent;
Expand Down Expand Up @@ -246,7 +249,7 @@ static void dump_stmt(Printer *P, struct AstStmt *s)
static void dump_expr(Printer *P, struct AstExpr *e)
{
if (print_expr_kind(P, e)) {
fprintf(P->out, "(null)\n");
DUMP_LITERAL(P, "(null)\n");
return;
}
++P->indent;
Expand All @@ -268,7 +271,7 @@ static void dump_expr(Printer *P, struct AstExpr *e)
break;
case PAW_TINT:
DUMP_MSG(P, "type: int\n");
DUMP_FMT(P, "value: %" PRId64 "\n",
DUMP_FMT(P, "value: %I\n",
V_INT(e->literal.basic.value));
break;
case PAW_TFLOAT:
Expand Down Expand Up @@ -336,7 +339,7 @@ static void dump_expr(Printer *P, struct AstExpr *e)
DUMP_MSG(P, "target: ");
dump_expr(P, e->selector.target);
if (e->selector.is_index) {
DUMP_FMT(P, "index: %" PRId64 "\n", e->selector.index);
DUMP_FMT(P, "index: %I\n", e->selector.index);
} else {
DUMP_NAME(P, e->selector.name);
}
Expand All @@ -359,26 +362,17 @@ static void dump_expr(Printer *P, struct AstExpr *e)
DUMP_MSG(P, "}\n");
}

void pawAst_dump_decl(FILE *out, struct AstDecl *decl)
void pawAst_dump(struct Ast *ast)
{
Printer P = {.out = out};
dump_decl(&P, decl);
}

void pawAst_dump_expr(FILE *out, struct AstExpr *expr)
{
Printer P = {.out = out};
dump_expr(&P, expr);
}

void pawAst_dump_stmt(FILE *out, struct AstStmt *stmt)
{
Printer P = {.out = out};
dump_stmt(&P, stmt);
}

void pawAst_dump_path(FILE *out, struct AstPath *path)
{
Printer P = {.out = out};
dump_path(&P, path);
Buffer buf;
paw_Env *P = ENV(ast);
pawL_init_buffer(P, &buf);
Printer print = {
.buf = &buf,
.P = P,
};
for (int i = 0; i < ast->items->count; ++i) {
dump_decl(&print, ast->items->data[i]);
}
pawL_push_result(P, &buf);
}
8 changes: 1 addition & 7 deletions src/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,6 @@ static inline struct AstSegment *pawAst_path_add(struct Ast *ast, struct AstPath

#define AST_KINDOF(x) ((x)->hdr.kind)

// TODO: convert AST to string instead of printing
#include <stdio.h>

void pawAst_dump_path(FILE *out, struct AstPath *path);
void pawAst_dump_decl(FILE *out, struct AstDecl *decl);
void pawAst_dump_expr(FILE *out, struct AstExpr *expr);
void pawAst_dump_stmt(FILE *out, struct AstStmt *stmt);
void pawAst_dump(struct Ast *ast);

#endif // PAW_AST_H
29 changes: 1 addition & 28 deletions src/auxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void grow_buffer(paw_Env *P, Buffer *buf, int boxloc)
{
paw_assert(buf->alloc <= SIZE_MAX / 2);
const size_t alloc = buf->alloc * 2;
if (pawL_boxed(buf)) {
if (L_IS_BOXED(buf)) {
Foreign *ud = V_FOREIGN(P->top.p[boxloc]);
pawM_resize(P, buf->data, buf->alloc, alloc);
ud->data = buf->data;
Expand Down Expand Up @@ -74,33 +74,6 @@ static void add_nstring(paw_Env *P, Buffer *buf, const char *str, size_t len, in
memcpy(ptr, str, len);
}

void pawL_add_value(paw_Env *P, Buffer *buf, paw_Type type)
{
size_t len; // value must be on top of the stack
const char *str = pawV_to_string(P, P->top.p[-1], type, &len);
if (str == NULL) {
// add the type name and address
str = paw_push_fstring(P, "%s (%p)",
"<type>" /*TODO: paw_typename(P, -1)*/,
paw_userdata(P, -1));
len = paw_length(P, -1);
}
add_nstring(P, buf, str, len, -3);
pawC_stkdec(P, 2); // pop value and string
}

// Table and stringify algorithm modified from micropython
static const uint8_t kLogBase2[] = {
0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5,
};

size_t pawL_integer_format_size(size_t nbits, int base)
{
assert(2 <= base && base <= 32);
return nbits / kLogBase2[base - 1] + 1;
}

void pawL_add_char(paw_Env *P, Buffer *buf, char c)
{
char *ptr = reserve_memory(P, buf, 1, -1);
Expand Down
10 changes: 2 additions & 8 deletions src/auxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ typedef struct Buffer {
char stack[BUFFER_LIMIT];
} Buffer;

static inline paw_Bool pawL_boxed(const Buffer *buf)
{
return buf->data != buf->stack;
}
#define L_IS_BOXED(buf) ((buf)->data != (buf)->stack)

void pawL_init_buffer(paw_Env *P, Buffer *buf);
void pawL_discard_result(paw_Env *P, Buffer *buf);
void pawL_push_result(paw_Env *P, Buffer *buf);
void pawL_buffer_resize(paw_Env *P, Buffer *buf, size_t n);

#define pawL_add_literal(P, buf, s) pawL_add_nstring(P, buf, s, paw_lengthof(s))
#define L_ADD_LITERAL(P, buf, s) pawL_add_nstring(P, buf, s, paw_lengthof(s))

void pawL_add_char(paw_Env *P, Buffer *buf, char c);
void pawL_add_string(paw_Env *P, Buffer *buf, const char *s);
Expand All @@ -42,7 +39,4 @@ void pawL_add_value(paw_Env *P, Buffer *print, paw_Type type);
void pawL_add_vfstring(paw_Env *P, Buffer *buf, const char *fmt, va_list arg);
void pawL_add_fstring(paw_Env *P, Buffer *buf, const char *fmt, ...);

// TODO: Move to bigint.h or value.h?
size_t pawL_integer_format_size(size_t nbits, int base);

#endif // PAW_AUX_H
14 changes: 7 additions & 7 deletions src/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ static void mangle_type(struct Generator *G, Buffer *buf, struct HirType *type)
paw_Env *P = ENV(G);
if (HIR_IS_BASIC_T(type)) {
if (HIR_IS_UNIT_T(type)) {
pawL_add_literal(P, buf, "0");
L_ADD_LITERAL(P, buf, "0");
} else if (type->adt.base == PAW_TBOOL) {
pawL_add_literal(P, buf, "b");
L_ADD_LITERAL(P, buf, "b");
} else if (type->adt.base == PAW_TINT) {
pawL_add_literal(P, buf, "i");
L_ADD_LITERAL(P, buf, "i");
} else if (type->adt.base == PAW_TFLOAT) {
pawL_add_literal(P, buf, "f");
L_ADD_LITERAL(P, buf, "f");
} else if (type->adt.base == PAW_TSTR) {
pawL_add_literal(P, buf, "s");
L_ADD_LITERAL(P, buf, "s");
}
} else if (HirIsGeneric(type)) {
struct HirGeneric *var = &type->generic;
Expand Down Expand Up @@ -656,7 +656,7 @@ static void code_setter(struct HirVisitor *V, struct HirExpr *lhs, struct HirExp
String *name = lhs->select.name;
struct HirType *target = HIR_TYPEOF(suf->target);
if (lhs->select.is_index) {
pawK_code_U(fs, OP_SETTUPLE, lhs->select.index);
pawK_code_U(fs, OP_SETFIELD, lhs->select.index);
} else {
const struct HirVarInfo info = resolve_attr(G, target, name);
pawK_code_U(fs, OP_SETFIELD, info.index);
Expand Down Expand Up @@ -1394,7 +1394,7 @@ static void code_selector_expr(struct HirVisitor *V, struct HirSelector *e)

V->VisitExpr(V, e->target);
if (e->is_index) {
pawK_code_U(fs, OP_GETTUPLE, e->index);
pawK_code_U(fs, OP_GETFIELD, e->index);
} else {
const struct HirVarInfo info = resolve_attr(G, HIR_TYPEOF(e->target), e->name);
pawK_code_U(fs, OP_GETFIELD, info.index);
Expand Down
6 changes: 5 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@
# define PAW_UNREACHABLE __builtin_unreachable
#elif defined(_MSC_VER)
# define PAW_NODISCARD _Check_return_
# define PAW_UNREACHABLE __assume(0)
# define PAW_UNREACHABLE paw_unreachable_
_Noreturn static inline void paw_unreachable_(void)
{
__assume(0);
}
#else
# define PAW_NODISCARD
# define PAW_UNREACHABLE paw_unreachable_
Expand Down
25 changes: 0 additions & 25 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ static Object **get_gc_list(Object *o)
return &O_CLOSURE(o)->gc_list;
case VPROTO:
return &O_PROTO(o)->gc_list;
case VINSTANCE:
return &O_INSTANCE(o)->gc_list;
case VVARIANT:
return &O_VARIANT(o)->gc_list;
case VTUPLE:
Expand Down Expand Up @@ -145,17 +143,6 @@ static void traverse_tuple(paw_Env *P, Tuple *t)
traverse_fields(P, t->elems, t->nelems);
}

static void traverse_instance(paw_Env *P, Instance *i)
{
traverse_fields(P, i->fields, i->nfields);
}

static void traverse_method(paw_Env *P, Method *m)
{
mark_object(P, V_OBJECT(m->self));
mark_object(P, V_OBJECT(m->f));
}

static void traverse_list(paw_Env *P, List *a)
{
paw_Int itr = PAW_ITER_INIT;
Expand Down Expand Up @@ -229,12 +216,6 @@ static void traverse_objects(paw_Env *P)
case VTUPLE:
traverse_tuple(P, O_TUPLE(o));
break;
case VINSTANCE:
traverse_instance(P, O_INSTANCE(o));
break;
case VMETHOD:
traverse_method(P, O_METHOD(o));
break;
case VLIST:
traverse_list(P, O_LIST(o));
break;
Expand Down Expand Up @@ -360,12 +341,6 @@ void pawG_free_object(paw_Env *P, Object *o)
case VPROTO:
pawV_free_proto(P, O_PROTO(o));
break;
case VINSTANCE:
pawV_free_instance(P, O_INSTANCE(o));
break;
case VMETHOD:
pawV_free_method(P, O_METHOD(o));
break;
case VNATIVE:
pawV_free_native(P, O_NATIVE(o));
break;
Expand Down
Loading

0 comments on commit f8c9e92

Please sign in to comment.