Initial commit master
authorCian Bagshaw <cian@cianb.xyz>
Fri, 12 Aug 2022 21:00:59 +0000 (22:00 +0100)
committerCian Bagshaw <cian@cianb.xyz>
Fri, 12 Aug 2022 21:00:59 +0000 (22:00 +0100)
Created module 'A' files, containing node and respective constructor
functions prototypes and code, as described in chapter one.

.gitignore [new file with mode: 0644]
include/A.h [new file with mode: 0644]
src/A.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..a007fea
--- /dev/null
@@ -0,0 +1 @@
+build/*
diff --git a/include/A.h b/include/A.h
new file mode 100644 (file)
index 0000000..9c2db23
--- /dev/null
@@ -0,0 +1,55 @@
+/* types, of pointers to corresponding structs */
+typedef struct A_stm_ *A_stm;
+typedef struct A_exp_ *A_exp;
+typedef struct A_expList_ *A_expList;
+
+/* binary operation */
+typedef enum { A_plus, A_minus, A_times, A_div } A_binop;
+
+/* statement */
+struct A_stm_ {
+       /* types: compound, assign, print */
+       enum { A_compoundStm, A_assignStm, A_printStm } type;
+       union {
+               struct { A_stm stm1, stm2; }    compound;
+               struct { char *id; A_exp exp; } assign;
+               struct { A_expList exps; }              print;
+       } u;
+};
+
+/* statement constrctor functions */
+A_stm A_CompoundStm    ( A_stm stm1, A_stm stm2 );
+A_stm A_AssignStm      ( char *id, A_exp exp );
+A_stm A_PrintStm       ( A_expList exps );
+
+/* expression */
+struct A_exp_ {
+       /* types: id, number, operation, sequence */
+       enum { A_idExp, A_numExp, A_opExp, A_eseqExp } type;
+       union {
+                       char *id;
+                       int num;
+                       struct { A_exp exp1; A_binop op; A_exp exp2; } op;
+                       struct { A_stm stm; A_exp exp; } eseq;
+       } u;
+};
+
+/* expression constructor functions */
+A_exp A_IdExp  ( char *id );
+A_exp A_NumExp ( int num );
+A_exp A_OpExp  ( A_exp exp1, A_binop op, A_exp exp2 );
+A_exp A_EseqExp        ( A_stm stm, A_exp exp );
+
+/* expression list */
+struct A_expList_ {
+       /* types: pair, last */
+       enum { A_pairExpList, A_lastExpList } type;
+       union {
+               struct { A_exp head; A_expList tail; } pair;
+               A_exp last;
+       } u;
+};
+
+/* expression list constructor functions */
+A_expList A_PairExpList (A_exp head, A_expList tail);
+A_expList A_LastExplist (A_exp last);
diff --git a/src/A.c b/src/A.c
new file mode 100644 (file)
index 0000000..751de8c
--- /dev/null
+++ b/src/A.c
@@ -0,0 +1,88 @@
+# include <stdlib.h>
+# include "A.h"
+
+/*
+ *     node constructor functions
+ */
+
+/* A_CompoundStm - returns a compound statement of stm1, stm2 */
+A_stm A_CompoundStm (A_stm stm1, A_stm stm2) {
+       A_stm s                         = malloc(sizeof(*s));
+       s->type                         = A_compoundStm;
+       s->u.compound.stm1      = stm1;
+       s->u.compound.stm2      = stm2;
+       return s;
+}
+
+/* A_AssignStm - returns a assign statement of id, exp */
+A_stm A_AssignStm (char *id, A_exp exp) {
+       A_stm s                 = malloc(sizeof(*s));
+       s->type                 = A_assignStm;
+       s->u.assign.id  = id;
+       s->u.assign.exp = exp;
+       return s;
+}
+
+/* A_PrintStm - returns a print statement of exps */
+A_stm A_PrintStm (A_expList exps) {
+       A_stm s                 = malloc(sizeof(*s));
+       s->type                 = A_printStm;
+       s->u.print.exps = exps;
+       return s;
+}
+
+/* A_IdExp - returns an id expression of id */
+A_exp A_IdExp (char *id) {
+       A_exp e = malloc(sizeof(*e));
+       e->type = A_idExp;
+       e->u.id = id;
+       return e;
+}
+
+/* A_NumExp - returns a number expression of num */
+A_exp A_NumExp (int num) {
+       A_exp e         = malloc(sizeof(*e));
+       e->type         = A_numExp;
+       e->u.num        = num;
+       return e;
+}
+
+/* A_OpExp - returns an operation expression of exp1, op, exp2 */
+A_exp A_OpExp (A_exp exp1, A_binop op, A_exp exp2) {
+       A_exp e                 = malloc(sizeof(*e));
+       e->type                 = A_opExp;
+       e->u.op.exp1    = exp1;
+       e->u.op.op              = op;
+       e->u.op.exp2    = exp2;
+       return e;
+}
+
+/* A_EseqExp - returns a seqeunce expression of stm, exp */
+A_exp A_EseqExp (A_stm stm, A_exp exp) {
+       A_exp e                 = malloc(sizeof(*e));
+       e->type                 = A_eseqExp;
+       e->u.eseq.stm   = stm;
+       e->u.eseq.exp   = exp;
+       return e;
+}
+
+/* A_PairExpList - returns a list expression pair of head, tail */
+A_expList A_PairExpList (A_exp head, A_expList tail) {
+       A_expList l             = malloc(sizeof(*l));
+       l->type                 = A_pairExpList;
+       l->u.pair.head  = head;
+       l->u.pair.tail  = tail;
+       return l;
+}
+
+/* A_LastExpList - returns a expression list end of last */
+A_expList A_LastExpList (A_exp last) {
+       A_expList l     = malloc(sizeof(*l));
+       l->type         = A_lastExpList;
+       l->u.last       = last;
+       return l;
+}
+
+int main (int argc, char** argv) {
+       return 0;
+}