diff --git a/01_Scanner/cleancode/README.md b/01_Scanner/cleancode/README.md new file mode 100644 index 00000000..7103e7fb --- /dev/null +++ b/01_Scanner/cleancode/README.md @@ -0,0 +1,3 @@ +# I make a clean version for learning.(only tree file) +`if require: chmod +x run.sh` +run it: ./run.sh diff --git a/01_Scanner/cleancode/const.h b/01_Scanner/cleancode/const.h new file mode 100644 index 00000000..9eac31f3 --- /dev/null +++ b/01_Scanner/cleancode/const.h @@ -0,0 +1,20 @@ +#pragma once +#include +#include +#include +extern int Line; +extern int Putback; +extern FILE* Infile; + +typedef struct token{ + int type_idx; + int value; +}Token; + +enum { + T_PLUS, T_MINUS, T_STAR, T_SLASH, T_INTLIT +}; + +extern int scan(Token*); + +static char const * const token_str[] = { "+", "-", "*", "/", "intlit" }; diff --git a/01_Scanner/cleancode/data b/01_Scanner/cleancode/data new file mode 100644 index 00000000..cc0f5fe5 --- /dev/null +++ b/01_Scanner/cleancode/data @@ -0,0 +1 @@ +1 + 3 * 5 /2 diff --git a/01_Scanner/cleancode/main.c b/01_Scanner/cleancode/main.c new file mode 100644 index 00000000..f493b649 --- /dev/null +++ b/01_Scanner/cleancode/main.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include "const.h" + +int Line = 0; +int Putback = '\n'; +FILE *Infile = NULL; + +static void scanfile() +{ + Token t; + while (scan(&t)) + { + printf("token: %s", token_str[t.type_idx]); + if (t.type_idx == T_INTLIT) + printf(",value: %d", t.value); + putchar('\n'); + } +} +int main(int argc, char *argv[]) +{ + if (argc != 2){ + puts("usage:scanner input.txt"); + exit(0); + } + Infile = fopen(argv[1], "r"); + printf("opend file: %s\n",argv[1]); + if(Infile == NULL){ + puts("file not open!"); + } + scanfile(); + return 0; +} diff --git a/01_Scanner/cleancode/run.sh b/01_Scanner/cleancode/run.sh new file mode 100644 index 00000000..5cc63468 --- /dev/null +++ b/01_Scanner/cleancode/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +gcc main.c scan.c -o scanner +./scanner data diff --git a/01_Scanner/cleancode/scan.c b/01_Scanner/cleancode/scan.c new file mode 100644 index 00000000..b0e4c29e --- /dev/null +++ b/01_Scanner/cleancode/scan.c @@ -0,0 +1,87 @@ +#include "const.h" +#include +static int chrpos(char *s, int c) +{ + char *p; + + p = strchr(s, c); + return (p ? p - s : -1); +} +static void putback(int c) +{ + Putback = c; +} + +static int next() +{ + int c; + if (Putback) + { + c = Putback; + Putback = 0; + return c; + } + c = fgetc(Infile); + if (c == '\n') + Line++; + return c; +} +static int scanint(int c) +{ + int k, val = 0; + + while ((k = chrpos("0123456789", c)) >= 0) + { + val = val * 10 + k; + c = next(); + } + + putback(c); + return val; +} + +static int skip(void) +{ + int c; + + c = next(); + while (' ' == c || '\t' == c || '\n' == c || '\r' == c || '\f' == c) + { + c = next(); + } + return (c); +} + +int scan(Token *t) +{ + int c = skip(); + // printf("read char: %c\n", c); + switch (c) + { + case EOF: + return (0); + case '+': + t->type_idx = T_PLUS; + break; + case '-': + t->type_idx = T_MINUS; + break; + case '*': + t->type_idx = T_STAR; + break; + case '/': + t->type_idx = T_SLASH; + break; + default: + if (isdigit(c)) + { + t->type_idx = T_INTLIT; + t->value = scanint(c); + break; + } + printf("Unrecognised character %c on line %d\n", c, Line); + exit(1); + } + + return 1; +}