Commits

Seth Hobson committed d5fee02

Initial commit

  • Participants

Comments (0)

Files changed (44)

File C/play/argvplay.c

+/* play with argv, array of pointers */
+#include <stdio.h>
+
+main(int argc, char * argv[]) {
+  int i;
+  printf("argc: value (decimal) is %d, address (hex) is 0x%0x\n",argc, &argc);
+  printf("argv: value (hex) of argv is 0x%0x, address (hex) is 0x%0x\n", argv, &argv);
+  for (i=0; i < argc; i++) {
+    printf("argv[%d]\n", i);
+    printf("\tvalue (hex) is 0x%0x\n", argv[i]);
+    printf("\taddress (hex) is 0x%0x\n",&argv[1]);
+    printf("\tpointed to value (string) is %s\n", argv[i]);
+    printf("\tpointed to value (string) is %s\n", *(argv+i));
+  }
+}

File C/play/chgptr.c

+/* Playing with pointer casting */
+
+#include <stdio.h>
+
+
+#define LETTER_1 0x41
+#define LETTER_2 0x42
+#define LETTER_3 0X43
+#define LETTER_4 0x44
+
+int main() {
+
+  int x=0;
+  char *cp;
+
+  printf("%d %c %c %c %c\n", x, LETTER_1, LETTER_2, LETTER_3, LETTER_4);
+
+  cp  = (char *) &x;
+  *cp = LETTER_1;
+  *(cp+1) = LETTER_2;
+  *(cp+2) = LETTER_3;
+  *(cp+3) = LETTER_4;
+
+  printf("%d %c %c %c %c\n", x, LETTER_1, LETTER_2, LETTER_3, LETTER_4);
+
+  x = 0x45464748;
+  printf("%d %c %c %c %c\n", x, *cp, *(cp+1), *(cp+2), *(cp+3));
+
+  char string[5] = "CAFE";
+  int *y = (int *)&string;
+  printf("%d %x\n", *y, *y);
+}  

File C/play/greet.c

+/* greet.c */
+
+#define JAY "jay"
+
+#include <stdio.h>
+void greet(void) {
+  printf("Hello %s\n", JAYJAY);
+}

File C/play/greet.h

+/* greet. h */
+
+void greet(void);

File C/play/greet.i

