metareflection / dolorem-c Goto Github PK
View Code? Open in Web Editor NEWDolorem with a C target
Dolorem with a C target
I am trying to define a defmacro
variant that takes its arguments instead of hard-coding an args
parameter. This is how it's used:
(defmacro2 (add left-arg right-arg) (progn
(:= left (lower left-arg))
(:= right (lower right-arg))
(add-cexp left right)
(set-expression left (print-to-mem "(%s) + (%s)" (get-expression left) (get-expression right)))
left))
Arguably, it's rather redundant compared to just using args
directly since we need to name things after lowering anyways. But it's an exercise for me to play with the system.
This is how I defined it, hardcoding two arguments:
(defmacro comment
(make-cexp "" "" "" ""))
(comment "how to generalize to N arguments -- need a recursive builder")
(defmacro defmacro2 (progn
(comment "why is it an error to wrap an expect-ident around the RHS of name")
(:= name (car (car args)))
(:= params (cdr (car args)))
(:= param1 (car params))
(:= val1 (cons (make-ident-val "car") (cons (make-ident-val "args") (make-nil-val))))
(:= param2 (car (cdr params)))
(:= val2 (cons (make-ident-val "car") (cons (cons (make-ident-val "cdr") (cons (make-ident-val "args") (make-nil-val))) (make-nil-val))))
(:= body (car (cdr args)))
(call-macro "defmacro" (cons
name
(cons
(cons (make-ident-val "progn")
(cons (cons (make-ident-val ":=") (cons param1 (cons val1 (make-nil-val))))
(cons (cons (make-ident-val ":=") (cons param2 (cons val2 (make-nil-val))))
(cons body (make-nil-val)))))
(make-nil-val))))
))
Do you know why I am not supposed to use expect-ident
on the name?
Next, I'd like to experiment with the variant for any number of arguments. It will require recursion. Any tips?
Thanks.
% ./dolorem a.dlr
compiling the following: [|||void* make_cexp(void* a, void* before, void* global, void* header);]
compiling the following: [|||void* add_cexp(void* a, void* b);]
compiling the following: [|||void append_cexp(void* a, char* a, char* before, char* global, char* header);]
compiling the following: [|||void* call_macro(char* name, void* e);]
compiling the following: [|||void lower_compile(void* a);]
compiling the following: [|||void* car(void* a);]
compiling the following: [|||char* get_expression(void* a);]
compiling the following: [|||char* get_context(void* a);]
compiling the following: [|||char* get_global(void* a);]
compiling the following: [|||char* get_header(void* a);]
compiling the following: [|||void set_expression(void* a, char* b);]
compiling the following: [|||void* cdr(void* a);]
compiling the following: [|||void* lower(void* a);]
compiling the following: [|||void* cdr(void* a);]
compiling the following: [|||void* cons(void* a, void* b);]
compiling the following: [|||char* expect_ident(void* a);]
compiling the following: [|||char* expect_type(void* a);]
compiling the following: [|||void* make_nil_val();]
compiling the following: [|||void* make_int_val(long i);]
compiling the following: [|||void* make_string_val(char* i);]
compiling the following: [|||void* make_ident_val(char* i);]
compiling the following: [|||void* make_char_val(char i);]
compiling the following: [|||char* print_to_mem(char* fmt, ...);]
compiling the following: [|||void lower_mark_as_macro(char* a);]
compiling the following: [|||void puts(char* a);]
compiling the following: [|||int printf(char* fmt, ...);]
compiling the following: [||void* defun(void* args) {
lower_compile(call_macro("function", args));
return make_cexp("", "", "", "");
}
|void* defun(void* args);]
tcc: error: file 'crti.o' not found
tcc: error: library 'c' not found
tcc: error: file 'crtn.o' not found
: ERR: linking failure: dlopen(/tmp/doloutDLXOM9, 0x000A): tried: '/tmp/doloutDLXOM9' (), '/private/tmp/doloutDLXOM9' ()
% make
gcc -c -Wall -Wextra -fpic main.c -o main.o -g -O0
gcc -c -Wall -Wextra -fpic list.c -o list.o -g -O0
gcc -c -Wall -Wextra -fpic cgen.c -o cgen.o -g -O0
gcc -c -Wall -Wextra -fpic hashmap.c -o hashmap.o -g -O0
gcc -c -Wall -Wextra -fpic include.c -o include.o -g -O0
gcc -c -Wall -Wextra -fpic lower.c -o lower.o -g -O0
lower.c:8:10: fatal error: 'libtcc.h' file not found
#include <libtcc.h>
^~~~~~~~~~
1 error generated.
make: *** [Makefile:9: lower.o] Error 1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.