Add some preprocessor magic
This commit is contained in:
parent
9e39e9d017
commit
030cbfe481
37
main.c
37
main.c
@ -14,25 +14,36 @@ F ::= (E)|i
|
|||||||
|
|
||||||
|
|
||||||
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
|
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
|
||||||
|
#define fatal(...) { eprintf(__VA_ARGS__); putc('\n', stderr); exit(1); }
|
||||||
|
#define ENUM(x) x,
|
||||||
|
#define STR(x) #x,
|
||||||
|
|
||||||
|
|
||||||
|
#define __TokenType(f) \
|
||||||
|
f(EMPTY) \
|
||||||
|
f(INTEGER) \
|
||||||
|
f(PLUS) \
|
||||||
|
f(MULT) \
|
||||||
|
f(LBRACE) \
|
||||||
|
f(RBRACE) \
|
||||||
|
f(LF) \
|
||||||
|
f(EF) \
|
||||||
|
|
||||||
typedef enum TokenType {
|
typedef enum TokenType {
|
||||||
INTEGER = 1,
|
__TokenType(ENUM)
|
||||||
PLUS = 2,
|
|
||||||
MULT = 4,
|
|
||||||
LBRACE = 8,
|
|
||||||
RBRACE = 16,
|
|
||||||
LF = 32,
|
|
||||||
EF = 64,
|
|
||||||
} TokenType;
|
} TokenType;
|
||||||
|
|
||||||
|
const char *TokenTypeStr[] = {
|
||||||
|
__TokenType(STR)
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
typedef struct _Token {
|
typedef struct _Token {
|
||||||
TokenType type;
|
TokenType type;
|
||||||
int value;
|
int value;
|
||||||
} Token;
|
} Token;
|
||||||
|
|
||||||
|
|
||||||
void fatal(const char* message);
|
|
||||||
Token scan();
|
Token scan();
|
||||||
|
|
||||||
int e();
|
int e();
|
||||||
@ -90,8 +101,7 @@ Token scan() {
|
|||||||
token.type = LF; break;
|
token.type = LF; break;
|
||||||
case EOF: token.type = EF; break;
|
case EOF: token.type = EF; break;
|
||||||
default:
|
default:
|
||||||
eprintf("'%c' (%d)\n", s, s);
|
fatal("Unexpected symbol: '%c' (%X)", s, s);
|
||||||
fatal("Unexpected symbol");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
@ -134,13 +144,8 @@ int f() {
|
|||||||
fatal("Unexpected EOF");
|
fatal("Unexpected EOF");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fatal("Unexpected token");
|
fatal("Unexpected token: %s (%d)", TokenTypeStr[current.type], current.type);
|
||||||
}
|
}
|
||||||
current = scan();
|
current = scan();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fatal(const char* message) {
|
|
||||||
eprintf("Error: %s\n", message);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user