1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! This module contains predefined functions for the Rurtle environment.
//!
//! All functions that should be "built in" into the Rurtle language (all the
//! fundamental stuff) are defined here or in submodules.
pub use super::{Environment, Function, ResultType, RuntimeError};
pub use super::value::Value;
use super::Function::Native;
use std::collections::HashMap;

// Example function.
//
// The first argument is the `Environment` in which the function is called, the
// second argument is are the (already evaluated) arguments.
fn print(_: &mut Environment, args: &[Value]) -> ResultType {
    println!("{}", args[0]);
    Ok(Value::Nothing)
}

/// Helper macro to extract the given arguments.
///
/// The first parameter is the slice of arguments to match against, followed by
/// a list of patterns delimited by , and prefixed by `arg`. Finally there is
/// the => expr part which specifies what should happen with the arguments.
///
/// If a argument can't be matched with the given pattern, a
/// `Err(RuntimeError(..))` is returned.
///
/// # Example
///
/// ```
/// fn three_args(_: &mut Environment, args: &[Value]) -> ResultType {
///     get_args!(args, arg Value::Number(a),
///                     arg Value::String(ref b),
///                     arg Value::List(ref c), => {
///     Ok(Value::Nothing)
/// })
/// }
/// ```
///
/// *Note*: There is a comma even behind the last pattern, don't forget it or
/// the compiler will spit fire at you!
macro_rules! get_args {
    ($args:expr, $(arg $ps:pat,)* => $b:expr) => {
        get_args!(index 0, $args, $(arg $ps,)* => $b)
    };

    (index $ind:expr, $args:expr, arg $p:pat, $(arg $ps:pat,)* => $b:expr) => {
        {
            let arg = &$args[$ind];
            if let $p = *arg {
                get_args!(index $ind+1, $args, $(arg $ps,)* => $b)
            } else {
                Err(RuntimeError(format!("invalid argument: {:?}", arg)))
            }
        }
    };

    (index $ind:expr, $args:expr, => $b:expr) => { $b };
}

mod turtle;
mod env;
mod types;
mod string;

/// A helpful macro to construct a `HashMap`
macro_rules! map {
    ($($k:expr => $v:expr,) *) => {
        {
            let mut result = HashMap::new();
            $(result.insert($k.to_string(), $v);)*
            result
        }
    }
}

/// Return a HashMap of the built-in functions
pub fn default_functions() -> HashMap<String, Function> {
    map!{
        "PRINT" => Native(1, print),

        // Turtle control/draw functions
        "FORWARD" => Native(1, turtle::forward),
        "BACKWARD" => Native(1, turtle::backward),
        "LEFT" => Native(1, turtle::left),
        "RIGHT" => Native(1, turtle::right),
        "COLOR" => Native(3, turtle::color),
        "BGCOLOR" => Native(3, turtle::bgcolor),
        "CLEAR" => Native(0, turtle::clear),
        "PENDOWN" => Native(0, turtle::pendown),
        "PENUP" => Native(0, turtle::penup),
        "HOME" => Native(0, turtle::home),
        "REALIGN" => Native(1, turtle::realign),
        "HIDE" => Native(0, turtle::hide),
        "SHOW" => Native(0, turtle::show),
        "WRITE" => Native(1, turtle::write),
        "FLOOD" => Native(0, turtle::flood),

        // Environment functions to set variables
        "MAKE" => Native(2, env::make),
        "GLOBAL" => Native(2, env::global),
        // Other environment functions
        "SCREENSHOT" => Native(1, env::screenshot),
        "PROMPT" => Native(1, env::prompt),
        "THROW" => Native(1, env::throw),

        // Haskellesque names
        "HEAD" => Native(1, types::head),
        "TAIL" => Native(1, types::tail),
        // Logo (alias) names
        "FIRST" => Native(1, types::head),
        "BUTFIRST" => Native(1, types::tail),
        // other list functions
        "LENGTH" => Native(1, types::length), // also works for strings
        "ISEMPTY" => Native(1, types::isempty),
        "GETINDEX" => Native(2, types::getindex),
        "FIND" => Native(2, types::find),
        // conversion
        "NOT" => Native(1, types::not),
        "TONUMBER" => Native(1, types::tonumber),
        "TOSTRING" => Native(1, types::tostring),
        "NOTHING" => Native(0, types::nothing),

        // String manipulating functions
        "REPLACE" => Native(3, string::replace),
        "CONTAINS" => Native(2, string::contains),
        "CHARS" => Native(1, string::chars),
        "SPLIT" => Native(2, string::split),
    }
}