#------------------------------------------------------------------------------
# Define the default compiler environment
main_env = BuildEnv.new do |env|
- env["CFLAGS"] += ['-O3', '-Wall', '-Wextra', '-Werror', '--std=c99', '--pedantic']
- env["CPPPATH"] += Dir['modules/libcds/source/**/'] +
- Dir['modules/libc/source/**/']
+ env["CFLAGS"] += ['-O3', '-Wall', '-Wextra', '--std=c99', '--pedantic']
+ env["CPPPATH"] += Dir['modules/libc/source/**/']
end
#------------------------------------------------------------------------------
# Release Build Targets
#------------------------------------------------------------------------------
# Build third party libraries
-main_env.Library('build/lib/libcds.a', FileList['modules/libcds/source/**/*.c'])
main_env.Library('build/lib/libc.a', FileList['modules/libc/source/**/*.c'])
-runtime_libs = ['build/lib/libcds.a', 'build/lib/libc.a']
+runtime_libs = ['build/lib/libc.a']
# Build the parser
main_env.Program('parser', FileList['source/*.c'] + runtime_libs)
*****************************************************************************/
// Types
typedef struct AST {
- int type;
slist_node_t link;
slist_t children;
+ char* text;
+ int type;
} AST;
typedef struct strbuf_t {
// String Buffer
static void strbuf_init(strbuf_t* buf);
static void strbuf_putc(strbuf_t* buf, int ch);
+static char* strbuf_string(strbuf_t* buf);
/*
*****************************************************************************/
AST* msg = ast_new(UNARY_MSG);
ast_add_child(msg, expr);
ast_add_child(msg, ast_tok(IDENTIFIER));
- printf("%d\n", (int)slist_size(&msg->children));
expr = msg;
}
return expr;
*****************************************************************************/
static int token(void)
{
+ // Re-init the token
+ strbuf_init(&Token);
+
// Skip any whitespace.
whitespace();
+ // Get the next real token
if (EOF == current()) {
return EOF;
} else if ('$' == current()) {
*****************************************************************************/
static AST* ast_new(int type)
{
- AST* tree = (AST*)calloc(1,sizeof(AST));
+ AST* tree = (AST*)malloc(sizeof(AST));
+ memset(tree, 0, sizeof(AST));
tree->type = type;
return tree;
}
static AST* ast_tok(int type)
{
+ AST* ast = ast_new(type);
expect(type);
- return ast_new(type);
+ ast->text = strbuf_string(&Token);
+ return ast;
}
static void ast_add_child(AST* parent, AST* child)
{
int indent = depth * 2;
printf("%*s(", indent, "");
- printf("%d", tree->type);
+ printf("%d %s", tree->type, tree->text ? tree->text : "");
if (slist_size(&tree->children) == 0) {
printf(")\n");
} else {
// strbuf_putc(buf, *str++);
//}
-//static char* strbuf_string(strbuf_t* buf)
-//{
-// char* str = buf->string;
-// strbuf_init(buf);
-// return str;
-//}
+static char* strbuf_string(strbuf_t* buf)
+{
+ char* str = buf->string;
+ strbuf_init(buf);
+ return str;
+}