^syntax for meta

This commit is contained in:
Erik Svedäng 2016-03-14 12:51:40 +01:00
parent 895ffaa3c2
commit 5141e4e47e
4 changed files with 36 additions and 22 deletions

View File

@ -230,6 +230,13 @@
(defn second [xs]
(nth xs 1))
(defn third [xs]
(nth xs 1))
(defn doc [x]
(meta-get x :doc))
(defn template [text substitutions]
(reduce (fn [t pair] (str-replace t (first pair) (str (second pair))))
text

View File

@ -17,3 +17,10 @@
;; (f s)))
;;(bake g)
;; ^doc "Hej på dig"
;; ^ann '(:fn () :void)
;; (defn f [] "yeah")

View File

@ -1493,7 +1493,7 @@ Obj *p_meta_set_BANG(Obj** args, int arg_count) {
o->meta = obj_new_environment(NULL);
}
env_assoc(o->meta, args[1], args[2]);
return nil;
return o;
}
Obj *p_meta_get(Obj** args, int arg_count) {

View File

@ -1,5 +1,7 @@
#include "reader.h"
#include <ctype.h>
#include "env.h"
#include "obj_string.h"
int read_line_nr;
int read_line_pos;
@ -247,6 +249,24 @@ Obj *read_internal(Obj *env, char *s, Obj *filename) {
return cons1;
}
}
else if(CURRENT == '^') {
read_pos++;
Obj *key_symbol = read_internal(env, s, filename);
if(key_symbol->tag != 'Y') {
eval_error = obj_new_string("Invalid key for meta data.");
return nil;
}
Obj *key = obj_new_keyword(key_symbol->s);
Obj *value = read_internal(env, s, filename);
Obj *form = read_internal(env, s, filename);
Obj *head = obj_new_symbol("meta-set!");
Obj *new_form = obj_list(head, form, key, value);
return new_form;
}
else if(is_ok_in_symbol(CURRENT, true)) {
int line = read_line_nr, pos = read_line_pos;
char name[512];
@ -257,27 +277,7 @@ Obj *read_internal(Obj *env, char *s, Obj *filename) {
}
name[i] = '\0';
Obj *symbol = obj_new_symbol(name);
obj_set_line_info(symbol, line, pos, filename);
/* &&dispatch_do, */
/* &&dispatch_let, */
/* &&dispatch_not, */
/* &&dispatch_or, */
/* &&dispatch_and, */
/* &&dispatch_quote, */
/* &&dispatch_while, */
/* &&dispatch_if, */
/* &&dispatch_match, */
/* &&dispatch_reset, */
/* &&dispatch_fn, */
/* &&dispatch_macro, */
/* &&dispatch_def, */
/* &&dispatch_defp, */
/* &&dispatch_ref, */
/* &&dispatch_catch, */
//printf("New symbol with dispatch_index %d\n", symbol->dispatch_index);
obj_set_line_info(symbol, line, pos, filename);
return symbol;
}
else if(CURRENT == ':') {