Skip to content

Commit

Permalink
js, lua, perl, python, racket: Add number?, fn?, macro?
Browse files Browse the repository at this point in the history
  • Loading branch information
wasamasa committed Oct 11, 2017
1 parent 7cabea4 commit 9e1b175
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions js/core.js
Expand Up @@ -194,11 +194,14 @@ var ns = {'type': types._obj_type,
'nil?': types._nil_Q,
'true?': types._true_Q,
'false?': types._false_Q,
'number?': types._number_Q,
'string?': types._string_Q,
'symbol': types._symbol,
'symbol?': types._symbol_Q,
'keyword': types._keyword,
'keyword?': types._keyword_Q,
'fn?': types._fn_Q,
'macro?': types._macro_Q,

'pr-str': pr_str,
'str': str,
Expand Down
6 changes: 6 additions & 0 deletions js/types.js
Expand Up @@ -88,6 +88,7 @@ function _clone (obj) {
function _nil_Q(a) { return a === null ? true : false; }
function _true_Q(a) { return a === true ? true : false; }
function _false_Q(a) { return a === false ? true : false; }
function _number_Q(obj) { return typeof obj === 'number'; }
function _string_Q(obj) {
return typeof obj === 'string' && obj[0] !== '\u029e';
}
Expand Down Expand Up @@ -136,6 +137,8 @@ Function.prototype.clone = function() {
}
return temp;
};
function _fn_Q(obj) { return _function_Q(obj) && !obj._ismacro_; }
function _macro_Q(obj) { return _function_Q(obj) && !!obj._ismacro_; }


// Lists
Expand Down Expand Up @@ -205,13 +208,16 @@ exports._clone = types._clone = _clone;
exports._nil_Q = types._nil_Q = _nil_Q;
exports._true_Q = types._true_Q = _true_Q;
exports._false_Q = types._false_Q = _false_Q;
exports._number_Q = types._number_Q = _number_Q;
exports._string_Q = types._string_Q = _string_Q;
exports._symbol = types._symbol = _symbol;
exports._symbol_Q = types._symbol_Q = _symbol_Q;
exports._keyword = types._keyword = _keyword;
exports._keyword_Q = types._keyword_Q = _keyword_Q;
exports._function = types._function = _function;
exports._function_Q = types._function_Q = _function_Q;
exports._fn_Q = types._fn_Q = _fn_Q;
exports._macro_Q = types._macro_Q = _macro_Q;
exports._list = types._list = _list;
exports._list_Q = types._list_Q = _list_Q;
exports._vector = types._vector = _vector;
Expand Down
3 changes: 3 additions & 0 deletions lua/core.lua
Expand Up @@ -240,11 +240,14 @@ M.ns = {
['nil?'] = function(a) return a==Nil end,
['true?'] = function(a) return a==true end,
['false?'] = function(a) return a==false end,
['number?'] = function(a) return types._number_Q(a) end,
symbol = function(a) return types.Symbol:new(a) end,
['symbol?'] = function(a) return types._symbol_Q(a) end,
['string?'] = function(a) return types._string_Q(a) and "\177" ~= string.sub(a,1,1) end,
keyword = function(a) return "\177"..a end,
['keyword?'] = function(a) return types._keyword_Q(a) end,
['fn?'] = function(a) return types._fn_Q(a) end,
['macro?'] = function(a) return types._macro_Q(a) end,

['pr-str'] = pr_str,
str = str,
Expand Down
11 changes: 11 additions & 0 deletions lua/types.lua
Expand Up @@ -94,6 +94,11 @@ function M._nil_Q(obj)
return obj == Nil
end

-- Numbers
function M._number_Q(obj)
return type(obj) == "number"
end

-- Strings
function M._string_Q(obj)
return type(obj) == "string"
Expand Down Expand Up @@ -186,6 +191,12 @@ end
function M._malfunc_Q(obj)
return utils.instanceOf(obj, M.MalFunc)
end
function M._fn_Q(obj)
return type(obj) == "function" or (M._malfunc_Q(obj) and not obj.ismacro)
end
function M._macro_Q(obj)
return M._malfunc_Q(obj) and obj.ismacro
end

-- Atoms

Expand Down
5 changes: 4 additions & 1 deletion perl/core.pm
Expand Up @@ -8,7 +8,7 @@ use Time::HiRes qw(time);
use readline;
use types qw(_sequential_Q _equal_Q _clone $nil $true $false
_nil_Q _true_Q _false_Q
_symbol _symbol_Q _string_Q _keyword _keyword_Q _list_Q _vector_Q
_number_Q _symbol _symbol_Q _string_Q _keyword _keyword_Q _list_Q _vector_Q _sub_Q _function_Q
_hash_map _hash_map_Q _assoc_BANG _dissoc_BANG _atom_Q);
use reader qw(read_str);
use printer qw(_pr_str);
Expand Down Expand Up @@ -217,11 +217,14 @@ our $core_ns = {
'nil?' => sub { _nil_Q($_[0]->nth(0)) ? $true : $false },
'true?' => sub { _true_Q($_[0]->nth(0)) ? $true : $false },
'false?' => sub { _false_Q($_[0]->nth(0)) ? $true : $false },
'number?' => sub { _number_Q($_[0]->nth(0)) ? $true : $false },
'symbol' => sub { Symbol->new(${$_[0]->nth(0)}) },
'symbol?' => sub { _symbol_Q($_[0]->nth(0)) ? $true : $false },
'string?' => sub { _string_Q($_[0]->nth(0)) ? $true : $false },
'keyword' => sub { _keyword(${$_[0]->nth(0)}) },
'keyword?' => sub { _keyword_Q($_[0]->nth(0)) ? $true : $false },
'fn?' => sub { (_sub_Q($_[0]->nth(0)) || (_function_Q($_[0]->nth(0)) && !$_[0]->nth(0)->{ismacro})) ? $true : $false },
'macro?' => sub { (_function_Q($_[0]->nth(0)) && $_[0]->nth(0)->{ismacro}) ? $true : $false },

'pr-str' => sub { pr_str($_[0]) },
'str' => sub { str($_[0]) },
Expand Down
6 changes: 5 additions & 1 deletion perl/types.pm
Expand Up @@ -6,7 +6,7 @@ use feature qw(switch);
use Exporter 'import';
our @EXPORT_OK = qw(_sequential_Q _equal_Q _clone
$nil $true $false _nil_Q _true_Q _false_Q
_symbol _symbol_Q _string_Q _keyword _keyword_Q _list_Q _vector_Q
_number_Q _symbol _symbol_Q _string_Q _keyword _keyword_Q _list_Q _vector_Q _sub_Q _function_Q
_hash_map _hash_map_Q _assoc_BANG _dissoc_BANG _atom_Q);

use Data::Dumper;
Expand Down Expand Up @@ -103,6 +103,7 @@ sub _false_Q { return $_[0] eq $false }
package Integer;
sub new { my $class = shift; bless \do { my $x=$_[0] }, $class }
}
sub _number_Q { (ref $_[0]) =~ /^Integer/ }


{
Expand Down Expand Up @@ -213,6 +214,9 @@ sub _hash_map_Q { (ref $_[0]) =~ /^HashMap/ }
}
}

sub _sub_Q { (ref $_[0]) =~ /^CODE/ }
sub _function_Q { (ref $_[0]) =~ /^Function/ }


# FunctionRef

Expand Down
5 changes: 5 additions & 0 deletions python/core.py
Expand Up @@ -128,11 +128,16 @@ def swap_BANG(atm,f,*args):
'nil?': types._nil_Q,
'true?': types._true_Q,
'false?': types._false_Q,
'number?': types._number_Q,
'string?': types._string_Q,
'symbol': types._symbol,
'symbol?': types._symbol_Q,
'keyword': types._keyword,
'keyword?': types._keyword_Q,
'fn?': lambda x: (types._function_Q(x) and not hasattr(x, '_ismacro_')),
'macro?': lambda x: (types._function_Q(x) and
hasattr(x, '_ismacro_') and
x._ismacro_),

'pr-str': pr_str,
'str': do_str,
Expand Down
4 changes: 3 additions & 1 deletion python/mal_types.py
Expand Up @@ -68,6 +68,7 @@ def _string_Q(exp):
return len(exp) == 0 or exp[0] != _u("\u029e")
else:
return False
def _number_Q(exp): return type(exp) == int

# Symbols
class Symbol(str): pass
Expand All @@ -93,7 +94,8 @@ def fn(*args):
fn.__ast__ = ast
fn.__gen_env__ = lambda args: Env(env, params, args)
return fn
def _function_Q(f): return type(f) == type(function_Q)
def _function_Q(f):
return callable(f)

# lists
class List(list):
Expand Down
5 changes: 5 additions & 0 deletions racket/core.rkt
Expand Up @@ -57,11 +57,16 @@
'nil? _nil?
'true? (lambda (x) (eq? x #t))
'false? (lambda (x) (eq? x #f))
'number? number?
'symbol (lambda (s) (if (symbol? s) s (string->symbol s)))
'symbol? symbol?
'string? _string?
'keyword (lambda (s) (if (_keyword? s) s (_keyword s)))
'keyword? _keyword?
'fn? (lambda (s) (if (malfunc? s)
(not (malfunc-macro? s))
(procedure? s)))
'macro? (lambda (s) (and (malfunc? s) (malfunc-macro? s)))

'pr-str (lambda a (pr_lst a #t " "))
'str (lambda a (pr_lst a #f ""))
Expand Down

0 comments on commit 9e1b175

Please sign in to comment.