mescc: Import tinycc test suite.

* make.scm (check-scaffold-tinycc): New target.
* guile/guix/make.scm (<target>): Add baseline field.
  (method-check): Handle baseline.
* scaffold/tinycc/00_assignment.c: New file.
* scaffold/tinycc/00_assignment.expect: New file.
* scaffold/tinycc/01_comment.c: New file.
* scaffold/tinycc/01_comment.expect: New file.
* scaffold/tinycc/02_printf.c: New file.
* scaffold/tinycc/02_printf.expect: New file.
* scaffold/tinycc/03_struct.c: New file.
* scaffold/tinycc/03_struct.expect: New file.
* scaffold/tinycc/04_for.c: New file.
* scaffold/tinycc/04_for.expect: New file.
* scaffold/tinycc/05_array.c: New file.
* scaffold/tinycc/05_array.expect: New file.
* scaffold/tinycc/06_case.c: New file.
* scaffold/tinycc/06_case.expect: New file.
* scaffold/tinycc/07_function.c: New file.
* scaffold/tinycc/07_function.expect: New file.
* scaffold/tinycc/08_while.c: New file.
* scaffold/tinycc/08_while.expect: New file.
* scaffold/tinycc/09_do_while.c: New file.
* scaffold/tinycc/09_do_while.expect: New file.
* scaffold/tinycc/10_pointer.c: New file.
* scaffold/tinycc/10_pointer.expect: New file.
* scaffold/tinycc/11_precedence.c: New file.
* scaffold/tinycc/11_precedence.expect: New file.
* scaffold/tinycc/12_hashdefine.c: New file.
* scaffold/tinycc/12_hashdefine.expect: New file.
* scaffold/tinycc/13_integer_literals.c: New file.
* scaffold/tinycc/13_integer_literals.expect: New file.
* scaffold/tinycc/14_if.c: New file.
* scaffold/tinycc/14_if.expect: New file.
* scaffold/tinycc/15_recursion.c: New file.
* scaffold/tinycc/15_recursion.expect: New file.
* scaffold/tinycc/16_nesting.c: New file.
* scaffold/tinycc/16_nesting.expect: New file.
* scaffold/tinycc/17_enum.c: New file.
* scaffold/tinycc/17_enum.expect: New file.
* scaffold/tinycc/18_include.h: New file.
* scaffold/tinycc/18_include.c: New file.
* scaffold/tinycc/18_include.expect: New file.
* scaffold/tinycc/19_pointer_arithmetic.c: New file.
* scaffold/tinycc/19_pointer_arithmetic.expect: New file.
* scaffold/tinycc/20_pointer_comparison.c: New file.
* scaffold/tinycc/20_pointer_comparison.expect: New file.
* scaffold/tinycc/21_char_array.c: New file.
* scaffold/tinycc/21_char_array.expect: New file.
* scaffold/tinycc/22_floating_point.c: New file.
* scaffold/tinycc/22_floating_point.expect: New file.
* scaffold/tinycc/23_type_coercion.c: New file.
* scaffold/tinycc/23_type_coercion.expect: New file.
* scaffold/tinycc/24_math_library.c: New file.
* scaffold/tinycc/24_math_library.expect: New file.
* scaffold/tinycc/25_quicksort.c: New file.
* scaffold/tinycc/25_quicksort.expect: New file.
* scaffold/tinycc/26_character_constants.c: New file.
* scaffold/tinycc/26_character_constants.expect: New file.
* scaffold/tinycc/27_sizeof.c: New file.
* scaffold/tinycc/27_sizeof.expect: New file.
* scaffold/tinycc/28_strings.c: New file.
* scaffold/tinycc/28_strings.expect: New file.
* scaffold/tinycc/29_array_address.c: New file.
* scaffold/tinycc/29_array_address.expect: New file.
* scaffold/tinycc/30_hanoi.c: New file.
* scaffold/tinycc/30_hanoi.expect: New file.
* scaffold/tinycc/31_args.c: New file.
* scaffold/tinycc/31_args.expect: New file.
* scaffold/tinycc/32_led.c: New file.
* scaffold/tinycc/32_led.expect: New file.
* scaffold/tinycc/33_ternary_op.c: New file.
* scaffold/tinycc/33_ternary_op.expect: New file.
* scaffold/tinycc/34_array_assignment.c: New file.
* scaffold/tinycc/34_array_assignment.expect: New file.
* scaffold/tinycc/35_sizeof.c: New file.
* scaffold/tinycc/35_sizeof.expect: New file.
* scaffold/tinycc/36_array_initialisers.c: New file.
* scaffold/tinycc/36_array_initialisers.expect: New file.
* scaffold/tinycc/37_sprintf.c: New file.
* scaffold/tinycc/37_sprintf.expect: New file.
* scaffold/tinycc/38_multiple_array_index.c: New file.
* scaffold/tinycc/38_multiple_array_index.expect: New file.
* scaffold/tinycc/39_typedef.c: New file.
* scaffold/tinycc/39_typedef.expect: New file.
* scaffold/tinycc/40_stdio.c: New file.
* scaffold/tinycc/40_stdio.expect: New file.
* scaffold/tinycc/41_hashif.c: New file.
* scaffold/tinycc/41_hashif.expect: New file.
* scaffold/tinycc/42_function_pointer.c: New file.
* scaffold/tinycc/42_function_pointer.expect: New file.
* scaffold/tinycc/43_void_param.c: New file.
* scaffold/tinycc/43_void_param.expect: New file.
* scaffold/tinycc/44_scoped_declarations.c: New file.
* scaffold/tinycc/44_scoped_declarations.expect: New file.
* scaffold/tinycc/45_empty_for.c: New file.
* scaffold/tinycc/45_empty_for.expect: New file.
* scaffold/tinycc/46_grep.c: New file.
* scaffold/tinycc/46_grep.expect: New file.
* scaffold/tinycc/47_switch_return.c: New file.
* scaffold/tinycc/47_switch_return.expect: New file.
* scaffold/tinycc/48_nested_break.c: New file.
* scaffold/tinycc/48_nested_break.expect: New file.
* scaffold/tinycc/49_bracket_evaluation.c: New file.
* scaffold/tinycc/49_bracket_evaluation.expect: New file.
* scaffold/tinycc/50_logical_second_arg.c: New file.
* scaffold/tinycc/50_logical_second_arg.expect: New file.
* scaffold/tinycc/51_static.c: New file.
* scaffold/tinycc/51_static.expect: New file.
* scaffold/tinycc/52_unnamed_enum.c: New file.
* scaffold/tinycc/52_unnamed_enum.expect: New file.
* scaffold/tinycc/54_goto.c: New file.
* scaffold/tinycc/54_goto.expect: New file.
* scaffold/tinycc/55_lshift_type.c: New file.
* scaffold/tinycc/55_lshift_type.expect: New file.
* scaffold/tinycc/56_btype_excess-1.c: New file.
* scaffold/tinycc/56_btype_excess-1.expect: New file.
* scaffold/tinycc/57_btype_excess-2.c: New file.
* scaffold/tinycc/57_btype_excess-2.expect: New file.
* scaffold/tinycc/58_function_redefinition.c: New file.
* scaffold/tinycc/58_function_redefinition.expect: New file.
* scaffold/tinycc/59_function_array.c: New file.
* scaffold/tinycc/59_function_array.expect: New file.
* scaffold/tinycc/60_enum_redefinition.c: New file.
* scaffold/tinycc/60_enum_redefinition.expect: New file.
* scaffold/tinycc/61_undefined_enum.c: New file.
* scaffold/tinycc/61_undefined_enum.expect: New file.
* scaffold/tinycc/62_enumerator_redefinition.c: New file.
* scaffold/tinycc/62_enumerator_redefinition.expect: New file.
* scaffold/tinycc/63_local_enumerator_redefinition.c: New file.
* scaffold/tinycc/63_local_enumerator_redefinition.expect: New file.
* scaffold/tinycc/64_macro_nesting.c: New file.
* scaffold/tinycc/64_macro_nesting.expect: New file.
* scaffold/tinycc/67_macro_concat.c: New file.
* scaffold/tinycc/67_macro_concat.expect: New file.
* scaffold/tinycc/70_floating_point_literals.c: New file.
* scaffold/tinycc/70_floating_point_literals.expect: New file.
* scaffold/tinycc/71_macro_empty_arg.c: New file.
* scaffold/tinycc/71_macro_empty_arg.expect: New file.
* scaffold/tinycc/72_long_long_constant.c: New file.
* scaffold/tinycc/72_long_long_constant.expect: New file.
* scaffold/tinycc/73_arm64.c: New file.
* scaffold/tinycc/73_arm64.expect: New file.
* scaffold/tinycc/74_nocode_wanted.c: New file.
* scaffold/tinycc/74_nocode_wanted.expect: New file.
* scaffold/tinycc/75_array_in_struct_init.c: New file.
* scaffold/tinycc/75_array_in_struct_init.expect: New file.
* scaffold/tinycc/76_dollars_in_identifiers.c: New file.
* scaffold/tinycc/76_dollars_in_identifiers.expect: New file.
* scaffold/tinycc/77_push_pop_macro.c: New file.
* scaffold/tinycc/77_push_pop_macro.expect: New file.
* scaffold/tinycc/78_vla_label.c: New file.
* scaffold/tinycc/78_vla_label.expect: New file.
* scaffold/tinycc/79_vla_continue.c: New file.
* scaffold/tinycc/79_vla_continue.expect: New file.
* scaffold/tinycc/80_flexarray.c: New file.
* scaffold/tinycc/80_flexarray.expect: New file.
* scaffold/tinycc/81_types.c: New file.
* scaffold/tinycc/81_types.expect: New file.
* scaffold/tinycc/82_attribs_position.c: New file.
* scaffold/tinycc/82_attribs_position.expect: New file.
* scaffold/tinycc/83_utf8_in_identifiers.c: New file.
* scaffold/tinycc/83_utf8_in_identifiers.expect: New file.
* scaffold/tinycc/84_hex-float.c: New file.
* scaffold/tinycc/84_hex-float.expect: New file.
* scaffold/tinycc/85_asm-outside-function.c: New file.
* scaffold/tinycc/85_asm-outside-function.expect: New file.
* scaffold/tinycc/86_memory-model.c: New file.
* scaffold/tinycc/86_memory-model.expect: New file.
* scaffold/tinycc/87_dead_code.c: New file.
* scaffold/tinycc/87_dead_code.expect: New file.
* scaffold/tinycc/88_codeopt.c: New file.
* scaffold/tinycc/88_codeopt.expect: New file.
* scaffold/tinycc/89_nocode_wanted.c: New file.
* scaffold/tinycc/89_nocode_wanted.expect: New file.
* scaffold/tinycc/90_struct-init.c: New file.
* scaffold/tinycc/90_struct-init.expect: New file.
* scaffold/tinycc/91_ptr_longlong_arith32.c: New file.
* scaffold/tinycc/91_ptr_longlong_arith32.expect: New file.
* scaffold/tinycc/92_enum_bitfield.c: New file.
* scaffold/tinycc/92_enum_bitfield.expect: New file.
* scaffold/tinycc/93_integer_promotion.c: New file.
* scaffold/tinycc/93_integer_promotion.expect: New file.
* scaffold/tinycc/COPYING: New file.
* scaffold/tinycc/LICENSE: New file.

