public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
70193166c8a749bae071bc4e521a5d40e0781629 blob 4120 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
 
/*
 * Copyright (C) 2024 olang maintainers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdint.h>

#include "arena.h"
#include "ast.h"
#include "string_view.h"

ast_node_t *
ast_new_program(arena_t *arena, ast_node_t *fn_def)
{
    ast_node_t *node = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
    assert(node);

    node->kind = AST_NODE_PROGRAM;
    ast_program_t *program = &node->as_program;

    program->fn = fn_def;

    return node;
}

ast_node_t *
ast_new_node_fn_def(arena_t *arena, string_view_t id, string_view_t return_type, ast_node_t *block)
{
    ast_node_t *node_fn_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
    assert(node_fn_def);

    node_fn_def->kind = AST_NODE_FN_DEF;
    ast_fn_definition_t *fn_def = &node_fn_def->as_fn_def;

    fn_def->id = id;
    fn_def->return_type = return_type;
    fn_def->block = block;

    return node_fn_def;
}

ast_node_t *
ast_new_node_var_def(arena_t *arena, string_view_t id, string_view_t type, ast_node_t *value)
{
    ast_node_t *node_var_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
    assert(node_var_def);

    node_var_def->kind = AST_NODE_VAR_DEF;
    ast_var_definition_t *var_def = &node_var_def->as_var_def;

    var_def->id = id;
    var_def->type = type;
    var_def->value = value;

    return node_var_def;
}

ast_node_t *
ast_new_node_bin_op(arena_t *arena, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs)
{
    ast_node_t *node_bin_op = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
    assert(node_bin_op);

    node_bin_op->kind = AST_NODE_BINARY_OP;
    node_bin_op->as_bin_op.kind = kind;
    node_bin_op->as_bin_op.lhs = lhs;
    node_bin_op->as_bin_op.rhs = rhs;

    return node_bin_op;
}

ast_node_t *
ast_new_node_literal_u32(arena_t *arena, uint32_t value)
{
    ast_node_t *node_literal = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
    assert(node_literal);

    node_literal->kind = AST_NODE_LITERAL;
    node_literal->as_literal.kind = AST_LITERAL_U32;
    node_literal->as_literal.as_u32 = value;

    return node_literal;
}

ast_node_t *
ast_new_node_ref(arena_t *arena, string_view_t id)
{
    ast_node_t *node_ref = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
    assert(node_ref);

    node_ref->kind = AST_NODE_REF;
    node_ref->as_ref.id = id;

    return node_ref;
}

ast_node_t *
ast_new_node_return_stmt(arena_t *arena, ast_node_t *expr)
{
    ast_node_t *node_return_stmt = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
    assert(node_return_stmt);

    node_return_stmt->kind = AST_NODE_RETURN_STMT;
    node_return_stmt->as_return_stmt.expr = expr;

    return node_return_stmt;
}

ast_node_t *
ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then, ast_node_t *_else)
{
    ast_node_t *node_if_stmt = arena_alloc(arena, sizeof(ast_node_t));
    assert(node_if_stmt);

    node_if_stmt->kind = AST_NODE_IF_STMT;
    node_if_stmt->as_if_stmt.cond = cond;
    node_if_stmt->as_if_stmt.then = then;
    node_if_stmt->as_if_stmt._else = _else;

    return node_if_stmt;
}

ast_node_t *
ast_new_node_block(arena_t *arena)
{
    ast_node_t *node_block = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
    assert(node_block);

    node_block->kind = AST_NODE_BLOCK;

    node_block->as_block.nodes = (list_t *)arena_alloc(arena, sizeof(list_t));
    assert(node_block->as_block.nodes);

    list_init(node_block->as_block.nodes, arena);

    return node_block;
}
debug log:

solving 7019316 ...
found 7019316 in https://git.johnnyrichard.com/olang.git

Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox