use racket lang for the body of macros?

Issue #2 closed
phoniz created an issue

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)

  1. phoniz 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 about syntax-case

  2. Taegyoon Kim 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.

  3. Taegyoon Kim repo owner

    I think this issue is solved now.

    In L++ 0.2.7, it became possible via define-syntax-rule and format.

    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.

  4. Log in to comment