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

[C] lisp to bbcode [BBCODE] [FAGGOTRY]

Name: Anonymous 2007-12-27 22:52


#include <stdio.h>
#include <string.h>

#define MAX_NESTING 128
#define MAX_POST_SIZE 4096

typedef struct {

    int id;
    const char * name;

} Tags;

typedef struct {

    int memory[MAX_NESTING];
    int * ptr;
    size_t elements;

} Stack;

void stackinit(Stack *);
int pushid(Stack *, int);
int popid(Stack *);

#define pushid(x,y) ((x)->elements++ == MAX_NESTING ? -1 : (*(x)->ptr++ = y))
#define popid(x) ((x)->elements ? (x)->elements--,*--(x)->ptr : 0)
#define stackinit(x) (void)((x)->elements = 0, (x)->ptr = (x)->memory)

int main(void) {

    char buf[MAX_POST_SIZE] = {0};
    char * p = buf;
    int c;
    size_t n;
    Stack stack;

    Tags tags[] = {
        { 0, ")"},
        { 1, "br"},
        { 2, "b"},
        { 3, "i"},
        { 4, "u"},
        { 5, "o"},
        { 6, "code"},
        { 7, "spoiler"} /*add more tags */
    };

    stackinit(&stack);


    fread(buf, 1, sizeof buf - 1, stdin);
    while(*p != 0) {
        if(*p == '\\' && p[1]) {
            putchar(p[1]);
            p += 2;
            continue;
        } else if(*p == '(') {
            if(p[1] == ' ') {
                p++;
                continue;
            }

            if(strtok(++p, " \n") == NULL) {
                fputs(p, stdout);
                break;
            } else {
                printf("[%s]", p);
               
                for(n = 0; n < sizeof tags / sizeof tags[0]; n++)
                    if(strcmp(p, tags[n].name) == 0)
                        if(pushid(&stack, tags[n].id) == -1) {
                            fprintf(stderr, "max nesting level reached\n");
                            return -1;
                        }
                p += strlen(p);
            }
        }
        else if(*p == ')') {
            c = popid(&stack);
            for(n = 0; n < sizeof tags / sizeof tags[0]; n++)
                if(c == tags[n].id) {
                    c ? printf("[/%s]", tags[n].name) : putchar(*tags[n].name);
                    break;
                }
        }

        else putchar(*p);

        p++;
    }

    putchar('\n');


    return 0;

}


Example:

$ cc foo.c -o a
$ ./a
(i (b this is bold and italic) and this just italic)
^D
[i][b]this is bold and italic[/b] and this just italic[/i]

Name: Anonymous 2007-12-28 0:40

(i (b (o (u EXPERT PROGRAMMER))))

Name: Anonymous 2007-12-28 0:47

push? pop?
What happened to the cons and cudders?

Name: Anonymous 2007-12-28 0:54

Why didn't you write that in lisp?

Name: Anonymous 2007-12-28 1:25

So let me get this straight, you wrote a C program to convert LISP code into BBCode code. What next, writing a Python interpreter in LISP in C to convert BBCode to Python in Python?

Name: Anonymous 2007-12-28 1:29

This actually isn't a bad idea

Name: Anonymous 2007-12-28 4:22

>>1
You're the man.  Not a malloc in sight.  Truly Enlightened C is winrar.

I'd have made Tags an anonymous type, however:

struct { int id; const char * name; } tags[] = {
        { 0, ")"},
        { 1, "br"},
        { 2, "b"},
        { 3, "i"},
        { 4, "u"},
        { 5, "o"},
        { 6, "code"},
        { 7, "spoiler"} /*add more tags */
    };

Name: Anonymous 2007-12-28 5:54

>>7
There are no mallocs only because he uses arbitrarily-sized buffers to hold the input and tag stack. And he didn't even make MAX_NESTING large enough to handle the total possible number of tags that MAX_POST_SIZE might contain. Not very enlightened.

