Cthulhu  0.2.10
Cthulhu compiler collection
ast.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #pragma once
4 
5 #include "core/where.h"
6 
7 #include "cthulhu/tree/ops.h"
8 
9 #include <gmp.h>
10 
11 typedef struct node_t node_t;
12 typedef struct scan_t scan_t;
13 typedef struct vector_t vector_t;
14 
15 typedef enum pl0_type_t
16 {
17 #define PL0_TYPE(id, name) id,
18 #include "pl0/pl0.inc"
19 
20  ePl0Count
22 
23 typedef struct pl0_t
24 {
26  const node_t *node;
27 
28  union {
29  /* integer literal */
30  mpz_t digit;
31 
32  /* an identifier */
33  const char *ident;
34 
35  /* a procedure to call */
36  const char *procedure;
37 
38  /* a value to print */
39  struct pl0_t *print;
40 
41  /* a unary operation */
42  struct
43  {
45  struct pl0_t *operand;
46  };
47 
48  /* a binary or compare operation */
49  struct
50  {
51  struct pl0_t *lhs;
52  struct pl0_t *rhs;
53 
54  union {
57  };
58  };
59 
60  /* an assignment */
61  struct
62  {
63  const char *dst;
64  struct pl0_t *src;
65  };
66 
67  /* a conditional branch */
68  struct
69  {
70  struct pl0_t *cond;
71  struct pl0_t *then;
72  };
73 
74  /* statements */
76 
77  /* a declaration */
78  struct
79  {
80  const char *name;
81 
82  union {
83  /* a procedure */
84  struct
85  {
86  /* the procedures local variables */
88 
89  /* the procedure body */
91  };
92 
93  /* a variable */
94  struct pl0_t *value;
95  };
96  };
97 
98  /* an import */
100 
101  struct
102  {
103  /* all immutable globals in the program */
104  const vector_t *consts;
105 
106  /* all mutable globals in the program */
108 
109  /* all procedures in the program */
110  const vector_t *procs;
111 
112  /* the public name of this module, defaults to the file name */
113  const vector_t *mod;
114 
115  /* all modules imported by this module */
117 
118  /* the entry point function, if there is one */
119  struct pl0_t *entry;
120  };
121  };
122 } pl0_t;
123 
124 pl0_t *pl0_digit(scan_t *scan, where_t where, mpz_t digit);
125 pl0_t *pl0_ident(scan_t *scan, where_t where, const char *ident);
126 
129 
131 pl0_t *pl0_odd(scan_t *scan, where_t where, pl0_t *operand);
132 
133 pl0_t *pl0_print(scan_t *scan, where_t where, pl0_t *operand);
134 pl0_t *pl0_assign(scan_t *scan, where_t where, const char *dst, pl0_t *src);
135 pl0_t *pl0_call(scan_t *scan, where_t where, const char *procedure);
136 
137 pl0_t *pl0_branch(scan_t *scan, where_t where, pl0_t *cond, pl0_t *then);
138 pl0_t *pl0_loop(scan_t *scan, where_t where, pl0_t *cond, pl0_t *body);
139 
140 pl0_t *pl0_stmts(scan_t *scan, where_t where, vector_t *stmts);
141 
142 pl0_t *pl0_procedure(scan_t *scan, where_t where, const char *name, vector_t *locals, vector_t *body);
143 pl0_t *pl0_value(scan_t *scan, where_t where, const char *name, pl0_t *value);
144 
145 pl0_t *pl0_import(scan_t *scan, where_t where, vector_t *parts);
146 
147 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,
148  const vector_t *procs, pl0_t *entry);
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_type_t
Definition: ast.h:16
@ ePl0Count
Definition: ast.h:20
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
a position in a source file
Definition: node.h:23
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