+# 1 "greet.c"
+# 1 "<built-in>"
+# 1 "<command-line>"
+# 1 "greet.c"
+
+
+
+
+# 1 "/usr/include/stdio.h" 1 3 4
+# 28 "/usr/include/stdio.h" 3 4
+# 1 "/usr/include/features.h" 1 3 4
+# 361 "/usr/include/features.h" 3 4
+# 1 "/usr/include/sys/cdefs.h" 1 3 4
+# 365 "/usr/include/sys/cdefs.h" 3 4
+# 1 "/usr/include/bits/wordsize.h" 1 3 4
+# 366 "/usr/include/sys/cdefs.h" 2 3 4
+# 362 "/usr/include/features.h" 2 3 4
+# 385 "/usr/include/features.h" 3 4
+# 1 "/usr/include/gnu/stubs.h" 1 3 4
+
+
+
+# 1 "/usr/include/bits/wordsize.h" 1 3 4
+# 5 "/usr/include/gnu/stubs.h" 2 3 4
+
+
+# 1 "/usr/include/gnu/stubs-32.h" 1 3 4
+# 8 "/usr/include/gnu/stubs.h" 2 3 4
+# 386 "/usr/include/features.h" 2 3 4
+# 29 "/usr/include/stdio.h" 2 3 4
+
+
+
+
+
+# 1 "/usr/lib/gcc/i686-redhat-linux/4.4.6/include/stddef.h" 1 3 4
+# 211 "/usr/lib/gcc/i686-redhat-linux/4.4.6/include/stddef.h" 3 4
+typedef unsigned int size_t;
+# 35 "/usr/include/stdio.h" 2 3 4
+
+# 1 "/usr/include/bits/types.h" 1 3 4
+# 28 "/usr/include/bits/types.h" 3 4
+# 1 "/usr/include/bits/wordsize.h" 1 3 4
+# 29 "/usr/include/bits/types.h" 2 3 4
+
+
+typedef unsigned char __u_char;
+typedef unsigned short int __u_short;
+typedef unsigned int __u_int;
+typedef unsigned long int __u_long;
+
+
+typedef signed char __int8_t;
+typedef unsigned char __uint8_t;
+typedef signed short int __int16_t;
+typedef unsigned short int __uint16_t;
+typedef signed int __int32_t;
+typedef unsigned int __uint32_t;
+
+
+
+
+__extension__ typedef signed long long int __int64_t;
+__extension__ typedef unsigned long long int __uint64_t;
+
+
+
+
+
+
+
+__extension__ typedef long long int __quad_t;
+__extension__ typedef unsigned long long int __u_quad_t;
+# 131 "/usr/include/bits/types.h" 3 4
+# 1 "/usr/include/bits/typesizes.h" 1 3 4
+# 132 "/usr/include/bits/types.h" 2 3 4
+
+
+__extension__ typedef __u_quad_t __dev_t;
+__extension__ typedef unsigned int __uid_t;
+__extension__ typedef unsigned int __gid_t;
+__extension__ typedef unsigned long int __ino_t;
+__extension__ typedef __u_quad_t __ino64_t;
+__extension__ typedef unsigned int __mode_t;
+__extension__ typedef unsigned int __nlink_t;
+__extension__ typedef long int __off_t;
+__extension__ typedef __quad_t __off64_t;
+__extension__ typedef int __pid_t;
+__extension__ typedef struct { int __val[2]; } __fsid_t;
+__extension__ typedef long int __clock_t;
+__extension__ typedef unsigned long int __rlim_t;
+__extension__ typedef __u_quad_t __rlim64_t;
+__extension__ typedef unsigned int __id_t;
+__extension__ typedef long int __time_t;
+__extension__ typedef unsigned int __useconds_t;
+__extension__ typedef long int __suseconds_t;
+
+__extension__ typedef int __daddr_t;
+__extension__ typedef long int __swblk_t;
+__extension__ typedef int __key_t;
+
+
+__extension__ typedef int __clockid_t;
+
+
+__extension__ typedef void * __timer_t;
+
+
+__extension__ typedef long int __blksize_t;
+
+
+
+
+__extension__ typedef long int __blkcnt_t;
+__extension__ typedef __quad_t __blkcnt64_t;
+
+
+__extension__ typedef unsigned long int __fsblkcnt_t;
+__extension__ typedef __u_quad_t __fsblkcnt64_t;
+
+
+__extension__ typedef unsigned long int __fsfilcnt_t;
+__extension__ typedef __u_quad_t __fsfilcnt64_t;
+
+__extension__ typedef int __ssize_t;
+
+
+
+typedef __off64_t __loff_t;
+typedef __quad_t *__qaddr_t;
+typedef char *__caddr_t;
+
+
+__extension__ typedef int __intptr_t;
+
+
+__extension__ typedef unsigned int __socklen_t;
+# 37 "/usr/include/stdio.h" 2 3 4
+# 45 "/usr/include/stdio.h" 3 4
+struct _IO_FILE;
+
+
+
+typedef struct _IO_FILE FILE;
+
+
+
+
+
+# 65 "/usr/include/stdio.h" 3 4
+typedef struct _IO_FILE __FILE;
+# 75 "/usr/include/stdio.h" 3 4
+# 1 "/usr/include/libio.h" 1 3 4
+# 32 "/usr/include/libio.h" 3 4
+# 1 "/usr/include/_G_config.h" 1 3 4
+# 15 "/usr/include/_G_config.h" 3 4
+# 1 "/usr/lib/gcc/i686-redhat-linux/4.4.6/include/stddef.h" 1 3 4
+# 16 "/usr/include/_G_config.h" 2 3 4
+
+
+
+
+# 1 "/usr/include/wchar.h" 1 3 4
+# 83 "/usr/include/wchar.h" 3 4
+typedef struct
+{
+  int __count;
+  union
+  {
+
+    unsigned int __wch;
+
+
+
+    char __wchb[4];
+  } __value;
+} __mbstate_t;
+# 21 "/usr/include/_G_config.h" 2 3 4
+
+typedef struct
+{
+  __off_t __pos;
+  __mbstate_t __state;
+} _G_fpos_t;
+typedef struct
+{
+  __off64_t __pos;
+  __mbstate_t __state;
+} _G_fpos64_t;
+# 53 "/usr/include/_G_config.h" 3 4
+typedef int _G_int16_t __attribute__ ((__mode__ (__HI__)));
+typedef int _G_int32_t __attribute__ ((__mode__ (__SI__)));
+typedef unsigned int _G_uint16_t __attribute__ ((__mode__ (__HI__)));
+typedef unsigned int _G_uint32_t __attribute__ ((__mode__ (__SI__)));
+# 33 "/usr/include/libio.h" 2 3 4
+# 53 "/usr/include/libio.h" 3 4
+# 1 "/usr/lib/gcc/i686-redhat-linux/4.4.6/include/stdarg.h" 1 3 4
+# 40 "/usr/lib/gcc/i686-redhat-linux/4.4.6/include/stdarg.h" 3 4
+typedef __builtin_va_list __gnuc_va_list;
+# 54 "/usr/include/libio.h" 2 3 4
+# 170 "/usr/include/libio.h" 3 4
+struct _IO_jump_t; struct _IO_FILE;
+# 180 "/usr/include/libio.h" 3 4
+typedef void _IO_lock_t;
+
+
+
+
+
+struct _IO_marker {
+  struct _IO_marker *_next;
+  struct _IO_FILE *_sbuf;
+
+
+
+  int _pos;
+# 203 "/usr/include/libio.h" 3 4
+};
+
+
+enum __codecvt_result
+{
+  __codecvt_ok,
+  __codecvt_partial,
+  __codecvt_error,
+  __codecvt_noconv
+};
+# 271 "/usr/include/libio.h" 3 4
+struct _IO_FILE {
+  int _flags;
+
+
+
+
+  char* _IO_read_ptr;
+  char* _IO_read_end;
+  char* _IO_read_base;
+  char* _IO_write_base;
+  char* _IO_write_ptr;
+  char* _IO_write_end;
+  char* _IO_buf_base;
+  char* _IO_buf_end;
+
+  char *_IO_save_base;
+  char *_IO_backup_base;
+  char *_IO_save_end;
+
+  struct _IO_marker *_markers;
+
+  struct _IO_FILE *_chain;
+
+  int _fileno;
+
+
+
+  int _flags2;
+
+  __off_t _old_offset;
+
+
+
+  unsigned short _cur_column;
+  signed char _vtable_offset;
+  char _shortbuf[1];
+
+
+
+  _IO_lock_t *_lock;
+# 319 "/usr/include/libio.h" 3 4
+  __off64_t _offset;
+# 328 "/usr/include/libio.h" 3 4
+  void *__pad1;
+  void *__pad2;
+  void *__pad3;
+  void *__pad4;
+  size_t __pad5;
+
+  int _mode;
+
+  char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
+
+};
+
+
+typedef struct _IO_FILE _IO_FILE;
+
+
+struct _IO_FILE_plus;
+
+extern struct _IO_FILE_plus _IO_2_1_stdin_;
+extern struct _IO_FILE_plus _IO_2_1_stdout_;
+extern struct _IO_FILE_plus _IO_2_1_stderr_;
+# 364 "/usr/include/libio.h" 3 4
+typedef __ssize_t __io_read_fn (void *__cookie, char *__buf, size_t __nbytes);
+
+
+
+
+
+
+
+typedef __ssize_t __io_write_fn (void *__cookie, __const char *__buf,
+     size_t __n);
+
+
+
+
+
+
+
+typedef int __io_seek_fn (void *__cookie, __off64_t *__pos, int __w);
+
+
+typedef int __io_close_fn (void *__cookie);
+# 416 "/usr/include/libio.h" 3 4
+extern int __underflow (_IO_FILE *);
+extern int __uflow (_IO_FILE *);
+extern int __overflow (_IO_FILE *, int);
+# 460 "/usr/include/libio.h" 3 4
+extern int _IO_getc (_IO_FILE *__fp);
+extern int _IO_putc (int __c, _IO_FILE *__fp);
+extern int _IO_feof (_IO_FILE *__fp) __attribute__ ((__nothrow__));
+extern int _IO_ferror (_IO_FILE *__fp) __attribute__ ((__nothrow__));
+
+extern int _IO_peekc_locked (_IO_FILE *__fp);
+
+
+
+
+
+extern void _IO_flockfile (_IO_FILE *) __attribute__ ((__nothrow__));
+extern void _IO_funlockfile (_IO_FILE *) __attribute__ ((__nothrow__));
+extern int _IO_ftrylockfile (_IO_FILE *) __attribute__ ((__nothrow__));
+# 490 "/usr/include/libio.h" 3 4
+extern int _IO_vfscanf (_IO_FILE * __restrict, const char * __restrict,
+   __gnuc_va_list, int *__restrict);
+extern int _IO_vfprintf (_IO_FILE *__restrict, const char *__restrict,
+    __gnuc_va_list);
+extern __ssize_t _IO_padn (_IO_FILE *, int, __ssize_t);
+extern size_t _IO_sgetn (_IO_FILE *, void *, size_t);
+
+extern __off64_t _IO_seekoff (_IO_FILE *, __off64_t, int, int);
+extern __off64_t _IO_seekpos (_IO_FILE *, __off64_t, int);
+
+extern void _IO_free_backup_area (_IO_FILE *) __attribute__ ((__nothrow__));
+# 76 "/usr/include/stdio.h" 2 3 4
+
+
+
+
+typedef __gnuc_va_list va_list;
+# 91 "/usr/include/stdio.h" 3 4
+typedef __off_t off_t;
+# 103 "/usr/include/stdio.h" 3 4
+typedef __ssize_t ssize_t;
+
+
+
+
+
+
+
+typedef _G_fpos_t fpos_t;
+
+
+
+
+# 161 "/usr/include/stdio.h" 3 4
+# 1 "/usr/include/bits/stdio_lim.h" 1 3 4
+# 162 "/usr/include/stdio.h" 2 3 4
+
+
+
+extern struct _IO_FILE *stdin;
+extern struct _IO_FILE *stdout;
+extern struct _IO_FILE *stderr;
+
+
+
+
+
+
+
+
+
+extern int remove (__const char *__filename) __attribute__ ((__nothrow__));
+
+extern int rename (__const char *__old, __const char *__new) __attribute__ ((__nothrow__));
+
+
+
+
+extern int renameat (int __oldfd, __const char *__old, int __newfd,
+       __const char *__new) __attribute__ ((__nothrow__));
+
+
+
+
+
+
+
+
+extern FILE *tmpfile (void) ;
+# 208 "/usr/include/stdio.h" 3 4
+extern char *tmpnam (char *__s) __attribute__ ((__nothrow__)) ;
+
+
+
+
+
+extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__)) ;
+# 226 "/usr/include/stdio.h" 3 4
+extern char *tempnam (__const char *__dir, __const char *__pfx)
+     __attribute__ ((__nothrow__)) __attribute__ ((__malloc__)) ;
+
+
+
+
+
+
+
+
+extern int fclose (FILE *__stream);
+
+
+
+
+extern int fflush (FILE *__stream);
+
+# 251 "/usr/include/stdio.h" 3 4
+extern int fflush_unlocked (FILE *__stream);
+# 265 "/usr/include/stdio.h" 3 4
+
+
+
+
+
+
+extern FILE *fopen (__const char *__restrict __filename,
+      __const char *__restrict __modes) ;
+
+
+
+
+extern FILE *freopen (__const char *__restrict __filename,
+        __const char *__restrict __modes,
+        FILE *__restrict __stream) ;
+# 294 "/usr/include/stdio.h" 3 4
+
+# 305 "/usr/include/stdio.h" 3 4
+extern FILE *fdopen (int __fd, __const char *__modes) __attribute__ ((__nothrow__)) ;
+# 318 "/usr/include/stdio.h" 3 4
+extern FILE *fmemopen (void *__s, size_t __len, __const char *__modes)
+  __attribute__ ((__nothrow__)) ;
+
+
+
+
+extern FILE *open_memstream (char **__bufloc, size_t *__sizeloc) __attribute__ ((__nothrow__)) ;
+
+
+
+
+
+
+extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __attribute__ ((__nothrow__));
+
+
+
+extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf,
+      int __modes, size_t __n) __attribute__ ((__nothrow__));
+
+
+
+
+
+extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf,
+         size_t __size) __attribute__ ((__nothrow__));
+
+
+extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__));
+
+
+
+
+
+
+
+
+extern int fprintf (FILE *__restrict __stream,
+      __const char *__restrict __format, ...);
+
+
+
+
+extern int printf (__const char *__restrict __format, ...);
+
+extern int sprintf (char *__restrict __s,
+      __const char *__restrict __format, ...) __attribute__ ((__nothrow__));
+
+
+
+
+
+extern int vfprintf (FILE *__restrict __s, __const char *__restrict __format,
+       __gnuc_va_list __arg);
+
+
+
+
+extern int vprintf (__const char *__restrict __format, __gnuc_va_list __arg);
+
+extern int vsprintf (char *__restrict __s, __const char *__restrict __format,
+       __gnuc_va_list __arg) __attribute__ ((__nothrow__));
+
+
+
+
+
+extern int snprintf (char *__restrict __s, size_t __maxlen,
+       __const char *__restrict __format, ...)
+     __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 4)));
+
+extern int vsnprintf (char *__restrict __s, size_t __maxlen,
+        __const char *__restrict __format, __gnuc_va_list __arg)
+     __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0)));
+
+# 416 "/usr/include/stdio.h" 3 4
+extern int vdprintf (int __fd, __const char *__restrict __fmt,
+       __gnuc_va_list __arg)
+     __attribute__ ((__format__ (__printf__, 2, 0)));
+extern int dprintf (int __fd, __const char *__restrict __fmt, ...)
+     __attribute__ ((__format__ (__printf__, 2, 3)));
+
+
+
+
+
+
+
+
+extern int fscanf (FILE *__restrict __stream,
+     __const char *__restrict __format, ...) ;
+
+
+
+
+extern int scanf (__const char *__restrict __format, ...) ;
+
+extern int sscanf (__const char *__restrict __s,
+     __const char *__restrict __format, ...) __attribute__ ((__nothrow__));
+# 447 "/usr/include/stdio.h" 3 4
+extern int fscanf (FILE *__restrict __stream, __const char *__restrict __format, ...) __asm__ ("" "__isoc99_fscanf")
+
+                               ;
+extern int scanf (__const char *__restrict __format, ...) __asm__ ("" "__isoc99_scanf")
+                              ;
+extern int sscanf (__const char *__restrict __s, __const char *__restrict __format, ...) __asm__ ("" "__isoc99_sscanf")
+
+                          __attribute__ ((__nothrow__));
+# 467 "/usr/include/stdio.h" 3 4
+
+
+
+
+
+
+
+
+extern int vfscanf (FILE *__restrict __s, __const char *__restrict __format,
+      __gnuc_va_list __arg)
+     __attribute__ ((__format__ (__scanf__, 2, 0))) ;
+
+
+
+
+
+extern int vscanf (__const char *__restrict __format, __gnuc_va_list __arg)
+     __attribute__ ((__format__ (__scanf__, 1, 0))) ;
+
+
+extern int vsscanf (__const char *__restrict __s,
+      __const char *__restrict __format, __gnuc_va_list __arg)
+     __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__scanf__, 2, 0)));
+# 498 "/usr/include/stdio.h" 3 4
+extern int vfscanf (FILE *__restrict __s, __const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vfscanf")
+
+
+
+     __attribute__ ((__format__ (__scanf__, 2, 0))) ;
+extern int vscanf (__const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vscanf")
+
+     __attribute__ ((__format__ (__scanf__, 1, 0))) ;
+extern int vsscanf (__const char *__restrict __s, __const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vsscanf")
+
+
+
+     __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__scanf__, 2, 0)));
+# 526 "/usr/include/stdio.h" 3 4
+
+
+
+
+
+
+
+
+
+extern int fgetc (FILE *__stream);
+extern int getc (FILE *__stream);
+
+
+
+
+
+extern int getchar (void);
+
+# 554 "/usr/include/stdio.h" 3 4
+extern int getc_unlocked (FILE *__stream);
+extern int getchar_unlocked (void);
+# 565 "/usr/include/stdio.h" 3 4
+extern int fgetc_unlocked (FILE *__stream);
+
+
+
+
+
+
+
+
+
+
+
+extern int fputc (int __c, FILE *__stream);
+extern int putc (int __c, FILE *__stream);
+
+
+
+
+
+extern int putchar (int __c);
+
+# 598 "/usr/include/stdio.h" 3 4
+extern int fputc_unlocked (int __c, FILE *__stream);
+
+
+
+
+
+
+
+extern int putc_unlocked (int __c, FILE *__stream);
+extern int putchar_unlocked (int __c);
+
+
+
+
+
+
+extern int getw (FILE *__stream);
+
+
+extern int putw (int __w, FILE *__stream);
+
+
+
+
+
+
+
+
+extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
+     ;
+
+
+
+
+
+
+extern char *gets (char *__s) ;
+
+# 660 "/usr/include/stdio.h" 3 4
+extern __ssize_t __getdelim (char **__restrict __lineptr,
+          size_t *__restrict __n, int __delimiter,
+          FILE *__restrict __stream) ;
+extern __ssize_t getdelim (char **__restrict __lineptr,
+        size_t *__restrict __n, int __delimiter,
+        FILE *__restrict __stream) ;
+
+
+
+
+
+
+
+extern __ssize_t getline (char **__restrict __lineptr,
+       size_t *__restrict __n,
+       FILE *__restrict __stream) ;
+
+
+
+
+
+
+
+
+extern int fputs (__const char *__restrict __s, FILE *__restrict __stream);
+
+
+
+
+
+extern int puts (__const char *__s);
+
+
+
+
+
+
+extern int ungetc (int __c, FILE *__stream);
+
+
+
+
+
+
+extern size_t fread (void *__restrict __ptr, size_t __size,
+       size_t __n, FILE *__restrict __stream) ;
+
+
+
+
+extern size_t fwrite (__const void *__restrict __ptr, size_t __size,
+        size_t __n, FILE *__restrict __s) ;
+
+# 732 "/usr/include/stdio.h" 3 4
+extern size_t fread_unlocked (void *__restrict __ptr, size_t __size,
+         size_t __n, FILE *__restrict __stream) ;
+extern size_t fwrite_unlocked (__const void *__restrict __ptr, size_t __size,
+          size_t __n, FILE *__restrict __stream) ;
+
+
+
+
+
+
+
+
+extern int fseek (FILE *__stream, long int __off, int __whence);
+
+
+
+
+extern long int ftell (FILE *__stream) ;
+
+
+
+
+extern void rewind (FILE *__stream);
+
+# 768 "/usr/include/stdio.h" 3 4
+extern int fseeko (FILE *__stream, __off_t __off, int __whence);
+
+
+
+
+extern __off_t ftello (FILE *__stream) ;
+# 787 "/usr/include/stdio.h" 3 4
+
+
+
+
+
+
+extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos);
+
+
+
+
+extern int fsetpos (FILE *__stream, __const fpos_t *__pos);
+# 810 "/usr/include/stdio.h" 3 4
+
+# 819 "/usr/include/stdio.h" 3 4
+
+
+extern void clearerr (FILE *__stream) __attribute__ ((__nothrow__));
+
+extern int feof (FILE *__stream) __attribute__ ((__nothrow__)) ;
+
+extern int ferror (FILE *__stream) __attribute__ ((__nothrow__)) ;
+
+
+
+
+extern void clearerr_unlocked (FILE *__stream) __attribute__ ((__nothrow__));
+extern int feof_unlocked (FILE *__stream) __attribute__ ((__nothrow__)) ;
+extern int ferror_unlocked (FILE *__stream) __attribute__ ((__nothrow__)) ;
+
+
+
+
+
+
+
+
+extern void perror (__const char *__s);
+
+
+
+
+
+
+# 1 "/usr/include/bits/sys_errlist.h" 1 3 4
+# 27 "/usr/include/bits/sys_errlist.h" 3 4
+extern int sys_nerr;
+extern __const char *__const sys_errlist[];
+# 849 "/usr/include/stdio.h" 2 3 4
+
+
+
+
+extern int fileno (FILE *__stream) __attribute__ ((__nothrow__)) ;
+
+
+
+
+extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__)) ;
+# 868 "/usr/include/stdio.h" 3 4
+extern FILE *popen (__const char *__command, __const char *__modes) ;
+
+
+
+
+
+extern int pclose (FILE *__stream);
+
+
+
+
+
+extern char *ctermid (char *__s) __attribute__ ((__nothrow__));
+# 908 "/usr/include/stdio.h" 3 4
+extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__));
+
+
+
+extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__)) ;
+
+
+extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__));
+# 938 "/usr/include/stdio.h" 3 4
+
+# 6 "greet.c" 2
+void greet(void) {
+  printf("Hello %s\n", JAYJAY);
+}

