From 98b109176a6416e16f1da1f7a9c99672b6c9a67a Mon Sep 17 00:00:00 2001 From: Sanel Zukan Date: Fri, 18 Jul 2008 12:34:35 +0000 Subject: [PATCH] Added range function Reimplemented iota via range Added global *args* variable that contains program/script arguments --- tools/capone/lib/common.ss | 22 +++++++++++++++++----- tools/capone/src/capone.cpp | 26 ++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/tools/capone/lib/common.ss b/tools/capone/lib/common.ss index 40c2ec1..a4ff656 100644 --- a/tools/capone/lib/common.ss +++ b/tools/capone/lib/common.ss @@ -83,15 +83,27 @@ (else (throw "Unsupported type in 'for' loop")))))) +;; +;; range function; returns a list of numbers in form [start end) +;; +;; Althought we could wrote this function cleanly without decrementors +;; using recursion call after 'cons', we would loose tail call optimization +;; yielding much slower function. +;; +(define (range start end) + (let loop ((s (- start 1)) + (e (- end 1)) + (lst '())) + (if (>= s e) + lst + (loop s (- e 1) (cons e lst))))) + ;; ;; iota function; returns a list of numbers ;; + (define (iota n) - (let loop ((n n) - (lst '())) - (if (= n 0) - lst - (loop (- n 1) (cons n lst))))) + (range 0 n)) ;; ;; function for easier timing diff --git a/tools/capone/src/capone.cpp b/tools/capone/src/capone.cpp index 0c11ffd..2d4696e 100644 --- a/tools/capone/src/capone.cpp +++ b/tools/capone/src/capone.cpp @@ -15,6 +15,8 @@ extern "C" { #define BASE_FILE "capone.init" #define CHECK_ARGV(argv, pshort, plong) ((strcmp(argv, pshort) == 0) || (strcmp(argv, plong) == 0)) +extern pointer reverse_in_place(scheme *sc, pointer term, pointer list); + const char* next_param(int curr, char** argv, int argc) { int j = curr + 1; if(j >= argc) @@ -33,7 +35,21 @@ void help(void) { puts(" -e, --eval [str] Evaluate given expression\n"); } -void do_file_or_expr(FILE* f, const char* expr, const char* dir) { +void register_args_var(scheme* sc, int argc, char** argv) { + pointer args = sc->NIL; + for(int i = 0; i < argc; i++) { + pointer v = mk_string(sc, argv[i]); + args = cons(sc, v, args); + } + + args = reverse_in_place(sc, sc->NIL, args); + scheme_define(sc, + sc->global_env, + mk_symbol(sc, "*args*"), + args); +} + +void do_file_or_expr(FILE* f, const char* expr, const char* dir, int argc, char** argv) { scheme sc; if(!scheme_init(&sc)) { puts("Unable to load interpreter!"); @@ -61,6 +77,8 @@ void do_file_or_expr(FILE* f, const char* expr, const char* dir) { /* define 'load-extension' function first */ scheme_define(&sc, sc.global_env, mk_symbol(&sc,"load-extension"), mk_foreign_func(&sc, scm_load_ext)); + register_args_var(&sc, argc, argv); + register_dbus_functions(&sc); register_re_functions(&sc); register_sys_functions(&sc); @@ -120,7 +138,7 @@ int main(int argc, char** argv) { } if(expr) { - do_file_or_expr(NULL, expr, l); + do_file_or_expr(NULL, expr, l, argc, argv); } else if(filename) { FILE* f = fopen(filename, "r"); if(!f) { @@ -128,12 +146,12 @@ int main(int argc, char** argv) { return 1; } - do_file_or_expr(f, NULL, l); + do_file_or_expr(f, NULL, l, argc, argv); fclose(f); } else { printf("\033[33mcapone " VERSION "\033[0m (based on tinyscheme 1.39)\n"); printf("Type \"(quit)\" or press Ctrl-C to exit interpreter when you are done."); - do_file_or_expr(stdin, NULL, l); + do_file_or_expr(stdin, NULL, l, argc, argv); } return 0;