Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transpiler/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
OBJS = c.parser.o c.lexer.o main.o ast.o ast_sqz.o symrec.o type.o stringlib.o diagnostics.o ast_sem.o ast_typing.o codegen.o builtin_func.o builtin_measure.o builtin_gate.o
LEX = flex
YACC = bison
SUBDIRS := preprocessor
SUBDIRS := preprocessor ena data
LIBS := $(foreach dir,$(SUBDIRS),$(dir)/lib$(dir).a)

YACC_ARGS =
Expand Down
196 changes: 38 additions & 158 deletions src/transpiler/ast.c
Original file line number Diff line number Diff line change
@@ -1,183 +1,63 @@
#include "ast.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "stringlib.h"
#include "common.h"
int scope_level = 0;

ast_node *new_ast_node(ast_node_type node_type, const ast_identifier_node *id_node, const ast_const_node *const_node, const typerec_t *type, const ast_node *left, const ast_node *middle, const ast_node *right)
{
ast_node *node = IALLOC(ast_node);
node->node_type = node_type;
node->identifier = (ast_identifier_node *)id_node;
node->left = (ast_node *)left;
node->right = (ast_node *)right;
node->middle = (ast_node *)middle;
node->type = (typerec_t *)type;
node->constant = (ast_const_node *)const_node;
return node;
}
#define STRING_GEN(STRING) #STRING,
const char *ast_to_str[] = { FOREACH_AST_TYPE (STRING_GEN) };
#undef STRING_GEN

int register_type_if_required(ast_node *decl, ast_node *identifier)
struct
{
ast_node *node = decl;
BOOL has_typedef = FALSE;
while (node)
{
if (node->node_type == AST_STG_TYPEDEF)
{
has_typedef = TRUE;
break;
}
node = node->left;
}

if (!has_typedef)
{
return VAL_FAILED;
}

identifier = identifier->middle;

if (!identifier || identifier->node_type != AST_VARIABLE_DECLARATOR)
{
perror("typedef requires its type name");
}

node = decl;
while (node->right)
{
if (node->right->middle->type)
{
node = node->right;
break;
}

node = node->right->right;
}

int size = 0;
type_t *t = NULL, *root = NULL;
typerec_t *root_rec = NULL;
while (node && node->middle->type)
{
ast_node *type_node = node->middle;
if (!type_node->type)
{
perror("unknown type");
}
uint32_t node_id;
} ast_ctx = { .node_id = 0 };

type_t *sub = type_node->type->handle;
if (!t)
{
t = sub;
root = sub;
root_rec = type_node->type;
}
else
{
t->next = sub;
t = sub;
}

size = sub->meta->size;
node = node->right;
}

puttype(identifier->middle->identifier->sym->name, root_rec->type_type, root);
return VAL_OK;
}

void append_left_child(ast_node *parent, const ast_node *child)
{
parent->left = (ast_node *)child;
}
void append_right_child(ast_node *parent, const ast_node *child)
{
parent->right = (ast_node *)child;
}
void append_middle_child(ast_node *parent, const ast_node *child)
ast_t *
new_ast ()
{
parent->middle = (ast_node *)child;
ast_t *ast = IALLOC (ast_t);
ast->id = next_id ();
return ast;
}

const ast_node *find_last_left_child(const ast_node *parent)
void
init_ast_ctx ()
{
ast_node *node = (ast_node *)parent;
while (node->left)
{
node = node->left;
}

return node;
ast_ctx.node_id = 0;
}
const ast_node *find_last_right_child(const ast_node *parent)
{
ast_node *node = (ast_node *)parent;
while (node->right)
{
node = node->right;
}

return node;
}
const ast_node *find_last_middle_child(const ast_node *parent)
node_id_t
next_id ()
{
ast_node *node = (ast_node *)parent;
while (node->middle)
{
node = node->middle;
}

return node;
return Id (ast_ctx++);
}

ast_const_node *new_ast_int_const(int i)
{
ast_const_node *n = IALLOC(ast_const_node);
n->data.i = i;
return n;
}
ast_const_node *new_ast_float_const(float f)
node_id_t
new_node_id (uint32_t id)
{
ast_const_node *n = IALLOC(ast_const_node);
n->data.f = f;
return n;
}
ast_const_node *new_ast_str_const(const char *s)
{
ast_const_node *n = IALLOC(ast_const_node);
n->data.s = (char *)s;
return n;
return (node_id_t){ .id = id };
}

ast_const_node *new_ast_bool_const(int b)
const char *
to_ast_string (ast_tag_t tag)
{
ast_const_node *n = IALLOC(ast_const_node);
n->data.i = b;

return n;
return ast_to_str[tag];
}

ast_identifier_node *new_identifier_node(symrec_t *symbol, type_t *type, int scope_level)
{
ast_identifier_node *node = IALLOC(ast_identifier_node);
node->scope_level = scope_level;
node->sym = symbol;
node->type = type;
return node;
}
int get_scope_level()
int
is_assignment_operator (ast_tag_t tag)
{
return scope_level;
return tag == AST_ASSIGN || tag == AST_ASSIGN_MUL || tag == AST_ASSIGN_DIV
|| tag == AST_ASSIGN_MOD || tag == AST_ASSIGN_ADD
|| tag == AST_ASSIGN_SUB || tag == AST_ASSIGN_LSHIFT
|| tag == AST_ASSIGN_RSHIFT || tag == AST_ASSIGN_AND
|| tag == AST_ASSIGN_OR || tag == AST_ASSIGN_XOR;
}
void inc_scope_level()
{
scope_level++;
}
void dec_scope_level()

int
is_binary_operator (ast_tag_t tag)
{
scope_level--;
return tag == AST_MUL || tag == AST_DIV || tag == AST_MOD || tag == AST_ADD
|| tag == AST_SUB || tag == AST_LSHIFT || tag == AST_RSHIFT
|| tag == AST_LT || tag == AST_GT || tag == AST_LEQ || tag == AST_GEQ
|| tag == AST_EQ || tag == AST_NEQ || tag == AST_AND || tag == AST_OR
|| tag == AST_XOR || tag == AST_LAND || tag == AST_LOR;
}
Loading