File C/play/main.c

+/* main.c */
+
+#include "greet.h"
+
+int main(void) {
+  greet();
+}

File C/play/ptrplay.c

+/* Playing with pointers */
+
+#include <stdio.h>
+
+main() {
+
+  int i = 5;
+  int *p = &i;
+
+  printf("i's value (decimal) is   %d\n", i);
+  printf("i's address (hex) is %x\n", &i);
+  printf("p's value (decimal) is  %ld\n", p);
+  printf("p's value (hex) is %x\n", p);
+  printf("p's address (hex) is %x\n", &p);
+  printf("p's pointed-to value (decimal) is %d\n", *p);
+  
+  return 1;
+}

File C/play/testslide.c

+#include <stdio.h>
+
+int main(void) {
+  int * ptr;
+  int ptee;
+  ptee = 5;
+  ptr = &ptee;
+
+  *ptr = *ptr + 2;
+  printf("ptee = %d\n", ptee);
+  printf("*ptr = %d\n", *ptr);
+
+  ptee = ptee - 1;
+  printf("ptee = %d\n", ptee);
+  printf("*ptr = %d\n", *ptr);
+}

File C/play/warnerr.c

+
+int main(void) {
+  int * numberPointers[3];
+  int * pointer;
+  int number;
+
+  number = pointer;
+  *pointer =number;
+  pointer = numberPointers[1];
+  pointer = numberPointers;
+  numberPointers[2] = number;
+  numberPointers[2] = &number;
+  numberPointers[2] = pointer;
+}

