From c14dab7f3a1cf72619fadce9b81b55426dc0b32e Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Mon, 10 Apr 2023 07:45:35 +0530 Subject: 5.19: initial commit, make use of makefiles and split main into multiple files --- 5.19/Makefile | 12 ++++++++++ 5.19/err.c | 9 ++++++++ 5.19/err.h | 1 + 5.19/getch.c | 22 ++++++++++++++++++ 5.19/getch.h | 2 ++ 5.19/main.c | 46 ++++++++++++++++++++++++++++++++++++++ 5.19/token.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5.19/token.h | 14 ++++++++++++ 8 files changed, 178 insertions(+) create mode 100644 5.19/Makefile create mode 100644 5.19/err.c create mode 100644 5.19/err.h create mode 100644 5.19/getch.c create mode 100644 5.19/getch.h create mode 100644 5.19/main.c create mode 100644 5.19/token.c create mode 100644 5.19/token.h (limited to '5.19') diff --git a/5.19/Makefile b/5.19/Makefile new file mode 100644 index 0000000..5152fbe --- /dev/null +++ b/5.19/Makefile @@ -0,0 +1,12 @@ +objects = main.o token.o getch.o err.o + +undcl : $(objects) + cc -o undcl $(objects) + +main.o token.o : getch.h +main.o : token.h +getch.o : err.h + +.PHONY : clean +clean : + rm undcl $(objects) diff --git a/5.19/err.c b/5.19/err.c new file mode 100644 index 0000000..0bc092c --- /dev/null +++ b/5.19/err.c @@ -0,0 +1,9 @@ +#include +#include +#include "err.h" + +void err(char *s) +{ + printf("fatal error: %s\n", s); + exit(1); +} diff --git a/5.19/err.h b/5.19/err.h new file mode 100644 index 0000000..1fa2cfc --- /dev/null +++ b/5.19/err.h @@ -0,0 +1 @@ +void err(char *s); diff --git a/5.19/getch.c b/5.19/getch.c new file mode 100644 index 0000000..e41671e --- /dev/null +++ b/5.19/getch.c @@ -0,0 +1,22 @@ +#include +#include +#include "err.h" +#include "getch.h" + +#define MAXBUFF 100 + +char buff[MAXBUFF]; +static size_t top; + +void ungetch(char c) +{ + if (top < MAXBUFF) + buff[top++] = c; + else + err("buff: stack overflow"); +} + +char getch(void) +{ + return (top > 0) ? buff[--top] : getchar(); +} diff --git a/5.19/getch.h b/5.19/getch.h new file mode 100644 index 0000000..cbfe8ad --- /dev/null +++ b/5.19/getch.h @@ -0,0 +1,2 @@ +void ungetch(char c); +char getch(void); diff --git a/5.19/main.c b/5.19/main.c new file mode 100644 index 0000000..3e1d2e9 --- /dev/null +++ b/5.19/main.c @@ -0,0 +1,46 @@ +#include +#include +#include "token.h" +#include "getch.h" + +#define OUTLEN 1000 + +char out[OUTLEN]; + +int main(void) +{ + int errstat; + char temp[OUTLEN + MAXTOKEN], type; + + while (gettoken() != EOF) { + errstat = 0; + strcpy(out, token); + while ((type = gettoken()) != '\n') { + if (type == PARENS || type == BRACKETS ) { + strcat(out, token); + } else if (type == '*') { + if ((type = peaktoken()) == PARENS || + type == BRACKETS) + sprintf(temp, "(*%s)", out); + else + sprintf(temp, "*%s", out); + strcpy(out, temp); + } else if (type == NAME) { + sprintf(temp, "%s %s", token, out); + out[0] = '\0'; /* rust sisters btfo */ + strncat(out, temp, OUTLEN - 1); + } else { + errstat = 1; + printf("err: invalid input at %s\n", token); + while ((type = gettoken()) != '\n') + ; + ungetch('\n'); + } + } + + if (!errstat) + printf("%s\n", out); + } + + return 0; +} diff --git a/5.19/token.c b/5.19/token.c new file mode 100644 index 0000000..7f4d555 --- /dev/null +++ b/5.19/token.c @@ -0,0 +1,72 @@ +#include +#include +#include "getch.h" +#include "token.h" + +enum { NO, YES }; + +char tokentype; +char token[MAXTOKEN]; +char prevtoken; + +int gettoken(void) +{ + char c, *p = token; + char getch(void); + void ungetch(char); + + if (prevtoken == YES) { + prevtoken = NO; + return tokentype; + } + + while(isblank(c = getch())) + ; + + if (c == '/') { /* ignore comments */ + if ((c = getch()) == '/') { + while ((c = getch()) != '\n') + ; + } else if (c == '*') { + while (getch() != '*' || (c = getch()) != '/') + if (c == '*') + ungetch('*'); + return gettoken(); + } else { + ungetch(c); + c = '/'; + } + } + + if (c == '(') { + if ((c = getch()) == ')') { + strcpy(token, "()"); + return tokentype = PARENS; + } else { + ungetch(c); + return tokentype = '('; + } + } else if (c == '[') { + for (*p++ = '['; (*p++ = getch()) != ']';) + ; + *p = '\0'; + return tokentype = BRACKETS; + } else if (isalpha(c)) { + for (*p++ = c; isalnum(c = getch());) + *p++ = c; + *p = '\0'; + ungetch(c); + return tokentype = NAME; + } else { + return tokentype = c; + } +} + +int peaktoken() +{ + char type; + + type = gettoken(); + prevtoken = YES; + return type; +} diff --git a/5.19/token.h b/5.19/token.h new file mode 100644 index 0000000..32a910a --- /dev/null +++ b/5.19/token.h @@ -0,0 +1,14 @@ +#ifndef TOKEN_H + +#define MAXTOKEN 100 + +enum { NAME, PARENS, BRACKETS }; + +int gettoken(void); +int peaktoken(void); + +extern char tokentype; +extern char token[MAXTOKEN]; + +#define TOKEN_H +#endif -- cgit v1.2.3