Skip to content

Commit

Permalink
working conways game of life
Browse files Browse the repository at this point in the history
  • Loading branch information
dvtate committed Jun 20, 2022
1 parent d1b7260 commit 55bdb4f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 86 deletions.
105 changes: 37 additions & 68 deletions examples/conway.s
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
let time = import('libtime.so');

// Useful stuff
let while = (:
let cond = i[0], act = i[1], break = o
if (cond(break), (:
act(break)
while(cond, act)
))
)

let foreach = (:
let list = i[0], action = i[1]
let index = 0, end = size(list)
Expand All @@ -15,39 +16,14 @@ let foreach = (:
index = index + 1
))
)

/*
let map = (:
let list = i[0], fn = i[1]
let list = copy(i[0])
let ret = foreach(i[0], (: ret[i[1]] = fn(i)))
if(ret == empty, list, ret)
)

let split = (:
let s = i[0], delim = i[1]
let ret = [""], ind = 0
let is_delim = (:
let c = i, ret = o
foreach(delim, (:
if (i[0] == c, (:
ret(1)
))
))
)
foreach(s, (:
if (is_delim(i[0]), (:
ret = ret + ""
), (:
))
))
);
*/

// Game State
let world = [
" ",
" ### # ",
Expand All @@ -61,67 +37,60 @@ let world = [
]

// Standins for proper shortcircuit operators
let or = (: if (i[0], i[0], i[1]) );
let and = (: if (i[0], i[1], 0 ) );
let or = (: if (i[0], i[0], i[1]) )
let and = (: if (i[0], i[1], 0 ) )

// Get number of neighbors for cell at given coord
let neighbors = (:
let x = i[0], y = i[1];
let x = i[0], y = i[1]

let s = and(world[x + 1], (: world[x + 1][y] == '#' ));
let n = and(x > 0, (: world[x - 1][y] == '#' ));
let w = world[x][y - 1] == '#';
let e = world[x][y + 1] == '#';
let se = and(world[x + 1], (: world[x + 1][y + 1] ));
let sw = and(world[x + 1], (: world[x + 1][y - 1] ));
let ne = and(x > 0, (: world[x - 1][y + 1] ));
let nw = and(x > 0, (: world[x - 1][y - 1] ));

//print(up, down, left, right);

let ret = 0;
foreach([n, s, e, w, ne, nw, se, sw], (:
let s = and(world[x + 1], (: world[x + 1][y] == '#' ))
let n = and(x > 0, (: world[x - 1][y] == '#' ))
let w = world[x][y - 1] == '#'
let e = world[x][y + 1] == '#'
let se = and(world[x + 1], (: world[x + 1][y + 1] == '#' ))
let sw = and(world[x + 1], (: world[x + 1][y - 1] == '#'))
let ne = and(x > 0, (: world[x - 1][y + 1] == '#' ))
let nw = and(x > 0, (: world[x - 1][y - 1] == '#' ))

let ret = 0
foreach([n, s, e, w, ne, nw, se, sw], (:
ret = ret + Num(i[0])
));
o(ret);
))
o(ret)
)

let print_world = (:
foreach(world, (:
print(i[0], empty());
print(i[0])
))
);

// Update the world based on rules of Conways game of life
let update_world = (:
let ret = copy(world);

foreach (world, (:
let row = i[1];
print(world[row]);
ret[row] = "";
//print(world[row]);
let row = i[1]
ret[row] = ""
foreach(world[row], (:
let col = i[1];

let ns = neighbors(row, col);

//print('cell', row, col, "neighbors", ns);
//print(world[row], world[row][col], ns);
// print('i', i);
let col = i[1]
let ns = neighbors(row, col)
ret[row] = ret[row] + if (
or(
and(i[0] == ' ', ns == 3),
and(i[0] == '#', or(ns == 2, ns == 3))),
'#', ' '
);
and(i[0] == ' ', ns == 3),
and(i[0] == '#', or(ns == 2, ns == 3))),
'#',
' '
)
))
))
world = ret
);

print_world()
update_world()
print('----------------------------')

print_world()
print('----------------------------')
// main loop
while ((: true ), (:
print_world()
update_world()
time.delay(0.04)
))

2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void print_help_msg(){
<<"scl exec <bytecode file> # Execute bytecode\n"
<<"scl eval <entry file> # Build and Exect programs\n";

#ifndef SCL_DEBUG_MSG
#ifndef SCL_DEBUG_MODE
std::cout <<"\nRecompile with debugging enabled for verbose output\n";
#else
std::cout <<"\nYou are using the debug version of SCL\n";
Expand Down
49 changes: 32 additions & 17 deletions vm/operators/internal_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,43 @@ namespace vm_util {

/// Numeric index, prolly list
Value index_value(Frame& f, Value& v, ValueTypes::int_t index) {
// Only accepts lists
if (!std::holds_alternative<ValueTypes::list_ref>(v.v)) {
if (std::holds_alternative<ValueTypes::list_ref>(v.v)) {
// Extract list
const auto &list = *std::get<ValueTypes::list_ref>(v.v);

// Python-style negative indices
if (index < 0)
index += list.size();

// Out of range
if (index >= list.size())
return Value();

// Return indexed value
return list[index];
} else if (std::holds_alternative<ValueTypes::str_t>(v.v)) {
// Extract string
const auto &str = std::get<ValueTypes::str_t>(v.v);

// Python-style negative indices
if (index < 0)
index += str.size();

// Out of range
if (index >= str.size())
return Value();

// Return indexed value
std::string ret = "a";
ret[0] = str[index];
return Value(ret);
} else {
f.rt->running->throw_error(gen_error_object(
"TypeError",
std::string("Numeric indicies only availible for lists, not ") + v.type_name(),
std::string("Numeric indices not available for type ") + v.type_name(),
f));
return Value();
}

// Extract list
const auto& list = *std::get<ValueTypes::list_ref>(v.v);

// Python-style negative indices
if (index < 0)
index += list.size();

// Out of range
if (index >= list.size())
return Value();

// Return indexed value
return list[index];
}

/// String index, prolly object
Expand Down

0 comments on commit 55bdb4f

Please sign in to comment.