File Prolog/app.pl

+
+% append.pl
+%
+% Fall 2012
+% Dr. Fenwick
+%
+
+% base case
+app([], List2, List2).
+
+% two lists
+app([Head|Tail], List2, [Head|Result]) :- app(Tail,List2,Result).

File Prolog/del.pl

+%
+% del.pl - delete atom at top-level of list
+%
+
+del(X, [], []).
+del(X, [X], []).
+%del(X, [Y], [Y]) :- X \== Y.
+del(X, [X|T], Result) :- del(X, T, Result).
+del(X, [Y|T], [Y|Rtail]) :- X \== Y, del(X,T,Rtail).

File Prolog/exciting.pl

+
+/* Exciting life problem */
+/* All people that are wealthy and are smart are happy.
+   Those people that read are smart.
+   John can read and is wealthy.
+   Cindy can read.
+   Kate is wealthy.
+   Happy people have exciting lifes.
+
+Can anyone be found with an exciting life?   */
+
+/* facts or rules with the same name should be placed together. */
+/* We call a collection of facts or rules with the same name a  */
+/* prolog function.                                             */
+wealthy(kate).
+wealthy(john).
+
+/* read is a predefined function so this was made canread */
+canread(john).
+canread(cindy).
+
+/* note that variables are capitalized */
+/* smart function */
+smart(X) :- canread(X).
+
+/* happy function */
+happy(X) :- wealthy(X), smart(X).
+
+/* exciting life function */
+excitinglife(X) :- happy(X).
+
+

