diff options
Diffstat (limited to 'comp/lucas-standen-NEA/code2')
16 files changed, 299 insertions, 23 deletions
diff --git a/comp/lucas-standen-NEA/code2/Makefile b/comp/lucas-standen-NEA/code2/Makefile index ffbcffb..60e84c6 100644 --- a/comp/lucas-standen-NEA/code2/Makefile +++ b/comp/lucas-standen-NEA/code2/Makefile @@ -1,21 +1,20 @@ CC = cc CFLAGS = -O0 -ggdb -all: _zpy _parser _tokenizer _comp _appendsnprintf _util - ${CC} zpy.o parser.o tokenizer.o comp.o appendsnprintf.o util.o -o zpy ${CFLAGS} +all: + ${CC} *.c -c ${CFLAGS} + ${CC} *.o -o zpy ${CFLAGS} + cd stdlib && make +clean: + rm -rf zpy *.o *.core sample out stdlib/*.o + cd stdlib && make clean -_zpy: zpy.c - ${CC} zpy.c -c -o zpy.o ${CFLAGS} -_parser: parser.c - ${CC} parser.c -c -o parser.o ${CFLAGS} -_tokenizer: tokenizer.c - ${CC} tokenizer.c -c -o tokenizer.o ${CFLAGS} -_comp: comp.c - ${CC} comp.c -c -o comp.o ${CFLAGS} -_appendsnprintf: appendsnprintf.c - ${CC} appendsnprintf.c -c -o appendsnprintf.o ${CFLAGS} -_util: util.c - ${CC} util.c -c -o util.o ${CFLAGS} +install: all + mkdir -p /usr/local/share/zpylib + mkdir -p /usr/local/share/zpylib/include + cp ./zpy /usr/local/bin/zpy -clean: - rm -rf zpy *.o *.core sample out + cd stdlib && make install + +uninstall: + rm /usr/local/bin/zpy diff --git a/comp/lucas-standen-NEA/code2/examples/Makefile b/comp/lucas-standen-NEA/code2/examples/Makefile new file mode 100644 index 0000000..ccb9b35 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/examples/Makefile @@ -0,0 +1,6 @@ +all: + zpy raylib_example.zpy -o raylib_example -f -lraylib -f -lm -i raylib.h + zpy fib_example.zpy -o fib_example -f -ggdb + zpy str_example.zpy -o str_example -f -ggdb +clean: + rm -rf fib_example raylib_example diff --git a/comp/lucas-standen-NEA/code2/examples/fib_example b/comp/lucas-standen-NEA/code2/examples/fib_example Binary files differnew file mode 100755 index 0000000..e81a20c --- /dev/null +++ b/comp/lucas-standen-NEA/code2/examples/fib_example diff --git a/comp/lucas-standen-NEA/code2/examples/fib_example.zpy b/comp/lucas-standen-NEA/code2/examples/fib_example.zpy new file mode 100644 index 0000000..a79b7d7 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/examples/fib_example.zpy @@ -0,0 +1,13 @@ +(defun fib int n:int) + (if (< n 2)) + (return n) + (endif) + + (return (+ (fib (- n 1)) (fib (- n 2)))) +(endfun) + +(defun main int) + (let n:int (readint)) + (printint (fib n)) + (printchar '\n') +(endfun) diff --git a/comp/lucas-standen-NEA/code2/examples/raylib_example b/comp/lucas-standen-NEA/code2/examples/raylib_example Binary files differnew file mode 100755 index 0000000..2e3f1e9 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/examples/raylib_example diff --git a/comp/lucas-standen-NEA/code2/examples/raylib_example.zpy b/comp/lucas-standen-NEA/code2/examples/raylib_example.zpy new file mode 100644 index 0000000..931c0e4 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/examples/raylib_example.zpy @@ -0,0 +1,16 @@ +(defun main int) + (InitWindow 800 800 "test") + (SetTargetFPS 40) + (let x:int 0) + (let y:int 0) + (for i:int 0 (= (WindowShouldClose) 0) 0) + (BeginDrawing) + (ClearBackground RAYWHITE) + (DrawText "test" x y 40 RED) + (EndDrawing) + + (set x (+ x 5)) + (set y (+ y 5)) + (endfor) + (CloseWindow) +(endfun) diff --git a/comp/lucas-standen-NEA/code2/examples/str_example b/comp/lucas-standen-NEA/code2/examples/str_example Binary files differnew file mode 100755 index 0000000..ca715dd --- /dev/null +++ b/comp/lucas-standen-NEA/code2/examples/str_example diff --git a/comp/lucas-standen-NEA/code2/examples/str_example.zpy b/comp/lucas-standen-NEA/code2/examples/str_example.zpy new file mode 100644 index 0000000..9b67f5f --- /dev/null +++ b/comp/lucas-standen-NEA/code2/examples/str_example.zpy @@ -0,0 +1,5 @@ +(defun main int) + (let str:string* (String "hello")) + (printchar str->_str[0]) + (str->free str) +(endfun) diff --git a/comp/lucas-standen-NEA/code2/stdlib/Makefile b/comp/lucas-standen-NEA/code2/stdlib/Makefile new file mode 100644 index 0000000..d915d38 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/stdlib/Makefile @@ -0,0 +1,17 @@ +CC=cc +CLFAGS=-O0 -ggdb + +stdlib: + cd String && make + ${CC} *.c -c ${CFLAGS} + +clean: + rm -rf *.o + cd String && make clean + +install: + cp *.o /usr/local/share/zpylib/ + cp *.h /usr/local/share/zpylib/include/ + cp String/String.o /usr/local/share/zpylib + cp String/String.h /usr/local/share/zpylib/include + diff --git a/comp/lucas-standen-NEA/code2/stdlib/String/Makefile b/comp/lucas-standen-NEA/code2/stdlib/String/Makefile new file mode 100644 index 0000000..e61ca1d --- /dev/null +++ b/comp/lucas-standen-NEA/code2/stdlib/String/Makefile @@ -0,0 +1,7 @@ +CC=cc +CFLAGS=-ggdb + +all: String.c + ${CC} String.c -c -o String.o ${CFLAGS} +clean: + rm -rf String.o diff --git a/comp/lucas-standen-NEA/code2/stdlib/String/String.c b/comp/lucas-standen-NEA/code2/stdlib/String/String.c new file mode 100644 index 0000000..2814a1c --- /dev/null +++ b/comp/lucas-standen-NEA/code2/stdlib/String/String.c @@ -0,0 +1,137 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "String.h" + +size_t STRINGSPLITCOUNT = 0; + +void __stringfree(string *self){ + free(self->_str); + free(self); +} + +void __stringappendchar(string *self, char c){ + self->_len++; + self->_str = realloc(self->_str, self->_len); + self->_str[self->_len-1] = c; +} + +int __stringinsert(string *self, string *substring, size_t point){ + if (point > self->_len) return 1; + + size_t len = self->_len + substring->_len; + + char *start = malloc(point); + char *end = malloc(self->_len - point); + + int i; + for (i = 0; i < point; i++){ + start[i] = self->_str[i]; + } + + int j = 0; + for (; i < self->_len; i++){ + end[j] = self->_str[i]; + j++; + } + + self->_str = realloc(self->_str, len); + + for (i = 0; i < strlen(start); i++){ + self->_str[i] = start[i]; + } + j = 0; + for (; j < substring->_len; i++){ + self->_str[i] = substring->_str[j]; + j++; + } + j = 0; + for (; i < len; i++){ + self->_str[i] = end[j]; + j++; + } + + self->_len = len; + free(start); + free(end); + + return 0; +} + +void __stringreplacechar(string *self, char orig, char new){ + for (int i = 0; i < self->_len; i++) + if (self->_str[i] == orig) self->_str[i] = new; +} + +int __stringcountchar(string *self, char c){ + int count = 0; + for (int i = 0; i < self->_len; i++) + if (self->_str[i] == c) count++; + + return count; +} + +int __stringcmp(string *str1, string *str2){ + return strcmp(str1->_str, str2->_str); +} + +void __stringfromcstring(string *self, char *cstring){ + size_t len = strlen(cstring); + + self->_str = realloc(self->_str, len); + + memcpy(self->_str, cstring, len); + + self->_len = len; +} + +char *__stringtocstring(string *self){ + char *cstring = malloc(self->_len + 1); + memcpy(cstring, self->_str, self->_len); + cstring[self->_len + 1] = '\0'; + return cstring; +} + +string **__stringsplit(string *self, char delim){ + int splitcount = self->countchar(self, delim) + 1; + string **strs = malloc(sizeof(string **)); + + int j = 0; + for (int i = 0; i < splitcount; i++){ + char *str = calloc(0, self->_len); + int charcount = 0; + for (; self->_str[j] != delim && j < self->_len; j++){ + str[charcount] = self->_str[j]; + charcount++; + } + j++; + str[charcount+1] = '\0'; + strs[i] = String(str); + free(str); + } + + STRINGSPLITCOUNT = splitcount; + return strs; +} + +string *String(char *cstring){ // returns an allocated String from a C string input + size_t len = strlen(cstring); + string *str = malloc(sizeof(string)); + + str->_str = calloc(0, len); + memcpy(str->_str, cstring, len); + str->_len = len; + + str->free = &__stringfree; + str->appendchar = &__stringappendchar; + str->insert = &__stringinsert; + str->replacechar = &__stringreplacechar; + str->countchar = &__stringcountchar; + str->cmp = &__stringcmp; + str->fromcstring = &__stringfromcstring; + str->tocstring = &__stringtocstring; + str->split = &__stringsplit; + + return str; +} diff --git a/comp/lucas-standen-NEA/code2/stdlib/String/String.h b/comp/lucas-standen-NEA/code2/stdlib/String/String.h new file mode 100644 index 0000000..865defe --- /dev/null +++ b/comp/lucas-standen-NEA/code2/stdlib/String/String.h @@ -0,0 +1,27 @@ +#include <stddef.h> +extern size_t STRINGSPLITCOUNT; +typedef struct string string; +typedef struct string { + char *_str; // not null terminated string, length encoded + size_t _len; + void (*free)(string*); + void (*appendchar)(string *, char); + int (*insert)(string *, string *, size_t); + void (*replacechar)(string *, char, char); + int (*countchar)(string *, char); + int (*cmp)(string *, string*); + void (*fromcstring)(string *, char *); + char *(*tocstring)(string *); + string **(*split)(string *, char); +} string; + +string *String(char *cstring); +void __stringfree(string *self); +void __stringappendchar(string *self, char c); +int __stringinsert(string *self, string *substring, size_t point); +void __stringreplacechar(string *self, char orig, char new); +int __stringcountchar(string *self, char c); +int __stringcmp(string *str1, string *str2); +void __stringfromcstring(string *self, char *cstring); +char *__stringtocstring(string *self); +string **__stringsplit(string *self, char delim); diff --git a/comp/lucas-standen-NEA/code2/stdlib/zpylib.c b/comp/lucas-standen-NEA/code2/stdlib/zpylib.c new file mode 100644 index 0000000..11b6a88 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/stdlib/zpylib.c @@ -0,0 +1,40 @@ +#include <stdlib.h> +#include <time.h> +#include <stdio.h> + +#include "./String/String.h" + +void printchar(char c){ + putchar(c); +} + +void printint(int i){ + printf("%d", i); +} + +void printfloat(double f){ + printf("%f", f); +} + +char readchar(){ + char c; + scanf("%c", &c); + return c; +} + +int readint(){ + int i; + scanf("%d", &i); + return i; +} + +double readfloat(){ + double f; + scanf("%lf", &f); + return f; +} + +int randint(int lower, int upper){ + srand(time(NULL)); + return rand() % (((upper + 1) - lower) + lower); +} diff --git a/comp/lucas-standen-NEA/code2/stdlib/zpylib.h b/comp/lucas-standen-NEA/code2/stdlib/zpylib.h new file mode 100644 index 0000000..d86dc2d --- /dev/null +++ b/comp/lucas-standen-NEA/code2/stdlib/zpylib.h @@ -0,0 +1,9 @@ +#include <String.h> +#include <stddef.h> + +void printstr(char *str); +void printint(int i); +void printfloat(double f); +int readint(); +void readfloat(double f); +int randint(int lower, int upper); diff --git a/comp/lucas-standen-NEA/code2/zpy b/comp/lucas-standen-NEA/code2/zpy Binary files differnew file mode 100755 index 0000000..10b0fdd --- /dev/null +++ b/comp/lucas-standen-NEA/code2/zpy diff --git a/comp/lucas-standen-NEA/code2/zpy.c b/comp/lucas-standen-NEA/code2/zpy.c index ddb7b90..35357f2 100644 --- a/comp/lucas-standen-NEA/code2/zpy.c +++ b/comp/lucas-standen-NEA/code2/zpy.c @@ -10,10 +10,8 @@ char infilename[128]; char outfilename[128]; -/* char libs[64][128]; int libcount = 0; -*/ bool omitc = false; char compilerflags[64][128]; int compilerflagscount = 0; @@ -26,13 +24,12 @@ void processargs(int argc, char **argv){ i++; strcpy(outfilename, argv[i]); break; - /* + case 'i': i++; strcpy(libs[libcount], argv[i]); libcount++; break; - */ case 'c': omitc = true; break; @@ -71,7 +68,11 @@ int main(int argc, char **argv){ if (stringTokens == NULL) die("couldn't parse file, is it formated properly?"); - + + fprintf(fout, "#include <zpylib.h>\n"); + for (int i = 0; i < libcount; i++){ + fprintf(fout, "#include <%s>\n", libs[i]); + } for (int i = 0; i < stringTokens->count; i++){ stringTokens->strs[i]++; @@ -81,11 +82,10 @@ int main(int argc, char **argv){ Compile(line, fout); } fclose(fout); - fclose(f); if (omitc == false){ char *cmd = malloc(512); - snprintf(cmd, 512, "cc ./tmp.zpy.c -o %s ", outfilename); + snprintf(cmd, 512, "cc ./tmp.zpy.c /usr/local/share/zpylib/*.o -o %s -I/usr/local/share/zpylib/include -Wno-implicit-function-declaration ", outfilename); for (int i = 0; i < compilerflagscount; i++){ cmd = appendsnprintf(cmd, 512, "%s ", compilerflags[i]); } |