summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code2
diff options
context:
space:
mode:
authorthing1 <thing1@seacrossedlovers.xyz>2024-10-01 08:55:32 +0100
committerthing1 <thing1@seacrossedlovers.xyz>2024-10-01 08:55:32 +0100
commitef8cf00bbf9f74eb3b6aabc1d99f5358e81741c7 (patch)
tree3479ca1b82573426151cb270c63a588af737d3c0 /comp/lucas-standen-NEA/code2
parentdc7732b6f073d5450657d94c815ca445a1a08fbd (diff)
added some cool examples to zpy and got electronics working
Diffstat (limited to 'comp/lucas-standen-NEA/code2')
-rw-r--r--comp/lucas-standen-NEA/code2/Makefile7
-rw-r--r--comp/lucas-standen-NEA/code2/comp.c2
-rw-r--r--comp/lucas-standen-NEA/code2/examples/Makefile3
-rwxr-xr-xcomp/lucas-standen-NEA/code2/examples/fib_examplebin24488 -> 25496 bytes
-rwxr-xr-xcomp/lucas-standen-NEA/code2/examples/raylib_examplebin1085488 -> 1086488 bytes
-rw-r--r--comp/lucas-standen-NEA/code2/examples/raylib_example.zpy12
-rwxr-xr-xcomp/lucas-standen-NEA/code2/examples/spaceinvadersbin0 -> 1086520 bytes
-rw-r--r--comp/lucas-standen-NEA/code2/examples/spaceinvaders.zpy161
-rwxr-xr-xcomp/lucas-standen-NEA/code2/examples/str_examplebin24856 -> 25952 bytes
-rw-r--r--comp/lucas-standen-NEA/code2/examples/str_example.zpy14
-rw-r--r--comp/lucas-standen-NEA/code2/stdlib/String/String.c12
-rw-r--r--comp/lucas-standen-NEA/code2/stdlib/String/String.h5
-rw-r--r--comp/lucas-standen-NEA/code2/tokenizer.c15
-rwxr-xr-xcomp/lucas-standen-NEA/code2/zpybin36888 -> 37072 bytes
14 files changed, 216 insertions, 15 deletions
diff --git a/comp/lucas-standen-NEA/code2/Makefile b/comp/lucas-standen-NEA/code2/Makefile
index 9db83ac..b5a1aed 100644
--- a/comp/lucas-standen-NEA/code2/Makefile
+++ b/comp/lucas-standen-NEA/code2/Makefile
@@ -1,7 +1,7 @@
CC = cc
CFLAGS = -O0 -ggdb
-all:
+zpy:
${CC} *.c -c ${CFLAGS}
${CC} *.o -o zpy ${CFLAGS}
cd stdlib && make
@@ -10,15 +10,16 @@ clean:
cd stdlib && make clean
cd examples && make clean
-install: all
+install: zpy
mkdir -p /usr/local/share/zpylib
mkdir -p /usr/local/share/zpylib/include
cp ./zpy /usr/local/bin/zpy
cd stdlib && make install
-example: all
+example: zpy
cd examples && make
uninstall:
rm /usr/local/bin/zpy
+ rm -rf /usr/local/share/zpylib
diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c
index f06631b..7d97cdc 100644
--- a/comp/lucas-standen-NEA/code2/comp.c
+++ b/comp/lucas-standen-NEA/code2/comp.c
@@ -203,7 +203,7 @@ char *compile(astNode *node){
out = appendsnprintf(out, MAXOUTLEN, "return %s;\n", node->args[0]);
}
else if (strcmp(names[23], node->func) == 0){
- out = appendsnprintf(out, MAXOUTLEN, "calloc(0, %s)", node->args[0]);
+ out = appendsnprintf(out, MAXOUTLEN, "malloc(%s)", node->args[0]);
neededmemptr = true;
}
else if (strcmp(names[24], node->func) == 0){
diff --git a/comp/lucas-standen-NEA/code2/examples/Makefile b/comp/lucas-standen-NEA/code2/examples/Makefile
index 13178a5..cbdcda1 100644
--- a/comp/lucas-standen-NEA/code2/examples/Makefile
+++ b/comp/lucas-standen-NEA/code2/examples/Makefile
@@ -1,6 +1,7 @@
all:
zpy raylib_example.zpy -o raylib_example -f -lraylib -f -lm -i raylib.h
+ zpy spaceinvaders.zpy -o spaceinvaders -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 str_example
+ rm -rf fib_example raylib_example str_example spaceinvaders
diff --git a/comp/lucas-standen-NEA/code2/examples/fib_example b/comp/lucas-standen-NEA/code2/examples/fib_example
index e7d4958..bdd94ec 100755
--- a/comp/lucas-standen-NEA/code2/examples/fib_example
+++ b/comp/lucas-standen-NEA/code2/examples/fib_example
Binary files differ
diff --git a/comp/lucas-standen-NEA/code2/examples/raylib_example b/comp/lucas-standen-NEA/code2/examples/raylib_example
index 74d52ed..08ea8fd 100755
--- a/comp/lucas-standen-NEA/code2/examples/raylib_example
+++ b/comp/lucas-standen-NEA/code2/examples/raylib_example
Binary files differ
diff --git a/comp/lucas-standen-NEA/code2/examples/raylib_example.zpy b/comp/lucas-standen-NEA/code2/examples/raylib_example.zpy
index 90ef8b8..7c6e599 100644
--- a/comp/lucas-standen-NEA/code2/examples/raylib_example.zpy
+++ b/comp/lucas-standen-NEA/code2/examples/raylib_example.zpy
@@ -1,15 +1,15 @@
(defun main int)
- (InitWindow 800 800 "test_test")
- (SetTargetFPS 30)
+ (InitWindow 800 800 "test test")
+ (SetTargetFPS 60)
(let img:Image (LoadImage "dvd.png"))
(let tex:Texture (LoadTextureFromImage img))
(UnloadImage img)
- (let x:int (randint 1 9))
- (let y:int (randint 1 9))
- (let xd:int (randint 1 5))
- (let yd:int (randint 1 5))
+ (let x:int (randint 4 9))
+ (let y:int (randint 4 9))
+ (let xd:int (randint 3 5))
+ (let yd:int (randint 3 5))
(for i:int 0 (= (WindowShouldClose) 0) 0)
(BeginDrawing)
diff --git a/comp/lucas-standen-NEA/code2/examples/spaceinvaders b/comp/lucas-standen-NEA/code2/examples/spaceinvaders
new file mode 100755
index 0000000..ec88002
--- /dev/null
+++ b/comp/lucas-standen-NEA/code2/examples/spaceinvaders
Binary files differ
diff --git a/comp/lucas-standen-NEA/code2/examples/spaceinvaders.zpy b/comp/lucas-standen-NEA/code2/examples/spaceinvaders.zpy
new file mode 100644
index 0000000..698999a
--- /dev/null
+++ b/comp/lucas-standen-NEA/code2/examples/spaceinvaders.zpy
@@ -0,0 +1,161 @@
+(struct entity)
+ (def x:int)
+ (def y:int)
+ (def width:int)
+ (def height:int)
+ (def ofx:int)
+ (def ofy:int)
+ (def exists:bool)
+(endstruct)
+
+(defun touching bool a:entity* b:entity*)
+ (def r1:Rectangle)
+ (def r2:Rectangle)
+
+ (set r1.x a->x)
+ (set r1.y a->y)
+ (set r1.width a->width)
+ (set r1.height a->height)
+
+ (set r2.x b->x)
+ (set r2.y b->y)
+ (set r2.width b->width)
+ (set r2.height b->height)
+ (return (CheckCollisionRecs r1 r2))
+(endfun)
+
+(defun main int)
+ (InitWindow 800 800 "test test")
+ (SetTargetFPS 60)
+
+ (let p:entity* (alloc (sizeof entity)))
+ (set p->x 400)
+ (set p->y 700)
+ (set p->width 50)
+ (set p->height 30)
+ (set p->ofx 25)
+ (set p->ofy 15)
+
+ (let b:entity* (alloc (sizeof entity)))
+ (set b->x 400)
+ (set b->y 700)
+ (set b->width 10)
+ (set b->height 6)
+ (set b->ofx 5)
+ (set b->ofy 3)
+ (set b->exists false)
+
+ (let b2:entity* (alloc (sizeof entity)))
+ (set b2->x 400)
+ (set b2->y 700)
+ (set b2->width 10)
+ (set b2->height 6)
+ (set b2->ofx 5)
+ (set b2->ofy 3)
+ (set b2->exists false)
+
+ (let e:entity* (alloc (sizeof entity)))
+ (set e->x 400)
+ (set e->y 100)
+ (set e->width 60)
+ (set e->height 30)
+ (set e->ofx 30)
+ (set e->ofy 15)
+ (set e->exists true)
+
+ (let ehp:int 5)
+ (let edx:int 1)
+
+ (let reload:int 0)
+
+ (for i:int 0 (= (WindowShouldClose) 0) 0)
+(if (= ehp 0))
+(exit 0)
+(elif (= e->y 700))
+(exit 1)
+(endif)
+ (if (!= reload 0)
+ (set reload (- reload 1))
+ (endif)
+
+ (BeginDrawing)
+ (ClearBackground BLACK)
+ (DrawRectangle (- p->x p->ofx) (+ p->y p->ofy) p->width p->height GREEN)
+ (if (= b->exists true))
+ (DrawRectangle (- b->x b->ofx) (+ b->y b->ofy) b->width b->height BLUE)
+ (endif)
+ (if (= b2->exists true))
+ (DrawRectangle (- b2->x b2->ofx) (+ b2->y b2->ofy) b2->width b2->height PURPLE)
+ (endif)
+ (if (= e->exists true))
+ (DrawRectangle (- e->x e->ofx) (+ e->y e->ofy) e->width e->height RED)
+ (endif)
+ (EndDrawing)
+
+ (if (IsKeyDown KEY_LEFT))
+ (set p->x (- p->x 6))
+ (elif (IsKeyDown KEY_RIGHT))
+ (set p->x (+ p->x 6))
+ (endif)
+
+ (if (IsKeyPressed KEY_SPACE))
+ (if (= reload 0))
+ (if (!= b->exists true))
+ (set b->exists true)
+ (else)
+ (set b2->exists true)
+ (endif)
+ (set reload 30)
+ (endif)
+ (endif)
+
+ (if (= b->exists true))
+ (set b->y (- b->y 10))
+ (if (< b->y 0))
+ (set b->y 700)
+ (set b->exists false)
+ (endif)
+ (else)
+ (set b->x p->x)
+ (endif)
+
+ (if (touching b e))
+ (set b->y 700)
+ (set b->exists false)
+ (set ehp (- ehp 1)
+ (if (<= ehp 0))
+ (set e->exists false)
+ (endif)
+ (endif)
+
+ (if (= b2->exists true))
+ (set b2->y (- b2->y 10))
+ (if (< b2->y 0))
+ (set b2->y 700)
+ (set b2->exists false)
+ (endif)
+ (else)
+ (set b2->x p->x)
+ (endif)
+
+ (if (touching b2 e))
+ (set b2->y 700)
+ (set b2->exists false)
+ (set ehp (- ehp 1)
+ (if (<= ehp 0))
+ (set e->exists false)
+ (endif)
+ (endif)
+
+ (set e->x (+ (* 4 edx) e->x)
+ (if (= e->x 800))
+ (set edx (* edx -1)
+ (set e->y (+ e->y 10))
+ (elif (= e->x 0))
+ (set edx (* edx -1)
+ (set e->y (+ e->y 10))
+ (endif)
+
+ (endfor)
+ (CloseWindow)
+(endfun)
diff --git a/comp/lucas-standen-NEA/code2/examples/str_example b/comp/lucas-standen-NEA/code2/examples/str_example
index 8f86844..c543ab7 100755
--- a/comp/lucas-standen-NEA/code2/examples/str_example
+++ b/comp/lucas-standen-NEA/code2/examples/str_example
Binary files differ
diff --git a/comp/lucas-standen-NEA/code2/examples/str_example.zpy b/comp/lucas-standen-NEA/code2/examples/str_example.zpy
index 7311660..bffc3f0 100644
--- a/comp/lucas-standen-NEA/code2/examples/str_example.zpy
+++ b/comp/lucas-standen-NEA/code2/examples/str_example.zpy
@@ -1,6 +1,14 @@
(defun main int)
- (let str:string* (String "hello"))
- (printchar str->_str[0])
+ (let str:string* (String "hello_world"))
+ (printstr str->_str)
+ (printchar '\n')
+
+ (let strs:string** (str->split str '_'))
+
+ (printstr strs[0]->_str)
+ (printchar '\n')
+ (printstr strs[1]->_str)
+ (printchar '\n')
+
(str->free str)
- (printstr "<first_letter\n")
(endfun)
diff --git a/comp/lucas-standen-NEA/code2/stdlib/String/String.c b/comp/lucas-standen-NEA/code2/stdlib/String/String.c
index 2814a1c..6f11119 100644
--- a/comp/lucas-standen-NEA/code2/stdlib/String/String.c
+++ b/comp/lucas-standen-NEA/code2/stdlib/String/String.c
@@ -11,6 +11,16 @@ void __stringfree(string *self){
free(self);
}
+void __stringprint(string *self){
+ printf("%s\n", self->_str);
+}
+
+void __stringinput(string *self, size_t len){
+ char *tmp = calloc(0, len);
+ fgets(tmp, len, stdin);
+ self->fromcstring(self, tmp);
+}
+
void __stringappendchar(string *self, char c){
self->_len++;
self->_str = realloc(self->_str, self->_len);
@@ -132,6 +142,8 @@ string *String(char *cstring){ // returns an allocated String from a C string in
str->fromcstring = &__stringfromcstring;
str->tocstring = &__stringtocstring;
str->split = &__stringsplit;
+ str->print = &__stringprint;
+ str->input = &__stringinput;
return str;
}
diff --git a/comp/lucas-standen-NEA/code2/stdlib/String/String.h b/comp/lucas-standen-NEA/code2/stdlib/String/String.h
index 865defe..b1d07f6 100644
--- a/comp/lucas-standen-NEA/code2/stdlib/String/String.h
+++ b/comp/lucas-standen-NEA/code2/stdlib/String/String.h
@@ -13,6 +13,8 @@ typedef struct string {
void (*fromcstring)(string *, char *);
char *(*tocstring)(string *);
string **(*split)(string *, char);
+ void (*print)(string *);
+ void (*input)(string *, size_t);
} string;
string *String(char *cstring);
@@ -25,3 +27,6 @@ int __stringcmp(string *str1, string *str2);
void __stringfromcstring(string *self, char *cstring);
char *__stringtocstring(string *self);
string **__stringsplit(string *self, char delim);
+void *print(string *self);
+void *input(string *self, size_t len);
+
diff --git a/comp/lucas-standen-NEA/code2/tokenizer.c b/comp/lucas-standen-NEA/code2/tokenizer.c
index 7e8cae1..c756c0c 100644
--- a/comp/lucas-standen-NEA/code2/tokenizer.c
+++ b/comp/lucas-standen-NEA/code2/tokenizer.c
@@ -63,7 +63,20 @@ top:
head->children[argCount] = tokenize(chunk);
argCount++;
}else {
- i += readuntil(&line[i], ' ', chunk); // reads func name or arg
+ if (line[i] == '"'){
+ i += readuntil(&line[i+1], '"', chunk); // reads a comptime string
+ i++;
+ char *tmp = malloc(strlen(chunk)+2);
+ tmp[0] = '"';
+ tmp[1] = '\0';
+ strcat(tmp, chunk);
+ strcat(tmp, "\"");
+ chunk = tmp;
+
+ }
+ else {
+ i += readuntil(&line[i], ' ', chunk); // reads func name or arg
+ }
if (head->func == NULL){
head->func = chunk;
} else{
diff --git a/comp/lucas-standen-NEA/code2/zpy b/comp/lucas-standen-NEA/code2/zpy
index 107d933..d153a2f 100755
--- a/comp/lucas-standen-NEA/code2/zpy
+++ b/comp/lucas-standen-NEA/code2/zpy
Binary files differ