#include #include #include #include #include #include "precedent.h" #include "hashtable.h" #include "parse.h" #include "eval.h" int main(int argc, char** argv) { int visual = 0; if (argc > 1) { for (int i = 1; i < argc; i++) { if (strcmp("--version", argv[i]) == 0) { printf("precedent, version 0.1\n"); printf("Copyright 2023 Kai Stevenson\n"); printf("All rights reserved. No part of the software may be sold\nor used in derivative works without explicit consent\n"); return 0; } else if (strcmp("--help", argv[i]) == 0) { printf("usage: precedent [--visualize] [--version] [--help]\n"); return 0; } else if (strcmp("--visualize", argv[i]) == 0) { visual = 1; } } } else { printf("usage: precedent [--visualize] [--version] [--help]\n"); return 0; } /* dirname may modify path, copy first */ char* path = argv[1]; char* pathCpy = malloc((strlen(path) + 1) * sizeof(char)); strcpy(pathCpy, path); char* dirName = dirname(pathCpy); char* baseDir = malloc((strlen(dirName) + 2) * sizeof(char)); strcpy(baseDir, dirName); strcat(baseDir, "/"); sym_t* finalSymbol = parse_symbol_final(read_file(path)); if (visual) { printf("--BEGIN VISUALIZATION--\n"); write_symbol_visual(finalSymbol, 0); printf("--END VISUALIZATION--\n"); } init_eval(); eval(finalSymbol); free(dirName); free(pathCpy); free(baseDir); return 0; } char* read_file(char* path) { FILE* file = fopen(path, "r"); fseek(file, 0, SEEK_END); unsigned long int length = ftell(file); fseek(file, 0, SEEK_SET); char* out = malloc((length + 1) * sizeof(char)); fread(out, length, 1, file); fclose(file); out[length] = '\0'; return out; } /* wtf */ char* preprocess(char* str, char* baseDir) { char* final = str; while (1) { int i = 0; while (str[i] != '@') { i++; if (str[i] == '\0') { printf("%s\n", final); return final; } } i++; char* includeName = malloc(20 * sizeof(char)); int f = 0; while (isalnum(str[i]) || str[i] == '/' || str[i] == '.' || str[i] == '_') { includeName[f] = str[i]; str[i] = ' '; i++; f++; } final = str; includeName[f] = '\0'; char* path = malloc((strlen(baseDir) + strlen(includeName) + 1) * sizeof(char)); strcpy(path, baseDir); strcat(path, includeName); char* toInclude = preprocess(read_file(path), baseDir); char* newStr = malloc((strlen(toInclude) + strlen(final) + 1) * sizeof(char)); strcpy(newStr, toInclude); strcat(newStr, final); /* free(includeName); free(path); free(str); free(toInclude); */ final = newStr; } }