変換例
入力 ;※defineの代わりにdefinlineと書くとinline展開をしてくれる(とする。) (definline (ntab indent) (if (> indent 0) (begin (display " ") (ntab (- indent 1)) )))
;出力の様なインデントを付ける ;入力はCコードを文字列にしたものを与える (define (c-indent s) (let loop ((si 0)(i 0)(tab? #f)(indent 0)(quote? #f)) (if (< i (string-length s)) (begin (cond ((and quote? (char=? #\\ (string-ref s i))) (loop si (+ i 1) tab? indent (if (pair? quote?) (cdr quote?) (cons 'escape quote?))) ) ((or (char=? #\" (string-ref s i)) (char=? #\' (string-ref s i))) ; (puts 'enter (string-ref s i) quote?) (if quote? (if (or(eq? (car quote?) 'escape) (not(eqv? (cdr quote?) (string-ref s i))) ) (loop si (+ i 1) tab? indent (cdr quote?)) (loop si (+ i 1) tab? indent #f) ) (loop si (+ i 1) tab? indent (string-ref s i)) ) ) ((and (not quote?) (char=? #\newline (string-ref s i))) (if tab? (ntab indent)) (display (substring s si i)) (newline) (loop (+ i 1) (+ i 1) #t indent quote?) ) ((and (not quote?) (char=? #\{ (string-ref s i))) ; (puts 'enter "{" quote?) (ntab indent) (loop si (+ i 1) #f (+ indent 1) quote?) ) ((and (not quote?) (char=? #\} (string-ref s i))) ; (puts 'enter "}" quote?) (loop si (+ i 1) tab? (- indent 1) quote?) ) (else (if (and quote? (eq? (car quote?) 'escape)) (loop si (+ i 1) tab? indent (cdr quote?)) (loop si (+ i 1) tab? indent quote?) ) )) ) (if (< si(string-length s)) (begin (if tab? (ntab indent)) (display (substring s si)) (newline) ) #t )))) 出力(c-indentでインデントしたもの) pointer c_indent(pointer s) { pointer result = scheme_NIL; c_indent: { pointer si = 0; pointer i = 0; pointer tabp = scheme_F; pointer indent = 0; pointer quotep = scheme_F; loop: if (less(i,string_length(s))) { { pointer g223 = scheme_NIL; if (quotep) { g223 = chareqp('\\',string_ref(s,i)); } else { g223 = scheme_F; } { pointer g222 = g223; if (g222) { pointer g225 = scheme_NIL; if (pairp(quotep)) { g225 = cdr(quotep); } else { g225 = cons(make_symbol("escape"),quotep); } { pointer g224 = g225; { pointer g226i = i + 1 ; pointer g227quotep = g224; i = g226i; quotep = g227quotep; } goto loop; } } else { pointer g229 = scheme_NIL; pointer res = chareqp('"',string_ref(s,i)); if (res) { g229 = res; } else { g229 = chareqp('\'',string_ref(s,i)); } { pointer g228 = g229; if (g228) { if (quotep) { pointer g231 = scheme_NIL; pointer res = eqp(car(quotep),make_symbol("escape")); if (res) { g231 = res; } else { g231 = not(eqvp(cdr(quotep),string_ref(s,i))); } { pointer g230 = g231; if (g230) { pointer g232i = i + 1; pointer g233quotep = cdr(quotep); i = g232i; quotep = g233quotep; goto loop; } else { pointer g234i = i + 1; pointer g235quotep = scheme_F; i = g234i; quotep = g235quotep; goto loop; } } } else { pointer g236i = i + 1; pointer g237quotep = string_ref(s,i); i = g236i; quotep = g237quotep; goto loop; } } else { pointer g239 = scheme_NIL; if (not(quotep)) { g239 = chareqp('\n',string_ref(s,i)); } else { g239 = scheme_F; } { pointer g238 = g239; if (g238) { if (tabp) { pointer indent = indent; ntab: if (greater(indent,0)) { display(" "); indent = indent - 1; goto ntab; } } display(substring(s,si,i)); newline(); { pointer g241si = i + 1; pointer g242i = i + 1; pointer g243tabp = scheme_T; si = g241si; i = g242i; tabp = g243tabp; } goto loop; } else { pointer g245 = scheme_NIL; if (not(quotep)) { g245 = chareqp('{',string_ref(s,i)); } else { g245 = scheme_F; } { pointer g244 = g245; if (g244) { { pointer indent = indent; ntab: if (greater(indent,0)) { display(" "); indent = indent - 1; goto ntab; } } { pointer g247i = i + 1; pointer g248tabp = scheme_F; pointer g249indent = indent + 1; i = g247i; tabp = g248tabp; indent = g249indent; } goto loop; } else { pointer g251 = scheme_NIL; if (not(quotep)) { g251 = chareqp('}',string_ref(s,i)); } else { g251 = scheme_F; } { pointer g250 = g251; if (g250) { pointer g252i = i + 1; pointer g253indent = indent - 1; i = g252i; indent = g253indent; goto loop; } else { pointer g255 = scheme_NIL; if (quotep) { g255 = eqp(car(quotep),make_symbol("escape")); } else { g255 = scheme_F; } { pointer g254 = g255; if (g254) { pointer g256i = i + 1; pointer g257quotep = cdr(quotep); i = g256i; quotep = g257quotep; goto loop; } else { i = i + 1; goto loop; } } } } } } } } } } } } } } else if (less(si,string_length(s))) { if (tabp) { pointer indent = indent; ntab: if (greater(indent,0)) { display(" "); indent = indent - 1; goto ntab; } } display(substring(s,si)); result = newline(); } else { result = scheme_T; } } return result; }