Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

c as sexps

Name: Anonymous 2007-09-28 20:34 ID:AmQp9pj3

(cc->string
 `(include (lib "string.h")
           (lib "stdio.h"))
 
 `(defun ((void remchars)
          ((ptr char) str)
          (char c))
    (declare (size_t offset) 0)
    (while (&& (deref str) (!= (deref str) c))
      (inc! str))
    (for (_ (deref str) (inc! str))
       (while (= (ref str offset) c)
          (inc! offset))
       (set! (deref str) (ref str offset)))))

Annotations for this paste:

Annotation number 1:     gtk
Pasted by:     Cin
    1 second ago
   
Paste contents:   
Raw Source | Display As
(printf "~A\n"
 (cc->string
  `(include (lib "gtk/gtk.h"))

  `(defun ((static void callback)
           ((ptr GtkWidget) widget)
           (gpointer data))
     (g_print "Hello again - %s was pressed\n"
              (cast data (ptr gchar))))

  `(defun ((static gboolean delete_event)
           ((ptr GtkWidget) widget)
           ((ptr GdkEvent) event)
           (gpointer data))
     (gtk_main_quit)
     (return FALSE))

  `(defun ((int main)
           (int argc)
           ((ptr char) argv (array)))
     (declare ((ptr GtkWidget) window))
     (declare ((ptr GtkWidget) button))
     (declare ((ptr GtkWidget) box1))

     (gtk_init (address-of argc) (address-of argv))

     (set! window (gtk_window_new GTK_WINDOW_TOPLEVEL))

     (gtk_window_set_title (GTK_WINDOW window) "Hello Buttons!")

     (g_signal_connect (G_OBJECT window) "delete_event"
                       (G_CALLBACK delete_event) NULL)

     (gtk_container_set_border_width (GTK_CONTAINER window) 10)

     (set! box1 (gtk_hbox_new FALSE 0))
    
     (gtk_container_add (GTK_CONTAINER window) box1)

     (set! button (gtk_button_new_with_label "Button 1"))

     (g_signal_connect (G_OBJECT button) "clicked"
                       (G_CALLBACK callback) (cast "button 1" gpointer))

     (gtk_box_pack_start (GTK_BOX box1) button TRUE TRUE 0)

     (gtk_widget_show button)

     (set! button (gtk_button_new_with_label "Button 2"))

     (g_signal_connect (G_OBJECT button) "clicked"
                       (G_CALLBACK callback) (cast "button 2" gpointer))

     (gtk_box_pack_start (GTK_BOX box1) button TRUE TRUE 0)

     (gtk_widget_show button)

     (gtk_widget_show box1)

     (gtk_widget_show window)

     (gtk_main)

     (return 0))))

Name: Anonymous 2007-09-28 20:34 ID:AmQp9pj3

#include <gtk/gtk.h>

static void callback(GtkWidget* widget, gpointer data)
{
     g_print("Hello again - %s was pressed\n", (gchar*)data);
}

static gboolean delete_event(GtkWidget* widget, GdkEvent* event, gpointer data)
{
     gtk_main_quit();

     return(FALSE);
}

int main(int argc, char* argv [])
{
     GtkWidget* window;
     GtkWidget* button;
     GtkWidget* box1;
     gtk_init(&(argc), &(argv));

     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     gtk_window_set_title(GTK_WINDOW(window), "Hello Buttons!");

     g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);

     gtk_container_set_border_width(GTK_CONTAINER(window), 10);

     box1 = gtk_hbox_new(FALSE, 0);
     gtk_container_add(GTK_CONTAINER(window), box1);

     button = gtk_button_new_with_label("Button 1");
     g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"button 1");

     gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);

     gtk_widget_show(button);

     button = gtk_button_new_with_label("Button 2");
     g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"button 2");

     gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);

     gtk_widget_show(button);

     gtk_widget_show(box1);

     gtk_widget_show(window);

     gtk_main();

     return(0);
}

Name: Anonymous 2007-09-28 20:35 ID:AmQp9pj3

ARE YOU SCARED YET?

Name: Anonymous 2007-09-28 20:35 ID:eg8kWW2q

What's with all the backquotes?

Name: Anonymous 2007-09-28 20:49 ID:AmQp9pj3

>>4
i'm still experimenting

