Skip to content

Commit

Permalink
change function keyword to type declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
riicchhaarrd committed Aug 3, 2021
1 parent d01de1f commit 0618e08
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
61 changes: 45 additions & 16 deletions ast.c
Expand Up @@ -143,9 +143,9 @@ static int type_qualifiers(struct ast_context *ctx, int *qualifiers)
return 0;
}

static struct ast_node *type_declaration(struct ast_context *ctx)
static int type_declaration(struct ast_context *ctx, struct ast_node **data_type_node)
{
struct ast_node *data_type_node = NULL;
*data_type_node = NULL;

int pre_qualifiers = TQ_NONE;

Expand All @@ -164,16 +164,16 @@ static struct ast_node *type_declaration(struct ast_context *ctx)
struct ast_node* primitive_type_node = push_node( ctx, AST_PRIMITIVE_DATA_TYPE );
primitive_type_node->primitive_data_type_data.primitive_type = primitive_type;
primitive_type_node->primitive_data_type_data.qualifiers = pre_qualifiers | post_qualifiers;
data_type_node = primitive_type_node;
*data_type_node = primitive_type_node;

if(is_pointer)
{
struct ast_node* pointer_type_node = push_node( ctx, AST_POINTER_DATA_TYPE );
pointer_type_node->pointer_data_type_data.data_type = primitive_type_node;
data_type_node = pointer_type_node;
*data_type_node = pointer_type_node;
}
}
return data_type_node;
return *data_type_node ? 0 : ( pre_qualifiers == TQ_NONE ? 0 : 1 );
}

static struct ast_node *expression(struct ast_context *ctx);
Expand Down Expand Up @@ -274,9 +274,15 @@ static struct ast_node *factor(struct ast_context *ctx)
return NULL;

struct ast_node *so_node = push_node(ctx, AST_SIZEOF);

struct ast_node *subject = type_declaration(ctx);
if(!subject)

struct ast_node *subject = NULL;
int td = type_declaration(ctx, &subject);
if(td)
{
debug_printf("error in type declaration\n");
return NULL;
}
if(!subject)
{
subject = expression(ctx);
if(!subject)
Expand Down Expand Up @@ -729,8 +735,14 @@ static void print_ast(struct ast_node *n, int depth)

static int variable_declaration( struct ast_context* ctx, struct ast_node **out_decl_node )
{
struct ast_node *type_decl = type_declaration(ctx);
if(type_decl)
struct ast_node *type_decl = NULL;
int td = type_declaration(ctx, &type_decl);
if(td)
{
debug_printf("error in type declaration\n");
return 1;
}
if(type_decl)
{
if ( accept( ctx, TK_IDENT ) )
{
Expand Down Expand Up @@ -1007,13 +1019,24 @@ static struct ast_node *program(struct ast_context *ctx)

while(1)
{
if(!accept(ctx, TK_FUNCTION))
{
struct ast_node *decl = push_node(ctx, AST_FUNCTION_DECL);
if(!accept(ctx, TK_EOF))
break;

struct ast_node* type_decl = NULL;
int td = type_declaration( ctx , &type_decl );
if(td)
{
debug_printf("error in type declaration\n");
return NULL;
}
if ( type_decl )
{
struct ast_node *decl = push_node(ctx, AST_FUNCTION_DECL);
decl->func_decl_data.return_data_type = type_decl;
decl->func_decl_data.numparms = 0;
if(accept(ctx, TK_IDENT))
{
debug_printf("expected ident after function keyword\n");
debug_printf("expected ident after function\n");
return NULL;
}
struct ast_node *id = identifier(ctx, ctx->current_token->string);
Expand Down Expand Up @@ -1054,8 +1077,14 @@ static struct ast_node *program(struct ast_context *ctx)
return NULL;
decl->func_decl_data.body = block_node;
linked_list_prepend(program_node->program_data.body, decl);
} else break;
}
}
else
{
//TODO: implement global variables assignment, function prototypes and a preprocessor
debug_printf("expected function return type got '%s'\n", token_type_to_string(ctx->current_token->type));
return NULL;
}
}
return program_node;
}

Expand Down
1 change: 1 addition & 0 deletions ast.h
Expand Up @@ -133,6 +133,7 @@ struct ast_function_decl
struct ast_node *parameters[32];
int numparms;
struct ast_node *body;
struct ast_node *return_data_type;
};

struct ast_program
Expand Down
6 changes: 3 additions & 3 deletions examples/hello-world.oc → examples/hello-world.c
@@ -1,4 +1,4 @@
function strlen(const char *s)
int strlen(const char *s)
{
int i = 0;
while(s[i])
Expand All @@ -8,12 +8,12 @@ function strlen(const char *s)
return i;
}

function print(const char *s)
void print(const char *s)
{
write(1, s, strlen(s) + 1);
}

function main()
int main()
{
print("hello, world!\n");

Expand Down
2 changes: 0 additions & 2 deletions lex.c
Expand Up @@ -308,8 +308,6 @@ static int token(struct lexer *lex, struct token *tk)
tk->type = TK_WHILE;
else if(!strcmp(s, "if"))
tk->type = TK_IF;
else if(!strcmp(s, "function"))
tk->type = TK_FUNCTION;
else if(!strcmp(s, "return"))
tk->type = TK_RETURN;
else if(!strcmp(s, "break"))
Expand Down
1 change: 0 additions & 1 deletion token.h
Expand Up @@ -45,7 +45,6 @@ enum TOKEN_TYPE
TK_IF,
TK_FOR,
TK_WHILE,
TK_FUNCTION,
TK_RETURN,
TK_BREAK,
TK_LOOP,
Expand Down

0 comments on commit 0618e08

Please sign in to comment.