Compare commits

...

2 Commits

Author SHA1 Message Date
0x4261756D 6d31d5da98 Add Conways Game of Life 2022-12-22 10:29:57 +01:00
0x4261756D 23c6ec8dad Add more intrinsics 2022-12-22 10:29:25 +01:00
2 changed files with 385 additions and 1 deletions

View File

@ -87,10 +87,13 @@ fn main()
("println", (Vec::from([Datatype::Any]), Vec::new())),
("-", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))),
("+", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))),
("*", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))),
("<", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Bool]))),
(">", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Bool]))),
(">=", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Bool]))),
("==", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Bool]))),
("!=", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Bool]))),
("&&", (Vec::from([Datatype::Bool, Datatype::Bool]), Vec::from([Datatype::Bool]))),
("decrease", (Vec::from([Datatype::Int]), Vec::from([Datatype::Int]))),
]);
let args: Vec<String> = env::args().collect();
@ -301,12 +304,24 @@ fn interpret_program(operations: &Vec<Operation>, queue: &mut Vec<String>, funct
let addend2 = queue.remove(0).parse::<i64>().unwrap();
queue.push((addend1 + addend2).to_string());
}
"*" =>
{
let multiplicant1 = queue.remove(0).parse::<i64>().unwrap();
let multiplicant2 = queue.remove(0).parse::<i64>().unwrap();
queue.push((multiplicant1 * multiplicant2).to_string());
}
">" =>
{
let first = queue.remove(0).parse::<i64>().unwrap();
let second = queue.remove(0).parse::<i64>().unwrap();
queue.push((first > second).to_string());
}
">=" =>
{
let first = queue.remove(0).parse::<i64>().unwrap();
let second = queue.remove(0).parse::<i64>().unwrap();
queue.push((first >= second).to_string());
}
"<" =>
{
let first = queue.remove(0).parse::<i64>().unwrap();
@ -325,6 +340,12 @@ fn interpret_program(operations: &Vec<Operation>, queue: &mut Vec<String>, funct
let second = queue.remove(0).parse::<i64>().unwrap();
queue.push((first != second).to_string());
}
"&&" =>
{
let first = queue.remove(0).parse::<bool>().unwrap();
let second = queue.remove(0).parse::<bool>().unwrap();
queue.push((first && second).to_string());
}
"decrease" =>
{
let val = queue.remove(0).parse::<i64>().unwrap();
@ -356,7 +377,16 @@ fn interpret_program(operations: &Vec<Operation>, queue: &mut Vec<String>, funct
{
return Err(format!("Attempted an out of bounds write for array {} ({} < 0) at {}:{}", arr.name, position, line, col));
}
arr.data[position as usize] = queue.remove(0);
let data = queue.remove(0);
if debug
{
println!("write before: {} {} {:?}", position, data, arr);
}
arr.data[position as usize] = data;
if debug
{
println!("write after: {:?}", arr);
}
}
"read" =>
{

354
tests/conway.qbl Normal file
View File

@ -0,0 +1,354 @@
//valid, #
// #
//###
//
//
//
//
//
//--------
//
//# #
// ##
// #
//
//
//
//
//--------
//
// #
//# #
// ##
//
//
//
//
//--------
//
// #
// ##
// ##
//
//
//
//
//--------
//
// #
// #
// ###
//
//
//
//
//--------
//:END:
arr field { int 64 }
arr field2 { int 64 }
1 0 coordToIndex 1 field.write
2 1 coordToIndex 1 field.write
0 2 coordToIndex 1 field.write
1 2 coordToIndex 1 field.write
2 2 coordToIndex 1 field.write
printArray
true 3
while
{
// i
iteration
0 dup 1 > -
}
deq
function => iteration
{
true 0
while
{
// y
true 0 req
// true x y
while
{
// x y
dup swp req
// y x x
dup swp
// x y x y
neighbourhood
// x y c
dup swp req
// x y x c
req dup swp req
// x y x y c
coordToIndex req req req
// i x y c
dup field.read
// x y c i v
req req req swp
// x y c v i
req req
// c v i x y
update
// x y
check
req
// lt x+1 y
}
// x y
deq check
// lt y+1
}
deq
copyArrays
printArray
}
function => copyArrays
{
true 0
while
{
// 0
dup field2.read
dup field.write
checkArr
}
deq
}
function => printArray
{
true 0
while
{
// y
true 0 req
// true x y
while
{
// x y
dup req dup swp
// x y x y
coordToIndex
// x y i
req req field.read
// x y v
req req printSym
// x y
check req
// lt x+1 y
}
// x y
"" deq req println check
// lt y+1
}
deq
"--------" println
}
function int => printSym
{
// x
1 ==
// eq
if
{
"#"
}
else
{
" "
}
print
}
function int => bool int checkArr
{
// i
1 + field.length dup
// i+1 l i+1
< req
// lt i+1
}
function int => bool int check
{
//???
// i
1 + 8 dup < req
//???
// i+1 8 i+1
}
function int int int => update
{
// count value index
3 dup req req req
// 3 c c v i
== req req req
// eq c v i
if
{
1 deq deq field2.write
}
else
{
// c v i
2 req req req
// 2 c v i
== 1 req req req
// 1 v i eq
== req && req
// teq i
if
{
1
}
else
{
0
}
field2.write
}
}
function int int => int neighbourhood
{
// x y
dup 1 req req -
// x y x-1
0 req req dup >=
// x y x-1 ge
req req req
// ge x y x-1
if
{
req dup swp
// x y x-1 y
req req
// x-1 y x y
row
// x y abc
}
else
{
// x y x-1
req req
// x-1 x y
deq 0
// x y 0
}
dup swp req req
// y x abc x
dup swp req row
// x y abc def
req req +
// x y abcdef
dup swp req req
// y x abcdef x
dup swp req
// x y x y abcdef
coordToIndex
// x y abcdef i
req req req
// i x y abcdef
field.read
// x y abcdef v
req req -
// x y abcdef-v
1 req req req
// 1 x y abcdef
+ req req
// x+1 y abcdef
dup 8 req req req
// x+1 l x+1 y abcdef
< req req req
// lt x+1 y abcdef
if
{
row
// abcdef ghi
}
else
{
deq deq 0
// abcdef 0
}
+
// abcdefghi
}
function int int => int row
{
// x y
1 req dup -
// x y y-1
dup req req
// y-1 x x y
0 dup swp req req
// 0 y-1 x y-1 x y
> req req req req
// gt x y-1 x y
if
{
deq deq 0
// x y 0
}
else
{
// x y-1 x y
coordToIndex req req
// i x y
field.read
// x y a
}
// x y a
dup swp req req
// y x a x
dup swp req
// x y x y a
coordToIndex
// x y a i
req req req field.read
// x y a b
req req +
// x y a+b
req 1 req
// a+b x 1 y
req req + req
// x y+1 a+b
req req 8
// a+b x y+1 l
req req dup
// y+1 l a+b x y+1
< req req req
// lt a+b x y+1
if
{
// a+b x y+1
req coordToIndex
// a+b i
req field.read
// a+b c
}
else
{
req deq deq 0
// a+b 0
}
+
// a+b+c
}
function int int => int coordToIndex
{
8 req * +
}