1 //////////////////////////////////////////////////////////////////////////////
2 // This file is part of Teyjus.                                             //
3 //                                                                          //
4 // Teyjus is free software: you can redistribute it and/or modify           //
5 // it under the terms of the GNU General Public License as published by     //
6 // the Free Software Foundation, either version 3 of the License, or        //
7 // (at your option) any later version.                                      //
8 //                                                                          //
9 // Teyjus is distributed in the hope that it will be useful,                //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of           //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
12 // GNU General Public License for more details.                             //
13 //                                                                          //
14 // You should have received a copy of the GNU General Public License        //
15 // along with Teyjus.  If not, see <http://www.gnu.org/licenses/>.          //
16 //////////////////////////////////////////////////////////////////////////////
17 /****************************************************************************/
18 /* File types.c. This file contains "abstract syntax" representation of     */
19 /* type skeletons that is used for parsing those in pervasives.in.          */
20 /****************************************************************************/
21 #include <stdlib.h>
22 #include "types.h"
23 #include "../util/util.h"
24 
mkSortType(char * name)25 Type mkSortType(char* name)
26 {
27     Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
28     rtPtr -> tag = SORT;
29     rtPtr -> data.sort = name;
30     return rtPtr;
31 }
32 
mkSkVarType(char * index)33 Type mkSkVarType(char* index)
34 {
35     Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
36     rtPtr -> tag = SKVAR;
37     rtPtr -> data.skvar = index;
38     return rtPtr;
39 }
40 
mkStrFuncType(char * name,char * arity)41 Type mkStrFuncType(char* name, char* arity)
42 {
43     Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
44     rtPtr -> tag = FUNC;
45     rtPtr -> data.func.name = name;
46     rtPtr -> data.func.arity = arity;
47     return rtPtr;
48 }
49 
50 
mkStrType(Type func,int arity,TypeList args)51 Type mkStrType(Type func, int arity, TypeList args)
52 {
53     Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
54     rtPtr -> tag = STR;
55     rtPtr -> data.str.functor = func;
56     rtPtr -> data.str.arity   = arity;
57     rtPtr -> data.str.args    = args;
58     return rtPtr;
59 }
60 
mkArrowType(Type lop,Type rop)61 Type mkArrowType(Type lop, Type rop)
62 {
63     Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
64     rtPtr -> tag = ARROW;
65     rtPtr -> data.arrow.lop = lop;
66     rtPtr -> data.arrow.rop = rop;
67     return rtPtr;
68 
69 }
70 
freeType(Type ty)71 void freeType(Type ty)
72 {
73     if (ty -> tag == SORT) free(ty->data.sort);
74     else if (ty -> tag == SKVAR) free(ty->data.skvar);
75     else if (ty -> tag == FUNC) {
76         free(ty->data.func.name);
77         free(ty->data.func.arity);
78     }
79     free(ty);
80 }
81 
82 
addItem(Type data,TypeList typeList)83 TypeList addItem(Type data, TypeList typeList)
84 {
85     TypeList new = (TypeList)UTIL_malloc(sizeof(TypeList_));
86     new -> oneType = data;
87     if (typeList) new -> next = typeList;
88     else new -> next = NULL;
89     typeList = new;
90     return typeList;
91 }
92 
addItemToEnd(TypeList typeList,Type data)93 TypeList addItemToEnd(TypeList typeList, Type data)
94 {
95     TypeList new = (TypeList)UTIL_malloc(sizeof(TypeList_));
96     new -> oneType = data;
97     new -> next = NULL;
98     if (typeList) {
99         TypeList temp = typeList;
100         while (temp -> next) temp = temp -> next;
101         temp -> next = new;
102     } else typeList = new;
103     return typeList;
104 }
105 
append(TypeList typeList1,TypeList typeList2)106 TypeList append(TypeList typeList1, TypeList typeList2)
107 {
108     if (typeList1) {
109         TypeList temp = typeList1;
110         while (temp -> next) temp = temp -> next;
111         temp -> next = typeList2;
112     } else typeList1 = typeList2;
113     return typeList1;
114 }
115