File Prolog/family.pl

+% family.pl
+
+% gender predicates
+male(douglas).
+male(gregory).
+male(james).
+male(george).
+male(bill).
+male(jim).
+male(william).
+male(jose).
+male(jason).
+male(cameron).
+female(rita).
+female(rosa).
+female(janet).
+female(mary).
+female(maria).
+female(juanita).
+female(brittany).
+female(linda).
+female(erica).
+
+
+% parent predicates parent(Child,Parent)
+parent(rosa,douglas).
+parent(gregory,douglas).
+parent(rosa,rita).
+parent(gregory,rita).
+parent(janet,james).
+parent(janet,rosa).
+parent(bill,george).
+parent(mary,george).
+parent(jim,george).
+parent(bill,janet).
+parent(mary,janet).
+parent(jim,janet).
+parent(maria,james).
+parent(maria,rosa).
+parent(jose,william).
+parent(jose,maria).
+parent(juanita,william).
+parent(juanita,maria).
+parent(jason,erica).
+parent(jason,gregory).
+parent(cameron,jason).
+parent(cameron,brittany).
+parent(linda,jason).
+parent(linda,brittany).
+
+% inferred relationships
+% Child C has mother M if it can be shown that
+% M is the parent of C and M is a female
+mother(C,M) :- parent(C,M),female(M).
+father(Child,Father) :- parent(Child,Father),male(Father).

File Prolog/farmer.pl

