school

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

ast.c (2961B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 #include "astg.h"
      6 
      7 void getBrackets(char *in, int bpos, char *out){ 
      8 	// gets the content of the brackets that open at bpos
      9 	if (in[0] != '(' && in[0] != '[')
     10 		out = NULL;
     11 
     12 	char *input = malloc(strlen(in) + 1); // cpy in for mem safety
     13 	char *Pinput = input;
     14 	memcpy(input, in, strlen(in) + 1);
     15 
     16 	int i = 0;
     17 	while (i != bpos){
     18 		input++;
     19 		i++;
     20 	}
     21 	i = 0;
     22 	int depth = 0;
     23 	while (input[0] != '\0'){ // loop through input
     24 		out[i] = input[0];
     25 		if (input[0] == '(' || input[0] == '[')
     26 			depth++;
     27 		if (input[0] == ')' || input[0] == ']')
     28 			depth--;
     29 		if (depth == 0){
     30 			i++;
     31 			break;
     32 		}
     33 
     34 		input++;
     35 		i++;
     36 	}
     37 
     38 	out[i] = '\0';
     39 	free(Pinput);
     40 }
     41 
     42 int getContents(char *brackets){ // get all the content in brackets
     43 	int i = 0;
     44 	char *num = malloc(strlen(brackets));
     45 	while (brackets[0] != '\0'){
     46 		if (brackets[0] != '[' && brackets[0] != ']'){
     47 			num[i] = brackets[0];
     48 			i++;
     49 		}
     50 		brackets++;
     51 	}
     52 	num[i] = '\0';
     53 	int out = atoi(num);
     54 	free(num);
     55 	return out;
     56 }
     57 
     58 ast_node *genAst(char *expression){
     59 	ast_node *out = malloc(sizeof(ast_node));
     60 	
     61 	/* take the expression
     62 	 * get the first operation from it
     63 	 * get the first number from after the expression
     64 	 * if that number is another expression{
     65 	 * 	grab everything inside its braket and then call this function on it
     66 	 * }
     67 	 * get the second number from the expression
     68 	 * if that number is another expression{
     69 	 * 	grab everything inside its braket and then call this function on it
     70 	 * }
     71 	 */
     72 
     73 	int i = 0, j = 0;
     74 	int currentTokenVal = -1;
     75 
     76 	char *currentToken = malloc(strlen(expression)+1);
     77 	while (expression[i] != '\0'){
     78 		if (expression[i] == '(' || expression[i] == '['){
     79 			getBrackets(expression, i, currentToken);
     80 			currentTokenVal++;
     81 			
     82 			int depth = 0;
     83 
     84 			if (i != 0){
     85 				while (expression[i] != '\0'){
     86 					if (expression[i] == '(' || expression[i] == '[')
     87 						depth++;
     88 					if (expression[i] == ')' || expression[i] == ']')
     89 						depth--;
     90 					if (depth == 0){
     91 						break;
     92 					}
     93 					i++;
     94 				}
     95 			}
     96 			
     97 			if (currentTokenVal == 0){
     98 				switch (currentToken[1]) {
     99 					case '+':
    100 						out->operation= ADD;
    101 						break;
    102 					case '-':
    103 						out->operation = SUB;
    104 						break;
    105 					case '*':
    106 						out->operation = MUL;
    107 						break;
    108 					case '/':
    109 						out->operation = DIV;
    110 						break;
    111 				}
    112 			}
    113 
    114 			if (currentTokenVal == 1){
    115 				if (currentToken[0] == '(')
    116 					out->left = genAst(currentToken);
    117 				else {
    118 					out->realLeft = getContents(currentToken);
    119 					out->left = NULL;
    120 				}
    121 			} else if (currentTokenVal == 2){ 
    122 				if (currentToken[0] == '(')
    123 					out->right= genAst(currentToken);
    124 				else {
    125 					out->realRight = getContents(currentToken);
    126 					out->right = NULL;
    127 				}
    128 			}
    129 		} 
    130 	i++;
    131 	}
    132 	free(currentToken);	
    133 
    134 	return out;
    135 }
    136 
    137 int main(int argc, char **argv){
    138 	if (argc < 2) // die if no argument given
    139 		exit(1);
    140 
    141 	ast_node *head = genAst(argv[1]);
    142 
    143 	printf("%d\n", exec(head));
    144 
    145 	freeAst(head);
    146 }