summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2022-12-06 22:28:48 +0100
committerDaniel Weipert <code@drogueronin.de>2022-12-06 22:28:48 +0100
commitf7fddeaf9f0453054d4666d8a5b544d4c4cefebd (patch)
tree1f84126d93313358fd62da6d2c2497a3d0643341
parentf17b517e56503600355de910ce576efab56b1287 (diff)
Fix token typing and work towards ast generation
-rw-r--r--Makefile2
-rw-r--r--ast.c27
-rw-r--r--ast.h13
-rw-r--r--grammar.y37
4 files changed, 56 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index 8f87383..58f4163 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-build: grammar.y lex.l
+build: grammar.y lex.l ast.h ast.c
bison \
-o build/grammar.tab.c \
-d grammar.y \
diff --git a/ast.c b/ast.c
index d2b8782..2287b5c 100644
--- a/ast.c
+++ b/ast.c
@@ -16,3 +16,30 @@ ast_node* create_program_node(ast_node* previous_node, ast_node* current_node) {
return node;
}
+
+ast_node* create_type_node(char* type) {
+ ast_node* node = malloc(sizeof(ast_node));
+ node->type = TYPE_NODE;
+
+ ast_node_data* data = malloc(sizeof(ast_node_data));
+ data->value = type;
+ node->data = data;
+
+ printf("type:: %s\n", type);
+
+ return node;
+}
+
+ast_node* create_argument_node(char* identifier, ast_node* type) {
+ ast_node* node = malloc(sizeof(ast_node));
+ node->type = ARGUMENT_NODE;
+
+ ast_node_data* data = malloc(sizeof(ast_node_data));
+ data->value = identifier;
+ data->type = type;
+ node->data = data;
+
+ printf("argument:: %s -- %s\n", identifier, type->data->value);
+
+ return node;
+}
diff --git a/ast.h b/ast.h
index 3b59d2b..7b2a35e 100644
--- a/ast.h
+++ b/ast.h
@@ -3,11 +3,20 @@
#include <stdlib.h>
-#define PROGRAM_NODE 0
+enum NODE_TYPE {
+ PROGRAM_NODE,
+ TYPE_NODE,
+ ARGUMENT_NODE
+};
+
+typedef struct ast_node_data {
+ void* value;
+ void* type;
+} ast_node_data;
typedef struct ast_node {
int type;
- void* value;
+ ast_node_data* data;
struct ast_node* left;
struct ast_node* right;
} ast_node;
diff --git a/grammar.y b/grammar.y
index c85da97..8d660a9 100644
--- a/grammar.y
+++ b/grammar.y
@@ -30,9 +30,9 @@ ast_node* yyast = NULL;
%token CONST
%token VAR
-%token TYPE_INTEGER
-%token TYPE_STRING
-%token TYPE_VOID
+%token <string> TYPE_INTEGER
+%token <string> TYPE_STRING
+%token <string> TYPE_VOID
%token PARENTHESIS_LEFT
%token PARENTHESIS_RIGHT
@@ -44,8 +44,8 @@ ast_node* yyast = NULL;
%token COMMA;
%token COLON;
-%token NUMBER;
-%token IDENTIFIER
+%token <number> NUMBER;
+%token <string> IDENTIFIER
%token COMMENT;
%token END_OF_LINE;
@@ -60,9 +60,6 @@ ast_node* yyast = NULL;
%type <node> statement
%type <node> expression
-%type <node> IDENTIFIER
-%type <node> RETURN
-
%start program
%%
@@ -73,41 +70,41 @@ program: { $$ = NULL; }
// function henshin(): void {}
// function henshin(hen: integer, shin: integer): void {}
function:
- | FUNCTION IDENTIFIER PARENTHESIS_LEFT PARENTHESIS_RIGHT COLON return_type BRACE_LEFT statements BRACE_RIGHT
- | FUNCTION IDENTIFIER PARENTHESIS_LEFT arguments PARENTHESIS_RIGHT COLON return_type BRACE_LEFT statements BRACE_RIGHT { $$ = create_node(); $$->value = $2; }
+ FUNCTION IDENTIFIER PARENTHESIS_LEFT PARENTHESIS_RIGHT COLON return_type BRACE_LEFT statements BRACE_RIGHT
+ | FUNCTION IDENTIFIER PARENTHESIS_LEFT arguments PARENTHESIS_RIGHT COLON return_type BRACE_LEFT statements BRACE_RIGHT
arguments:
- | argument
+ argument
| arguments COMMA argument
argument:
- | IDENTIFIER COLON variable_type
+ IDENTIFIER COLON variable_type { $$ = create_argument_node($1, $3); }
return_type:
- | TYPE_INTEGER
- | TYPE_STRING
- | TYPE_VOID
+ TYPE_INTEGER { $$ = create_type_node($1); }
+ | TYPE_STRING { $$ = create_type_node($1); }
+ | TYPE_VOID { $$ = create_type_node($1); }
variable_type:
- | TYPE_INTEGER
- | TYPE_STRING
+ TYPE_INTEGER { $$ = create_type_node($1); }
+ | TYPE_STRING { $$ = create_type_node($1); }
statements:
- | statement
+ statement
| statements END_OF_LINE statement
statement:
// const henshin: integer = 2
| CONST IDENTIFIER COLON variable_type ASSIGN expression
- | RETURN expression { $$ = create_node(); $$->left = $1; $$->right = $2; }
+ | RETURN expression
| IDENTIFIER PARENTHESIS_LEFT PARENTHESIS_RIGHT
| function
expression:
- | NUMBER
+ NUMBER
| IDENTIFIER
%%