Skip to content

Commit

Permalink
feat: Add __asm__ statement
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Nov 29, 2024
1 parent cdf4d85 commit 59f43dc
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ typed_token *next_keyword_or_identifier(char **inp_ptr)
return new_simp_tkn(TKN_DEFAULT);
else if (strcmp(val, "extern") == 0)
return new_simp_tkn(TKN_EXTERN);
else if (strcmp(val, "__asm__") == 0)
return new_simp_tkn(TKN_ASM);
else
return new_tkn(TKN_ID, val, ident_tkn_debug);
}
Expand Down
1 change: 1 addition & 0 deletions lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typed_token *tokenize(char *inp);
#define TKN_TYPEDEF 12
#define TKN_GOTO 13
#define TKN_EXTERN 14
#define TKN_ASM 15

// Single letter symbols
#define TKN_L_PAREN 16
Expand Down
81 changes: 81 additions & 0 deletions parser/asm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

#include <stdlib.h>

#include "../lexer.h"
#include "parser.h"
#include "asm.h"
#include "statement.h"
#include "../codegen/codegen.h"
#include "../linked_list.h"

apply_result *asm_apply(parser_node *node, context *ctx)
{
node_asm *as = (node_asm *)node->data;
list_node *curr = as->lines->first;
while (curr)
{
add_text(ctx, curr->value);
curr = curr->next;
}
return NULL;
}

void asm_debug(int depth, parser_node *node)
{
node_asm *as = (node_asm *)node->data;
printtabs(depth);
list_node *curr = as->lines->first;
printf("Asm:\n");
while (curr)
{
printtabs(depth + 1);
printf("\"%s\"", (char *)curr->value);
curr = curr->next;
}
}

parser_node *parse_asm(typed_token **tkns_ptr)
{
typed_token *tkn = *tkns_ptr;

if (tkn->type_id == TKN_ASM)
{
tkn = tkn->next;
if (tkn->type_id == TKN_L_PAREN)
{
tkn = tkn->next;
linked_list *lines = new_linked_list();
while (tkn)
{
if (tkn->type_id == TKN_R_PAREN)
{
tkn = tkn->next;
if (tkn->type_id == TKN_SEMICOLON)
{
tkn = tkn->next;
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_compound_statement));
node->debug = asm_debug;
node->apply = asm_apply;
node_asm *as = (node_asm *)node->data;
as->lines = lines;

return node;
}
}
else if (tkn->type_id == TKN_LIT_STR)
{
add_to_list(lines, tkn->data);
tkn = tkn->next;
}
else
{
return NULL;
}
}
}
}
return NULL;
}
15 changes: 15 additions & 0 deletions parser/asm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef ASM_H
#define ASM_H

#include "../lexer.h"
#include "parser.h"
#include "../linked_list.h"

parser_node *parse_asm(typed_token **tkns_ptr);

typedef struct
{
linked_list *lines;
} node_asm;

#endif
9 changes: 9 additions & 0 deletions parser/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "if.h"
#include "while.h"
#include "goto.h"
#include "asm.h"

apply_result *compound_statement_apply(parser_node *node, context *ctx)
{
Expand Down Expand Up @@ -165,6 +166,14 @@ parser_node *parse_statement(typed_token **tkns_ptr)
{
typed_token *tkn = *tkns_ptr;
parser_node *ret = NULL;

ret = parse_asm(&tkn);
if (ret)
{
*tkns_ptr = tkn;
return ret;
}

ret = parse_var_decl(&tkn);
if (ret)
{
Expand Down

0 comments on commit 59f43dc

Please sign in to comment.