jitters: ast: add ast definitions
This commit is contained in:
parent
7e37ad6e2b
commit
451e34af8b
64
src/ast/ast.c
Normal file
64
src/ast/ast.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
#include "ast.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <err.h>
|
||||
|
||||
static void *xmalloc(size_t size)
|
||||
{
|
||||
void *ret = malloc(size);
|
||||
|
||||
if (ret == NULL)
|
||||
err(1, NULL);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct ast_node *make_num(int val)
|
||||
{
|
||||
struct ast_node *ret = xmalloc(sizeof(*ret));
|
||||
|
||||
ret->kind = NUM;
|
||||
ret->val.num = val;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct ast_node *make_unop(enum unop_kind op, struct ast_node *rhs)
|
||||
{
|
||||
struct ast_node *ret = xmalloc(sizeof(*ret));
|
||||
|
||||
ret->kind = UNOP;
|
||||
ret->val.un_op.op = op;
|
||||
ret->val.un_op.rhs = rhs;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct ast_node *make_binop(enum binop_kind op, struct ast_node *lhs,
|
||||
struct ast_node *rhs)
|
||||
{
|
||||
struct ast_node *ret = xmalloc(sizeof(*ret));
|
||||
|
||||
ret->kind = BINOP;
|
||||
ret->val.bin_op.op = op;
|
||||
ret->val.bin_op.lhs = lhs;
|
||||
ret->val.bin_op.rhs = rhs;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void destroy_ast(struct ast_node *ast)
|
||||
{
|
||||
if (!ast)
|
||||
return;
|
||||
|
||||
if (ast->kind == BINOP)
|
||||
{
|
||||
destroy_ast(ast->val.bin_op.lhs);
|
||||
destroy_ast(ast->val.bin_op.rhs);
|
||||
}
|
||||
else if (ast->kind == UNOP)
|
||||
destroy_ast(ast->val.un_op.rhs);
|
||||
|
||||
free(ast);
|
||||
}
|
59
src/ast/ast.h
Normal file
59
src/ast/ast.h
Normal file
|
@ -0,0 +1,59 @@
|
|||
#ifndef AST_H
|
||||
#define AST_H
|
||||
|
||||
// Forward declaration
|
||||
struct ast_node;
|
||||
|
||||
enum unop_kind
|
||||
{
|
||||
IDENTITY,
|
||||
NEGATE,
|
||||
};
|
||||
|
||||
enum binop_kind
|
||||
{
|
||||
PLUS,
|
||||
MINUS,
|
||||
TIMES,
|
||||
DIVIDE,
|
||||
};
|
||||
|
||||
struct unop_node
|
||||
{
|
||||
enum unop_kind op;
|
||||
struct ast_node *rhs;
|
||||
};
|
||||
|
||||
struct binop_node
|
||||
{
|
||||
enum binop_kind op;
|
||||
struct ast_node *lhs;
|
||||
struct ast_node *rhs;
|
||||
};
|
||||
|
||||
struct ast_node
|
||||
{
|
||||
enum node_kind
|
||||
{
|
||||
UNOP,
|
||||
BINOP,
|
||||
NUM,
|
||||
} kind;
|
||||
union ast_val
|
||||
{
|
||||
struct unop_node un_op;
|
||||
struct binop_node bin_op;
|
||||
int num;
|
||||
} val;
|
||||
};
|
||||
|
||||
struct ast_node *make_num(int val);
|
||||
|
||||
struct ast_node *make_unop(enum unop_kind op, struct ast_node *rhs);
|
||||
|
||||
struct ast_node *make_binop(enum binop_kind op, struct ast_node *lhs,
|
||||
struct ast_node *rhs);
|
||||
|
||||
void destroy_ast(struct ast_node *ast);
|
||||
|
||||
#endif /* !AST_H */
|
4
src/ast/local.am
Normal file
4
src/ast/local.am
Normal file
|
@ -0,0 +1,4 @@
|
|||
jitters_SOURCES += \
|
||||
%D%/ast.c \
|
||||
%D%/ast.h \
|
||||
$(NULL)
|
|
@ -14,4 +14,5 @@ AM_CPPFLAGS = -I$(top_builddir)/src -I$(srcdir)
|
|||
# Initialise those variables for use in included files
|
||||
jitters_LDADD =
|
||||
|
||||
include %D%/ast/local.am
|
||||
include %D%/parse/local.am
|
||||
|
|
Loading…
Reference in a new issue