summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCian Bagshaw <cian@cianb.xyz>2022-08-12 22:00:59 +0100
committerCian Bagshaw <cian@cianb.xyz>2022-08-12 22:00:59 +0100
commitbb19326918a308adb70b1a5d7b330307ac2babd7 (patch)
tree4fabdbdf9934662707102874caaab745e9694047
downloadmodCompImp-master.tar.bz2
modCompImp-master.zip
Initial commitHEADmaster
Created module 'A' files, containing node and respective constructor functions prototypes and code, as described in chapter one.
-rw-r--r--.gitignore1
-rw-r--r--include/A.h55
-rw-r--r--src/A.c88
3 files changed, 144 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a007fea
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
build/*
diff --git a/include/A.h b/include/A.h
new file mode 100644
index 0000000..9c2db23
--- /dev/null
+++ b/include/A.h
@@ -0,0 +1,55 @@
1/* types, of pointers to corresponding structs */
2typedef struct A_stm_ *A_stm;
3typedef struct A_exp_ *A_exp;
4typedef struct A_expList_ *A_expList;
5
6/* binary operation */
7typedef enum { A_plus, A_minus, A_times, A_div } A_binop;
8
9/* statement */
10struct A_stm_ {
11 /* types: compound, assign, print */
12 enum { A_compoundStm, A_assignStm, A_printStm } type;
13 union {
14 struct { A_stm stm1, stm2; } compound;
15 struct { char *id; A_exp exp; } assign;
16 struct { A_expList exps; } print;
17 } u;
18};
19
20/* statement constrctor functions */
21A_stm A_CompoundStm ( A_stm stm1, A_stm stm2 );
22A_stm A_AssignStm ( char *id, A_exp exp );
23A_stm A_PrintStm ( A_expList exps );
24
25/* expression */
26struct A_exp_ {
27 /* types: id, number, operation, sequence */
28 enum { A_idExp, A_numExp, A_opExp, A_eseqExp } type;
29 union {
30 char *id;
31 int num;
32 struct { A_exp exp1; A_binop op; A_exp exp2; } op;
33 struct { A_stm stm; A_exp exp; } eseq;
34 } u;
35};
36
37/* expression constructor functions */
38A_exp A_IdExp ( char *id );
39A_exp A_NumExp ( int num );
40A_exp A_OpExp ( A_exp exp1, A_binop op, A_exp exp2 );
41A_exp A_EseqExp ( A_stm stm, A_exp exp );
42
43/* expression list */
44struct A_expList_ {
45 /* types: pair, last */
46 enum { A_pairExpList, A_lastExpList } type;
47 union {
48 struct { A_exp head; A_expList tail; } pair;
49 A_exp last;
50 } u;
51};
52
53/* expression list constructor functions */
54A_expList A_PairExpList (A_exp head, A_expList tail);
55A_expList A_LastExplist (A_exp last);
diff --git a/src/A.c b/src/A.c
new file mode 100644
index 0000000..751de8c
--- /dev/null
+++ b/src/A.c
@@ -0,0 +1,88 @@
1# include <stdlib.h>
2# include "A.h"
3
4/*
5 * node constructor functions
6 */
7
8/* A_CompoundStm - returns a compound statement of stm1, stm2 */
9A_stm A_CompoundStm (A_stm stm1, A_stm stm2) {
10 A_stm s = malloc(sizeof(*s));
11 s->type = A_compoundStm;
12 s->u.compound.stm1 = stm1;
13 s->u.compound.stm2 = stm2;
14 return s;
15}
16
17/* A_AssignStm - returns a assign statement of id, exp */
18A_stm A_AssignStm (char *id, A_exp exp) {
19 A_stm s = malloc(sizeof(*s));
20 s->type = A_assignStm;
21 s->u.assign.id = id;
22 s->u.assign.exp = exp;
23 return s;
24}
25
26/* A_PrintStm - returns a print statement of exps */
27A_stm A_PrintStm (A_expList exps) {
28 A_stm s = malloc(sizeof(*s));
29 s->type = A_printStm;
30 s->u.print.exps = exps;
31 return s;
32}
33
34/* A_IdExp - returns an id expression of id */
35A_exp A_IdExp (char *id) {
36 A_exp e = malloc(sizeof(*e));
37 e->type = A_idExp;
38 e->u.id = id;
39 return e;
40}
41
42/* A_NumExp - returns a number expression of num */
43A_exp A_NumExp (int num) {
44 A_exp e = malloc(sizeof(*e));
45 e->type = A_numExp;
46 e->u.num = num;
47 return e;
48}
49
50/* A_OpExp - returns an operation expression of exp1, op, exp2 */
51A_exp A_OpExp (A_exp exp1, A_binop op, A_exp exp2) {
52 A_exp e = malloc(sizeof(*e));
53 e->type = A_opExp;
54 e->u.op.exp1 = exp1;
55 e->u.op.op = op;
56 e->u.op.exp2 = exp2;
57 return e;
58}
59
60/* A_EseqExp - returns a seqeunce expression of stm, exp */
61A_exp A_EseqExp (A_stm stm, A_exp exp) {
62 A_exp e = malloc(sizeof(*e));
63 e->type = A_eseqExp;
64 e->u.eseq.stm = stm;
65 e->u.eseq.exp = exp;
66 return e;
67}
68
69/* A_PairExpList - returns a list expression pair of head, tail */
70A_expList A_PairExpList (A_exp head, A_expList tail) {
71 A_expList l = malloc(sizeof(*l));
72 l->type = A_pairExpList;
73 l->u.pair.head = head;
74 l->u.pair.tail = tail;
75 return l;
76}
77
78/* A_LastExpList - returns a expression list end of last */
79A_expList A_LastExpList (A_exp last) {
80 A_expList l = malloc(sizeof(*l));
81 l->type = A_lastExpList;
82 l->u.last = last;
83 return l;
84}
85
86int main (int argc, char** argv) {
87 return 0;
88}