Commit Diff


commit - 6e073af25564022293cb488284a0e03794b24480
commit + 6d61db9e6483af40d1790fd1451b58091269958b
blob - aefa2251b7f72c15255a845417eee9d1df9ba636
blob + 5dfb4fedf2d0991829a43293c202e32da8e32509
--- cc/cc1/Makefile
+++ cc/cc1/Makefile
@@ -1,4 +1,4 @@
-CFLAGS = -ansi -Wall -Wextra -Wno-comment -O2
+CFLAGS = -ansi -Wall -Wextra -Wno-deprecated-non-prototype -Wno-implicit-int -Wno-comment -O2
 
 all: cc1
 
blob - 3c64eca78cc6024ea1b05532ac8cf85ffb87551c
blob + 8fceecaa912883c53b4bed96d0e52986e66fbaa3
--- cc/cc1/cc1.c
+++ cc/cc1/cc1.c
@@ -1,5 +1,101 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>
 
+#define IDENT_LEN 8
 
+// ERROR HANDLING
+
+int line;
+
+error (msg)
+char *msg;
+{
+	fprintf (stderr, "cc1: %d: error: %s\n", line, msg);
+	return exit (1), -1;
+}
+
+// LEXER
+
+enum {
+	TK_IDENT = 128,
+	TK_EOF,
+};
+
+union {
+	char ident[IDENT_LEN + 1];
+} lval;
+
+int peekd = EOF;
+
+readch (void)
+{
+	int ch = getchar ();
+
+	if (ch == '\n')
+		++line;
+	
+	return ch;
+}
+
+peekch (void)
+{
+	if (peekd != EOF)
+		peekd = readch ();
+
+	return peekd;
+}
+
+nextch (void)
+{
+	int ch;
+
+	if (peekd != EOF) {
+		ch = peekd;
+		peekd = EOF;
+	} else {
+		ch = readch ();
+	}
+
+	return ch;
+}
+
+isname (ch)
+{
+	return isalnum (ch) || ch == '_';
+}
+
+lex (void)
+{
+	size_t i;
+	int ch;
+
+	while (isspace (peekch ()))
+		nextch ();
+
+	ch = peekch ();
+
+	if (isdigit (ch)) {
+		return error ("TODO: integers");
+	} else if (isname (ch)) {
+		for (i = 0; isname (peekch ());) {
+			ch = nextch ();
+			if (i < IDENT_LEN)
+				lval.ident[i++] = ch;
+		}
+		lval.ident[i] = '\0';
+		return TK_IDENT;
+	} else switch (ch) {
+	case '(':
+	case ')':
+	case '{':
+	case '}':
+		return ch;
+	default:
+		return error ("invalid input");
+	}
+}
+
 main (void)
 {
 	return 0;
blob - e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
blob + feb71ac63f70bb96e7df4d2fb378017d5893f315
--- cc/cc1/test.c
+++ cc/cc1/test.c
@@ -0,0 +1,4 @@
+main ()
+{
+
+}