use racket lang for the body of macros?
Issue #2
closed
granted I am new to racket, but I'd like to use the racket string operations in the body of a macro, say for generating structs and their construction functions?
I would like a macro
(-> v x 10)
That when evaluated emits C++ like
v->x = 10;
I tried using format
but the emitted code is
format("~a->~a = ~a",v,x,x);
Comments (9)
-
reporter -
reporter - edited description
-
reporter - edited description
-
reporter I was able to generate
struct Vec { double x, y, z; }; Vec* mk_vec(double x,double y,double z) { Vec *v = new Vec(); v->x = x; v->y = y; v->z = z; return v;}
from
(code "struct Vec { double x, y, z; }" ) (defn "Vec*" mk_vec ("double x" "double y" "double z") (code "Vec *v = new Vec()") (-> v x x) (-> v y y) (-> v z z) (return v))
by adding
[(->) (format "~a->~a = ~a" (compile-expr (list-ref e 1)) (compile-expr (list-ref e 2)) (compile-expr (list-ref e 3)))]
Into
compile-expr
but I would like to add operators using macros. Currently reading aboutsyntax-case
-
repo owner In L++ 0.2.6, it became possible via
defmacro
.L++ Compiler 0.2.6 (C) 2014 KIM Taegyoon Enter code (EOF when done): (defmacro -> (var field value) `(code ,(format "~a->~a=~a" var field value))) (code "struct Vec { double x, y, z; }" ) (defn "Vec*" mk_vec ("double x" "double y" "double z") (code "Vec *v = new Vec()") (-> v x x) (-> v y y) (-> v z z) (return v)) (main) Compiled: #include <iostream> ; struct Vec { double x, y, z; }; Vec* mk_vec(double x,double y,double z) { Vec *v = new Vec(); v->x=x; v->y=y; v->z=z; return v;} ; int main(int argc, char **argv) { ; return 0;}; Current directory: C:\Users\art\Documents\proj\l++\ Output written to: a-out.cpp Binary written to: a-out #t
However,
(-> v z (+ z 1.0))
doesn't work for now. -
repo owner I think this issue is solved now.
In L++ 0.2.7, it became possible via
define-syntax-rule
andformat
.L++ Compiler 0.2.7 (C) 2014 KIM Taegyoon Enter code (EOF when done): (define-syntax-rule (-> var field value) (format "~a->~a=~a" var field value)) (code "struct Vec { double x, y, z; }" ) (defn "Vec*" mk_vec ("double x" "double y" "double z") (code "Vec *v = new Vec()") (-> v x x) (-> v y y) (-> v z z) (-> v z (+ z 1.0)) (return v)) (main) Compiled: #include <iostream> ; struct Vec { double x, y, z; }; Vec* mk_vec(double x,double y,double z) { Vec *v = new Vec(); v->x=x; v->y=y; v->z=z; v->z=(z+1.0); return v;} ; int main(int argc, char **argv) { ; return 0;}; Current directory: C:\Users\art\Documents\proj\l++\ Output written to: a-out.cpp Binary written to: a-out #t
As you see,
(-> v z (+ z 1.0))
works now. -
reporter excellent!
-
reporter - changed status to resolved
-
reporter - changed status to closed
- Log in to comment