Name: Anonymous 2007-12-28 6:03

>>8
It truly is EXPERT QUALITY.

Name: Anonymous 2007-12-28 10:15

You forgot [s] and [m].

Name: Anonymous 2007-12-28 11:39

peh, it'd be even faster to do in PERL with a couple regex's.

>>7
Hard-on for purely stack-allocated memory? I hope your fucking overly revered stack fucking overflows when you shit some nice large arrays on it because you were too fucking pussy to dynamically allocate them on the heap.

inb4 C99 variable static array sizes.

Name: Anonymous 2007-12-28 12:40

You absolute idiot, how dare you release code.

Here it is in lisp,

(defun bbcodeify (sexp) (when (listp sexp) (format t "~a~{~a~^ ~}~a" (car sexp) (cdr sexp) (car sexp)) (mapcar #'bbcodeify (cdr sexp)))

Name: Anonymous 2007-12-28 12:42

fuck.. excuse me in too stoned
(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))

* (bbcodeify '(i (b this is bold and italic) and this just italic))

"[I][B]THIS IS BOLD AND ITALIC[B] AND THIS JUST ITALIC[I]"

Name: Anonymous 2007-12-28 12:44

>>11
DO IT IN PERL WITH A COUPLE OF REGEX THEN YOU ALL-TALK-BUT-CANT-WALK FAGGOT!!!!!

Name: Anonymous 2007-12-28 13:49

>>11
Hard-on for purely stack-allocated memory? I hope your fucking overly revered stack fucking overflows when you shit some nice large arrays on it because you were too fucking pussy to dynamically allocate them on the heap.

No, I have a hard-on for static arrays.  I don't care much where the data is stored.  I prefer global arrays, though.  There is really little reason to use local variables.  Multithreading, perhaps.

Name: Anonymous 2007-12-28 13:50

this thread is hilarious

Name: Anonymous 2007-12-28 15:40

OP here
>>10
Only these two? I forgot more, intentionally.
(eg # or rem or br)
That's why I put that comment there.
Left it as an exercise to the reader :P

>>11
Hard-on for purely stack-allocated memory? I hope your fucking overly revered stack fucking overflows when you shit some nice large arrays on it because you were too fucking pussy to dynamically allocate them on the heap.
Too much of a fucking pussy?
Are you right on your mind you fucking moron?
Not only it would make the size of my code 3x bigger, do you want such snippet to eat 10MB from your memory because the programmer wasn't a pussy?
You're a moron, what I could have done is to read input in chunks, less than what it takes for my static stack to fill, parse, print on stdout and repeat.
No fucking need for (re)allocations.

>>12
Release code?
Do you see a licence there? Or my name? Or some makefile?
(why the fuck would anyone put a licence on that anyway)
What I wanted to do is write something that works in C in < 10 minutes, post it on /prog/ and see what code Anon would post.
(like your lisp snippet)
Your lisp snippet however takes the expression as a list, my snip takes it from stdin, there are advantages to my snip. (not that it would take a lot of code to change yours to do that)

>>15
Actually I see more reasons to use a local object than threads :P

Name: Anonymous 2007-12-28 16:06

>>7
[b]"Fail"[b]

Name: Anonymous 2007-12-28 16:30

>>18
This may surprise you, but you've just invented the recursive failing meme

Name: Anonymous 2007-12-28 17:17

Name: Anonymous 2007-12-28 17:26


C:\>gcc.exe foo.c -o a
foo.c:2:19: stdio.h: No such file or directory
foo.c:3:20: string.h: No such file or directory
foo.c:19: error: parse error before "size_t"
foo.c:19: warning: no semicolon at end of struct or union
foo.c:21: warning: data definition has no type or storage class
foo.c:23: error: parse error before '*' token
foo.c:24: error: parse error before '*' token
foo.c:25: error: parse error before '*' token
foo.c: In function `main':
foo.c:36: error: `size_t' undeclared (first use in this function)
foo.c:36: error: (Each undeclared identifier is reported only once
foo.c:36: error: for each function it appears in.)
foo.c:36: error: parse error before "n"
foo.c:50: error: `stack' undeclared (first use in this function)
foo.c:53: error: `stdin' undeclared (first use in this function)
foo.c:65: error: `NULL' undeclared (first use in this function)
foo.c:66: error: `stdout' undeclared (first use in this function)
foo.c:71: error: `n' undeclared (first use in this function)
foo.c:74: error: `stderr' undeclared (first use in this function)

C:\>


lol

Name: Anonymous 2007-12-28 17:31

>>21
Since you don't have stdio and string, what do you expect?
I bet you don't have the libc either.

Name: Anonymous 2007-12-28 17:40

module Language.BBCode.Parser
  (parseBBCode)
  where
import Language.BBCode
import Text.ParserCombinators.Parsec

bbcode = many (bbcodeElement <|> bbcodeText)
bbcodeElement = do element <- try bbcodeOpenElement
                   content <- bbcode
                   bbcodeCloseElement element
                   return (Element element content)
bbcodeOpenElement = do char '['
                       s <- many1 (oneOf "abcdefghijklmnopqrstuvwxyz_#*")
                       char ']'
                       return s
bbcodeCloseElement s = string ("[/" ++ s ++ "]")
bbcodeText = many1 (noneOf "[") >>= return . Text

parseBBCode bb = case parse bbcode "" bb of
                   Left  e  -> error $ show e
                   Right bs -> bs

Name: Anonymous 2007-12-28 17:41

>>17
use (read) you fucking idiot

Name: Anonymous 2007-12-28 17:44

(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))


(defun bbcodeify (sexp) (cond ((atom sexp) sexp) ((listp sexp) (format nil "[~a]~{~a~^ ~}[~a]" (car sexp) (mapcar #'bbcodeify (cdr sexp)) (car sexp)))))



Name: Anonymous 2007-12-28 17:44

>>25
you forgot close tags have [/x]

Name: Anonymous 2007-12-28 17:45

Name: Anonymous 2007-12-28 17:49

>>24
I know, read my post:
Your lisp snippet however takes the expression as a list, my snip takes it from stdin, there are advantages to my snip. (not that it would take a lot of code to change yours to do that)

Name: Anonymous 2007-12-28 17:57

php -r "while(print(preg_replace('/\((b|i|m|o|s|u|aa|br|sub|sup|code|spoiler) (.*)\)/','[$1]$2[/$1]',fgets(STDIN))));"

Doesn't understand nested sexps, it doesn't exit ever and it uses around 80% CPU.

LOL

Name: Anonymous 2007-12-28 18:02

/r/ enterprisey scalable Java solution

Name: Anonymous 2007-12-28 18:10

>>30
in before class LispToBBCodeTranslatorGeneratorFactory...

Name: Anonymous 2007-12-28 18:13

>>31
Was that supposed to be funny? :|

Name: Anonymous 2007-12-28 18:14

>>27
What?

Name: Anonymous 2007-12-28 18:14

>>32
:| awkwaaard

Name: Anonymous 2007-12-28 18:18

>>34
Was that supposed to be funny? C:\

Name: Anonymous 2007-12-28 18:18

Name: Anonymous 2007-12-28 18:19

:Λ)

Name: Anonymous 2007-12-28 18:22

php -r "while(print(preg_replace('/\((b|i|m|o|s|u|aa|br|sub|sup|code|spoiler) (.*)\)/','[$1]$2[/$1]',fgets(STDIN))));"

LOL

Name: Anonymous 2007-12-28 18:25

⌐_⌐

Name: Anonymous 2007-12-28 18:26

>>38
(b this should be bolded) this should not (b and this should be bolded too)

This is a problem which can't be evaluated with a regular expression - you need to use a stack-based grammar.

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