+
+opposite(E,W).
+opposite(W,E).
+
+solution([W,W,W,W],[]).
+unsafe([F,X,X,_]) :- opposite(F,X).
+unsafe([F,_,X,X]) :- opposite(F,X).
+
+safe([F,W,G,C]) :- \+(unsafe([F,W,G,C)).
+
+move([X,X,G,C], W, [Y,Y,G,C]) :- opposite(X,Y).
+move([X,W,X,C], G, [Y,W,Y,C]) :- opposite(X,Y).
+move([X,W,G,X], C, [Y,W,G,Y]) :- opposite(X,Y).
+move([X,W,G,C], Z, [Y,W,G,C]) :- opposite(X,Y).
+
+puzzle(P) :- length(X,Y), Y < 15, solution([E,E,E,E],X), append(([[E,E,E,E]],X,P), printSolution(P).
+
+

File Prolog/inssort.pl

+
+insertionsort(Unsorted, Sorted) :- inssort(Unsorted, [], Sorted).
+
+inssort([], Acc, Acc).
+inssort([Uh|Ut],Acc,Sorted) :- insert(Uh,Acc,Partial),inssort(Ut,Partial,Sorted).
+
+insert(I,[Sh|St],[Sh|List]) :- I>Sh,insert(I,St,List).
+insert(I, [Sh|St],[I,Sh|St] ) :- I=<Sh.
+insert(I, [], [I]).

File Prolog/isort.pl

+
+% accumulator (tail-recursive) version of insertion sort
+
+isort([Uh|Ut], SortedList) :- isort_acc(Ut, [Uh], SortedList).
+
+isort_acc([], Acc, Acc). 
+% isort_acc([], Acc, Answer) :- Answer is Acc.
+isort_acc([X], [], [X]).
+
+
+isort_acc([Uh|Ut], [Ah|At] ,SortedList ) :- Uh=<Ah,
+                                        isort_acc(Ut, [Uh,Ah|At] , SortedList).
+
+isort_acc([Uh|Ut], [Ah|At] ,SortedList ) :- Uh>Ah,
+                                      isort_acc([Uh], At, NewAcc),
+                                      isort_acc(Ut, [Ah|NewAcc] , SortedList).

File Prolog/len.pl

+%
+% len.pl - length of a list
+%
+
+% empty list has length of 0 is true stmt.
+len([],0).  
+
+% length of non-empty list is length of tail + 1.
+len([_|Tail], Len) :- len(Tail,N), Len is N+1.
+

File Prolog/mylast.pl

+%
+% mylast.pl - find last element in list
+%
+% Fall 2012
+% Dr. Fenwick
+%
+
+% base case, item in a 1-item list is the last item!
+mylast([X],X).
+
+% larger lists
+mylast([Head|Tail], X) :- mylast(Tail, X).

File Prolog/qsort.pl

+
+/*** quicksort */
+
+qsort([],[]).
+qsort([H|T], R) :- split(H,T,L1,L2),qsort(L1,S1),qsort(L2,S2),
+    append(S1,[H|S2],R).
+
+split(_,[],[],[]).
+split(H,[A|X],[A|Y],Z) :- A=<H,split(H,X,Y,Z).
+split(H,[A|X],Y,[A|Z]) :- A>H,split(H,X,Y,Z).

File Prolog/rev.pl

+%
+% rev.pl - to reverse a list
+%
+
+rev([],[]).
+rev([X],[X]).
+rev([H|T],Rlist) :- rev(T,Rtail),app(Rtail,[H],Rlist).

File chap3/Makefile

+# Makefile for WIC Interpreter
+# author: Seth Hobson
+CC = gcc
+C_FILES = wici.c instruction.c table.c stack.c
+OBJ = $(C_FILES:.c=.o)
+OUT = wici
+
+all: $(OBJ)
+	$(CC) -o $(OUT) $(OBJ)
+
+.c.o:
+	$(CC) -c -o $@ $<
+
+clean:
+	rm -rf $(OBJ) debug
+
+debug: $(OBJ)
+	$(CC) -g $(C_FILES) -o debug

File chap3/arithmetic.txt

+get A
+get B
+push A
+push B
+add % change to mult, div, or sub to test
+pop Sum
+put Sum
+halt

File chap3/binarydigits.txt

+get num
+push 0
+pop count
+L1  label
+push num
+push 0
+sub
+tstgt
+jf L2
+push num
+push 2
+div
+pop num
+push count
+push 1
+add
+pop count
+j L1
+L2  label
+put count
+halt

File chap3/instruction.c

+/**
+ * Implementation file of instruction table for the WIC Interpreter.
+ * @author Seth Hobson
+ * @version 20121002
+ */
+#include <stdio.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include "instruction.h"
+#include "table.h"
+#include "stack.h"
+
+// global variables for the stack, symbol table,
+// jump table, and instruction table
+static Stack stack;
+static tableType symbolTable;
+static tableType jumpTable;
+static instructionTable instructTable;
+
+/**
+ * Function: initializes all globals.
+ */
+void initialize()
+{
+    instructTable.count = 0;
+    initializeStack(&stack);
+    initializeTable(&symbolTable);
+    initializeTable(&jumpTable);
+}
+
+/**
+ * Function: fetches the operand at the current instruction,
+ * pops the stack, and stores the result and operand in the
+ * symbol table.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int pop(int pc)
+{
+    char *operand = fetchOperand(pc);
+    int opcode = stackPop(&stack);
+    store(&symbolTable, operand, opcode);
+    return pc + 1;
+}
+
+/**
+ * Function: pushes an operand onto the stack after checking
+ * whether it is an integer or string. If it is a string, the
+ * string is used as a key into the symbol table, from which the result
+ * is pushed.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int push(int pc)
+{
+    int temp;
+    char *operand = fetchOperand(pc);
+    
+    if (isdigit(operand[0]))
+        temp = (myatoi(operand)) ? atoi(operand) : 0;
+    else
+        temp = retrieve(&symbolTable, operand);
+    
+    stackPush(&stack, temp);
+    return pc + 1;
+}
+
+/**
+ * Function: reads a value from the user and stores it in the symbol table.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int get(int pc)
+{
+    int number;
+    char *operand = fetchOperand(pc);
+    
+    printf("enter %s > ", operand);
+    scanf("%d", &number);
+    
+    store(&symbolTable, operand, number);
+    return pc + 1;
+}
+
+/**
+ * Function: prints symbol and corresponding value to stdout
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int put(int pc)
+{
+    char *operand = fetchOperand(pc);
+    
+    int var = retrieve(&symbolTable, operand);
+    printf("%s = %d\n", operand, var);
+    
+    return pc + 1;
+}
+
+/**
+ * Function: performs a nop to insert a blank instruction
+ * @param pc the current instruction
+ * @return the current instruction
+ */
+int nop(int pc)
+{
+    return pc + 1;
+}
+
+/**
+ * Function: performs an unconditional jump
+ * @param pc the current instruction
+ * @return the instruction to jump to
+ */
+int jump(int pc)
+{
+    char *operand = fetchOperand(pc);
+    int newpc = retrieve(&jumpTable, operand);
+    return newpc;
+}
+
+/**
+ * Function: performs a conditional jump; when a value
+ * is popped off the stack, if its value is 0, the machine
+ * jumps to the popped instruction, otherwise to the incremented pc
+ * @param pc the current instruction
+ * @return either the address of the popped instruction or the incremented pc
+ */
+int jf(int pc)
+{
+    char *operand = fetchOperand(pc);
+    int val = stackPop(&stack);
+    if (val == 0)
+        return retrieve(&jumpTable, operand);
+    else
+        return pc + 1;
+}
+
+/**
+ * Function: performs the logical and instruction
+ * @param the current instruction
+ * @return the incremented pc
+ */
+int and(int pc)
+{
+    int rop = stackPop(&stack);
+    int lop = stackPop(&stack);
+    stackPush(&stack, lop && rop);
+    return pc + 1;
+}
+
+/**
+ * Function: performs the logical or instruction
+ * @param the current instruction
+ * @return the incremented pc
+ */
+int or(int pc)
+{
+    int rop = stackPop(&stack);
+    int lop = stackPop(&stack);
+    stackPush(&stack, lop || rop);
+    return pc + 1;
+}
+
+/**
+ * Function: performs the logical not instruction
+ * @param the current instruction
+ * @return the incremented pc
+ */
+int not(int pc)
+{
+    int val = stackPop(&stack);
+    stackPush(&stack, !val);
+    return pc + 1;
+}
+
+/**
+ * Function: performs the arithmetic add operation.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int add(int pc)
+{
+    int rop = stackPop(&stack);
+    int lop = stackPop(&stack);
+    stackPush(&stack, lop + rop);
+    return pc + 1;
+}
+
+/**
+ * Function: performs the arithmetic subtraction operation.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int sub(int pc)
+{
+    int rop = stackPop(&stack);
+    int lop = stackPop(&stack);
+    stackPush(&stack,lop - rop);
+    return pc + 1;
+}
+
+/**
+ * Function: performs the arithmetic multiplication operation.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int mult(int pc)
+{
+    int rop = stackPop(&stack);
+    int lop = stackPop(&stack);
+    stackPush(&stack, lop * rop);
+    return pc + 1;
+}
+
+/**
+ * Function: performs the arithmetic division operation.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int divide(int pc)
+{
+    int rop = stackPop(&stack);
+    int lop = stackPop(&stack);
+    stackPush(&stack, lop / rop);
+    return pc + 1;
+}
+
+/**
+ * Function: tests if the popped value is less than zero.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int testlt(int pc)
+{
+    int var = stackPop(&stack);
+    if (var < 0)
+        stackPush(&stack, 1);
+    else
+        stackPush(&stack, 0);
+    return pc + 1;
+}
+
+/**
+ * Function: tests if the popped value is greater than zero.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int testgt(int pc)
+{
+    int var = stackPop(&stack);
+    if (var > 0)
+        stackPush(&stack, 1);
+    else
+        stackPush(&stack, 0);
+    return pc + 1;
+}
+
+/**
+ * Function: tests if the popped value is equal to zero.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int testeq(int pc)
+{
+    int var = stackPop(&stack);
+    if (var == 0)
+        stackPush(&stack, 1);
+    else
+        stackPush(&stack, 0);
+    return pc + 1;
+}
+
+/**
+ * Function: tests if the popped value is not equal to zero.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int testne(int pc)
+{
+    int var = stackPop(&stack);
+    if (var != 0)
+        stackPush(&stack, 1);
+    else
+        stackPush(&stack, 0);
+    return pc + 1;
+}
+
+/**
+ * Function: tests if the popped value is less than or equal to zero.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int testle(int pc)
+{
+    int var = stackPop(&stack);
+    if (var <= 0)
+        stackPush(&stack, 1);
+    else
+        stackPush(&stack, 0);
+    return pc + 1;
+}
+
+/**
+ * Function: tests if the popped value is greater than or equal to zero.
+ * @param pc the current instruction
+ * @return the current pc incremented by 1
+ */
+int testge(int pc)
+{
+    int var = stackPop(&stack);
+    if (var >= 0)
+        stackPush(&stack, 1);
+    else
+        stackPush(&stack, 0);
+    return pc + 1;
+}
+
+/**
+ * Function: halts the machine.
+ * @return -1 to signal termination
+ */
+int halt()
+{
+    return -1;
+}
+
+/**
+ * Function: fetches the opcode at a specified address.
+ * @param address the address to be fetched from
+ * @return a string of characters 
+ */
+char *fetchOpcode(int address)
+{
+    return instructTable.instructions[address].opcode;
+}
+
+/**
+ * Function: fetches the operand at the specified address.
+ * @param address the address to be fetched from
+ * @return a string of characters
+ */
+char *fetchOperand(int address)
+{
+    return instructTable.instructions[address].operand;
+}
+
+/**
+ * Function: checks whether a given opcode has an associative operand.
+ * @param opcode the opcode to be checked for the presence of an operand
+ * @return 1 for true or 0 for false
+ */
+int hasOperand(char *opcode)
+{
+    char c = *opcode;
+    
+    if (c == 'j'|| c == 'J') {
+        return  (strcmp("j", opcode) == 0) ||
+                (strcmp("jf", opcode) == 0);
+    }
+    
+    if (c == 'l' || c == 'L') {
+        int hasLabel = 0;
+        while ((c = *(++opcode))) {
+            if (isspace(c))
+                break;
+            if (isdigit(c))
+                hasLabel = 1;
+            else
+                return 0;
+        }
+        return hasLabel;
+    }
+    
+    return  (strcmp("get", opcode) == 0) ||
+            (strcmp("put", opcode) == 0) ||
+            (strcmp("push", opcode) == 0) ||
+            (strcmp("pop", opcode) == 0);
+}
+
+/**
+ * Function: inserts an instruction into the instruction table.
+ * @param address the address to be stored into
+ * @param opcode the opcode to be stored
+ * @param operand the operand to be stored
+ */
+void insertInstruction(int address, char *opcode, char *operand)
+{
+    char *nop = "nop";
+    if (strcmp("label", operand) == 0) {
+        store(&jumpTable, opcode, address);
+        strncpy(instructTable.instructions[address].opcode, nop, OPCODE_LENGTH);
+        strncpy(instructTable.instructions[address].operand, opcode, OPERAND_LENGTH);
+    }
+    
+    else {
+        strncpy(instructTable.instructions[address].opcode, opcode, OPCODE_LENGTH);
+        strncpy(instructTable.instructions[address].operand, operand, OPERAND_LENGTH);
+    }
+    
+    instructTable.count++;
+}
+
+/**
+ * Function: prints the instruction table.
+ */
+void printInstructionTable()
+{
+    int i;
+    for (i = 0; i < instructTable.count; i++)
+        printf("%s %s\n", instructTable.instructions[i].opcode, instructTable.instructions[i].operand);
+}
+
+/**
+ * Function: prints the instruction table, symbol table, and jump table.
+ */
+void printAll()
+{
+    printf("printing symbol table...\n");
+    printTable(&symbolTable);
+    printf("printing jump table...\n");
+    printTable(&jumpTable);
+    printf("printing instruction table...\n");
+    printInstructionTable();
+}
+
+/**
+ * Function: checks if a given operand is purely numeric
+ * @param operand the operand to be checked
+ * @return 1 for true or 0 for false
+ */
+int myatoi(char *operand)
+{
+    if (operand[0] == '\0') // for empty string
+        return 0;
+    char c;
+    while ((c = *(operand++))) {
+        if (c < '0' || c > '9')
+            return 0;
+    }
+    return 1;
+}

File chap3/instruction.h

+/**
+ * Header file of an instruction table for WIC Interpreter.
+ * @author Seth Hobson
+ * @version 20121002
+ */
+#ifndef INSTRUCTION_H
+#define INSTRUCTION_H
+
+#define OPCODE_LENGTH 6
+#define OPERAND_LENGTH 21
+#define NUMBER_OF_INSTRUCTIONS 21
+
+typedef struct {
+    char opcode[OPCODE_LENGTH];
+    char operand[OPERAND_LENGTH];
+} Instruction;
+
+typedef struct {
+    int count;
+    Instruction instructions[NUMBER_OF_INSTRUCTIONS];
+} instructionTable;
+
+void initialize();
+int add(int pc);
+int pop(int pc);
+int get(int pc);
+int halt();
+int push(int pc);
+int put(int pc);
+int nop(int pc);
+int jump(int pc);
+int jf(int pc);
+int and(int pc);
+int or(int pc);
+int not(int pc);
+int sub(int pc);
+int mult(int pc);
+int divide(int pc);
+int testlt(int pc);
+int testgt(int pc);
+int testeq(int pc);
+int testne(int pc);
+int testle(int pc);
+int testge(int pc);
+char *fetchOpcode(int address);
+char *fetchOperand(int address);
+int hasOperand(char *opcode);
+void insertInstruction(int address, char *opcode, char *operand);
+int myatoi(char *operand);
+void printInstructionTable();
+void printAll();
+
+#endif

File chap3/instruction.o

Binary file added.

File chap3/jumps.txt

+get  A
+get  B
+push A
+push B
+sub
+tstlt
+jf L1
+push B
+pop MAX
+j L2
+L1   label
+push A
+pop MAX
+L2   label
+put MAX
+halt

File chap3/logic.txt

+get  X1
+get  X2
+get  X3
+get  X4
+push X1
+push X2
+not
+or
+push X3
+not
+or
+push X1
+not
+push X2
+or
+push X4
+or
+and
+pop Result
+put Result
+halt

File chap3/stack.c

+/**
+ * Implementation file for the stack in the WIC Interpreter
+ * @author Dr. Jay Fenwick
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include "stack.h"
+
+/**
+ * Function: initializes the stack
+ * @param s pointer to a stack struct
+ */
+void initializeStack(Stack * s)
+{
+    s->size = 0;
+}
+
+/**
+ * Function: pushes an integer onto the stack
+ * @param s pointer to a stack struct
+ * @param x the integer to be pushed onto the stack
+ */
+void stackPush(Stack *s, int x)
+{
+    if (s->size == STACK_SIZE) {
+        printf("Writing into full stack is illegal.\n");
+        abort();
+    }
+    else {
+      s->items[s->size++] = x;
+    }
+}
+
+/**
+ * Function: pops an integer off of the stack
+ * @param s pointer to a stack struct
+ * @return an integer from the top of the stack
+ */
+int stackPop(Stack *s)
+{
+    if (s->size == 0) {
+        printf("Popping from empty stack is illegal.\n");
+        exit(EXIT_FAILURE);
+    }
+    else {
+        return s->items[--s->size];
+    }
+}
+

File chap3/stack.h

+/**
+ * Header file for the WIC Interpreter
+ * @author Dr. Jay Fenwick
+ */
+#ifndef STACK_H
+#define STACK_H
+
+#define STACK_SIZE 21
+
+typedef struct {
+    int size;
+    int items[STACK_SIZE];
+} Stack;
+
+void initializeStack(Stack *s);
+void stackPush(Stack *s, int x);
+int stackPop(Stack *s);
+
+#endif

File chap3/stack.o

Binary file added.

File chap3/table.c

+/**
+ * Implementation file of a simple table for the WIC Interpreter.
+ * @author Seth Hobson
+ * @version 20121001
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "table.h"
+
+/**
+ * Function: initializes a new table
+ * @param Xtable pointer to a new table struct
+ */
+void initializeTable(tableType *Xtable)
+{
+    Xtable->size = 0;
+}
+
+/**
+ * Function: stores a value at specific key in the table
+ * @param Xtable pointer table struct
+ * @param key pointer to a key; if the key exists, it is updated with the value
+ * @param val a new value to be stored at a key
+ */
+void store(tableType *Xtable, char *key, int val)
+{
+    int index;
+    tableEntry *entry = findEntry(Xtable, key);
+    
+    if (entry)
+        entry->keyValue = val;
+    else {
+        index = Xtable->size;
+        tableEntry *newEntry = (tableEntry *)malloc(sizeof(tableEntry));
+        strcpy(newEntry->keys, key);
+        newEntry->keyValue = val;
+        Xtable->entries[index] = *newEntry;
+        Xtable->size++;
+        free(newEntry);
+    }
+}
+
+/**
+ * Function: retrieves a value from the table at a specified key
+ * @param Xtable pointer to a table struct
+ * @param key pointer to key to be retrieved from
+ * @return a value from the table at a specified key
+ */
+int retrieve(tableType *Xtable, char *key)
+{
+    tableEntry *entry = findEntry(Xtable, key);
+    
+    if (entry)
+        return entry->keyValue;
+    else {
+        printf("This key is currently not in the table.\n");
+        exit(EXIT_FAILURE);
+    }
+}
+
+/**
+ * Function: entry look-up helper function for storing and retrieving values
+ * @param Xtable pointer to a table struct
+ * @param key pointer to a key for look-up
+ * @return an entry in the table
+ */
+tableEntry *findEntry(tableType *Xtable, char *key)
+{
+    tableEntry *entry;
+    
+    int i;
+    for (i = 0; i < TABLE_ENTRIES_SIZE; i++) {
+        entry = &Xtable->entries[i];
+        if (strcmp(entry->keys, key) == 0)
+            return entry;
+    }
+    return NULL;
+}
+
+
+/**
+ * Function: prints the keys and values within the table
+ * @param Xtable pointer to a table struct
+ */
+void printTable(tableType *Xtable)
+{
+    tableEntry *entry;
+
+    int i;
+    for (i = 0; i < Xtable->size; i++) {
+        entry = &Xtable->entries[i];
+        printf("key: %s value: %d\n", entry->keys, entry->keyValue);
+    }
+}

File chap3/table.h

+/**
+ * Header file of a simple table. 
+ * @author Seth Hobson
+ * @version 20121001
+ */
+#ifndef TABLE_H
+#define TABLE_H
+
+#define TABLE_ENTRIES_SIZE 21
+#define KEY_ARRAY_SIZE 21
+
+typedef struct {
+    int keyValue;
+    char keys[KEY_ARRAY_SIZE];
+} tableEntry;
+
+typedef struct {
+    int size;
+    tableEntry entries[TABLE_ENTRIES_SIZE];
+} tableType;
+
+void initializeTable(tableType *Xtable);
+void store(tableType *Xtable, char *key, int val);
+int retrieve(tableType *Xtable, char *key);
+tableEntry *findEntry(tableType *Xtable, char *key);
+void printTable(tableType *Xtable);
+
+#endif

File chap3/table.o

Binary file added.

File chap3/testlt.txt

+get  A
+get  B
+push A
+push B
+sub
+tstlt
+pop LTresult
+put LTresult
+halt

File chap3/tests/instructionTest

Binary file added.

File chap3/tests/instructionTest.c

+/**
+ * Testing file for the instructions.
+ * @author Seth Hobson
+ * @version 20121001
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include "instruction.h"
+
+int main()
+{
+    printf("initializing instruction table...\n");
+    initialize();
+    
+    printf("inserting instructions into table...\n");
+    insertInstruction(0, "get", "A");
+    insertInstruction(1, "get", "B");
+    insertInstruction(2, "push", "A");
+    insertInstruction(3, "push", "B");
+    insertInstruction(4, "add", "");
+    insertInstruction(5, "pop", "C");
+    insertInstruction(6, "put", "C");
+    insertInstruction(7, "halt", "");
+    
+    printf("checking opcodes...");
+    if (strcmp("get", fetchOpcode(0)) == 0) printf(".");
+    if (strcmp("get", fetchOpcode(1)) == 0) printf(".");
+    if (strcmp("push", fetchOpcode(2)) == 0) printf(".");
+    if (strcmp("push", fetchOpcode(3)) == 0) printf(".");
+    if (strcmp("add", fetchOpcode(4)) == 0) printf(".");
+    if (strcmp("pop", fetchOpcode(5)) == 0) printf(".");
+    if (strcmp("put", fetchOpcode(6)) == 0) printf(".");
+    if (strcmp("halt", fetchOpcode(7)) == 0) printf(". passed!\n");
+    
+    
+    printf("checking operands...");
+    if (strcmp("A", fetchOperand(0)) == 0) printf(".");
+    if (strcmp("B", fetchOperand(1)) == 0) printf(".");
+    if (strcmp("A", fetchOperand(2)) == 0) printf(".");
+    if (strcmp("B", fetchOperand(3)) == 0) printf(".");
+    if (strcmp("", fetchOperand(4)) == 0) printf(".");
+    if (strcmp("C", fetchOperand(5)) == 0) printf(".");
+    if (strcmp("C", fetchOperand(6)) == 0) printf(".");
+    if (strcmp("", fetchOperand(7)) == 0) printf(". passed!\n");
+    
+    printf("printing instruction table...\n");
+    printInstructionList();
+    
+    printf("testing execution...\nplease enter integer values when prompted...\n");
+    int pc = 0;
+    char *opcode;
+    while (pc >= 0) {
+        opcode = fetchOpcode(pc);
+        if (strcmp(opcode, "get") == 0)
+            pc = get(pc);
+        else if (strcmp(opcode, "push") == 0)
+            pc = push(pc);
+        else if (strcmp(opcode, "add") == 0)
+            pc = add(pc);
+        else if (strcmp(opcode, "pop") == 0)
+            pc = pop(pc);
+        else if (strcmp(opcode, "put") == 0)
+            pc = put(pc);
+        else if (strcmp(opcode, "halt") == 0)
+            pc = halt();
+    }
+
+    printf("All tests passed\n");
+    return 0;
+}

File chap3/tests/stackTest

Binary file added.

File chap3/tests/stackTest.c

+/*
+ * stackTest is a TDD unit test for the stack
+ * @author Dr.Fenwick
+ * @version Spring 2012
+ */
+#include <stdio.h>
+#include "stack.h"
+
+int main(void) 
+{
+    Stack myStack;
+    int tmp;
+
+    printf("initializing stack...\n");
+    initializeStack(&myStack);
+    printf("pushing onto stack...\n");
+    stackPush(&myStack, 3);
+    printf("pushing onto stack...\n");
+    stackPush(&myStack, 99);
+    printf("pushing onto stack...\n");
+    stackPush(&myStack, 79);
+
+    printf("popping from stack...\n");
+    tmp = stackPop(&myStack); 
+    if (tmp != 79)
+        printf("You suck at writing a stack\n");
+    printf("popping from stack...\n");
+    tmp = stackPop(&myStack); 
+    if (tmp != 99)
+        printf("You suck at writing a stack\n");
+    printf("popping from stack...\n");
+    tmp = stackPop(&myStack); 
+    if (tmp != 3)
+        printf("You suck at writing a stack\n");
+        
+    /* test popping an empty stack????? */
+    /* spec says it writes to stderr and aborts */