head and tail now operate on strings
This commit is contained in:
29
functions.c
29
functions.c
@@ -271,6 +271,20 @@ lval* builtin_join(lenv* env, lval* val){
|
|||||||
}
|
}
|
||||||
lval* builtin_head(lenv* env, lval* val){
|
lval* builtin_head(lenv* env, lval* val){
|
||||||
LASSERT_ARG_COUNT("head", val, val, 1);
|
LASSERT_ARG_COUNT("head", val, val, 1);
|
||||||
|
|
||||||
|
if (val->cell_list[0]->type == LVAL_STR) { //Return first character
|
||||||
|
char* strVal = val->cell_list[0]->data.str;
|
||||||
|
char* result = calloc(1, sizeof(char));
|
||||||
|
if (strVal != NULL && strlen(strVal) > 0) {
|
||||||
|
result = calloc(2, sizeof(char));
|
||||||
|
result[0] = strVal[0];
|
||||||
|
}
|
||||||
|
lval_delete(val);
|
||||||
|
lval* resultLval = lval_str(result);
|
||||||
|
free(result);
|
||||||
|
return resultLval;
|
||||||
|
}
|
||||||
|
|
||||||
LASSERT_TYPE("head", val, val->cell_list[0], LVAL_Q_EXPR);
|
LASSERT_TYPE("head", val, val->cell_list[0], LVAL_Q_EXPR);
|
||||||
LASSERT_MIN_ARG_COUNT("head", val, val->cell_list[0], 1);
|
LASSERT_MIN_ARG_COUNT("head", val, val->cell_list[0], 1);
|
||||||
|
|
||||||
@@ -280,6 +294,21 @@ lval* builtin_head(lenv* env, lval* val){
|
|||||||
}
|
}
|
||||||
lval* builtin_tail(lenv* env, lval* val){
|
lval* builtin_tail(lenv* env, lval* val){
|
||||||
LASSERT_ARG_COUNT("tail",val , val, 1);
|
LASSERT_ARG_COUNT("tail",val , val, 1);
|
||||||
|
|
||||||
|
if (val->cell_list[0]->type == LVAL_STR) { //Return last character
|
||||||
|
char* strVal = val->cell_list[0]->data.str;
|
||||||
|
size_t strLength = strlen(strVal);
|
||||||
|
char* result = calloc(1, sizeof(char));
|
||||||
|
if (strVal != NULL && strLength > 0) {
|
||||||
|
result = calloc(2, sizeof(char));
|
||||||
|
result[0] = strVal[strLength-1];
|
||||||
|
}
|
||||||
|
lval_delete(val);
|
||||||
|
lval* resultLval = lval_str(result);
|
||||||
|
free(result);
|
||||||
|
return resultLval;
|
||||||
|
}
|
||||||
|
|
||||||
LASSERT_TYPE("tail", val, val->cell_list[0], LVAL_Q_EXPR);
|
LASSERT_TYPE("tail", val, val->cell_list[0], LVAL_Q_EXPR);
|
||||||
LASSERT_MIN_ARG_COUNT("tail", val, val->cell_list[0], 1);
|
LASSERT_MIN_ARG_COUNT("tail", val, val->cell_list[0], 1);
|
||||||
|
|
||||||
|
|||||||
1
main.c
1
main.c
@@ -9,7 +9,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <editline/readline.h>
|
#include <editline/readline.h>
|
||||||
#include <editline/history.h>
|
|
||||||
|
|
||||||
#include "mpc.h"
|
#include "mpc.h"
|
||||||
#include "lang.h"
|
#include "lang.h"
|
||||||
|
|||||||
3
util.c
3
util.c
@@ -4,7 +4,8 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
char * strdup(char* s) {
|
char * strdup(char* s) {
|
||||||
char *d = calloc(1, strlen (s) + 1);
|
if (s == NULL) return NULL;
|
||||||
|
char *d = calloc(strlen (s) + 1, sizeof(char));
|
||||||
if (d == NULL) return NULL;
|
if (d == NULL) return NULL;
|
||||||
strcpy (d,s);
|
strcpy (d,s);
|
||||||
return d;
|
return d;
|
||||||
|
|||||||
Reference in New Issue
Block a user