Add misc dir
This commit is contained in:
175
misc/io.c
Normal file
175
misc/io.c
Normal file
@@ -0,0 +1,175 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include "util.h"
|
||||
#include "io.h"
|
||||
|
||||
#include <limits.h>
|
||||
#ifndef PATH_MAX
|
||||
#ifdef __linux__
|
||||
#include <linux/limits.h>
|
||||
#else
|
||||
#define PATH_MAX 512
|
||||
#endif
|
||||
#endif
|
||||
|
||||
char* strtail(char* str, int len, int count) {
|
||||
int index = len - count;
|
||||
if (index <= 0) return str;
|
||||
return str + (sizeof(char) * (index));
|
||||
}
|
||||
|
||||
char* strhead(char* str, int count) {
|
||||
return str + (sizeof(char) * count);
|
||||
}
|
||||
|
||||
char* trimstr(char* str, int len, int end) {
|
||||
int count = len - end;
|
||||
char oldchar = str[count];
|
||||
str[count] = '\0';
|
||||
char* newstr = (char*)malloc(sizeof(char) * (1 + count));
|
||||
strcpy(newstr, str);
|
||||
str[count] = oldchar;
|
||||
return newstr;
|
||||
}
|
||||
|
||||
char* getname(char* f, int len) {
|
||||
int startind = 0;
|
||||
int endind = len - 1;
|
||||
bool readsmt = false;
|
||||
|
||||
for(int i = endind; i >= 0; i--) {
|
||||
if(f[i] == '/') {
|
||||
if(!readsmt) {
|
||||
endind = i-1;
|
||||
f[i] = '\0';
|
||||
continue;
|
||||
}
|
||||
startind = i+1;
|
||||
break;
|
||||
}
|
||||
readsmt = true;
|
||||
}
|
||||
|
||||
int sz = sizeof(char)*(endind - startind + 2);
|
||||
char* startstr = strhead(f, startind);
|
||||
char* retstr = (char*)malloc(sz);
|
||||
snprintf(retstr, sz, "%s", startstr);
|
||||
return retstr;
|
||||
}
|
||||
|
||||
char* getfullname(char* fname, int fnamelen, char* dirname, int dirlen) {
|
||||
int sz = sizeof(char)*(fnamelen+dirlen+2);
|
||||
char* fullname = (char*)malloc(sz);
|
||||
sprintf(fullname, "%s/%s", dirname, fname);
|
||||
return fullname;
|
||||
}
|
||||
|
||||
bool isdotjack(char* f, int len) {
|
||||
const char* ext = ".jack";
|
||||
return strcmp(strtail(f, len, strlen(ext)), ext) == 0;
|
||||
}
|
||||
|
||||
bool isdir(char* f, int len) {
|
||||
bool readsmt = false;
|
||||
for(int i = len-1; i >= 0; i--) {
|
||||
if(f[i] == '.')
|
||||
if(readsmt)
|
||||
return false;
|
||||
else
|
||||
continue;
|
||||
if(f[i] == '/')
|
||||
return 1;
|
||||
readsmt = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
char* getoutname(char* fullname, int len) {
|
||||
char* trimmed = trimstr(fullname, len, 4);
|
||||
int sz = sizeof(char) * (len-1);
|
||||
char* outname = (char*)malloc(sz);
|
||||
sprintf(outname, "%svm", trimmed);
|
||||
return outname;
|
||||
}
|
||||
|
||||
FILELIST* addfile(FILELIST* l, char* fullname, char* name) {
|
||||
FILELIST* new = (FILELIST*)malloc(sizeof(FILELIST));
|
||||
new->name = name;
|
||||
new->fullname = fullname;
|
||||
new->next = l;
|
||||
new->outname = getoutname(fullname, strlen(fullname));
|
||||
return new;
|
||||
}
|
||||
|
||||
FILELIST* getfilesfromdir(char* dir) {
|
||||
FILELIST* filelist = NULL;
|
||||
DIR* d = opendir(dir);
|
||||
|
||||
if(d == NULL) {
|
||||
eprintf("Error while opening directory '%s': %s\n", dir, strerror(errno));
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
int len = strlen(dir);
|
||||
struct dirent* thisfile;
|
||||
while(thisfile = readdir(d), thisfile != NULL) {
|
||||
int thislen = strlen(thisfile->d_name);
|
||||
if(isdotjack(thisfile->d_name, thislen)) {
|
||||
char* fullname = getfullname(thisfile->d_name, thislen, dir, len);
|
||||
char* name = ezheapstr(thisfile->d_name);
|
||||
filelist = addfile(filelist, fullname, name);
|
||||
}
|
||||
}
|
||||
|
||||
closedir(d);
|
||||
|
||||
if(filelist == NULL) {
|
||||
eprintf("Directory '%s' doesn't have any .jack file\n", dir);
|
||||
exit(1);
|
||||
}
|
||||
return filelist;
|
||||
}
|
||||
|
||||
FILELIST* getsinglefile(char* file) {
|
||||
int len = strlen(file);
|
||||
if(isdotjack(file, len)){
|
||||
char* name = getname(file, len);
|
||||
char* fullname = heapstr(file, len);
|
||||
|
||||
FILE* input = fopen(fullname, "r");
|
||||
if(input == NULL) {
|
||||
eprintf("Error while reading file '%s': %s\n", file, strerror(errno));
|
||||
exit(errno);
|
||||
}
|
||||
fclose(input);
|
||||
|
||||
return addfile(NULL, fullname, name);
|
||||
}
|
||||
else {
|
||||
eprintf("Input file must be named like 'Xxx.vm'\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
FILELIST* getfiles(char* input) {
|
||||
int inplen = strlen(input);
|
||||
bool isitdir = isdir(input, inplen);
|
||||
|
||||
if(isitdir)
|
||||
return getfilesfromdir(input);
|
||||
else
|
||||
return getsinglefile(input);
|
||||
}
|
||||
|
||||
void freefilelist(FILELIST* fs) {
|
||||
free(fs->name);
|
||||
free(fs->fullname);
|
||||
if(fs->next != NULL)
|
||||
freefilelist(fs->next);
|
||||
}
|
14
misc/io.h
Normal file
14
misc/io.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef IO_H
|
||||
#define IO_H
|
||||
|
||||
typedef struct flist {
|
||||
char* name;
|
||||
char* fullname;
|
||||
char* outname;
|
||||
struct flist* next;
|
||||
} FILELIST;
|
||||
|
||||
|
||||
FILELIST* getfiles(char* input);
|
||||
void freefilelist(FILELIST* fs);
|
||||
#endif
|
159
misc/os.c
Normal file
159
misc/os.c
Normal file
@@ -0,0 +1,159 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "os.h"
|
||||
#include "util.h"
|
||||
|
||||
#define MATH_COUNT 5
|
||||
SUBROUTDEC* math[MATH_COUNT];
|
||||
#define STRING_COUNT 12
|
||||
SUBROUTDEC* stringclass[STRING_COUNT];
|
||||
#define ARRAY_COUNT 2
|
||||
SUBROUTDEC* array[ARRAY_COUNT];
|
||||
#define OUTPUT_COUNT 6
|
||||
SUBROUTDEC* output[OUTPUT_COUNT];
|
||||
#define SCREEN_COUNT 6
|
||||
SUBROUTDEC* screen[SCREEN_COUNT];
|
||||
#define KEYBOARD_COUNT 4
|
||||
SUBROUTDEC* keyboard[KEYBOARD_COUNT];
|
||||
#define MEMORY_COUNT 4
|
||||
SUBROUTDEC* memory[MEMORY_COUNT];
|
||||
#define SYS_COUNT 3
|
||||
SUBROUTDEC* sys[SYS_COUNT];
|
||||
|
||||
CLASS* mkclass(const char* name) {
|
||||
CLASS* class = (CLASS*)malloc(sizeof(CLASS));
|
||||
class->name = ezheapstr(name);
|
||||
return class;
|
||||
}
|
||||
|
||||
SUBROUTDEC* mkdec(const char* class, SUBROUTCLASS subroutclass, char* type, const char* name) {
|
||||
SUBROUTDEC* dec = (SUBROUTDEC*)malloc(sizeof(SUBROUTDEC));
|
||||
dec->class = mkclass(class);
|
||||
dec->subroutclass = subroutclass;
|
||||
dec->type = type;
|
||||
dec->name = ezheapstr(name);
|
||||
return dec;
|
||||
}
|
||||
|
||||
void populatemath() {
|
||||
math[0] = mkdec("Math", function, "int", "multiply");
|
||||
math[1] = mkdec("Math", function, "int", "divide");
|
||||
math[2] = mkdec("Math", function, "int", "min");
|
||||
math[3] = mkdec("Math", function, "int", "max");
|
||||
math[4] = mkdec("Math", function, "int", "sqrt");
|
||||
}
|
||||
|
||||
void populatestringclass() {
|
||||
stringclass[0] = mkdec("String", constructor, "String", "new");
|
||||
stringclass[1] = mkdec("String", method, "int", "dispose");
|
||||
stringclass[2] = mkdec("String", method, "int", "length");
|
||||
stringclass[3] = mkdec("String", method, "char", "charAt");
|
||||
stringclass[4] = mkdec("String", method, "void", "setCharAt");
|
||||
stringclass[5] = mkdec("String", method, "String", "appendChar");
|
||||
stringclass[6] = mkdec("String", method, "void", "eraseLastChar");
|
||||
stringclass[7] = mkdec("String", method, "int", "intValue");
|
||||
stringclass[8] = mkdec("String", method, "void", "setInt");
|
||||
stringclass[9] = mkdec("String", function, "char", "backSpace");
|
||||
stringclass[10] = mkdec("String", function, "char", "doubleQuote");
|
||||
stringclass[11] = mkdec("String", function, "char", "newLine");
|
||||
}
|
||||
|
||||
void populatearray() {
|
||||
array[0] = mkdec("Array", function, "Array", "new");
|
||||
array[1] = mkdec("Array", method, "void", "dispose");
|
||||
}
|
||||
|
||||
void populateoutput() {
|
||||
output[0] = mkdec("Output", function, "void", "moveCursor");
|
||||
output[1] = mkdec("Output", function, "void", "printChar");
|
||||
output[2] = mkdec("Output", function, "void", "printString");
|
||||
output[3] = mkdec("Output", function, "void", "printInt");
|
||||
output[4] = mkdec("Output", function, "void", "printLn");
|
||||
output[5] = mkdec("Output", function, "void", "backSpace");
|
||||
}
|
||||
|
||||
void populatescreen() {
|
||||
screen[0] = mkdec("Screen", function, "void", "clearScreen");
|
||||
screen[1] = mkdec("Screen", function, "void", "setColor");
|
||||
screen[2] = mkdec("Screen", function, "void", "drawPixel");
|
||||
screen[3] = mkdec("Screen", function, "void", "drawLine");
|
||||
screen[4] = mkdec("Screen", function, "void", "drawRectangle");
|
||||
screen[5] = mkdec("Screen", function, "void", "drawCircle");
|
||||
}
|
||||
|
||||
void populatekeyboard() {
|
||||
keyboard[0] = mkdec("Keyboard", function, "char", "keyPressed");
|
||||
keyboard[1] = mkdec("Keyboard", function, "char", "readChar");
|
||||
keyboard[2] = mkdec("Keyboard", function, "String", "readLine");
|
||||
keyboard[3] = mkdec("Keyboard", function, "int", "readInt");
|
||||
}
|
||||
|
||||
void populatememory() {
|
||||
memory[0] = mkdec("Memory", function, "int", "peek");
|
||||
memory[1] = mkdec("Memory", function, "void", "poke");
|
||||
memory[2] = mkdec("Memory", function, "Array", "alloc");
|
||||
memory[3] = mkdec("Memory", function, "void", "deAlloc");
|
||||
}
|
||||
|
||||
void populatesys() {
|
||||
sys[0] = mkdec("Sys", function, "void", "halt");
|
||||
sys[1] = mkdec("Sys", function, "void", "error");
|
||||
sys[2] = mkdec("Sys", function, "void", "wait");
|
||||
}
|
||||
|
||||
void populateos() {
|
||||
populatemath();
|
||||
populatestringclass();
|
||||
populatearray();
|
||||
populateoutput();
|
||||
populatescreen();
|
||||
populatekeyboard();
|
||||
populatememory();
|
||||
populatesys();
|
||||
}
|
||||
|
||||
void freedecs(SUBROUTDEC** array, int size) {
|
||||
for(int i = 0; i < size; i++) {
|
||||
free(array[i]->class->name);
|
||||
free(array[i]->class);
|
||||
free(array[i]->name);
|
||||
free(array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void freeos() {
|
||||
freedecs(math, MATH_COUNT);
|
||||
freedecs(stringclass, STRING_COUNT);
|
||||
freedecs(array, ARRAY_COUNT);
|
||||
freedecs(output, OUTPUT_COUNT);
|
||||
freedecs(screen, SCREEN_COUNT);
|
||||
freedecs(keyboard, KEYBOARD_COUNT);
|
||||
freedecs(memory, MEMORY_COUNT);
|
||||
freedecs(sys, SYS_COUNT);
|
||||
}
|
||||
|
||||
SUBROUTDEC* getdec(SUBROUTDEC** array, int size, const char* name) {
|
||||
for(int i = 0; i < size; i++)
|
||||
if(!strcmp(array[i]->name, name))
|
||||
return array[i];
|
||||
}
|
||||
|
||||
SUBROUTDEC* getossubroutdec(SUBROUTCALL* call) {
|
||||
if(!strcmp(call->parentname, "Math"))
|
||||
return getdec(math, MATH_COUNT, call->name);
|
||||
if(!strcmp(call->parentname, "String"))
|
||||
return getdec(stringclass, STRING_COUNT, call->name);
|
||||
if(!strcmp(call->parentname, "Array"))
|
||||
return getdec(array, ARRAY_COUNT, call->name);
|
||||
if(!strcmp(call->parentname, "Output"))
|
||||
return getdec(output, OUTPUT_COUNT, call->name);
|
||||
if(!strcmp(call->parentname, "Screen"))
|
||||
return getdec(screen, SCREEN_COUNT, call->name);
|
||||
if(!strcmp(call->parentname, "Keyboard"))
|
||||
return getdec(keyboard, KEYBOARD_COUNT, call->name);
|
||||
if(!strcmp(call->parentname, "Memory"))
|
||||
return getdec(memory, MEMORY_COUNT, call->name);
|
||||
if(!strcmp(call->parentname, "Sys"))
|
||||
return getdec(sys, SYS_COUNT, call->name);
|
||||
return NULL;
|
||||
}
|
9
misc/os.h
Normal file
9
misc/os.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef OS_H
|
||||
#define OS_H
|
||||
#include "parser-tree.h"
|
||||
|
||||
SUBROUTDEC* getossubroutdec(SUBROUTCALL* call);
|
||||
void populateos();
|
||||
void freeos();
|
||||
|
||||
#endif
|
82
misc/util.c
Normal file
82
misc/util.c
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "util.h"
|
||||
|
||||
char* heapstr(const char* str, int len) {
|
||||
int size = sizeof(char) * (len + 1);
|
||||
char* outstr = (char*)malloc(size);
|
||||
strcpy(outstr, str);
|
||||
return outstr;
|
||||
}
|
||||
|
||||
char* ezheapstr(const char* str) {
|
||||
return heapstr(str, strlen(str));
|
||||
}
|
||||
|
||||
void* copy(void* v, int size) {
|
||||
void* copy = malloc(size);
|
||||
memcpy(copy, v, size);
|
||||
return copy;
|
||||
}
|
||||
|
||||
int countplaces(int n) {
|
||||
int places = 1;
|
||||
int divisor = 1;
|
||||
if(n < 0) {
|
||||
n = -n;
|
||||
places++;
|
||||
}
|
||||
while(n / divisor >= 10) {
|
||||
places++;
|
||||
divisor *= 10;
|
||||
}
|
||||
return places;
|
||||
}
|
||||
|
||||
char* itoa(int i) {
|
||||
int size = sizeof(char)*(countplaces(i)+1);
|
||||
char* a = (char*)malloc(size);
|
||||
snprintf(a, size, "%i", i);
|
||||
return a;
|
||||
}
|
||||
|
||||
STRINGLIST* onestr(const char* str) {
|
||||
STRINGLIST* strlist = (STRINGLIST*)malloc(sizeof(STRINGLIST));
|
||||
strlist->content = ezheapstr(str);
|
||||
strlist->next = NULL;
|
||||
return strlist;
|
||||
}
|
||||
|
||||
STRINGLIST* initstrlist(const char** strs, int count) {
|
||||
STRINGLIST* strlist = (STRINGLIST*)malloc(sizeof(STRINGLIST));
|
||||
STRINGLIST* curr = strlist;
|
||||
for(int i = 0; i < count-1; i++) {
|
||||
curr->content = ezheapstr(strs[i]);
|
||||
curr->next = (STRINGLIST*)malloc(sizeof(STRINGLIST));
|
||||
curr = curr->next;
|
||||
}
|
||||
curr->content = ezheapstr(strs[count-1]);
|
||||
curr->next = NULL;
|
||||
return strlist;
|
||||
}
|
||||
|
||||
void printstrlist(STRINGLIST* strlist, FILE* stream) {
|
||||
while(strlist != NULL) {
|
||||
fprintf(stream, "%s\n", strlist->content);
|
||||
strlist = strlist->next;
|
||||
}
|
||||
}
|
||||
|
||||
void freestrlist(STRINGLIST* strlist) {
|
||||
STRINGLIST* next = strlist->next;
|
||||
free(strlist);
|
||||
if(next != NULL)
|
||||
freestrlist(next);
|
||||
}
|
||||
|
||||
bool existsinarray(STRINGARRAY* arr, const char* item) {
|
||||
for(int i = 0; i < arr->size; i++)
|
||||
if(!strcmp(arr->items[i], item))
|
||||
return true;
|
||||
return false;
|
||||
}
|
37
misc/util.h
Normal file
37
misc/util.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* util
|
||||
* Random utilities. */
|
||||
|
||||
// Macros
|
||||
#define eprintf(...) fprintf (stderr, __VA_ARGS__)
|
||||
#define count(array, type) ((sizeof(array)) / (sizeof(type)))
|
||||
#define strcount(array) count(array, char*)
|
||||
#define mkstrlist(name, array) STRINGARRAY name = { .items = array, .size = strcount(array) }
|
||||
|
||||
typedef struct stringlist {
|
||||
char* content;
|
||||
struct stringlist* next;
|
||||
} STRINGLIST;
|
||||
|
||||
typedef struct {
|
||||
const char** items;
|
||||
const int size;
|
||||
} STRINGARRAY;
|
||||
|
||||
char* heapstr(const char* str, int len);
|
||||
char* ezheapstr(const char* str);
|
||||
int countplaces(int n);
|
||||
char* itoa(int i);
|
||||
void* copy(void* v, int size);
|
||||
|
||||
STRINGLIST* onestr(const char* str);
|
||||
STRINGLIST* initstrlist(const char** strs, int count);
|
||||
void printstrlist(STRINGLIST* strlist, FILE* stream);
|
||||
void freestrlist(STRINGLIST* strlist);
|
||||
|
||||
bool existsinarray(STRINGARRAY* arr, const char* item);
|
||||
#endif
|
Reference in New Issue
Block a user