summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/extended_environment.rs66
-rw-r--r--src/main.rs48
2 files changed, 72 insertions, 42 deletions
diff --git a/src/extended_environment.rs b/src/extended_environment.rs
index d13cc2b..7457e00 100644
--- a/src/extended_environment.rs
+++ b/src/extended_environment.rs
@@ -1,11 +1,35 @@
+use oorandom;
use macroquad::prelude::*;
-use std::{cell::RefCell, collections::HashMap, rc::Rc};
-use rust_lisp::model::{Env, FloatType, RuntimeError, Symbol, Value};
+use std::{cell::RefCell, collections::HashMap, rc::Rc, vec::Vec};
+use rust_lisp::model::{Env, FloatType, IntType, RuntimeError, Symbol, Value};
use rust_lisp::utils::{require_arg, require_typed_arg};
pub type HashMapRc = Rc<RefCell<HashMap<Value, Value>>>;
-pub fn extend_environment(env: &Rc<RefCell<Env>>) {
+pub const BACKGROUND_COLOR : Color = Color::new(0.44, 0.76, 0.27, 1.0);
+pub const PAINTED_COLOR : Color = Color::new(0.06, 0.36, 0.20, 1.0);
+
+static mut RNG_INC: u64 = 0;
+
+pub fn extend_environment(env: &Rc<RefCell<Env>>) {
+ env.borrow_mut().define(
+ Symbol::from("randi-range"),
+ Value::NativeFunc(
+ |_env, args| {
+ let min = require_typed_arg::<IntType>("randi-range", &args, 0).unwrap();
+ let max = require_typed_arg::<IntType>("randi-range", &args, 1).unwrap();
+
+ unsafe {
+ let mut rng = oorandom::Rand32::new(RNG_INC);
+ let r = rng.rand_u32();
+ let r = r % ((max - min) as u32);
+ let r = (r as i32) + min;
+ RNG_INC += 1;
+ return Ok(Value::Int(r));
+ }
+ })
+ );
+
env.borrow_mut().define(
Symbol::from("mq-draw-circle"),
Value::NativeFunc(
@@ -13,13 +37,47 @@ pub fn extend_environment(env: &Rc<RefCell<Env>>) {
let x = require_typed_arg::<FloatType>("mq-draw-circle", &args, 0).unwrap();
let y = require_typed_arg::<FloatType>("mq-draw-circle", &args, 1).unwrap();
let radius = require_typed_arg::<FloatType>("mq-draw-circle", &args, 2).unwrap();
- let color = GREEN;
+ let color = PAINTED_COLOR;
draw_circle(x, y, radius, color);
return Ok(Value::NIL);
})
);
+ env.borrow_mut().define(
+ Symbol::from("mq-draw-rectangle"),
+ Value::NativeFunc(
+ |_env, args| {
+ let x = require_typed_arg::<FloatType>("mq-draw-square", &args, 0).unwrap();
+ let y = require_typed_arg::<FloatType>("mq-draw-square", &args, 1).unwrap();
+ let width = require_typed_arg::<FloatType>("mq-draw-square", &args, 2).unwrap();
+ let height = require_typed_arg::<FloatType>("mq-draw-square", &args, 3).unwrap();
+ let alpha = require_typed_arg::<FloatType>("mq-draw-square", &args, 4).ok();
+ let mut color = PAINTED_COLOR.clone();
+
+ if let Some(a) = alpha {
+ color.a = a;
+ }
+
+ draw_rectangle(x, y, width, height, color);
+ return Ok(Value::NIL);
+ })
+ );
+
+ env.borrow_mut().define(
+ Symbol::from("mq-is-key-pressed"),
+ Value::NativeFunc(|_env, args| {
+ let keycode = require_typed_arg::<IntType>("mq-get-key-pressed", &args, 0).unwrap();
+ let keys_down : Vec<i32> = get_keys_down().into_iter().map(|e| e as i32).collect();
+
+ if keys_down.contains(&keycode) {
+ return Ok(Value::True);
+ }
+
+ return Ok(Value::False);
+ }),
+ );
+
// Hashtable functions
// TODO: convert to elisp compatible hash table command
diff --git a/src/main.rs b/src/main.rs
index 62a3562..97f748d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,58 +6,30 @@ use rust_lisp::default_env;
use rust_lisp::lisp;
use rust_lisp::parser::parse;
use rust_lisp::interpreter::eval;
-use rust_lisp::model::{Symbol, Value};
+use rust_lisp::model::{Value};
mod extended_environment;
-#[macroquad::main("BasicShapes")]
+#[macroquad::main("Snake")]
async fn main() {
- // create a base environment
+ request_new_screen_size(34.0 * 18.0, 24.0 * 24.0);
let env = Rc::new(RefCell::new(default_env()));
// define new methods
extended_environment::extend_environment(&env);
- // parse into an iterator of syntax trees (one for each root)
- // let mut ast_iter = parse("(+ \"Hello \" \"world!\")");
- // let first_expression = ast_iter.next().unwrap().unwrap();
-
- // evaluate
- // let evaluation_result = eval(env.clone(), &first_expression).unwrap();
- // let mut ast_iter = parse("(test_draw)");
- // let first_expression = ast_iter.next().unwrap().unwrap();
-
- // let first_expression = lisp! {(test_draw)};
-
- // let mut inc = parse("(begin (set a 0) (+ a 1))");
- // let mut inc = parse("(begin (define a 0) (set a (+ a 1)))");
- // let inc_exp = inc.next().unwrap().unwrap();
-
- // let inc_exp = lisp! {
- // (begin (define a 0) (set a (+ a 1)))
- // };
- // eval(env.clone(), &inc_exp).unwrap();
-
- // let inc_exp = lisp! {
- // (set a (+ a 1))
- // };
-
- let gol_exp = parse(include_str!("../scripts/main.el"));
- for exp in gol_exp {
+ let main_script = parse(include_str!("../scripts/main.el"));
+ for exp in main_script {
eval(env.clone(), &exp.unwrap()).unwrap();
}
- // let mut inc = parse("(set a (+ a 1))");
- // let inc_exp = inc.next().unwrap().unwrap();
-
loop {
- clear_background(RED);
+ clear_background(extended_environment::BACKGROUND_COLOR);
- draw_line(40.0, 40.0, 100.0, 200.0, 15.0, BLUE);
- draw_rectangle(screen_width() / 2.0 - 60.0, 100.0, 120.0, 60.0, GREEN);
- eval(env.clone(), &first_expression).unwrap();
- let inc_eval = eval(env.clone(), &inc_exp).unwrap();
- draw_text(&format!("IT WORKS! {}!", &inc_eval), 20.0, 20.0, 30.0, DARKGRAY);
+ eval(env.clone(), &lisp! {
+ (update { Value::Float(get_frame_time()) } )
+ }).unwrap();
+ eval(env.clone(), &lisp! { (draw) }).unwrap();
next_frame().await
}