include
This commit is contained in:
Jan Nieuwenhuizen 2017-07-09 18:33:56 +02:00
parent 64db2eaf7d
commit 6a391df5e0
183 changed files with 5893 additions and 23 deletions

View file

@ -110,7 +110,9 @@
(hash target-hash (default #f)) ; string
(method target-method (default method-file)) ; <method>
(inputs target-inputs (default (list))) ; list
; For check targets
(baseline target-baseline (default #f)) ; string: file-name
(exit target-exit (default #f)) ; number
(signal target-signal (default #f))) ; number
@ -121,17 +123,20 @@
(let* ((inputs (target-inputs t))
(file-name (target-file-name (build (car inputs))))
(run file-name)
(baseline (target-baseline t))
(exit (target-exit t))
(signal (target-signal t))
(log (string-append file-name "-check.log")))
(format (current-error-port) " CHECK\t~a" (basename file-name))
(receive (output result)
;; FIXME: quiet MES tests are not fun
(if (string-prefix? "tests/" run) (values #f (system** run))
(gulp-pipe* run))
(if (string-prefix? "tests/" run) (values #f (system** run "arg1" "arg2" "arg3" "arg4" "arg5"))
(gulp-pipe* run "arg1" "arg2" "arg3" "arg4" "arg5"))
(if (file-exists? log) (delete-file log))
(if (or baseline (and output (not (string-null? output)))) (with-output-to-file log (lambda _ (display output))))
(if baseline (set! result (system* "diff" "-bu" baseline log)))
(let ((status (if (string? result) 0
(or (status:term-sig result) (status:exit-val result)))))
(if (not (string-null? output)) (with-output-to-file log (lambda _ (display output))))
(store #:add-file log)
(format (current-error-port) "\t[~a]\n"
(if (or (and signal (= status signal))
@ -209,10 +214,11 @@
((method-build method) method o))
(store #:add o #:key hash)))))
(define* (check name #:key (exit 0) (signal #f) (dependencies '()))
(define* (check name #:key baseline (exit 0) (signal #f) (dependencies '()))
(target (file-name (string-append "check-" name))
(method method-check)
(inputs (cons (get-target name) dependencies))
(baseline baseline)
(exit exit)
(signal signal)))
@ -267,13 +273,14 @@
"--include=mlibc/libc-gcc.c"
))
(define* (CC.gcc #:key (libc #t) (cc (if libc %CC %CC32)) (c-flags (if libc %C-FLAGS %C32-FLAGS)) (defines '()))
(define* (CC.gcc #:key (libc #t) (cc (if libc %CC %CC32)) (c-flags (if libc %C-FLAGS %C32-FLAGS)) (defines '()) (includes '()))
(method (name "CC.gcc")
(build (lambda (o t)
(let* ((input-files (map target-file-name (target-inputs t)))
(command `(,cc
"-c"
,@(append-map (cut list "-D" <>) defines)
,@(append-map (cut list "-I" <>) includes)
,@(if libc '() '("-nostdinc" "-fno-builtin"))
,@c-flags
"-o" ,(target-file-name t)
@ -284,7 +291,7 @@
(exit 1)))))
(inputs (list (store #:add-file "mlibc/libc-gcc.c"))))) ;; FIXME: FLAGS
(define* (CPP.mescc #:key (cc %MESCC) (defines '()))
(define* (CPP.mescc #:key (cc %MESCC) (defines '()) (includes '()))
(method (name "CPP.mescc")
(build (lambda (o t)
(let ((input-files (map target-file-name (target-inputs t))))
@ -293,6 +300,7 @@
`(,cc
"-E"
,@(append-map (cut list "-D" <>) defines)
,@(append-map (cut list "-I" <>) includes)
"-o" ,(target-file-name t)
,@input-files)))))))
@ -396,19 +404,19 @@
(format (current-error-port) "FAILED:~s\n" command)
(exit 1)))))))
(define* (cpp.mescc input-file-name #:key (cc %MESCC) (defines '()))
(define* (cpp.mescc input-file-name #:key (cc %MESCC) (defines '()) (includes '()))
(let* ((c-target (target (file-name input-file-name)))
(base-name (base-name input-file-name ".c"))
(suffix ".E")
(target-file-name (string-append base-name suffix)))
(target (file-name target-file-name)
(inputs (list c-target))
(method (CPP.mescc #:cc cc #:defines defines)))))
(method (CPP.mescc #:cc cc #:defines defines #:includes includes)))))
(define mini-libc-mes.E (cpp.mescc "mlibc/mini-libc-mes.c"))
(define libc-mes.E (cpp.mescc "mlibc/libc-mes.c"))
(define* (compile.gcc input-file-name #:key (libc #t) (cc (if libc %CC %CC32)) (defines '()))
(define* (compile.gcc input-file-name #:key (libc #t) (cc (if libc %CC %CC32)) (defines '()) (includes '()))
(let* ((base-name (base-name input-file-name ".c"))
(cross (if libc "" "mlibc-"))
(suffix (string-append "." cross "o"))
@ -416,55 +424,55 @@
(c-target (target (file-name input-file-name))))
(target (file-name target-file-name)
(inputs (list c-target))
(method (CC.gcc #:cc cc #:libc libc #:defines defines)))))
(method (CC.gcc #:cc cc #:libc libc #:defines defines #:includes includes)))))
(define* (compile.mescc input-file-name #:key (cc %CC) (libc libc-mes.E) (defines '()))
(define* (compile.mescc input-file-name #:key (cc %CC) (libc libc-mes.E) (defines '()) (includes '()))
(let* ((base-name (base-name input-file-name ".c"))
;;(foo (format (current-error-port) "COMPILE[~s .c] base=~s\n" input-file-name base-name))
(suffix (cond ((not libc) ".0-M1")
((eq? libc libc-mes.E) ".M1")
(else ".mini-M1")))
(target-file-name (string-append base-name suffix))
(E-target (cpp.mescc input-file-name #:cc cc #:defines defines)))
(E-target (cpp.mescc input-file-name #:cc cc #:defines defines #:includes includes)))
(target (file-name target-file-name)
(inputs `(,@(if libc (list libc) '()) ,E-target))
(method (CC.mescc #:cc cc)))))
(define* (m1-asm input-file-name #:key (cc %MESCC) (m1 %M1) (libc libc-mes.E) (defines '()))
(define* (m1-asm input-file-name #:key (cc %MESCC) (m1 %M1) (libc libc-mes.E) (defines '()) (includes '()))
(let* ((base-name (base-name input-file-name ".c"))
;;(foo (format (current-error-port) "m1-asm[~s .m1] base=~s\n" input-file-name base-name))
(suffix (cond ((not libc) ".0-hex2")
((eq? libc libc-mes.E) ".hex2")
(else ".mini-hex2")))
(target-file-name (string-append base-name suffix))
(m1-target (compile.mescc input-file-name #:cc cc #:libc libc #:defines defines))
(m1-target (compile.mescc input-file-name #:cc cc #:libc libc #:defines defines #:includes includes))
(libc.m1 (cond ((eq? libc libc-mes.E)
(compile.mescc "mlibc/libc-mes.c" #:libc #f #:defines defines))
(compile.mescc "mlibc/libc-mes.c" #:libc #f #:defines defines #:includes includes))
((eq? libc mini-libc-mes.E)
(compile.mescc "mlibc/mini-libc-mes.c" #:libc #f #:defines defines))
(compile.mescc "mlibc/mini-libc-mes.c" #:libc #f #:defines defines #:includes includes))
(else #f))))
(target (file-name target-file-name)
;;(inputs `(,@(if libc (list libc.m1) '()) ,m1-target))
(inputs `(,m1-target))
(method (M1.asm #:m1 m1)))))
(define* (bin.mescc input-file-name #:key (cc %MESCC) (hex2 %HEX2) (m1 %M1) (libc libc-mes.E) (dependencies '()) (defines '()))
(define* (bin.mescc input-file-name #:key (cc %MESCC) (hex2 %HEX2) (m1 %M1) (libc libc-mes.E) (dependencies '()) (defines '()) (includes '()))
(let* ((base-name (base-name input-file-name ".c"))
;;(foo (format (current-error-port) "bin[~s .c] base=~s\n" input-file-name base-name))
(suffix (cond ((not libc) ".0-guile")
((eq? libc libc-mes.E) ".guile")
(else ".mini-guile")))
(target-file-name (string-append base-name suffix))
(hex2-target (m1-asm input-file-name #:m1 m1 #:cc cc #:libc libc #:defines defines)))
(hex2-target (m1-asm input-file-name #:m1 m1 #:cc cc #:libc libc #:defines defines #:includes includes)))
(target (file-name target-file-name)
(inputs (cons hex2-target dependencies))
(method (LINK.hex2 #:hex2 hex2 #:debug? (eq? libc libc-mes.E))))))
(define* (bin.gcc input-file-name #:key (libc #t) (cc (if libc %CC %CC32)) (dependencies '()) (defines '()))
(define* (bin.gcc input-file-name #:key (libc #t) (cc (if libc %CC %CC32)) (dependencies '()) (defines '()) (includes '()))
(let* ((base-name (base-name input-file-name ".c"))
(suffix (if libc ".gcc" ".mlibc-gcc"))
(target-file-name (string-append base-name suffix))
(o-target (compile.gcc input-file-name #:cc cc #:libc libc #:defines defines)))
(o-target (compile.gcc input-file-name #:cc cc #:libc libc #:defines defines #:includes includes)))
(target (file-name target-file-name)
(inputs (list o-target))
(method (LINK.gcc #:cc cc #:libc libc)))))

View file

@ -145,7 +145,79 @@
(add-target (group "check-scaffold-tests" #:dependencies (filter (target-prefix? "check-scaffold/tests") %targets)))
(add-target (group "check-scaffold" #:dependencies (filter (target-prefix? "check-scaffold") %targets)))
(define* (add-tcc-test name)
(add-target (bin.gcc (string-append "scaffold/tinycc/" name ".c") #:libc #f #:includes '("scaffold/tinycc")))
(add-target (check (string-append "scaffold/tinycc/" name ".mlibc-gcc") #:baseline (string-append "scaffold/tinycc/" name ".expect")))
(add-target (bin.mescc (string-append "scaffold/tinycc/" name ".c") #:includes '("scaffold/tinycc")))
(add-target (check (string-append "scaffold/tinycc/" name ".guile") #:baseline (string-append "scaffold/tinycc/" name ".expect"))))
(map
add-tcc-test
'("00_assignment"
"01_comment"
"02_printf"
"03_struct"
"04_for"
"05_array"
"06_case"
"07_function"
"08_while"
"09_do_while"
"10_pointer"
"11_precedence"
"12_hashdefine"
"13_integer_literals"
"14_if"
"15_recursion"
"16_nesting"
"17_enum"
"18_include"
"19_pointer_arithmetic"
"20_pointer_comparison"
"21_char_array"
;;"22_floating_point" ; float
;;"23_type_coercion" ; float
;;"24_math_library" ; float
"25_quicksort"
;;"27_sizeof" ; float
;;"28_strings" ; TODO: strncpy strchr strrchr memset memcpy memcmp
"29_array_address"
;;"30_hanoi" ; fails with GCC
"31_args"
;;"32_led" ; unsupported: (decl (decl-spec-list (stor-spec (static)) (type-spec (fixed-type "int"))) (init-declr-list (init-declr (array-of (ident "d") (p-expr (fixed "32"))))))
;;"34_array_assignment" ; fails with GCC
"33_ternary_op"
"35_sizeof"
;;"36_array_initialisers" ; unspported: (decl (decl-spec-list (type-spec (fixed-type "int"))) (init-declr-list (init-declr (array-of (ident "Array") (p-expr (fixed "10"))) (initzer (initzer-list (initzer (p-expr (fixed "12"))) (initzer (p-expr (fixed "34"))) (initzer (p-expr (fixed "56"))) (initzer (p-expr (fixed "78"))) (initzer (p-expr (fixed "90"))) (initzer (p-expr (fixed "123"))) (initzer (p-expr (fixed "456"))) (initzer (p-expr (fixed "789"))) (initzer (p-expr (fixed "8642"))) (initzer (p-expr (fixed "9753"))))))))
;; "37_sprintf" ; integer formatting unsupported
;;"38_multiple_array_index" ; unspported: (decl (decl-spec-list (type-spec (fixed-type "int"))) (init-declr-list (init-declr (array-of (array-of (ident "a") (p-expr (fixed "4"))) (p-expr (fixed "4"))))))
;;"39_typedef" ; unsupported: (decl (decl-spec-list (stor-spec (typedef)) (type-spec (typename "MyFunStruct"))) (init-declr-list (init-declr (ptr-declr (pointer) (ident "MoreFunThanEver")))))
;;"40_stdio" ; f* functions
"41_hashif"
;;"42_function_pointer" ; f* functions
"43_void_param"
"44_scoped_declarations"
;; "45_empty_for" ; unsupported
;;"46_grep" ; f* functions
"47_switch_return"
"48_nested_break"
;;"49_bracket_evaluation" ; float
"50_logical_second_arg"
;;"51_static" ; unsupported: (decl (decl-spec-list (stor-spec (static)) (type-spec (fixed-type "int"))) (init-declr-list (init-declr (ident "fred") (initzer (p-expr (fixed "1234"))))))
;;"52_unnamed_enum" ; unsupported: (decl (decl-spec-list (stor-spec (typedef)) (type-spec (enum-def (enum-def-list (enum-defn (ident "e")) (enum-defn (ident "f")) (enum-defn (ident "g")))))) (init-declr-list (init-declr (ident "h"))))
"54_goto"
;;"55_lshift_type" ; unsigned
))
(add-target (group "check-scaffold-tinycc" #:dependencies (filter (target-prefix? "check-scaffold/tinycc") %targets)))
;;(add-target (group "check-scaffold" #:dependencies (filter (target-prefix? "check-scaffold") %targets)))
(add-target (bin.gcc "scaffold/hello.c"))
(add-target (check "scaffold/hello.gcc" #:exit 42))
@ -170,10 +242,10 @@
(add-target (check "scaffold/m.guile" #:exit 255))
(add-target (bin.gcc "scaffold/micro-mes.c" #:libc #f))
(add-target (check "scaffold/micro-mes.mlibc-gcc" #:exit 1))
(add-target (check "scaffold/micro-mes.mlibc-gcc" #:exit 6)) ; arg1 arg2 arg3 arg4 arg5
(add-target (bin.mescc "scaffold/micro-mes.c"))
(add-target (check "scaffold/micro-mes.guile" #:exit 1))
(add-target (check "scaffold/micro-mes.guile" #:exit 6)) ; arg1 arg2 arg3 arg4 arg5
(define snarf-bases
'("gc" "lib" "math" "mes" "posix" "reader" "vector"))

View file

@ -0,0 +1,18 @@
#include <stdio.h>
int main()
{
int a;
a = 42;
printf("%d\n", a);
int b = 64;
printf("%d\n", b);
int c = 12, d = 34;
printf("%d, %d\n", c, d);
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,3 @@
42
64
12, 34

View file

@ -0,0 +1,14 @@
#include <stdio.h>
int main()
{
printf("Hello\n");
printf("Hello\n"); /* this is a comment */ printf("Hello\n");
printf("Hello\n");
// this is also a comment sayhello();
printf("Hello\n");
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,5 @@
Hello
Hello
Hello
Hello
Hello

View file

@ -0,0 +1,18 @@
#include <stdio.h>
int main()
{
printf("Hello world\n");
int Count;
for (Count = -5; Count <= 5; Count++)
printf("Count = %d\n", Count);
printf("String 'hello', 'there' is '%s', '%s'\n", "hello", "there");
printf("Character 'A' is '%c'\n", 65);
printf("Character 'a' is '%c'\n", 'a');
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,15 @@
Hello world
Count = -5
Count = -4
Count = -3
Count = -2
Count = -1
Count = 0
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
String 'hello', 'there' is 'hello', 'there'
Character 'A' is 'A'
Character 'a' is 'a'

View file

@ -0,0 +1,31 @@
#include <stdio.h>
struct fred
{
int boris;
int natasha;
};
int main()
{
struct fred bloggs;
bloggs.boris = 12;
bloggs.natasha = 34;
printf("%d\n", bloggs.boris);
printf("%d\n", bloggs.natasha);
struct fred jones[2];
jones[0].boris = 12;
jones[0].natasha = 34;
jones[1].boris = 56;
jones[1].natasha = 78;
printf("%d\n", jones[0].boris);
printf("%d\n", jones[0].natasha);
printf("%d\n", jones[1].boris);
printf("%d\n", jones[1].natasha);
return 0;
}

View file

@ -0,0 +1,6 @@
12
34
12
34
56
78

15
scaffold/tinycc/04_for.c Normal file
View file

@ -0,0 +1,15 @@
#include <stdio.h>
int main()
{
int Count;
for (Count = 1; Count <= 10; Count++)
{
printf("%d\n", Count);
}
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,10 @@
1
2
3
4
5
6
7
8
9
10

View file

@ -0,0 +1,21 @@
#include <stdio.h>
int main()
{
int Count;
int Array[10];
for (Count = 1; Count <= 10; Count++)
{
Array[Count-1] = Count * Count;
}
for (Count = 0; Count < 10; Count++)
{
printf("%d\n", Array[Count]);
}
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,10 @@
1
4
9
16
25
36
49
64
81
100

29
scaffold/tinycc/06_case.c Normal file
View file

@ -0,0 +1,29 @@
#include <stdio.h>
int main()
{
int Count;
for (Count = 0; Count < 4; Count++)
{
printf("%d\n", Count);
switch (Count)
{
case 1:
printf("%d\n", 1);
break;
case 2:
printf("%d\n", 2);
break;
default:
printf("%d\n", 0);
break;
}
}
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,8 @@
0
0
1
1
2
2
3
0

View file

@ -0,0 +1,30 @@
#include <stdio.h>
int myfunc(int x)
{
return x * x;
}
void vfunc(int a)
{
printf("a=%d\n", a);
}
void qfunc()
{
printf("qfunc()\n");
}
int main()
{
printf("%d\n", myfunc(3));
printf("%d\n", myfunc(4));
vfunc(1234);
qfunc();
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,4 @@
9
16
a=1234
qfunc()

View file

@ -0,0 +1,24 @@
#include <stdio.h>
int main()
{
int a;
int p;
int t;
a = 1;
p = 0;
t = 0;
while (a < 100)
{
printf("%d\n", a);
t = a;
a = t + p;
p = t;
}
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,11 @@
1
1
2
3
5
8
13
21
34
55
89

View file

@ -0,0 +1,24 @@
#include <stdio.h>
int main()
{
int a;
int p;
int t;
a = 1;
p = 0;
t = 0;
do
{
printf("%d\n", a);
t = a;
a = t + p;
p = t;
} while (a < 100);
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,11 @@
1
1
2
3
5
8
13
21
34
55
89

View file

@ -0,0 +1,40 @@
#include <stdio.h>
struct ziggy
{
int a;
int b;
int c;
} bolshevic;
int main()
{
int a;
int *b;
int c;
a = 42;
b = &a;
printf("a = %d\n", *b);
bolshevic.a = 12;
bolshevic.b = 34;
bolshevic.c = 56;
printf("bolshevic.a = %d\n", bolshevic.a);
printf("bolshevic.b = %d\n", bolshevic.b);
printf("bolshevic.c = %d\n", bolshevic.c);
struct ziggy *tsar = &bolshevic;
printf("tsar->a = %d\n", tsar->a);
printf("tsar->b = %d\n", tsar->b);
printf("tsar->c = %d\n", tsar->c);
b = &(bolshevic.b);
printf("bolshevic.b = %d\n", *b);
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,8 @@
a = 42
bolshevic.a = 12
bolshevic.b = 34
bolshevic.c = 56
tsar->a = 12
tsar->b = 34
tsar->c = 56
bolshevic.b = 34

View file

@ -0,0 +1,40 @@
#include <stdio.h>
int main()
{
int a;
int b;
int c;
int d;
int e;
int f;
int x;
int y;
a = 12;
b = 34;
c = 56;
d = 78;
e = 0;
f = 1;
printf("%d\n", c + d);
printf("%d\n", (y = c + d));
printf("%d\n", e || e && f);
printf("%d\n", e || f && f);
printf("%d\n", e && e || f);
printf("%d\n", e && f || f);
printf("%d\n", a && f | f);
printf("%d\n", a | b ^ c & d);
printf("%d, %d\n", a == a, a == b);
printf("%d, %d\n", a != a, a != b);
printf("%d\n", a != b && c != d);
printf("%d\n", a + b * c / f);
printf("%d\n", a + b * c / f);
printf("%d\n", (4 << 4));
printf("%d\n", (64 >> 4));
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,15 @@
134
134
0
1
1
1
1
46
1, 0
0, 1
1
1916
1916
64
4

View file

@ -0,0 +1,14 @@
#include <stdio.h>
#define FRED 12
#define BLOGGS(x) (12*(x))
int main()
{
printf("%d\n", FRED);
printf("%d, %d, %d\n", BLOGGS(1), BLOGGS(2), BLOGGS(3));
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,2 @@
12
12, 24, 36

View file

@ -0,0 +1,20 @@
#include <stdio.h>
int main()
{
int a = 24680;
int b = 01234567;
int c = 0x2468ac;
int d = 0x2468AC;
int e = 0b010101010101;
printf("%d\n", a);
printf("%d\n", b);
printf("%d\n", c);
printf("%d\n", d);
printf("%d\n", e);
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,5 @@
24680
342391
2386092
2386092
1365

21
scaffold/tinycc/14_if.c Normal file
View file

@ -0,0 +1,21 @@
#include <stdio.h>
int main()
{
int a = 1;
if (a)
printf("a is true\n");
else
printf("a is false\n");
int b = 0;
if (b)
printf("b is true\n");
else
printf("b is false\n");
return 0;
}
// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :

View file

@ -0,0 +1,2 @@
a is true
b is false

View file

@ -0,0 +1,21 @@
#include <stdio.h>
int factorial(int i)
{
if (i < 2)
return i;
else
return i * factorial(i - 1);
}
int main()
{
int Count;
for (Count = 1; Count <= 10; Count++)
printf("%d\n", factorial(Count));
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,10 @@
1
2
6
24
120
720
5040
40320
362880
3628800

View file

@ -0,0 +1,21 @@
#include <stdio.h>
int main()
{
int x, y, z;
for (x = 0; x < 2; x++)
{
for (y = 0; y < 3; y++)
{
for (z = 0; z < 3; z++)
{
printf("%d %d %d\n", x, y, z);
}
}
}
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,18 @@
0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 2 2

29
scaffold/tinycc/17_enum.c Normal file
View file

@ -0,0 +1,29 @@
#include <stdio.h>
enum fred
{
a,
b,
c,
d,
e = 54,
f = 73,
g,
h
};
int main()
{
enum fred frod;
printf("%d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h);
/* printf("%d\n", frod); */
frod = 12;
printf("%d\n", frod);
frod = e;
printf("%d\n", frod);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,3 @@
0 1 2 3 54 73 74 75
12
54

View file

@ -0,0 +1,12 @@
#include <stdio.h>
int main()
{
printf("including\n");
#include "18_include.h"
printf("done\n");
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,3 @@
including
included
done

View file

@ -0,0 +1 @@
printf("included\n");

View file

@ -0,0 +1,28 @@
#include <stdio.h>
int main()
{
int a;
int *b;
int *c;
a = 42;
b = &a;
c = NULL;
printf("%d\n", *b);
if (b == NULL)
printf("b is NULL\n");
else
printf("b is not NULL\n");
if (c == NULL)
printf("c is NULL\n");
else
printf("c is not NULL\n");
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,3 @@
42
b is not NULL
c is NULL

View file

@ -0,0 +1,24 @@
#include <stdio.h>
int main()
{
int a;
int b;
int *d;
int *e;
d = &a;
e = &b;
a = 12;
b = 34;
printf("%d\n", *d);
printf("%d\n", *e);
printf("%d\n", d == e);
printf("%d\n", d != e);
d = e;
printf("%d\n", d == e);
printf("%d\n", d != e);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,6 @@
12
34
0
1
1
0

View file

@ -0,0 +1,33 @@
#include <stdio.h>
int main()
{
int x = 'a';
char y = x;
char *a = "hello";
printf("%s\n", a);
int c;
c = *a;
char *b;
for (b = a; *b != 0; b++)
printf("%c: %d\n", *b, *b);
char destarray[10];
char *dest = &destarray[0];
char *src = a;
while (*src != 0)
*dest++ = *src++;
*dest = 0;
printf("copied string is %s\n", destarray);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,7 @@
hello
h: 104
e: 101
l: 108
l: 108
o: 111
copied string is hello

View file

@ -0,0 +1,50 @@
#include <stdio.h>
#include <math.h>
int main()
{
// variables
float a = 12.34 + 56.78;
printf("%f\n", a);
// infix operators
printf("%f\n", 12.34 + 56.78);
printf("%f\n", 12.34 - 56.78);
printf("%f\n", 12.34 * 56.78);
printf("%f\n", 12.34 / 56.78);
// comparison operators
printf("%d %d %d %d %d %d\n", 12.34 < 56.78, 12.34 <= 56.78, 12.34 == 56.78, 12.34 >= 56.78, 12.34 > 56.78, 12.34 != 56.78);
printf("%d %d %d %d %d %d\n", 12.34 < 12.34, 12.34 <= 12.34, 12.34 == 12.34, 12.34 >= 12.34, 12.34 > 12.34, 12.34 != 12.34);
printf("%d %d %d %d %d %d\n", 56.78 < 12.34, 56.78 <= 12.34, 56.78 == 12.34, 56.78 >= 12.34, 56.78 > 12.34, 56.78 != 12.34);
// assignment operators
a = 12.34;
a += 56.78;
printf("%f\n", a);
a = 12.34;
a -= 56.78;
printf("%f\n", a);
a = 12.34;
a *= 56.78;
printf("%f\n", a);
a = 12.34;
a /= 56.78;
printf("%f\n", a);
// prefix operators
printf("%f\n", +12.34);
printf("%f\n", -12.34);
// type coercion
a = 2;
printf("%f\n", a);
printf("%f\n", sin(2));
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,16 @@
69.120003
69.120000
-44.440000
700.665200
0.217330
1 1 0 0 0 1
0 1 1 1 0 0
0 0 0 1 1 1
69.120003
-44.439999
700.665222
0.217330
12.340000
-12.340000
2.000000
0.909297

View file

@ -0,0 +1,54 @@
#include <stdio.h>
void charfunc(char a)
{
printf("char: %c\n", a);
}
void intfunc(int a)
{
printf("int: %d\n", a);
}
void floatfunc(float a)
{
printf("float: %f\n", a);
}
int main()
{
charfunc('a');
charfunc(98);
charfunc(99.0);
intfunc('a');
intfunc(98);
intfunc(99.0);
floatfunc('a');
floatfunc(98);
floatfunc(99.0);
/* printf("%c %d %f\n", 'a', 'b', 'c'); */
/* printf("%c %d %f\n", 97, 98, 99); */
/* printf("%c %d %f\n", 97.0, 98.0, 99.0); */
char b = 97;
char c = 97.0;
printf("%d %d\n", b, c);
int d = 'a';
int e = 97.0;
printf("%d %d\n", d, e);
float f = 'a';
float g = 97;
printf("%f %f\n", f, g);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,12 @@
char: a
char: b
char: c
int: 97
int: 98
int: 99
float: 97.000000
float: 98.000000
float: 99.000000
97 97
97 97
97.000000 97.000000

View file

@ -0,0 +1,30 @@
#define _ISOC99_SOURCE 1
#include <stdio.h>
#include <math.h>
int main()
{
printf("%f\n", sin(0.12));
printf("%f\n", cos(0.12));
printf("%f\n", tan(0.12));
printf("%f\n", asin(0.12));
printf("%f\n", acos(0.12));
printf("%f\n", atan(0.12));
printf("%f\n", sinh(0.12));
printf("%f\n", cosh(0.12));
printf("%f\n", tanh(0.12));
printf("%f\n", exp(0.12));
printf("%f\n", fabs(-0.12));
printf("%f\n", log(0.12));
printf("%f\n", log10(0.12));
printf("%f\n", pow(0.12, 0.12));
printf("%f\n", sqrt(0.12));
printf("%f\n", round(12.34));
printf("%f\n", ceil(12.34));
printf("%f\n", floor(12.34));
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,18 @@
0.119712
0.992809
0.120579
0.120290
1.450506
0.119429
0.120288
1.007209
0.119427
1.127497
0.120000
-2.120264
-0.920819
0.775357
0.346410
12.000000
13.000000
12.000000

View file

@ -0,0 +1,83 @@
#include <stdio.h>
int array[16];
//Swap integer values by array indexes
void swap(int a, int b)
{
int tmp = array[a];
array[a] = array[b];
array[b] = tmp;
}
//Partition the array into two halves and return the
//index about which the array is partitioned
int partition(int left, int right)
{
int pivotIndex = left;
int pivotValue = array[pivotIndex];
int index = left;
int i;
swap(pivotIndex, right);
for(i = left; i < right; i++)
{
if(array[i] < pivotValue)
{
swap(i, index);
index += 1;
}
}
swap(right, index);
return index;
}
//Quicksort the array
void quicksort(int left, int right)
{
if(left >= right)
return;
int index = partition(left, right);
quicksort(left, index - 1);
quicksort(index + 1, right);
}
int main()
{
int i;
array[0] = 62;
array[1] = 83;
array[2] = 4;
array[3] = 89;
array[4] = 36;
array[5] = 21;
array[6] = 74;
array[7] = 37;
array[8] = 65;
array[9] = 33;
array[10] = 96;
array[11] = 38;
array[12] = 53;
array[13] = 16;
array[14] = 74;
array[15] = 55;
for (i = 0; i < 16; i++)
printf("%d ", array[i]);
printf("\n");
quicksort(0, 15);
for (i = 0; i < 16; i++)
printf("%d ", array[i]);
printf("\n");
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,2 @@
62 83 4 89 36 21 74 37 65 33 96 38 53 16 74 55
4 16 21 33 36 37 38 53 55 62 65 74 74 83 89 96

View file

@ -0,0 +1,17 @@
#include <stdio.h>
int main()
{
printf("%d\n", '\1');
printf("%d\n", '\10');
printf("%d\n", '\100');
printf("%d\n", '\x01');
printf("%d\n", '\x0e');
printf("%d\n", '\x10');
printf("%d\n", '\x40');
printf("test \x40\n");
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,8 @@
1
8
64
1
14
16
64
test @

View file

@ -0,0 +1,18 @@
#include <stdio.h>
int main()
{
char a;
int b;
double c;
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(b));
printf("%d\n", sizeof(c));
printf("%d\n", sizeof(!a));
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,4 @@
1
4
8
4

View file

@ -0,0 +1,45 @@
#include <stdio.h>
#include <string.h>
int main()
{
char a[10];
strcpy(a, "hello");
printf("%s\n", a);
strncpy(a, "gosh", 2);
printf("%s\n", a);
printf("%d\n", strcmp(a, "apple") > 0);
printf("%d\n", strcmp(a, "goere") > 0);
printf("%d\n", strcmp(a, "zebra") < 0);
printf("%d\n", strlen(a));
strcat(a, "!");
printf("%s\n", a);
printf("%d\n", strncmp(a, "apple", 2) > 0);
printf("%d\n", strncmp(a, "goere", 2) == 0);
printf("%d\n", strncmp(a, "goerg", 2) == 0);
printf("%d\n", strncmp(a, "zebra", 2) < 0);
printf("%s\n", strchr(a, 'o'));
printf("%s\n", strrchr(a, 'l'));
printf("%d\n", strrchr(a, 'x') == NULL);
memset(&a[1], 'r', 4);
printf("%s\n", a);
memcpy(&a[2], a, 2);
printf("%s\n", a);
printf("%d\n", memcmp(a, "apple", 4) > 0);
printf("%d\n", memcmp(a, "grgr", 4) == 0);
printf("%d\n", memcmp(a, "zebra", 4) < 0);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,19 @@
hello
gollo
1
1
1
5
gollo!
1
1
1
1
ollo!
lo!
1
grrrr!
grgrr!
1
1
1

View file

@ -0,0 +1,13 @@
#include <stdio.h>
#include <string.h>
int main()
{
char a[10];
strcpy(a, "abcdef");
printf("%s\n", &a[1]);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1 @@
bcdef

122
scaffold/tinycc/30_hanoi.c Normal file
View file

@ -0,0 +1,122 @@
/* example from http://barnyard.syr.edu/quickies/hanoi.c */
/* hanoi.c: solves the tower of hanoi problem. (Programming exercise.) */
/* By Terry R. McConnell (12/2/97) */
/* Compile: cc -o hanoi hanoi.c */
/* This program does no error checking. But then, if it's right,
it's right ... right ? */
/* The original towers of hanoi problem seems to have been originally posed
by one M. Claus in 1883. There is a popular legend that goes along with
it that has been often repeated and paraphrased. It goes something like this:
In the great temple at Benares there are 3 golden spikes. On one of them,
God placed 64 disks increasing in size from bottom to top, at the beginning
of time. Since then, and to this day, the priest on duty constantly transfers
disks, one at a time, in such a way that no larger disk is ever put on top
of a smaller one. When the disks have been transferred entirely to another
spike the Universe will come to an end in a large thunderclap.
This paraphrases the original legend due to DeParville, La Nature, Paris 1884,
Part I, 285-286. For this and further information see: Mathematical
Recreations & Essays, W.W. Rouse Ball, MacMillan, NewYork, 11th Ed. 1967,
303-305.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
/* This is the number of "disks" on tower A initially. Taken to be 64 in the
* legend. The number of moves required, in general, is 2^N - 1. For N = 64,
* this is 18,446,744,073,709,551,615 */
#define N 4
/* These are the three towers. For example if the state of A is 0,1,3,4, that
* means that there are three discs on A of sizes 1, 3, and 4. (Think of right
* as being the "down" direction.) */
int A[N], B[N], C[N];
void Hanoi(int,int*,int*,int*);
/* Print the current configuration of A, B, and C to the screen */
void PrintAll()
{
int i;
printf("A: ");
for(i=0;i<N;i++)printf(" %d ",A[i]);
printf("\n");
printf("B: ");
for(i=0;i<N;i++)printf(" %d ",B[i]);
printf("\n");
printf("C: ");
for(i=0;i<N;i++)printf(" %d ",C[i]);
printf("\n");
printf("------------------------------------------\n");
return;
}
/* Move the leftmost nonzero element of source to dest, leave behind 0. */
/* Returns the value moved (not used.) */
int Move(int *source, int *dest)
{
int i = 0, j = 0;
while (i<N && (source[i])==0) i++;
while (j<N && (dest[j])==0) j++;
dest[j-1] = source[i];
source[i] = 0;
PrintAll(); /* Print configuration after each move. */
return dest[j-1];
}
/* Moves first n nonzero numbers from source to dest using the rules of Hanoi.
Calls itself recursively.
*/
void Hanoi(int n,int *source, int *dest, int *spare)
{
int i;
if(n==1){
Move(source,dest);
return;
}
Hanoi(n-1,source,spare,dest);
Move(source,dest);
Hanoi(n-1,spare,dest,source);
return;
}
int main()
{
int i;
/* initialize the towers */
for(i=0;i<N;i++)A[i]=i+1;
for(i=0;i<N;i++)B[i]=0;
for(i=0;i<N;i++)C[i]=0;
printf("Solution of Tower of Hanoi Problem with %d Disks\n\n",N);
/* Print the starting state */
printf("Starting state:\n");
PrintAll();
printf("\n\nSubsequent states:\n\n");
/* Do it! Use A = Source, B = Destination, C = Spare */
Hanoi(N,A,B,C);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,71 @@
Solution of Tower of Hanoi Problem with 4 Disks
Starting state:
A: 1 2 3 4
B: 0 0 0 0
C: 0 0 0 0
------------------------------------------
Subsequent states:
A: 0 2 3 4
B: 0 0 0 0
C: 0 0 0 1
------------------------------------------
A: 0 0 3 4
B: 0 0 0 2
C: 0 0 0 1
------------------------------------------
A: 0 0 3 4
B: 0 0 1 2
C: 0 0 0 0
------------------------------------------
A: 0 0 0 4
B: 0 0 1 2
C: 0 0 0 3
------------------------------------------
A: 0 0 1 4
B: 0 0 0 2
C: 0 0 0 3
------------------------------------------
A: 0 0 1 4
B: 0 0 0 0
C: 0 0 2 3
------------------------------------------
A: 0 0 0 4
B: 0 0 0 0
C: 0 1 2 3
------------------------------------------
A: 0 0 0 0
B: 0 0 0 4
C: 0 1 2 3
------------------------------------------
A: 0 0 0 0
B: 0 0 1 4
C: 0 0 2 3
------------------------------------------
A: 0 0 0 2
B: 0 0 1 4
C: 0 0 0 3
------------------------------------------
A: 0 0 1 2
B: 0 0 0 4
C: 0 0 0 3
------------------------------------------
A: 0 0 1 2
B: 0 0 3 4
C: 0 0 0 0
------------------------------------------
A: 0 0 0 2
B: 0 0 3 4
C: 0 0 0 1
------------------------------------------
A: 0 0 0 0
B: 0 2 3 4
C: 0 0 0 1
------------------------------------------
A: 0 0 0 0
B: 1 2 3 4
C: 0 0 0 0
------------------------------------------

14
scaffold/tinycc/31_args.c Normal file
View file

@ -0,0 +1,14 @@
#include <stdio.h>
int main(int argc, char **argv)
{
int Count;
printf("hello world %d\n", argc);
for (Count = 1; Count < argc; Count++)
printf("arg %d: %s\n", Count, argv[Count]);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,6 @@
hello world 6
arg 1: arg1
arg 2: arg2
arg 3: arg3
arg 4: arg4
arg 5: arg5

266
scaffold/tinycc/32_led.c Normal file
View file

@ -0,0 +1,266 @@
/* example from http://barnyard.syr.edu/quickies/led.c */
/* led.c: print out number as if on 7 line led display. I.e., write integer
given on command line like this:
_ _ _
| _| _| |_| |_
| |_ _| | _| etc.
We assume the terminal behaves like a classical teletype. So the top
lines of all digits have to be printed first, then the middle lines of
all digits, etc.
By Terry R. McConnell
compile: cc -o led led.c
If you just want to link in the subroutine print_led that does all the
work, compile with -DNO_MAIN, and declare the following in any source file
that uses the call:
extern void print_led(unsigned long x, char *buf);
Bug: you cannot call repeatedly to print more than one number to a line.
That would require curses or some other terminal API that allows moving the
cursor to a previous line.
*/
#include <stdlib.h>
#include <stdio.h>
#define MAX_DIGITS 32
#define NO_MAIN
/* Print the top line of the digit d into buffer.
Does not null terminate buffer. */
void topline(int d, char *p){
*p++ = ' ';
switch(d){
/* all these have _ on top line */
case 0:
case 2:
case 3:
case 5:
case 7:
case 8:
case 9:
*p++ = '_';
break;
default:
*p++=' ';
}
*p++=' ';
}
/* Print the middle line of the digit d into the buffer.
Does not null terminate. */
void midline(int d, char *p){
switch(d){
/* those that have leading | on middle line */
case 0:
case 4:
case 5:
case 6:
case 8:
case 9:
*p++='|';
break;
default:
*p++=' ';
}
switch(d){
/* those that have _ on middle line */
case 2:
case 3:
case 4:
case 5:
case 6:
case 8:
case 9:
*p++='_';
break;
default:
*p++=' ';
}
switch(d){
/* those that have closing | on middle line */
case 0:
case 1:
case 2:
case 3:
case 4:
case 7:
case 8:
case 9:
*p++='|';
break;
default:
*p++=' ';
}
}
/* Print the bottom line of the digit d. Does not null terminate. */
void botline(int d, char *p){
switch(d){
/* those that have leading | on bottom line */
case 0:
case 2:
case 6:
case 8:
*p++='|';
break;
default:
*p++=' ';
}
switch(d){
/* those that have _ on bottom line */
case 0:
case 2:
case 3:
case 5:
case 6:
case 8:
*p++='_';
break;
default:
*p++=' ';
}
switch(d){
/* those that have closing | on bottom line */
case 0:
case 1:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
*p++='|';
break;
default:
*p++=' ';
}
}
/* Write the led representation of integer to string buffer. */
void print_led(unsigned long x, char *buf)
{
int i=0,n;
static int d[MAX_DIGITS];
/* extract digits from x */
n = ( x == 0L ? 1 : 0 ); /* 0 is a digit, hence a special case */
while(x){
d[n++] = (int)(x%10L);
if(n >= MAX_DIGITS)break;
x = x/10L;
}
/* print top lines of all digits */
for(i=n-1;i>=0;i--){
topline(d[i],buf);
buf += 3;
*buf++=' ';
}
*buf++='\n'; /* move teletype to next line */
/* print middle lines of all digits */
for(i=n-1;i>=0;i--){
midline(d[i],buf);
buf += 3;
*buf++=' ';
}
*buf++='\n';
/* print bottom lines of all digits */
for(i=n-1;i>=0;i--){
botline(d[i],buf);
buf += 3;
*buf++=' ';
}
*buf++='\n';
*buf='\0';
}
int main()
{
char buf[5*MAX_DIGITS];
print_led(1234567, buf);
printf("%s\n",buf);
return 0;
}
#ifndef NO_MAIN
int main(int argc, char **argv)
{
int i=0,n;
long x;
static int d[MAX_DIGITS];
char buf[5*MAX_DIGITS];
if(argc != 2){
fprintf(stderr,"led: usage: led integer\n");
return 1;
}
/* fetch argument from command line */
x = atol(argv[1]);
/* sanity check */
if(x<0){
fprintf(stderr,"led: %d must be non-negative\n",x);
return 1;
}
print_led(x,buf);
printf("%s\n",buf);
return 0;
}
#endif
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,4 @@
_ _ _ _
| _| _| |_| |_ |_ |
| |_ _| | _| |_| |

View file

@ -0,0 +1,15 @@
#include <stdio.h>
int main()
{
int Count;
for (Count = 0; Count < 10; Count++)
{
printf("%d\n", (Count < 5) ? (Count*Count) : (Count * 3));
}
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,10 @@
0
1
4
9
16
15
18
21
24
27

View file

@ -0,0 +1,23 @@
#include <stdio.h>
int main()
{
int a[4];
a[0] = 12;
a[1] = 23;
a[2] = 34;
a[3] = 45;
printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
int b[4];
b = a;
printf("%d %d %d %d\n", b[0], b[1], b[2], b[3]);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,2 @@
12 23 34 45
12 23 34 45

View file

@ -0,0 +1,14 @@
#include <stdio.h>
int main()
{
char a;
short b;
printf("%d %d\n", sizeof(char), sizeof(a));
printf("%d %d\n", sizeof(short), sizeof(b));
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,2 @@
1 1
2 2

View file

@ -0,0 +1,21 @@
#include <stdio.h>
int main()
{
int Count;
int Array[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753 };
for (Count = 0; Count < 10; Count++)
printf("%d: %d\n", Count, Array[Count]);
int Array2[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753, };
for (Count = 0; Count < 10; Count++)
printf("%d: %d\n", Count, Array2[Count]);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,20 @@
0: 12
1: 34
2: 56
3: 78
4: 90
5: 123
6: 456
7: 789
8: 8642
9: 9753
0: 12
1: 34
2: 56
3: 78
4: 90
5: 123
6: 456
7: 789
8: 8642
9: 9753

View file

@ -0,0 +1,17 @@
#include <stdio.h>
int main()
{
char Buf[100];
int Count;
for (Count = 1; Count <= 20; Count++)
{
sprintf(Buf, "->%02d<-\n", Count);
printf("%s", Buf);
}
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,20 @@
->01<-
->02<-
->03<-
->04<-
->05<-
->06<-
->07<-
->08<-
->09<-
->10<-
->11<-
->12<-
->13<-
->14<-
->15<-
->16<-
->17<-
->18<-
->19<-
->20<-

View file

@ -0,0 +1,32 @@
#include <stdio.h>
int main()
{
int a[4][4];
int b = 0;
int x;
int y;
for (x = 0; x < 4; x++)
{
for (y = 0; y < 4; y++)
{
b++;
a[x][y] = b;
}
}
for (x = 0; x < 4; x++)
{
printf("x=%d: ", x);
for (y = 0; y < 4; y++)
{
printf("%d ", a[x][y]);
}
printf("\n");
}
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,4 @@
x=0: 1 2 3 4
x=1: 5 6 7 8
x=2: 9 10 11 12
x=3: 13 14 15 16

View file

@ -0,0 +1,47 @@
#include <stdio.h>
typedef int MyInt;
struct FunStruct
{
int i;
int j;
};
typedef struct FunStruct MyFunStruct;
typedef MyFunStruct *MoreFunThanEver;
int main()
{
MyInt a = 1;
printf("%d\n", a);
MyFunStruct b;
b.i = 12;
b.j = 34;
printf("%d,%d\n", b.i, b.j);
MoreFunThanEver c = &b;
printf("%d,%d\n", c->i, c->j);
return 0;
}
/* "If the specification of an array type includes any type qualifiers,
the element type is so-qualified, not the array type." */
typedef int A[3];
extern A const ca;
extern const A ca;
extern const int ca[3];
typedef A B[1][2];
extern B const cb;
extern const B cb;
extern const int cb[1][2][3];
extern B b;
extern int b[1][2][3];
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,3 @@
1
12,34
12,34

View file

@ -0,0 +1,52 @@
#include <stdio.h>
int main()
{
FILE *f = fopen("fred.txt", "w");
fwrite("hello\nhello\n", 1, 12, f);
fclose(f);
char freddy[7];
f = fopen("fred.txt", "r");
if (fread(freddy, 1, 6, f) != 6)
printf("couldn't read fred.txt\n");
freddy[6] = '\0';
fclose(f);
printf("%s", freddy);
int InChar;
char ShowChar;
f = fopen("fred.txt", "r");
while ( (InChar = fgetc(f)) != EOF)
{
ShowChar = InChar;
if (ShowChar < ' ')
ShowChar = '.';
printf("ch: %d '%c'\n", InChar, ShowChar);
}
fclose(f);
f = fopen("fred.txt", "r");
while ( (InChar = getc(f)) != EOF)
{
ShowChar = InChar;
if (ShowChar < ' ')
ShowChar = '.';
printf("ch: %d '%c'\n", InChar, ShowChar);
}
fclose(f);
f = fopen("fred.txt", "r");
while (fgets(freddy, sizeof(freddy), f) != NULL)
printf("x: %s", freddy);
fclose(f);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,27 @@
hello
ch: 104 'h'
ch: 101 'e'
ch: 108 'l'
ch: 108 'l'
ch: 111 'o'
ch: 10 '.'
ch: 104 'h'
ch: 101 'e'
ch: 108 'l'
ch: 108 'l'
ch: 111 'o'
ch: 10 '.'
ch: 104 'h'
ch: 101 'e'
ch: 108 'l'
ch: 108 'l'
ch: 111 'o'
ch: 10 '.'
ch: 104 'h'
ch: 101 'e'
ch: 108 'l'
ch: 108 'l'
ch: 111 'o'
ch: 10 '.'
x: hello
x: hello

View file

@ -0,0 +1,85 @@
#include <stdio.h>
int main()
{
printf("#include test\n");
#if 1
#if 0
printf("a\n");
#else
printf("b\n");
#endif
#else
#if 0
printf("c\n");
#else
printf("d\n");
#endif
#endif
#if 0
#if 1
printf("e\n");
#else
printf("f\n");
#endif
#else
#if 1
printf("g\n");
#else
printf("h\n");
#endif
#endif
#define DEF
#ifdef DEF
#ifdef DEF
printf("i\n");
#else
printf("j\n");
#endif
#else
#ifdef DEF
printf("k\n");
#else
printf("l\n");
#endif
#endif
#ifndef DEF
#ifndef DEF
printf("m\n");
#else
printf("n\n");
#endif
#else
#ifndef DEF
printf("o\n");
#else
printf("p\n");
#endif
#endif
#define ONE 1
#define ZERO 0
#if ONE
#if ZERO
printf("q\n");
#else
printf("r\n");
#endif
#else
#if ZERO
printf("s\n");
#else
printf("t\n");
#endif
#endif
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,6 @@
#include test
b
g
i
p
r

View file

@ -0,0 +1,22 @@
#include <stdio.h>
int fred(int p)
{
printf("yo %d\n", p);
return 42;
}
int (*f)(int) = &fred;
/* To test what this is supposed to test the destination function
(fprint here) must not be called directly anywhere in the test. */
int (*fprintfptr)(FILE *, const char *, ...) = &fprintf;
int main()
{
fprintfptr(stdout, "%d\n", (*f)(24));
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,2 @@
yo 24
42

View file

@ -0,0 +1,15 @@
#include <stdio.h>
void fred(void)
{
printf("yo\n");
}
int main()
{
fred();
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1 @@
yo

View file

@ -0,0 +1,17 @@
#include <stdio.h>
int main()
{
int a;
for (a = 0; a < 2; a++)
{
int b = a;
}
printf("it's all good\n");
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1 @@
it's all good

View file

@ -0,0 +1,18 @@
#include <stdio.h>
int main()
{
int Count = 0;
for (;;)
{
Count++;
printf("%d\n", Count);
if (Count >= 10)
break;
}
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,10 @@
1
2
3
4
5
6
7
8
9
10

568
scaffold/tinycc/46_grep.c Normal file
View file

@ -0,0 +1,568 @@
/*
* The information in this document is subject to change
* without notice and should not be construed as a commitment
* by Digital Equipment Corporation or by DECUS.
*
* Neither Digital Equipment Corporation, DECUS, nor the authors
* assume any responsibility for the use or reliability of this
* document or the described software.
*
* Copyright (C) 1980, DECUS
*
* General permission to copy or modify, but not for profit, is
* hereby granted, provided that the above copyright notice is
* included and reference made to the fact that reproduction
* privileges were granted by DECUS.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> // tolower()
/*
* grep
*
* Runs on the Decus compiler or on vms, On vms, define as:
* grep :== "$disk:[account]grep" (native)
* grep :== "$disk:[account]grep grep" (Decus)
* See below for more information.
*/
char *documentation[] = {
"grep searches a file for a given pattern. Execute by",
" grep [flags] regular_expression file_list\n",
"Flags are single characters preceded by '-':",
" -c Only a count of matching lines is printed",
" -f Print file name for matching lines switch, see below",
" -n Each line is preceded by its line number",
" -v Only print non-matching lines\n",
"The file_list is a list of files (wildcards are acceptable on RSX modes).",
"\nThe file name is normally printed if there is a file given.",
"The -f flag reverses this action (print name no file, not if more).\n",
0 };
char *patdoc[] = {
"The regular_expression defines the pattern to search for. Upper- and",
"lower-case are always ignored. Blank lines never match. The expression",
"should be quoted to prevent file-name translation.",
"x An ordinary character (not mentioned below) matches that character.",
"'\\' The backslash quotes any character. \"\\$\" matches a dollar-sign.",
"'^' A circumflex at the beginning of an expression matches the",
" beginning of a line.",
"'$' A dollar-sign at the end of an expression matches the end of a line.",
"'.' A period matches any character except \"new-line\".",
"':a' A colon matches a class of characters described by the following",
"':d' character. \":a\" matches any alphabetic, \":d\" matches digits,",
"':n' \":n\" matches alphanumerics, \": \" matches spaces, tabs, and",
"': ' other control characters, such as new-line.",
"'*' An expression followed by an asterisk matches zero or more",
" occurrences of that expression: \"fo*\" matches \"f\", \"fo\"",
" \"foo\", etc.",
"'+' An expression followed by a plus sign matches one or more",
" occurrences of that expression: \"fo+\" matches \"fo\", etc.",
"'-' An expression followed by a minus sign optionally matches",
" the expression.",
"'[]' A string enclosed in square brackets matches any character in",
" that string, but no others. If the first character in the",
" string is a circumflex, the expression matches any character",
" except \"new-line\" and the characters in the string. For",
" example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"",
" matches \"abc\" but not \"axb\". A range of characters may be",
" specified by two characters separated by \"-\". Note that,",
" [a-z] matches alphabetics, while [z-a] never matches.",
"The concatenation of regular expressions is a regular expression.",
0};
#define LMAX 512
#define PMAX 256
#define CHAR 1
#define BOL 2
#define EOL 3
#define ANY 4
#define CLASS 5
#define NCLASS 6
#define STAR 7
#define PLUS 8
#define MINUS 9
#define ALPHA 10
#define DIGIT 11
#define NALPHA 12
#define PUNCT 13
#define RANGE 14
#define ENDPAT 15
int cflag=0, fflag=0, nflag=0, vflag=0, nfile=0, debug=0;
char *pp, lbuf[LMAX], pbuf[PMAX];
char *cclass();
char *pmatch();
void store(int);
void error(char *);
void badpat(char *, char *, char *);
int match(void);
/*** Display a file name *******************************/
void file(char *s)
{
printf("File %s:\n", s);
}
/*** Report unopenable file ****************************/
void cant(char *s)
{
fprintf(stderr, "%s: cannot open\n", s);
}
/*** Give good help ************************************/
void help(char **hp)
{
char **dp;
for (dp = hp; *dp; ++dp)
printf("%s\n", *dp);
}
/*** Display usage summary *****************************/
void usage(char *s)
{
fprintf(stderr, "?GREP-E-%s\n", s);
fprintf(stderr,
"Usage: grep [-cfnv] pattern [file ...]. grep ? for help\n");
exit(1);
}
/*** Compile the pattern into global pbuf[] ************/
void compile(char *source)
{
char *s; /* Source string pointer */
char *lp; /* Last pattern pointer */
int c; /* Current character */
int o; /* Temp */
char *spp; /* Save beginning of pattern */
s = source;
if (debug)
printf("Pattern = \"%s\"\n", s);
pp = pbuf;
while (c = *s++) {
/*
* STAR, PLUS and MINUS are special.
*/
if (c == '*' || c == '+' || c == '-') {
if (pp == pbuf ||
(o=pp[-1]) == BOL ||
o == EOL ||
o == STAR ||
o == PLUS ||
o == MINUS)
badpat("Illegal occurrence op.", source, s);
store(ENDPAT);
store(ENDPAT);
spp = pp; /* Save pattern end */
while (--pp > lp) /* Move pattern down */
*pp = pp[-1]; /* one byte */
*pp = (c == '*') ? STAR :
(c == '-') ? MINUS : PLUS;
pp = spp; /* Restore pattern end */
continue;
}
/*
* All the rest.
*/
lp = pp; /* Remember start */
switch(c) {
case '^':
store(BOL);
break;
case '$':
store(EOL);
break;
case '.':
store(ANY);
break;
case '[':
s = cclass(source, s);
break;
case ':':
if (*s) {
switch(tolower(c = *s++)) {
case 'a':
case 'A':
store(ALPHA);
break;
case 'd':
case 'D':
store(DIGIT);
break;
case 'n':
case 'N':
store(NALPHA);
break;
case ' ':
store(PUNCT);
break;
default:
badpat("Unknown : type", source, s);
}
break;
}
else badpat("No : type", source, s);
case '\\':
if (*s)
c = *s++;
default:
store(CHAR);
store(tolower(c));
}
}
store(ENDPAT);
store(0); /* Terminate string */
if (debug) {
for (lp = pbuf; lp < pp;) {
if ((c = (*lp++ & 0377)) < ' ')
printf("\\%o ", c);
else printf("%c ", c);
}
printf("\n");
}
}
/*** Compile a class (within []) ***********************/
char *cclass(char *source, char *src)
/* char *source; // Pattern start -- for error msg. */
/* char *src; // Class start */
{
char *s; /* Source pointer */
char *cp; /* Pattern start */
int c; /* Current character */
int o; /* Temp */
s = src;
o = CLASS;
if (*s == '^') {
++s;
o = NCLASS;
}
store(o);
cp = pp;
store(0); /* Byte count */
while ((c = *s++) && c!=']') {
if (c == '\\') { /* Store quoted char */
if ((c = *s++) == '\0') /* Gotta get something */
badpat("Class terminates badly", source, s);
else store(tolower(c));
}
else if (c == '-' &&
(pp - cp) > 1 && *s != ']' && *s != '\0') {
c = pp[-1]; /* Range start */
pp[-1] = RANGE; /* Range signal */
store(c); /* Re-store start */
c = *s++; /* Get end char and*/
store(tolower(c)); /* Store it */
}
else {
store(tolower(c)); /* Store normal char */
}
}
if (c != ']')
badpat("Unterminated class", source, s);
if ((c = (pp - cp)) >= 256)
badpat("Class too large", source, s);
if (c == 0)
badpat("Empty class", source, s);
*cp = c;
return(s);
}
/*** Store an entry in the pattern buffer **************/
void store(int op)
{
if (pp >= &pbuf[PMAX])
error("Pattern too complex\n");
*pp++ = op;
}
/*** Report a bad pattern specification ****************/
void badpat(char *message, char *source, char *stop)
/* char *message; // Error message */
/* char *source; // Pattern start */
/* char *stop; // Pattern end */
{
fprintf(stderr, "-GREP-E-%s, pattern is\"%s\"\n", message, source);
fprintf(stderr, "-GREP-E-Stopped at byte %ld, '%c'\n",
stop-source, stop[-1]);
error("?GREP-E-Bad pattern\n");
}
/*** Scan the file for the pattern in pbuf[] ***********/
void grep(FILE *fp, char *fn)
/* FILE *fp; // File to process */
/* char *fn; // File name (for -f option) */
{
int lno, count, m;
lno = 0;
count = 0;
while (fgets(lbuf, LMAX, fp)) {
++lno;
m = match();
if ((m && !vflag) || (!m && vflag)) {
++count;
if (!cflag) {
if (fflag && fn) {
file(fn);
fn = 0;
}
if (nflag)
printf("%d\t", lno);
printf("%s\n", lbuf);
}
}
}
if (cflag) {
if (fflag && fn)
file(fn);
printf("%d\n", count);
}
}
/*** Match line (lbuf) with pattern (pbuf) return 1 if match ***/
int match()
{
char *l; /* Line pointer */
for (l = lbuf; *l; ++l) {
if (pmatch(l, pbuf))
return(1);
}
return(0);
}
/*** Match partial line with pattern *******************/
char *pmatch(char *line, char *pattern)
/* char *line; // (partial) line to match */
/* char *pattern; // (partial) pattern to match */
{
char *l; /* Current line pointer */
char *p; /* Current pattern pointer */
char c; /* Current character */
char *e; /* End for STAR and PLUS match */
int op; /* Pattern operation */
int n; /* Class counter */
char *are; /* Start of STAR match */
l = line;
if (debug > 1)
printf("pmatch(\"%s\")\n", line);
p = pattern;
while ((op = *p++) != ENDPAT) {
if (debug > 1)
printf("byte[%ld] = 0%o, '%c', op = 0%o\n",
l-line, *l, *l, op);
switch(op) {
case CHAR:
if (tolower(*l++) != *p++)
return(0);
break;
case BOL:
if (l != lbuf)
return(0);
break;
case EOL:
if (*l != '\0')
return(0);
break;
case ANY:
if (*l++ == '\0')
return(0);
break;
case DIGIT:
if ((c = *l++) < '0' || (c > '9'))
return(0);
break;
case ALPHA:
c = tolower(*l++);
if (c < 'a' || c > 'z')
return(0);
break;
case NALPHA:
c = tolower(*l++);
if (c >= 'a' && c <= 'z')
break;
else if (c < '0' || c > '9')
return(0);
break;
case PUNCT:
c = *l++;
if (c == 0 || c > ' ')
return(0);
break;
case CLASS:
case NCLASS:
c = tolower(*l++);
n = *p++ & 0377;
do {
if (*p == RANGE) {
p += 3;
n -= 2;
if (c >= p[-2] && c <= p[-1])
break;
}
else if (c == *p++)
break;
} while (--n > 1);
if ((op == CLASS) == (n <= 1))
return(0);
if (op == CLASS)
p += n - 2;
break;
case MINUS:
e = pmatch(l, p); /* Look for a match */
while (*p++ != ENDPAT); /* Skip over pattern */
if (e) /* Got a match? */
l = e; /* Yes, update string */
break; /* Always succeeds */
case PLUS: /* One or more ... */
if ((l = pmatch(l, p)) == 0)
return(0); /* Gotta have a match */
case STAR: /* Zero or more ... */
are = l; /* Remember line start */
while (*l && (e = pmatch(l, p)))
l = e; /* Get longest match */
while (*p++ != ENDPAT); /* Skip over pattern */
while (l >= are) { /* Try to match rest */
if (e = pmatch(l, p))
return(e);
--l; /* Nope, try earlier */
}
return(0); /* Nothing else worked */
default:
printf("Bad op code %d\n", op);
error("Cannot happen -- match\n");
}
}
return(l);
}
/*** Report an error ***********************************/
void error(char *s)
{
fprintf(stderr, "%s", s);
exit(1);
}
/*** Main program - parse arguments & grep *************/
int main(int argc, char **argv)
{
char *p;
int c, i;
int gotpattern;
FILE *f;
if (argc <= 1)
usage("No arguments");
if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
help(documentation);
help(patdoc);
return 0;
}
nfile = argc-1;
gotpattern = 0;
for (i=1; i < argc; ++i) {
p = argv[i];
if (*p == '-') {
++p;
while (c = *p++) {
switch(tolower(c)) {
case '?':
help(documentation);
break;
case 'C':
case 'c':
++cflag;
break;
case 'D':
case 'd':
++debug;
break;
case 'F':
case 'f':
++fflag;
break;
case 'n':
case 'N':
++nflag;
break;
case 'v':
case 'V':
++vflag;
break;
default:
usage("Unknown flag");
}
}
argv[i] = 0;
--nfile;
} else if (!gotpattern) {
compile(p);
argv[i] = 0;
++gotpattern;
--nfile;
}
}
if (!gotpattern)
usage("No pattern");
if (nfile == 0)
grep(stdin, 0);
else {
fflag = fflag ^ (nfile > 0);
for (i=1; i < argc; ++i) {
if (p = argv[i]) {
if ((f=fopen(p, "r")) == NULL)
cant(p);
else {
grep(f, p);
fclose(f);
}
}
}
}
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,3 @@
File 46_grep.c:
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,24 @@
#include <stdio.h>
void fred(int x)
{
switch (x)
{
case 1: printf("1\n"); return;
case 2: printf("2\n"); break;
case 3: printf("3\n"); return;
}
printf("out\n");
}
int main()
{
fred(1);
fred(2);
fred(3);
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

View file

@ -0,0 +1,4 @@
1
2
out
3

View file

@ -0,0 +1,26 @@
#include <stdio.h>
int main()
{
int a;
char b;
a = 0;
while (a < 2)
{
printf("%d", a++);
break;
b = 'A';
while (b < 'C')
{
printf("%c", b++);
}
printf("e");
}
printf("\n");
return 0;
}
/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

Some files were not shown because too many files have changed in this diff Show more