wip arrays
This commit is contained in:
parent
c5ce5179f0
commit
cbb904abd9
21
compiler.c
21
compiler.c
|
|
@ -676,6 +676,27 @@ grouping()
|
|||
consume(TOKEN_RPAREN);
|
||||
}
|
||||
|
||||
|
||||
void calculate_strides(i32* dims, i32 num_dims, i32* out_strides) {
|
||||
int current_stride = 1;
|
||||
|
||||
for (int i = num_dims - 1; i >= 0; i--) {
|
||||
out_strides[i] = current_stride;
|
||||
current_stride *= dims[i];
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
define_array()
|
||||
{
|
||||
/* example
|
||||
i32 dims[3] = {3, 3, 3};
|
||||
i32 strides[3];
|
||||
|
||||
calculate_strides(dims, 3, strides);
|
||||
*/
|
||||
}
|
||||
|
||||
void
|
||||
number()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -82,3 +82,64 @@ nat_to_int(unsigned n)
|
|||
{
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
int
|
||||
int_add(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
int
|
||||
int_sub(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
int
|
||||
int_mul(int a, int b)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
int
|
||||
int_div(int a, int b)
|
||||
{
|
||||
return a / b;
|
||||
}
|
||||
int
|
||||
int_eq(int a, int b)
|
||||
{
|
||||
return a == b;
|
||||
}
|
||||
int
|
||||
int_ne(int a, int b)
|
||||
{
|
||||
return a != b;
|
||||
}
|
||||
int
|
||||
int_lt(int a, int b)
|
||||
{
|
||||
return a < b;
|
||||
}
|
||||
int
|
||||
int_le(int a, int b)
|
||||
{
|
||||
return a <= b;
|
||||
}
|
||||
int
|
||||
int_gt(int a, int b)
|
||||
{
|
||||
return a > b;
|
||||
}
|
||||
int
|
||||
int_ge(int a, int b)
|
||||
{
|
||||
return a >= b;
|
||||
}
|
||||
int
|
||||
int_neg(int f)
|
||||
{
|
||||
return -f;
|
||||
}
|
||||
int
|
||||
int_abs(int f)
|
||||
{
|
||||
return (f < 0) ? -f : f;
|
||||
}
|
||||
|
|
@ -147,6 +147,120 @@
|
|||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_add_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr INC2 INC2 LDA2 STH2kr LDA2 ADD2 !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_sub_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr INC2 INC2 LDA2 STH2kr LDA2 SUB2 !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_mul_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr INC2 INC2 LDA2 STH2kr LDA2 MUL2 !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_div_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr INC2 INC2 LDA2 STH2kr LDA2 sdiv
|
||||
!&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_eq_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr INC2 INC2 LDA2 STH2kr LDA2 EQU2 #00 SWP !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_ne_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr INC2 INC2 LDA2 STH2kr LDA2 NEQ2 #00 SWP !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_lt_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr INC2 INC2 LDA2 #8000 EOR2 STH2kr LDA2 #8000 EOR2 LTH2 #00 SWP !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_le_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr INC2 INC2 LDA2 #8000 EOR2 STH2kr LDA2 #8000 EOR2 GTH2 #00 SWP #01 EOR !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_gt_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr LDA2 #8000 EOR2 STH2kr INC2 INC2 LDA2 #8000 EOR2 LTH2 #00 SWP !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_ge_ ( b* a* -- result* )
|
||||
OVR2r LIT2r 0004 SUB2r STH2kr INC2 INC2 STA2
|
||||
STH2kr STA2
|
||||
STH2kr LDA2 #8000 EOR2 STH2kr INC2 INC2 LDA2 #8000 EOR2 GTH2 #00 SWP #01 EOR !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_neg_ ( f* -- result* )
|
||||
OVR2r LIT2r 0002 SUB2r STH2kr STA2
|
||||
#0000 STH2kr LDA2 SUB2 !&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@int_abs_ ( f* -- result* )
|
||||
OVR2r LIT2r 0002 SUB2r STH2kr STA2
|
||||
STH2kr LDA2 #8000 EOR2 #8000 LTH2 ?&then.1
|
||||
STH2kr LDA2 !&end.1
|
||||
|
||||
&then.1
|
||||
#0000 STH2kr LDA2 SUB2
|
||||
|
||||
&end.1
|
||||
!&return
|
||||
#0000
|
||||
|
||||
&return
|
||||
POP2r JMP2r
|
||||
|
||||
@str/<print> ( str* -- )
|
||||
LDAk DUP ?{ POP POP2 JMP2r }
|
||||
#18 DEO
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
|
||||
function main() {
|
||||
print(add(1, 1) as str);
|
||||
}
|
||||
print(add(1, 1) as str);
|
||||
halt;
|
||||
|
||||
/**
|
||||
* Add two numbers together
|
||||
|
|
|
|||
|
|
@ -3,3 +3,5 @@ bool flag = false;
|
|||
if (!flag) {
|
||||
print("flag is false");
|
||||
}
|
||||
|
||||
halt;
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
function main() {
|
||||
print(fib(23) as str);
|
||||
}
|
||||
print(fib(23) as str);
|
||||
halt;
|
||||
|
||||
/**
|
||||
* Recursively calculate fibonacci
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
nat i = 0;
|
||||
|
||||
for (nat x in [0..10]) {
|
||||
print(x as str);
|
||||
}
|
||||
//for (nat x in [0..10]) {
|
||||
// print(x as str);
|
||||
//}
|
||||
|
||||
for (nat j = 0; j < 10; j = j + 1) {
|
||||
print(j as str);
|
||||
}
|
||||
|
||||
halt;
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
int i = 122;
|
||||
int j = 32;
|
||||
|
||||
function main() {
|
||||
putchar((i - j));
|
||||
}
|
||||
print((i - j) as str);
|
||||
halt;
|
||||
|
|
|
|||
16
test/if.ul
16
test/if.ul
|
|
@ -1,11 +1,9 @@
|
|||
function main() {
|
||||
nat x = 20;
|
||||
nat x = 20;
|
||||
|
||||
if (x == 10) {
|
||||
print("x is 10");
|
||||
} else if (x == 20) {
|
||||
print("x is 20");
|
||||
} else {
|
||||
print("x is something else");
|
||||
}
|
||||
if (x == 10) {
|
||||
print("x is 10");
|
||||
} else if (x == 20) {
|
||||
print("x is 20");
|
||||
} else {
|
||||
print("x is something else");
|
||||
}
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
function main() {
|
||||
local_fn();
|
||||
halt;
|
||||
|
||||
function local_fn() {
|
||||
int i = 122;
|
||||
int j = 32;
|
||||
|
||||
putchar((i - j)); // prints ascii `Z`
|
||||
printf((i - j));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,4 @@
|
|||
/**
|
||||
* Main function
|
||||
*/
|
||||
function main() {
|
||||
print("Enter a string: ");
|
||||
str msg = read(32);
|
||||
print(msg);
|
||||
return 0;
|
||||
}
|
||||
print("Enter a string: ");
|
||||
str msg = read(32);
|
||||
print(msg);
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -34,38 +34,34 @@ plex Mouse implements Device {
|
|||
bool btn4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
function main() {
|
||||
Screen screen = open("/dev/screen/0", 0);
|
||||
Mouse mouse = open("/dev/mouse/0", 0);
|
||||
Screen screen = open("/dev/screen/0", 0);
|
||||
Mouse mouse = open("/dev/mouse/0", 0);
|
||||
|
||||
outline_swatch(screen, BLACK, 1, 1);
|
||||
outline_swatch(screen, WHITE, 21, 1);
|
||||
screen.draw();
|
||||
outline_swatch(screen, BLACK, 1, 1);
|
||||
outline_swatch(screen, WHITE, 21, 1);
|
||||
screen.draw();
|
||||
|
||||
loop {
|
||||
mouse.refresh();
|
||||
if (!mouse.left) continue;
|
||||
loop {
|
||||
mouse.refresh();
|
||||
if (!mouse.left) continue;
|
||||
|
||||
int box_size = 20;
|
||||
int x = 1;
|
||||
int y = 1;
|
||||
byte color = BLACK;
|
||||
outlined_swatch(screen, color, x, y);
|
||||
set_color(box_size, x, y, mouse.x, mouse.y, color);
|
||||
int box_size = 20;
|
||||
int x = 1;
|
||||
int y = 1;
|
||||
byte color = BLACK;
|
||||
outlined_swatch(screen, color, x, y);
|
||||
set_color(box_size, x, y, mouse.x, mouse.y, color);
|
||||
|
||||
color = WHITE;
|
||||
x = 21;
|
||||
outlined_swatch(screen, color, x, y);
|
||||
set_color(box_size, x, y, mouse.x, mouse.y, color);
|
||||
screen.draw();
|
||||
color = WHITE;
|
||||
x = 21;
|
||||
outlined_swatch(screen, color, x, y);
|
||||
set_color(box_size, x, y, mouse.x, mouse.y, color);
|
||||
screen.draw();
|
||||
|
||||
rectangle(screen, selected_color, x, y, 5, 5);
|
||||
}
|
||||
exit(0);
|
||||
rectangle(screen, selected_color, x, y, 5, 5);
|
||||
}
|
||||
halt;
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the click is within the bound and update the selected color if so.
|
||||
|
|
|
|||
10
test/plex.ul
10
test/plex.ul
|
|
@ -36,15 +36,13 @@ plex Rect {
|
|||
}
|
||||
}
|
||||
|
||||
Rect r = create_geometry();
|
||||
print(r.toS());
|
||||
halt;
|
||||
|
||||
function create_geometry() Rect {
|
||||
Point tl(10, 20);
|
||||
Point br(100, 50);
|
||||
Rect final_rect(tl, br, 90, 180);
|
||||
return final_rect;
|
||||
}
|
||||
|
||||
function main() int {
|
||||
Rect r = create_geometry();
|
||||
print(r.toS());
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue