Skip to content

Commit 0df1c4f

Browse files
committed
Seperate files and Change the Makefile
1 parent a2bac58 commit 0df1c4f

File tree

10 files changed

+219
-198
lines changed

10 files changed

+219
-198
lines changed

0

-8
This file was deleted.

6

-6
This file was deleted.

9cc

-835 KB
Binary file not shown.

9cc.c

-180
This file was deleted.

9cc.h

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#include <ctype.h>
2+
#include <stdarg.h>
3+
#include <stdbool.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
19
//----------------------------------------------------------------------
210
// 型宣言(Type declaration)
311
//----------------------------------------------------------------------
@@ -49,3 +57,20 @@ char *user_input;
4957
// プロトタイプ宣言(Prototype declaration)
5058
//----------------------------------------------------------------------
5159
void error_at(char *loc, char *fmt, ...);
60+
bool consume(char *op);
61+
void expect(char *op);
62+
int expect_number();
63+
bool at_eof();
64+
Token *new_token(TokenKind kind, Token *cur, char *str, int len);
65+
bool startswith(char *p, char *q);
66+
Node *new_node(NodeKind kind, Node *lhs, Node *rhs);
67+
Node *new_node_num(int val);
68+
Node *expr();
69+
Node *equality();
70+
Node *relational();
71+
Node *add();
72+
Node *mul();
73+
Node *unary();
74+
Node *primary();
75+
Token *tokenize();
76+
void gen(Node *node);

=

Whitespace-only changes.

Makefile

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CFLAGS=-std=c11 -g -static
2+
SRCS=$(wildcard *.c)
3+
OBJS=$(SRCS:.c=.o)
24

3-
9cc: 9cc.c
5+
9cc: $(OBJS)
6+
$(CC) -o 9cc $(OBJS) $(LDFLAGS)
7+
8+
$(OBJS): 9cc.h
49

510
test: 9cc
611
./test.sh

codegen.c

+94-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,97 @@
1-
#include <9cc.h>
1+
#include "9cc.h"
2+
#include <ctype.h>
3+
#include <stdarg.h>
4+
#include <stdbool.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
9+
Node *expr() {
10+
return equality();
11+
}
12+
13+
// equality = relatinal ("==" relational | "!=" relational)
14+
Node *equality() {
15+
Node *node = relational();
16+
17+
for (;;) {
18+
if (consume("=="))
19+
node = new_node(ND_EQ, node, relational());
20+
else if (consume("!="))
21+
node = new_node(ND_NE, node, relational());
22+
else
23+
return node;
24+
}
25+
}
26+
27+
// relational = add ("<" add | "<=" add | ">" add | ">=" add)
28+
Node *relational() {
29+
Node *node = add();
30+
31+
for (;;) {
32+
if (consume("<")) {
33+
node = new_node(ND_LT, node, add());}
34+
else if (consume("<="))
35+
node = new_node(ND_LE, node, add());
36+
else if (consume(">"))
37+
node = new_node(ND_LT, add(), node);
38+
else if (consume(">="))
39+
node = new_node(ND_LE, add(), node);
40+
else
41+
return node;
42+
}
43+
}
44+
45+
// add = mul ("+" mul | "-" mul)*
46+
Node *add() {
47+
Node *node = mul();
48+
49+
for (;;) {
50+
if (consume("+"))
51+
node = new_node(ND_ADD, node, mul());
52+
else if (consume("-"))
53+
node = new_node(ND_SUB, node, mul());
54+
else
55+
return node;
56+
}
57+
}
58+
59+
// mul = unary ("*" unary | "/" unary)*
60+
Node *mul() {
61+
Node *node = unary();
62+
63+
for (;;) {
64+
if (consume("*"))
65+
node = new_node(ND_MUL, node, unary());
66+
else if (consume("/"))
67+
node = new_node(ND_DIV, node, unary());
68+
else
69+
return node;
70+
}
71+
}
72+
73+
// unary = ("+" | "-")? primary
74+
Node *unary() {
75+
if (consume("+"))
76+
return primary();
77+
if (consume("-"))
78+
return new_node(ND_SUB, new_node_num(0), primary());
79+
return primary();
80+
}
81+
82+
Node *primary() {
83+
// 次のトークンが"("なら、"(" expr ")"のはず
84+
if (consume("(")) {
85+
Node *node = expr();
86+
expect(")");
87+
return node;
88+
}
89+
90+
// そうでなければ数値のはず
91+
return new_node_num(expect_number());
92+
}
93+
94+
295

396
// x86-64のスタック操作命令を使って,スタックマシンを模倣するアセンブリを吐く関数
497
void gen(Node *node) {

main.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1-
#include <9cc.h>
1+
#include "9cc.h"
2+
#include <ctype.h>
3+
#include <stdarg.h>
4+
#include <stdbool.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
9+
// エラーを報告するための関数
10+
// printfと同じ引数を取る
11+
void error_at(char *loc, char *fmt, ...) {
12+
va_list ap;
13+
va_start(ap, fmt);
14+
15+
int pos = loc - user_input;
16+
fprintf(stderr, "%s\n", user_input);
17+
fprintf(stderr, "%*s", pos, " ");
18+
fprintf(stderr, "^ ");
19+
fprintf(stderr, fmt, ap);
20+
fprintf(stderr, "\n");
21+
exit(1);
22+
}
23+
224

325
int main(int argc, char **argv) {
426
if (argc != 2) {

0 commit comments

Comments
 (0)