you pass the c->string procedure some quoted code, much like CL-WHO. e.g. `(html (head (title ,my-title))) or something

cool thing about this is you have access to all scheme stuff so you can do all sorts of syntactic abstraction that you don't get with plain c

it's pretty fun to look at C represented differently

Name: Anonymous 2007-09-28 21:09 ID:eg8kWW2q

>>5
I guess that's kind of fancy.

Name: Anonymous 2007-09-28 21:14 ID:jcJw9yO1

again lisp fails

Name: Anonymous 2007-09-28 21:19 ID:eg8kWW2q

>>7
How u figger?

Name: Anonymous 2007-09-28 21:53 ID:Heaven

>>8
I read that as "How are you, faggot?"

You should consider actually using real words.

Name: Anonymous 2007-09-28 22:00 ID:eg8kWW2q

>>9
You should consider using '`jauntier `' quotes.

Name: Anonymous 2007-09-28 22:02 ID:TCCtQZv7

i had once the same idea, but never implemented. that's awesome, because powerful macros are a great addition to c. can you try creating a simple object system?

Name: Anonymous 2007-09-28 22:10 ID:AOq2xx4s

>>10
stop using faggot quotes

Name: Anonymous 2007-09-28 22:25 ID:eg8kWW2q

>>11
By this point, why even make it look like C? Why not just use a Lisp to C compiler?

Name: Anonymous 2007-09-28 22:28 ID:AmQp9pj3

>>13
it's only about adding some syntactic abstraction to c while maintaining the low-level nature of its semantics (it's important to stress that bit). whether or not i'd finding writing c code in sexps really annoying, i don't know; but we'll see

Name: Anonymous 2007-09-28 22:33 ID:Heaven

>>4
fixed the backquotes:

> (display (ccode->string (include (lib "stdio.h")) (defun ((int main) void) (printf "Hello, World!\n") (return 0))))
#include <stdio.h>

int main(void)
{
     printf("Hello, World!\n");

     return(0);
}

Name: Anonymous 2007-09-28 22:36 ID:AmQp9pj3

i need some c code to test it with

Name: Anonymous 2007-09-28 22:38 ID:AmQp9pj3

[b]WHAT C CODE CAN I CONVERT TO CSEXP CODE?[/code]

Name: Anonymous 2007-09-28 22:38 ID:AmQp9pj3

>>17
lol'd at myself

Name: Anonymous 2007-09-28 23:09 ID:eg8kWW2q

>>14
Right, that's what I was telling >>11.

Name: Anonymous 2007-09-28 23:12 ID:Heaven

>>19
oh right

Name: Anonymous 2007-09-29 0:58 ID:ketYT9Kz

How do you do this code:

switch (count % 8)  /* count > 0 assumed */
{
   case 0:        do {  *to++ = *from++;
   case 7:              *to++ = *from++;
   case 6:              *to++ = *from++;
   case 5:              *to++ = *from++;
   case 4:              *to++ = *from++;
   case 3:              *to++ = *from++;
   case 2:              *to++ = *from++;
   case 1:              *to++ = *from++;
                     } while ((count -= 8) > 0);
}

Name: Anonymous 2007-09-29 1:01 ID:L7jAALXN

this is cool:


   (defmacro (with-drawing-context forms)
     (let ((hdc (caar forms))
           (hwnd (cadar forms))
           (ps (caddar forms))
           (body (cadr forms)))
       `(block
         (declare (HDC ,hdc))
         (declare (HWND ,hwnd))
         (declare (PAINTSTRUCT ,ps))
         (set! ,hdc (BeginPaint ,hwnd (address-of ,ps)))
         (begin ,body)
         (EndPaint ,hwnd (address-of ,ps)))))
 
   (with-drawing-context (hdc hwnd ps)
     (Rectangle hdc 0 0 50 50))


output:

{
     HDC hdc;
     HWND hwnd;
     PAINTSTRUCT ps;
     hdc = BeginPaint(hwnd, &(ps));
     Rectangle(hdc, 0, 0, 50, 50);
     EndPaint(hwnd, &(ps));
}

Name: Anonymous 2007-09-29 1:02 ID:Heaven

>>21
i'll try it

Name: Anonymous 2007-09-29 1:08 ID:HkeAyCtI

I think I'll do something like this for POV-Ray SDL, which is infinite fail.

Name: Anonymous 2007-09-29 1:12 ID:Heaven

>>21

(switch (% count 8)
  (case 0 (do)
     (start-block)
     (set! (deref (inc! to)) (deref (inc! from))))
  (case 7 (set! (deref (inc! to)) (deref (inc! from))))
  (case 6 (set! (deref (inc! to)) (deref (inc! from))))
  (case 5 (set! (deref (inc! to)) (deref (inc! from))))
  (case 4 (set! (deref (inc! to)) (deref (inc! from))))
  (case 3 (set! (deref (inc! to)) (deref (inc! from))))
  (case 2 (set! (deref (inc! to)) (deref (inc! from))))
  (case 1 (set! (deref (inc! to)) (deref (inc! from))))
   (end-block)
   (while (> (inc! count 8) 0)))

