Cthulhu  0.2.10
Cthulhu compiler collection
ast.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #include "pl0/ast.h"
4 
5 #include "cthulhu/broker/scan.h"
6 
7 #include "base/panic.h"
8 #include "arena/arena.h"
9 #include "scan/node.h"
10 
11 pl0_t *pl0_new(scan_t *scan, where_t where, pl0_type_t type)
12 {
13  CTASSERT(scan != NULL);
14  arena_t *arena = ctx_get_ast_arena(scan);
15 
16  pl0_t *node = ARENA_MALLOC(sizeof(pl0_t), "pl0", scan, arena);
17  node->node = node_new(scan, where);
18  node->type = type;
19 
20  ARENA_IDENTIFY(node->node, "node", node, arena);
21 
22  return node;
23 }
24 
25 pl0_t *pl0_digit(scan_t *scan, where_t where, mpz_t digit)
26 {
27  pl0_t *node = pl0_new(scan, where, ePl0Digit);
28  mpz_init_set(node->digit, digit);
29  return node;
30 }
31 
32 pl0_t *pl0_ident(scan_t *scan, where_t where, const char *ident)
33 {
34  pl0_t *node = pl0_new(scan, where, ePl0Ident);
35  node->ident = ident;
36  return node;
37 }
38 
39 pl0_t *pl0_binary(scan_t *scan, where_t where, binary_t binary, pl0_t *lhs, pl0_t *rhs)
40 {
41  pl0_t *node = pl0_new(scan, where, ePl0Binary);
42  node->binary = binary;
43  node->lhs = lhs;
44  node->rhs = rhs;
45  return node;
46 }
47 
48 pl0_t *pl0_compare(scan_t *scan, where_t where, compare_t compare, pl0_t *lhs, pl0_t *rhs)
49 {
50  pl0_t *node = pl0_new(scan, where, ePl0Compare);
51  node->compare = compare;
52  node->lhs = lhs;
53  node->rhs = rhs;
54  return node;
55 }
56 
57 pl0_t *pl0_unary(scan_t *scan, where_t where, unary_t unary, pl0_t *operand)
58 {
59  pl0_t *node = pl0_new(scan, where, ePl0Unary);
60  node->unary = unary;
61  node->operand = operand;
62  return node;
63 }
64 
65 pl0_t *pl0_odd(scan_t *scan, where_t where, pl0_t *operand)
66 {
67  pl0_t *node = pl0_new(scan, where, ePl0Odd);
68  node->operand = operand;
69  return node;
70 }
71 
72 pl0_t *pl0_print(scan_t *scan, where_t where, pl0_t *operand)
73 {
74  pl0_t *node = pl0_new(scan, where, ePl0Print);
75  node->print = operand;
76  return node;
77 }
78 
79 pl0_t *pl0_assign(scan_t *scan, where_t where, const char *dst, pl0_t *src)
80 {
81  pl0_t *node = pl0_new(scan, where, ePl0Assign);
82  node->dst = dst;
83  node->src = src;
84  return node;
85 }
86 
87 pl0_t *pl0_call(scan_t *scan, where_t where, const char *procedure)
88 {
89  pl0_t *node = pl0_new(scan, where, ePl0Call);
90  node->procedure = procedure;
91  return node;
92 }
93 
94 pl0_t *pl0_branch(scan_t *scan, where_t where, pl0_t *cond, pl0_t *then)
95 {
96  pl0_t *node = pl0_new(scan, where, ePl0Branch);
97  node->cond = cond;
98  node->then = then;
99  return node;
100 }
101 
102 pl0_t *pl0_loop(scan_t *scan, where_t where, pl0_t *cond, pl0_t *body)
103 {
104  pl0_t *node = pl0_new(scan, where, ePl0Loop);
105  node->cond = cond;
106  node->then = body;
107  return node;
108 }
109 
110 pl0_t *pl0_stmts(scan_t *scan, where_t where, vector_t *stmts)
111 {
112  pl0_t *node = pl0_new(scan, where, ePl0Stmts);
113  node->stmts = stmts;
114  return node;
115 }
116 
117 pl0_t *pl0_procedure(scan_t *scan, where_t where, const char *name, vector_t *locals, vector_t *body)
118 {
119  pl0_t *node = pl0_new(scan, where, ePl0Procedure);
120  node->name = name;
121  node->locals = locals;
122  node->body = body;
123  return node;
124 }
125 
126 pl0_t *pl0_value(scan_t *scan, where_t where, const char *name, pl0_t *value)
127 {
128  pl0_t *node = pl0_new(scan, where, ePl0Value);
129  node->name = name;
130  node->value = value;
131  return node;
132 }
133 
134 pl0_t *pl0_import(scan_t *scan, where_t where, vector_t *parts)
135 {
136  pl0_t *node = pl0_new(scan, where, ePl0Import);
137  node->path = parts;
138  return node;
139 }
140 
141 pl0_t *pl0_module(scan_t *scan, where_t where, const vector_t *mod, const vector_t *imports, const vector_t *consts, const vector_t *globals,
142  const vector_t *procs, pl0_t *entry)
143 {
144  pl0_t *node = pl0_new(scan, where, ePl0Module);
145  node->mod = mod;
146  node->imports = imports;
147  node->consts = consts;
148  node->globals = globals;
149  node->procs = procs;
150  node->entry = entry;
151  return node;
152 }
CT_BROKER_API arena_t * ctx_get_ast_arena(const scan_t *scan)
Definition: context.c:32
CT_NODISCARD CT_SCAN_API node_t * node_new(const scan_t *scan, where_t where)
create a new node on the heap
Definition: node.c:40
#define ARENA_IDENTIFY(ptr, name, parent, arena)
rename and reparent a pointer in a custom allocator
Definition: arena.h:409
#define ARENA_MALLOC(size, name, parent, arena)
allocate memory from a custom allocator
Definition: arena.h:392
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
binary_t
all binary operators
Definition: ops.h:32
unary_t
all unary operators
Definition: ops.h:48
compare_t
all comparison operators
Definition: ops.h:40
pl0_t * pl0_assign(scan_t *scan, where_t where, const char *dst, pl0_t *src)
Definition: ast.c:79
pl0_t * pl0_unary(scan_t *scan, where_t where, unary_t unary, pl0_t *operand)
Definition: ast.c:57
pl0_t * pl0_odd(scan_t *scan, where_t where, pl0_t *operand)
Definition: ast.c:65
pl0_t * pl0_digit(scan_t *scan, where_t where, mpz_t digit)
Definition: ast.c:25
pl0_t * pl0_import(scan_t *scan, where_t where, vector_t *parts)
Definition: ast.c:134
pl0_t * pl0_binary(scan_t *scan, where_t where, binary_t binary, pl0_t *lhs, pl0_t *rhs)
Definition: ast.c:39
pl0_t * pl0_new(scan_t *scan, where_t where, pl0_type_t type)
Definition: ast.c:11
pl0_t * pl0_call(scan_t *scan, where_t where, const char *procedure)
Definition: ast.c:87
pl0_t * pl0_procedure(scan_t *scan, where_t where, const char *name, vector_t *locals, vector_t *body)
Definition: ast.c:117
pl0_t * pl0_loop(scan_t *scan, where_t where, pl0_t *cond, pl0_t *body)
Definition: ast.c:102
pl0_t * pl0_print(scan_t *scan, where_t where, pl0_t *operand)
Definition: ast.c:72
pl0_t * pl0_compare(scan_t *scan, where_t where, compare_t compare, pl0_t *lhs, pl0_t *rhs)
Definition: ast.c:48
pl0_t * pl0_module(scan_t *scan, where_t where, const vector_t *mod, const vector_t *imports, const vector_t *consts, const vector_t *globals, const vector_t *procs, pl0_t *entry)
Definition: ast.c:141
pl0_t * pl0_stmts(scan_t *scan, where_t where, vector_t *stmts)
Definition: ast.c:110
pl0_t * pl0_value(scan_t *scan, where_t where, const char *name, pl0_t *value)
Definition: ast.c:126
pl0_t * pl0_branch(scan_t *scan, where_t where, pl0_t *cond, pl0_t *then)
Definition: ast.c:94
pl0_t * pl0_ident(scan_t *scan, where_t where, const char *ident)
Definition: ast.c:32
pl0_type_t
Definition: ast.h:16
an allocator object
Definition: arena.h:86
Definition: ast.h:24
struct pl0_t * print
Definition: ast.h:39
const char * procedure
Definition: ast.h:36
unary_t unary
Definition: ast.h:44
const char * name
Definition: ast.h:80
pl0_type_t type
Definition: ast.h:25
const vector_t * mod
Definition: ast.h:113
const char * ident
Definition: ast.h:33
struct pl0_t * lhs
Definition: ast.h:51
vector_t * path
Definition: ast.h:99
struct pl0_t * value
Definition: ast.h:94
vector_t * locals
Definition: ast.h:87
const vector_t * globals
Definition: ast.h:107
const vector_t * consts
Definition: ast.h:104
const vector_t * imports
Definition: ast.h:116
struct pl0_t * cond
Definition: ast.h:70
vector_t * stmts
Definition: ast.h:75
const node_t * node
Definition: ast.h:26
binary_t binary
Definition: ast.h:55
const vector_t * procs
Definition: ast.h:110
mpz_t digit
Definition: ast.h:30
struct pl0_t * src
Definition: ast.h:64
const char * dst
Definition: ast.h:63
vector_t * body
Definition: ast.h:90
compare_t compare
Definition: ast.h:56
struct pl0_t * entry
Definition: ast.h:119
struct pl0_t * then
Definition: ast.h:71
struct pl0_t * operand
Definition: ast.h:45
struct pl0_t * rhs
Definition: ast.h:52
a source file scanner
Definition: scan.h:24
a generic vector of pointers
Definition: vector.c:16
a location inside a scanner locations are inclusive and 0-based
Definition: where.h:23