Commit 548d84a6 authored by Chris Müller's avatar Chris Müller
Browse files

Add list length primitive to runtime environment

parent 43376d6e
......@@ -19,18 +19,15 @@
#include "cherry/runtime.h"
#define DECLARE_FUN(Name) \
struct org_cherry_value* Name(struct org_cherry_environment* env, struct org_cherry_value* args)
// org.cherry.core
struct org_cherry_value* org_cherry_core_type(struct org_cherry_environment* env, struct org_cherry_value* args);
struct org_cherry_value* org_cherry_core_raise(struct org_cherry_environment* env, struct org_cherry_value* args);
struct org_cherry_value* org_cherry_core_cons(struct org_cherry_environment* env, struct org_cherry_value* args);
struct org_cherry_value* org_cherry_core_list(struct org_cherry_environment* env, struct org_cherry_value* args);
DECLARE_FUN(org_cherry_core_head);
DECLARE_FUN(org_cherry_core_tail);
struct org_cherry_value* org_cherry_core_head(struct org_cherry_environment* env, struct org_cherry_value* args);
struct org_cherry_value* org_cherry_core_tail(struct org_cherry_environment* env, struct org_cherry_value* args);
struct org_cherry_value* org_cherry_core_length(struct org_cherry_environment* env, struct org_cherry_value* args);
struct org_cherry_value* org_cherry_core_tuple(struct org_cherry_environment* env, struct org_cherry_value* args);
......
......@@ -202,6 +202,8 @@ extern struct org_cherry_value* org_cherry_symbol_catch;
#define IS_SELF_EVALUATING(value) \
(IS_BOOLEAN(value) || IS_FIXNUM(value) || IS_CHAR(value) || IS_STRING(value) || IS_FLOAT(value))
#define IS_LIST(obj) (IS_PAIR(obj) || IS_NULL(obj))
#define IS_VARIABLE(value) \
IS_SYMBOL(value)
......
......@@ -87,6 +87,23 @@ org_cherry_core_tail(struct org_cherry_environment* env, struct org_cherry_value
}
struct org_cherry_value*
org_cherry_core_length(struct org_cherry_environment* env, struct org_cherry_value* args)
{
if(IS_NULL(args) || !IS_LIST(HEAD(args)))
org_cherry_env_raise(env, org_cherry_string("no list is given for the first operand"));
struct org_cherry_value* lst = HEAD(args);
cy_fixnum_t length = 0;
while(!IS_NULL(lst)) {
length++;
lst = TAIL(lst);
}
return org_cherry_fixnum(length);
}
struct org_cherry_value*
......
......@@ -97,6 +97,7 @@ org_cherry_environment(void)
proc_to_env(env, "list", org_cherry_core_list);
proc_to_env(env, "head", org_cherry_core_head);
proc_to_env(env, "tail", org_cherry_core_tail);
proc_to_env(env, "length", org_cherry_core_length);
proc_to_env(env, "tuple", org_cherry_core_tuple);
proc_to_env(env, "exit", org_cherry_system_exit);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment