123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #include <stdio.h>
- #include <stdlib.h>
- static unsigned interrupted = 0;
- #define ALLOC(s) alloc_mem(s)
- #define FREE(s) free_mem(s)
- typedef enum {
- NUM,
- SYMB,
- LIST,
- TABLE,
- LAMBDA
- } Type;
- typedef struct List {
- struct Atom *head;
- struct List *tail;
- } List;
- typedef struct Table {
- int *key;
- struct Atom *value;
- struct Table *next;
- } Table;
- typedef struct Atom {
- Type type;
- union {
- int num;
- int *symb;
- List *list;
- Table *table;
- };
- } Atom;
- typedef struct builtin_entry {
- int name[15];
- Atom *(*operation)(List*,Table**);
- } builtin_entry;
- #define EMPTY_STACK ": Empty stack"
- #define INSUFFICIENT_ARGS ": Insufficient arguments"
- #define INSUFFICIENT_ARGS_STK ": Insufficient arguments (stack)"
- #define INVALID_ARGS ": Invalid arguments"
- #define INVALID_ARGS_STK ": Invalid arguments (stack)"
- #define TOO_SHORT ": Too short"
- #define MUST_BE_INT ": Must be a integer"
- #define MUST_BE_SYM ": Must be a symbol"
- #define MUST_BE_LIST ": Must be a list"
- #define MUST_BE_SYMLIST ": Must be a symbol list"
- #define MUST_BE_TABLE ": Must be a table"
- #define MUST_BE_LAMBDA ": Must be a lambda"
- #define DUPLICATE_NULL ": Tried to duplicate a null"
- #define FREE_NULL ": Tried to free a null"
- #define EXTRA_PARENTHESIS ": Extra closing parenthesis"
- int _getchar();
- Atom* _warning(char*);
- Atom* _error(char*);
- void print_symbol(int*);
- void print_expression(Atom*);
- Atom* duplicate_atom(Atom*);
- List* duplicate_list(List*);
- Table* duplicate_table(Table*);
- int* duplicate_symbol(int*);
- void free_atom(Atom*);
- void free_list(List*);
- void free_table(Table*);
- List* cons(Atom*, List*);
- Atom* create_atom(Type);
- Atom* create_int(int);
- Atom* create_symbol(int*);
- Atom* create_list(List*);
- Atom* create_lambda(List*);
- void stack_push(Atom*, List**);
- void stack_pop(int, List**);
- int is_simple(Atom*);
- int is_list(Atom*);
- int is_symbol(Atom*);
- int valid_lambda(List*);
- unsigned is_symbol_list(List*);
- int skip_whitespace();
- Atom* parse_list();
- Atom* parse_symbol(int);
- Atom* parse_int(int);
- Atom* parse();
- unsigned symbols_are_equal(int*, int*);
- Table* table_lookup(Table*, int*);
- Table* table_set(Table**, int*, Atom*);
- void table_union(Table*, Table**);
- unsigned table_unset(Table**, int*);
- int table_size(Table*);
- unsigned equal(Atom*, Atom*);
- unsigned lists_are_equal(List*, List*);
- int list_size(List*);
- void* (builtin_lookup)(int*);
- Table* atom_lookup(int* name, Table* context);
- Atom* eval(Atom*, Table**);
- Atom* eval_symbol(int*, List*, Table**);
- Atom* eval_application(List*, Table**);
- Atom* eval_lambda(List*, List*, Table**);
- unsigned define_local_vars (List*, List*, Table**);
- void destroy_local_vars (List*, Table**);
- #define DECL_BUILTIN(x) \
- Atom* x (List*, Table**)
- #define BUILTIN(x) \
- Atom* x (List* li, Table** ctx)
- DECL_BUILTIN(_dup);
- DECL_BUILTIN(_drop);
- DECL_BUILTIN(_clear);
- DECL_BUILTIN(_swap);
- DECL_BUILTIN(_over);
- DECL_BUILTIN(_depth);
- DECL_BUILTIN(_pick);
- DECL_BUILTIN(_head);
-
-
- DECL_BUILTIN(_tip);
-
-
- DECL_BUILTIN(_tail);
-
-
- DECL_BUILTIN(_first);
-
-
- DECL_BUILTIN(_last);
-
-
- DECL_BUILTIN(_get);
-
-
- DECL_BUILTIN(_put);
-
-
-
- DECL_BUILTIN(_find);
-
-
- DECL_BUILTIN(_match);
-
- DECL_BUILTIN(_slice);
-
- DECL_BUILTIN(_concat);
-
- DECL_BUILTIN(_append);
-
- DECL_BUILTIN(_prepend);
-
- DECL_BUILTIN(_map);
- DECL_BUILTIN(_apply);
- DECL_BUILTIN(_reduce);
- DECL_BUILTIN(_filter);
- DECL_BUILTIN(_quote);
- DECL_BUILTIN(_unquote);
- DECL_BUILTIN(_hash);
- DECL_BUILTIN(_concat);
- DECL_BUILTIN(_split);
- DECL_BUILTIN(_list);
- DECL_BUILTIN(_lambda);
- DECL_BUILTIN(_table);
- DECL_BUILTIN(_sum);
- DECL_BUILTIN(_sub);
- DECL_BUILTIN(_mul);
- DECL_BUILTIN(_div);
- DECL_BUILTIN(_rem);
- DECL_BUILTIN(_power);
- DECL_BUILTIN(_eq);
- DECL_BUILTIN(_lt);
- DECL_BUILTIN(_le);
- DECL_BUILTIN(_gt);
- DECL_BUILTIN(_ge);
- DECL_BUILTIN(_and);
- DECL_BUILTIN(_or);
- DECL_BUILTIN(_xor);
- DECL_BUILTIN(_not);
- DECL_BUILTIN(_if);
- DECL_BUILTIN(_when);
- DECL_BUILTIN(_case);
- DECL_BUILTIN(_conds);
- DECL_BUILTIN(_for);
- DECL_BUILTIN(_while);
- DECL_BUILTIN(_recur);
- DECL_BUILTIN(_do);
- DECL_BUILTIN(_eval);
- DECL_BUILTIN(_print);
- DECL_BUILTIN(_equal);
- DECL_BUILTIN(_stack);
- DECL_BUILTIN(_help);
- DECL_BUILTIN(_catalog);
- DECL_BUILTIN(_user_error);
- DECL_BUILTIN(_user_warning);
- DECL_BUILTIN(_option);
- DECL_BUILTIN(_quit);
- DECL_BUILTIN(_trap);
|