mirror of
https://github.com/kanaka/mal.git
synced 2024-11-10 02:45:44 +03:00
go, rexx, vimscript, yorick: Add number?, fn?, macro?
This commit is contained in:
parent
2e13db6324
commit
7cecb87a7d
@ -20,6 +20,19 @@ func throw(a []MalType) (MalType, error) {
|
||||
return nil, MalError{a[0]}
|
||||
}
|
||||
|
||||
func fn_q(a []MalType) (MalType, error) {
|
||||
switch f := a[0].(type) {
|
||||
case MalFunc:
|
||||
return !f.GetMacro(), nil
|
||||
case Func:
|
||||
return true, nil
|
||||
case func([]MalType) (MalType, error):
|
||||
return true, nil
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// String functions
|
||||
|
||||
func pr_str(a []MalType) (MalType, error) {
|
||||
@ -464,6 +477,13 @@ var NS = map[string]MalType{
|
||||
"keyword?": func(a []MalType) (MalType, error) {
|
||||
return Keyword_Q(a[0]), nil
|
||||
},
|
||||
"number?": func(a []MalType) (MalType, error) {
|
||||
return Number_Q(a[0]), nil
|
||||
},
|
||||
"fn?": fn_q,
|
||||
"macro?": func(a []MalType) (MalType, error) {
|
||||
return MalFunc_Q(a[0]) && a[0].(MalFunc).GetMacro(), nil
|
||||
},
|
||||
|
||||
"pr-str": func(a []MalType) (MalType, error) { return pr_str(a) },
|
||||
"str": func(a []MalType) (MalType, error) { return str(a) },
|
||||
|
@ -41,6 +41,11 @@ func False_Q(obj MalType) bool {
|
||||
return ok && b == false
|
||||
}
|
||||
|
||||
func Number_Q(obj MalType) bool {
|
||||
_, ok := obj.(int)
|
||||
return ok
|
||||
}
|
||||
|
||||
// Symbols
|
||||
type Symbol struct {
|
||||
Val string
|
||||
|
@ -34,6 +34,15 @@ mal_keyword: procedure expose values. /* mal_keyword(a) */
|
||||
mal_keyword?: procedure expose values. /* mal_keyword?(a) */
|
||||
return new_boolean(keyword?(arg(1)))
|
||||
|
||||
mal_number?: procedure expose values. /* mal_number?(a) */
|
||||
return new_boolean(number?(arg(1)))
|
||||
|
||||
mal_fn?: procedure expose values. /* mal_fn?(a) */
|
||||
return new_boolean(nativefn?(arg(1)) | (func?(arg(1)) & (func_is_macro(arg(1)) \= 1)))
|
||||
|
||||
mal_macro?: procedure expose values. /* mal_macro?(a) */
|
||||
return new_boolean(func_macro?(arg(1)))
|
||||
|
||||
mal_pr_str: procedure expose values. /* mal_pr_str(...) */
|
||||
res = ""
|
||||
do i=1 to arg()
|
||||
@ -439,6 +448,9 @@ get_core_ns: procedure /* get_core_ns() */
|
||||
"symbol? mal_symbol?" ,
|
||||
"keyword mal_keyword" ,
|
||||
"keyword? mal_keyword?" ,
|
||||
"number? mal_number?" ,
|
||||
"fn? mal_fn?" ,
|
||||
"macro? mal_macro?" ,
|
||||
,
|
||||
"pr-str mal_pr_str" ,
|
||||
"str mal_str" ,
|
||||
|
1
rexx/t.mal
Normal file
1
rexx/t.mal
Normal file
@ -0,0 +1 @@
|
||||
(prn (+ 4 5))
|
@ -44,6 +44,9 @@ new_number: procedure /* new_number(n) */
|
||||
n = arg(1)
|
||||
return "numb_" || n
|
||||
|
||||
number?: procedure /* number?(obj) */
|
||||
return obj_type(arg(1)) == "numb"
|
||||
|
||||
new_nil: procedure /* new_nil() */
|
||||
return "nill_0"
|
||||
|
||||
|
@ -188,6 +188,9 @@ let CoreNs = {
|
||||
\ "string?": NewNativeFnLambda({a -> BoolNew(StringQ(a[0]))}),
|
||||
\ "keyword": NewNativeFnLambda({a -> KeywordNew(a[0].val)}),
|
||||
\ "keyword?": NewNativeFnLambda({a -> BoolNew(KeywordQ(a[0]))}),
|
||||
\ "number?": NewNativeFnLambda({a -> BoolNew(IntegerQ(a[0]))}),
|
||||
\ "fn?": NewNativeFnLambda({a -> BoolNew(NativeFunctionQ(a[0]) || FunctionQ(a[0]))}),
|
||||
\ "macro?": NewNativeFnLambda({a -> BoolNew(MacroQ(a[0]))}),
|
||||
\ "list": NewNativeFnLambda({a -> ListNew(a)}),
|
||||
\ "list?": NewNativeFnLambda({a -> BoolNew(ListQ(a[0]))}),
|
||||
\ "vector": NewNativeFnLambda({a -> VectorNew(a)}),
|
||||
|
@ -11,6 +11,13 @@ func mal_symbol(a) { return MalSymbol(val=a(1)->val); }
|
||||
func mal_symbol_q(a) { return new_boolean(structof(*a(1)) == MalSymbol); }
|
||||
func mal_keyword(a) { return MalKeyword(val=a(1)->val); }
|
||||
func mal_keyword_q(a) { return new_boolean(structof(*a(1)) == MalKeyword); }
|
||||
func mal_number_q(a) { return new_boolean(structof(*a(1)) == MalNumber); }
|
||||
func mal_fn_q(a)
|
||||
{
|
||||
if (structof(*a(1)) == MalNativeFunction) return MAL_TRUE;
|
||||
return new_boolean(structof(*a(1)) == MalFunction && !a(1)->macro);
|
||||
}
|
||||
func mal_macro_q(a) { return new_boolean(structof(*a(1)) == MalFunction && a(1)->macro); }
|
||||
|
||||
func string_helper(a, delimiter, readable)
|
||||
{
|
||||
@ -298,6 +305,9 @@ h_set, core_ns, "symbol", mal_symbol
|
||||
h_set, core_ns, "symbol?", mal_symbol_q
|
||||
h_set, core_ns, "keyword", mal_keyword
|
||||
h_set, core_ns, "keyword?", mal_keyword_q
|
||||
h_set, core_ns, "number?", mal_number_q
|
||||
h_set, core_ns, "fn?", mal_fn_q
|
||||
h_set, core_ns, "macro?", mal_macro_q
|
||||
|
||||
h_set, core_ns, "pr-str", mal_pr_str
|
||||
h_set, core_ns, "str", mal_str
|
||||
|
Loading…
Reference in New Issue
Block a user