Skip to content

Commit

Permalink
Style improvements, cleans up API, fix memory issues
Browse files Browse the repository at this point in the history
- More liberal use of static functions to clean up namespace
- General style and code improvements, comments, etc.
- Caught a few memory issues with Electric Fence
- Adds --allow-stderr to TestRunner for use with Electric Fence
- Write REPL history to file
  • Loading branch information
ndreynolds committed Apr 12, 2013
1 parent 9ac8b65 commit 331c1cf
Show file tree
Hide file tree
Showing 36 changed files with 1,872 additions and 1,889 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ lex.yy.?
tags
*.o
node_modules
.flathead_history
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

CC = gcc
CFLAGS = -Wall -Wextra -Wno-unused-parameter -O3 -std=c99 -pedantic -D_XOPEN_SOURCE
CFLAGS = -Wall -Wextra -Wno-unused-parameter -O4 -std=c99 -pedantic -D_XOPEN_SOURCE

YACC = bison
YACC_FLAGS = -y -d -t -v
Expand Down
8 changes: 4 additions & 4 deletions src/cli.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* cli.c -- Utilities for the command line interface.
*
* Copyright (c) 2012 Nick Reynolds
* Copyright (c) 2012-2013 Nick Reynolds
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -23,7 +23,7 @@


void
print_help()
fh_print_help()
{
printf("Usage: flat [options] [script.js] \n"
"\n"
Expand All @@ -36,7 +36,7 @@ print_help()
}

void
print_startup()
fh_print_startup()
{
printf(ANSI_BLUE);
printf(" ______ __ __ __ \n"
Expand All @@ -48,7 +48,7 @@ print_startup()
}

void
print_version()
fh_print_version()
{
printf("%s\n", VERSION);
}
Expand Down
8 changes: 4 additions & 4 deletions src/cli.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* cli.h -- Utilities for the command line interface.
*
* Copyright (c) 2012 Nick Reynolds
* Copyright (c) 2012-2013 Nick Reynolds
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -27,9 +27,9 @@
#define ANSI_WHITE "\033[37;1m"
#define ANSI_RESET "\033[0m"

void print_help(void);
void print_startup(void);
void print_version(void);
void fh_print_help(void);
void fh_print_startup(void);
void fh_print_version(void);
void cfprintf(FILE *, const char *, const char *, ...);

#endif
162 changes: 77 additions & 85 deletions src/debug.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* debug.c -- debugging functions
*
* Copyright (c) 2012 Nick Reynolds
* Copyright (c) 2012-2013 Nick Reynolds
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -27,106 +27,35 @@
// Value Debugging
// ----------------------------------------------------------------------------

void
fh_debug(FILE *stream, js_val *val, int indent, bool newline)
{
switch(val->type) {
case T_BOOLEAN:
fprintf(stream, "%s", !val->boolean.val ? "false" : "true");
break;
case T_NUMBER:
fh_debug_num(stream, val);
break;
case T_STRING:
if (fh->opt_interactive)
cfprintf(stream, ANSI_YELLOW, "'%s'", val->string.ptr);
else
fprintf(stream, "%s", val->string.ptr);
break;
case T_NULL:
cfprintf(stream, ANSI_GRAY, "null");
break;
case T_UNDEF:
cfprintf(stream, ANSI_GRAY, "undefined");
break;
case T_OBJECT:
if (IS_ARR(val))
fh_debug_arr(stream, val, indent);
else if (IS_FUNC(val))
cfprintf(stream, ANSI_BLUE, "[Function]");
else
fh_debug_obj(stream, val, indent, false);
break;
}
if (newline) fprintf(stream, "\n");
}

// Debug an object with extra verbosity, displaying non-enumerable properties.
void
fh_debug_verbose(FILE *stream, js_val *val, int indent)
{
switch(val->type) {
case T_BOOLEAN:
fprintf(stream, "Boolean: (%s)", !val->boolean.val ? "false" : "true");
break;
case T_NUMBER:
fh_debug_num(stream, val);
break;
case T_STRING:
cfprintf(stream, ANSI_YELLOW, "String: '%s'", val->string.ptr);
break;
case T_NULL:
cfprintf(stream, ANSI_GRAY, "null");
break;
case T_UNDEF:
cfprintf(stream, ANSI_GRAY, "undefined");
break;
case T_OBJECT:
if (IS_ARR(val))
fprintf(stream, "Array: ");
else if (IS_FUNC(val))
cfprintf(stream, ANSI_BLUE, "Function: ");
else
fprintf(stream, "Object: ");
break;
}

if (IS_OBJ(val))
fh_debug_obj(stream, val, indent, true);

fprintf(stream, "\n");
}

void
fh_debug_obj(FILE *stream, js_val *obj, int indent, bool force_enum)
static void
debug_obj(FILE *stream, js_val *obj, int indent, bool force_enum)
{
js_prop *x;
int i;
bool first = true;

OBJ_ITER(obj, x) {
if (!x->enumerable && !force_enum) continue;
if (first) {
for (i = 0; i < indent; i++) fprintf(stream, " ");
if (indent) fprintf(stream, "%*s", indent, " ");
fprintf(stream, "{");
first = false;
}
else {
fprintf(stream, ",\n");
for (i = 0; i < (indent + 1); i++) fprintf(stream, " ");
fprintf(stream, "%*s", indent + 1, " ");
}
fprintf(stream, " %s: ", x->name);
if (x->circular)
cfprintf(stream, ANSI_BLUE, "[Circular]");
else
fh_debug(stream, x->ptr, indent+3, false);
fh_debug(stream, x->ptr, indent + 3, false);
};

fprintf(stream, first ? "{}" : " }");
}

void
fh_debug_arr(FILE *stream, js_val *arr, int indent)
static void
debug_arr(FILE *stream, js_val *arr, int indent)
{
if (arr->object.length == 0) {
fprintf(stream, "[]");
Expand Down Expand Up @@ -157,8 +86,8 @@ fh_debug_arr(FILE *stream, js_val *arr, int indent)
fprintf(stream, " ]");
}

void
fh_debug_num(FILE *stream, js_val *num)
static void
debug_num(FILE *stream, js_val *num)
{
if (num->number.is_nan)
cfprintf(stream, ANSI_ORANGE, "NaN");
Expand All @@ -175,9 +104,72 @@ fh_debug_num(FILE *stream, js_val *num)
}

void
fh_debug_args(FILE *stream, js_args *args)
fh_debug(FILE *stream, js_val *val, int indent, bool newline)
{
unsigned int i;
for (i = 0; i < ARGLEN(args); i++)
fh_debug(stream, ARG(args, i), 0, 1);
switch (val->type) {
case T_BOOLEAN:
fprintf(stream, "%s", !val->boolean.val ? "false" : "true");
break;
case T_NUMBER:
debug_num(stream, val);
break;
case T_STRING:
if (fh->opt_interactive)
cfprintf(stream, ANSI_YELLOW, "'%s'", val->string.ptr);
else
fprintf(stream, "%s", val->string.ptr);
break;
case T_NULL:
cfprintf(stream, ANSI_GRAY, "null");
break;
case T_UNDEF:
cfprintf(stream, ANSI_GRAY, "undefined");
break;
case T_OBJECT:
if (IS_ARR(val))
debug_arr(stream, val, indent);
else if (IS_FUNC(val))
cfprintf(stream, ANSI_BLUE, "[Function]");
else
debug_obj(stream, val, indent, false);
break;
}
if (newline) fprintf(stream, "\n");
}

// Debug an object with extra verbosity, displaying non-enumerable properties.
void
fh_debug_verbose(FILE *stream, js_val *val, int indent)
{
switch (val->type) {
case T_BOOLEAN:
fprintf(stream, "Boolean: (%s)", !val->boolean.val ? "false" : "true");
break;
case T_NUMBER:
debug_num(stream, val);
break;
case T_STRING:
cfprintf(stream, ANSI_YELLOW, "String: '%s'", val->string.ptr);
break;
case T_NULL:
cfprintf(stream, ANSI_GRAY, "null");
break;
case T_UNDEF:
cfprintf(stream, ANSI_GRAY, "undefined");
break;
case T_OBJECT:
if (IS_ARR(val))
fprintf(stream, "Array: ");
else if (IS_FUNC(val))
cfprintf(stream, ANSI_BLUE, "Function: ");
else
fprintf(stream, "Object: ");
break;
}

if (IS_OBJ(val))
debug_obj(stream, val, indent, true);

fprintf(stream, "\n");
}

4 changes: 0 additions & 4 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@

#include "flathead.h"

void fh_debug_obj(FILE *, js_val *, int, bool);
void fh_debug_arr(FILE *, js_val *, int);
void fh_debug_num(FILE *, js_val *);
void fh_debug_args(FILE *, js_args *);
void fh_debug(FILE *, js_val *, int, bool);
void fh_debug_verbose(FILE *, js_val *, int);

Expand Down
Loading

0 comments on commit 331c1cf

Please sign in to comment.