school

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

tokenizer.c (1805B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include<string.h>
      4 
      5 #include "util.h"
      6 #include "appendsnprintf.h"
      7 
      8 typedef struct astNode {
      9 	char *func;
     10 	char *args[8];
     11 	struct astNode *children[8];
     12 } astNode;
     13 
     14 //# reads a block of code from src, until char, outputting to dst, it allows for brackets so it stays on the same depth
     15 static int readuntil(char *src, char c, char *dst){ 
     16 	int ptr = 0;
     17 	int depth = 0;
     18 	int i = 0;
     19 
     20 	while ((src[i] != c || depth != 0) && src[i] != '\0'){
     21 		if (c == ')'){
     22 			if (src[i] == '('){
     23 				depth++;
     24 			}else if (src[i] == ')'){
     25 				depth--;
     26 			}
     27 		}
     28 		dst[ptr] = src[i];
     29 		ptr++;
     30 		i++;
     31 	}
     32 	
     33 	dst[ptr] = '\0';
     34 
     35 	return i;
     36 }
     37 
     38 //# this function will converts one line of zpy into an astNode, which can be compiled
     39 astNode *tokenize(char *line){ // asume the first set of brackets have been stripped
     40 	astNode *head = malloc(sizeof(astNode));
     41 	head->func = NULL;
     42 	for (int i = 0; i < 8; i++){
     43 		head->args[i] = NULL;
     44 		head->children[i] = NULL;
     45 	}	
     46 
     47 	int depth = 0;
     48 	int argCount = 0;
     49 	int i = 0;
     50 
     51 top:
     52 	for (;i < strlen(line); i++){
     53 		char *chunk = malloc(strlen(line));
     54 		if (line[i] == ')'){ 
     55 			i++; 
     56 			goto top;
     57 		}
     58 		if (line[i] == '('){
     59 			i++;
     60 			i += readuntil(&line[i], ')', chunk); // reads a nested function
     61 			i++;
     62 			head->children[argCount] = tokenize(chunk);
     63 			argCount++;
     64 		}else { 
     65 			if (line[i] == '"'){
     66 				i += readuntil(&line[i+1], '"', chunk); // reads a comptime string 
     67 				i++;
     68 				char *tmp = malloc(strlen(chunk)+2);
     69 				tmp[0] = '"';
     70 				tmp[1] = '\0';
     71 				strcat(tmp, chunk);
     72 				strcat(tmp, "\"");
     73 				chunk = tmp;
     74 
     75 			}
     76 			else {
     77 				i += readuntil(&line[i], ' ', chunk); // reads func name or arg
     78 			}
     79 			if (head->func == NULL){
     80 				head->func = chunk;
     81 			} else{
     82 				head->args[argCount] = chunk;
     83 				argCount++;
     84 			} 
     85 		}
     86 	}
     87 
     88 	return head;
     89 }