Name: Anonymous 2007-09-29 1:13 ID:Heaven

>>21
essentially you are just playing with source code, so you can insert (start-block) and (end-block) anywhere and it will put the { in that part of the code.

Name: Anonymous 2007-09-29 1:20 ID:ketYT9Kz

>>25

Cool.  I assume do and while check to see if they get a block and emit {} if they do?

Name: Anonymous 2007-09-29 1:21 ID:Heaven

>>27
yeah, likewise with 'for' and the rest

Name: Anonymous 2007-09-29 1:27 ID:Heaven

oh, heh. just realised i can totally get it to use the ternary operator (foo ? bar : mu) when you use (if foo bar mu) inside an expression

Name: Anonymous 2007-09-29 1:32 ID:ketYT9Kz

>>28

I was working on something like this once except I was making a GUI that made the sexps look like C again.  Of course it was storing structure so you could do macro stuff.

Name: Anonymous 2007-09-29 2:02 ID:L7jAALXN

>>30
what, like, sexps on one side and the corresponding C code on the other side?

Name: Anonymous 2007-09-29 5:10 ID:Heaven

>>31
No, what you edit looked like C, but it was actually storing sexps inside.  So it always had valid syntax but some spaces were blank.

For example, typing x inside a function produced a value and selected it:
x;  // SEXP: (var-reference x)
Typing '+' next would take the currently selected value and replace it with an addition expression with a blank spot (and select that spot):
x + ?; // SEXP: (+ (var-reference x) (blank))

The end result of this was that you edit it almost like C code, but it is actually doing transformations on program structure.  Actually this was a pretty cool project I think I will work on it again.

Name: Anonymous 2007-09-29 7:16 ID:Heaven

ugly crap fuck off

Name: Anonymous 2007-09-29 10:53 ID:Heaven

Thanks for posting, OP.
I've been looking for examples of Lisp imitating other languages. I remember a series of conference lectures about it a few years ago, but I can't find them now.

Name: Anonymous 2010-12-17 1:39

FOLLOW THE NEW GNAA TWITTER AT http://twitter.com/Gary_Niger

Name: Anonymous 2012-06-26 0:13

鐅牧瑥搠ℳᝃ㡔锓邖薇㔙ᦖ匡袄頱ᔕ塁喙礠鐁ᖗ㍓椗劅襦Ȩ㝄❵䔡™硈㦕堣⦗䌣⌱䔆䍵с㙉ʼn噢ᆙ䠡镧㐹٣敖蘷⍲玀͇暄❶Ÿ鈆ቄᑴ饙䡘䈑㝁覈〶⠴〧ᄡᕁ榓␦酩爰虢十萑坸䔗⥲䦃ᐄ剳ᅘ㦆猣㑕長┗餖蝣萓ࡓ晧鐣权鍠䙙┴锃⥂सݤ萲硄ㄈ䘩碉ᖈ选ᕸ鉵⤶⠒霄ㅈጡᒅ別Х㊀圀㍵ᝁĥ䅩陉䠙⦁䡀Ї荃榀䔇䘓艈鄀݄၈२䑖䑴顆㉕蔶ᔗ兇㌒━堗饱ဓ數舗ᡇ⌷最馆灣ጐ䂒偆炀㚁㌷腡㙑蒐ʁ兖硱䍣蘂᠖萵椉皃᝵碉牐ဵ遶ր刃ᙤ肐㦖戩䀧㑔Ɠ㤔᎗ॆ醈䘆醂獨慕Ѕ報杴衐䐀蜒を塢鄐禅᜵᎗℀䤥劂匲㉠鈰餵撖刉ƃ⌈鈲夘䤑荱皖阉锳䥈➘用蔳ᕃ砃碁䠥㐗员瘳刦鑥鎇朑❩⥗し㍣餩㐰聦〢㥀ᑁ焇䤹⌙ᙐ桡ŗ杶呓␂摹⑘ⅹ畇萢䁩脧噗艥㦉㘖蕹Ȁ蒔⠵䝩ↁᐱ匱⅘焑鍦⦇㈧则Ȓ№褩甂㤆㈇敂暕邒衦䜅᝘ܴ蜖

Name: Anonymous 2012-06-26 0:43

This is a pretty cool idea. I wonder if OP ever posted the source anywhere.

Don't change these.
Name: Email:
Entire Thread Thread List