school

thing1's amazing school repo
Log | Files | Refs | Submodules | README

util.c (3356B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <stdbool.h>
      5 #include <errno.h>
      6 #include <error.h>
      7 #include <ctype.h>
      8 
      9 #include "types.h"
     10 
     11 
     12 // functions for user
     13 void Die(); // brings down the program
     14 void *CheckedMalloc(long size); // CheckedMalloc checked with autofree
     15 void *CheckedRealloc(void *out, long size); // realloc checked with autofree
     16 literal *giveType(char *tok);
     17 
     18 
     19 void Die(){
     20 	fprintf(stderr, "error occured, bringing down the program\n");
     21 	exit(1);
     22 }
     23 
     24 void *CheckedMalloc(long size){
     25 	void *out = malloc(size);
     26 	if (out == NULL)
     27 		Die();
     28 	return out;
     29 }
     30 
     31 void *CheckedRealloc(void *orig, long size){
     32 	void *out = realloc(orig, size);
     33 	if (out == NULL)
     34 		Die();
     35 	return out;
     36 }
     37 
     38 I64 *isNum(char *str){
     39 	for (int i = 0; i < strlen(str); i++){
     40 		if (isdigit(str[i]) == 0 && str[i] != '-'){
     41 			return NULL;	
     42 		}
     43 	}
     44 	I64 *out = CheckedMalloc(sizeof(I64));
     45 	out->data = strtol(str, NULL, 10);
     46 	return out;
     47 }
     48 
     49 Float *isFloat(char *str){
     50 	for (int i = 0; i < strlen(str); i++){
     51 		if (isdigit(str[i]) == 0 && str[i] != '-' && str[i] != '.'){
     52 			return NULL;	
     53 		}
     54 	}
     55 	Float *out = CheckedMalloc(sizeof(Float));
     56 	out->data = strtod(str, NULL);
     57 	return out;
     58 }
     59 
     60 Char *isChar(char *str){
     61 	if (str[0] == '\'' && str[strlen(str)-1] == '\''){
     62 		str++;
     63 		str[strlen(str)-1] = '\0';
     64 		if (strlen(str) == 1){
     65 			Char *out = CheckedMalloc(sizeof(Char));
     66 			out->data = str[0];
     67 			return out;
     68 		}
     69 		else return NULL;
     70 	}
     71 	return NULL;
     72 }
     73 
     74 Arr *isArr(char *str){
     75 	char *strbak = CheckedMalloc(strlen(str));
     76 	memcpy(strbak, str, strlen(str)+1);
     77 	if (strbak[0] == '[' && strbak[strlen(strbak)-1] == ']'){
     78 		Arr *arr = CheckedMalloc(sizeof(Arr));
     79 		arr->arr = CheckedMalloc(sizeof(literal));
     80 		strbak++;
     81 		strbak[strlen(strbak)-1] = '\0';
     82 		char *tok;
     83 		int length = 0;
     84 		size_t cCount = 1;
     85 		tok = strtok(strbak, ",");
     86 		while (tok != NULL){
     87 			cCount++;
     88 			arr = CheckedRealloc(arr, sizeof(Arr) * cCount);
     89 			arr->arr[cCount - 2] = *giveType(tok);
     90 			tok = strtok(NULL, ",");
     91 			length++;
     92 		}
     93 		arr->len = length;
     94 		free(strbak - 1);
     95 		return arr;
     96 	} else {
     97 		free(strbak);
     98 		return NULL;
     99 	}
    100 }
    101 
    102 Vdef *isVdef(char *str){
    103 	if (strchr(str, ':') != NULL){
    104 		Vdef *out = CheckedMalloc(sizeof(Vdef));
    105 		char *type;
    106 
    107 		char *tmp = strtok(str, ":");
    108 		out->id = CheckedMalloc(strlen(tmp)+1);
    109 		memcpy(out->id, tmp, strlen(tmp)+1);
    110 		
    111 		type = strtok(NULL, ":");	
    112 		if (strcmp(type, "i64") == 0) out->type = TI64;
    113 		else if (strcmp(type, "i32") == 0) out->type = TI32;
    114 		else if (strcmp(type, "u64") == 0) out->type = TU64;
    115 		else if (strcmp(type, "u32") == 0) out->type = TU32;
    116 
    117 		else if (strcmp(type, "char") == 0) out->type = TChar;
    118 		else if (strcmp(type, "float") == 0) out->type = Tfloat;
    119 		// do something with arrays here
    120 		else {
    121 			fprintf(stderr, "%s is not a valid data type\n", type);
    122 			Die();
    123 		}
    124 
    125 		return out;
    126 	}
    127 	return NULL;
    128 }
    129 
    130 literal *giveType(char *tok){
    131 	literal *out = CheckedMalloc(sizeof(literal));
    132 	Arr *arr = isArr(tok);
    133 	I64 *i64 = isNum(tok);
    134 	Float *fl = isFloat(tok);
    135 	Char *ch = isChar(tok);
    136 	Vdef *vdef = isVdef(tok);
    137 
    138 	if (arr != NULL){
    139 		out->arr = arr;
    140 	} else if (i64 != NULL){
    141 		out->i64 = i64;
    142 	} else if (fl != NULL){
    143 		out->fl = fl;
    144 	} else if (ch != NULL){
    145 		out->ch = ch;
    146 	}else if (vdef != NULL){
    147 		out->vdef = vdef;
    148 	} else {
    149 		fprintf(stderr, "data %s could not be typed\n", tok);
    150 		Die();
    151 	}
    152 	return out;
    153 }