authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 02:43:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 02:43:45-07:00
log4bbc074dd7d78f83c57103b22f95967f0936e904
treea3ff6adacd0724470e37452a0fa2f32615675f18
parent7d22a89eecde6e69323734fa7e8211555231510b

hello world IR code looks good


7 files changed, 348 insertions(+), 93 deletions(-)

README.md+20-12
......@@ -72,27 +72,35 @@ zig | C equivalent | Description
7272### Grammar
7373
7474```
75Root : many(FnDecl) token(EOF);
75Root : many(TopLevelDecl) token(EOF)
7676
77FnDecl : token(Fn) token(Symbol) ParamDeclList option(token(Arrow) Type) Block;
77TopLevelDecl : FnDef | ExternBlock
7878
79ParamDeclList : token(LParen) list(ParamDecl, token(Comma)) token(RParen);
79ExternBlock : token(Extern) token(LBrace) many(FnProtoDecl) token(RBrace)
8080
81ParamDecl : token(Symbol) token(Colon) Type;
81FnProto : token(Fn) token(Symbol) ParamDeclList option(token(Arrow) Type)
8282
83Type : token(Symbol) | PointerType;
83FnDecl : FnProto token(Semicolon)
8484
85PointerType : token(Star) token(Const) Type | token(Star) token(Mut) Type;
85FnDef : FnProto Block
8686
87Block : token(LBrace) many(Statement) token(RBrace);
87ParamDeclList : token(LParen) list(ParamDecl, token(Comma)) token(RParen)
8888
89Statement : ExpressionStatement | ReturnStatement ;
89ParamDecl : token(Symbol) token(Colon) Type
9090
91ExpressionStatement : Expression token(Semicolon) ;
91Type : token(Symbol) | PointerType
9292
93ReturnStatement : token(Return) Expression token(Semicolon) ;
93PointerType : token(Star) token(Const) Type | token(Star) token(Mut) Type
9494
95Expression : token(Number) | token(String) | FnCall ;
95Block : token(LBrace) many(Statement) token(RBrace)
9696
97FnCall : token(Symbol) token(LParen) list(Expression, token(Comma)) token(RParen) ;
97Statement : ExpressionStatement | ReturnStatement
98
99ExpressionStatement : Expression token(Semicolon)
100
101ReturnStatement : token(Return) Expression token(Semicolon)
102
103Expression : token(Number) | token(String) | FnCall
104
105FnCall : token(Symbol) token(LParen) list(Expression, token(Comma)) token(RParen)
98106```
src/codegen.cpp+149-56
......@@ -5,29 +5,33 @@
55
66#include <llvm-c/Core.h>
77
8struct FnTableEntry {
9 LLVMValueRef fn_value;
10 AstNode *proto_node;
11};
12
813struct CodeGen {
14 LLVMModuleRef mod;
915 AstNode *root;
10 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> fn_decls;
16 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> fn_defs;
1117 ZigList<ErrorMsg> errors;
1218 LLVMBuilderRef builder;
13 HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> external_fns;
14};
15
16struct ExpressionNode {
17 AstNode *type_node;
19 HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;
20 HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table;
1821};
1922
2023struct CodeGenNode {
2124 union {
2225 LLVMTypeRef type_ref; // for NodeTypeType
23 ExpressionNode expr; // for NodeTypeExpression
2426 } data;
2527};
2628
2729CodeGen *create_codegen(AstNode *root) {
2830 CodeGen *g = allocate<CodeGen>(1);
2931 g->root = root;
30 g->fn_decls.init(32);
32 g->fn_defs.init(32);
33 g->fn_table.init(32);
34 g->str_table.init(32);
3135 return g;
3236}
3337
......@@ -41,31 +45,81 @@ static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
4145 last_msg->msg = msg;
4246}
4347
48static LLVMTypeRef to_llvm_type(AstNode *type_node) {
49 assert(type_node->type == NodeTypeType);
50 assert(type_node->codegen_node);
51
52 return type_node->codegen_node->data.type_ref;
53}
54
4455static void analyze_node(CodeGen *g, AstNode *node) {
4556 switch (node->type) {
4657 case NodeTypeRoot:
47 for (int i = 0; i < node->data.root.fn_decls.length; i += 1) {
48 AstNode *child = node->data.root.fn_decls.at(i);
58 for (int i = 0; i < node->data.root.top_level_decls.length; i += 1) {
59 AstNode *child = node->data.root.top_level_decls.at(i);
4960 analyze_node(g, child);
5061 }
5162 break;
52 case NodeTypeFnDecl:
63 case NodeTypeExternBlock:
64 for (int fn_decl_i = 0; fn_decl_i < node->data.extern_block.fn_decls.length; fn_decl_i += 1) {
65 AstNode *fn_decl = node->data.extern_block.fn_decls.at(fn_decl_i);
66 analyze_node(g, fn_decl);
67
68 AstNode *fn_proto = fn_decl->data.fn_decl.fn_proto;
69 Buf *name = &fn_proto->data.fn_proto.name;
70 ZigList<AstNode *> *params = &fn_proto->data.fn_proto.params;
71
72 LLVMTypeRef *fn_param_values = allocate<LLVMTypeRef>(params->length);
73 for (int param_i = 0; param_i < params->length; param_i += 1) {
74 AstNode *param_node = params->at(param_i);
75 assert(param_node->type == NodeTypeParamDecl);
76 AstNode *param_type = param_node->data.param_decl.type;
77 fn_param_values[param_i] = to_llvm_type(param_type);
78 }
79 LLVMTypeRef return_type = to_llvm_type(fn_proto->data.fn_proto.return_type);
80
81 LLVMTypeRef fn_type = LLVMFunctionType(return_type, fn_param_values, params->length, 0);
82 LLVMValueRef fn_val = LLVMAddFunction(g->mod, buf_ptr(name), fn_type);
83 LLVMSetLinkage(fn_val, LLVMExternalLinkage);
84 LLVMSetFunctionCallConv(fn_val, LLVMCCallConv);
85
86 FnTableEntry *fn_table_entry = allocate<FnTableEntry>(1);
87 fn_table_entry->fn_value = fn_val;
88 fn_table_entry->proto_node = fn_proto;
89 g->fn_table.put(name, fn_table_entry);
90 }
91 break;
92 case NodeTypeFnDef:
5393 {
54 auto entry = g->fn_decls.maybe_get(&node->data.fn_decl.name);
94 AstNode *proto_node = node->data.fn_def.fn_proto;
95 assert(proto_node->type = NodeTypeFnProto);
96 Buf *proto_name = &proto_node->data.fn_proto.name;
97 auto entry = g->fn_defs.maybe_get(proto_name);
5598 if (entry) {
5699 add_node_error(g, node,
57 buf_sprintf("redefinition of '%s'", buf_ptr(&node->data.fn_decl.name)));
100 buf_sprintf("redefinition of '%s'", buf_ptr(proto_name)));
58101 } else {
59 g->fn_decls.put(&node->data.fn_decl.name, node);
60 for (int i = 0; i < node->data.fn_decl.params.length; i += 1) {
61 AstNode *child = node->data.fn_decl.params.at(i);
62 analyze_node(g, child);
63 }
64 analyze_node(g, node->data.fn_decl.return_type);
65 analyze_node(g, node->data.fn_decl.body);
102 g->fn_defs.put(proto_name, node);
103 analyze_node(g, proto_node);
66104 }
67105 break;
68106 }
107 case NodeTypeFnDecl:
108 {
109 AstNode *proto_node = node->data.fn_decl.fn_proto;
110 assert(proto_node->type == NodeTypeFnProto);
111 analyze_node(g, proto_node);
112 break;
113 }
114 case NodeTypeFnProto:
115 {
116 for (int i = 0; i < node->data.fn_proto.params.length; i += 1) {
117 AstNode *child = node->data.fn_proto.params.at(i);
118 analyze_node(g, child);
119 }
120 analyze_node(g, node->data.fn_proto.return_type);
121 break;
122 }
69123 case NodeTypeParamDecl:
70124 analyze_node(g, node->data.param_decl.type);
71125 break;
......@@ -131,47 +185,81 @@ static void analyze_node(CodeGen *g, AstNode *node) {
131185}
132186
133187
134/* TODO external fn
135 LLVMTypeRef puts_param_types[] = {LLVMPointerType(LLVMInt8Type(), 0)};
136 LLVMTypeRef puts_type = LLVMFunctionType(LLVMInt32Type(), puts_param_types, 1, 0);
137 LLVMValueRef puts_fn = LLVMAddFunction(mod, "puts", puts_type);
138 LLVMSetLinkage(puts_fn, LLVMExternalLinkage);
139 */
140
141188void semantic_analyze(CodeGen *g) {
189 g->mod = LLVMModuleCreateWithName("ZigModule");
190
142191 // Pass 1.
143192 analyze_node(g, g->root);
144193}
145194
146static LLVMTypeRef to_llvm_type(AstNode *type_node) {
147 assert(type_node->type == NodeTypeType);
148 assert(type_node->codegen_node);
149
150 return type_node->codegen_node->data.type_ref;
151}
195static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node);
152196
153197static LLVMValueRef gen_fn_call(CodeGen *g, AstNode *fn_call_node) {
154198 assert(fn_call_node->type == NodeTypeFnCall);
155199
156 zig_panic("TODO support external fn declarations");
157 //LLVMTypeRef fn_type = LLVMFunctionType(LLVMVoidType(), );
200 Buf *name = &fn_call_node->data.fn_call.name;
158201
159 // resolve function name
160 //LLVMValueRef result = LLVMBuildCall(g->builder,
202 auto entry = g->fn_table.maybe_get(name);
203 if (!entry) {
204 add_node_error(g, fn_call_node,
205 buf_sprintf("undefined function: '%s'", buf_ptr(name)));
206 return LLVMConstNull(LLVMInt32Type());
207 }
208 FnTableEntry *fn_table_entry = entry->value;
209 assert(fn_table_entry->proto_node->type == NodeTypeFnProto);
210 int expected_param_count = fn_table_entry->proto_node->data.fn_proto.params.length;
211 int actual_param_count = fn_call_node->data.fn_call.params.length;
212 if (expected_param_count != actual_param_count) {
213 add_node_error(g, fn_call_node,
214 buf_sprintf("wrong number of arguments. Expected %d, got %d.",
215 expected_param_count, actual_param_count));
216 return LLVMConstNull(LLVMInt32Type());
217 }
161218
219 LLVMValueRef *param_values = allocate<LLVMValueRef>(actual_param_count);
220 for (int i = 0; i < actual_param_count; i += 1) {
221 AstNode *expr_node = fn_call_node->data.fn_call.params.at(i);
222 param_values[i] = gen_expr(g, expr_node);
223 }
162224
163 //return value;
225 LLVMValueRef result = LLVMBuildCall(g->builder, fn_table_entry->fn_value,
226 param_values, actual_param_count, "");
227
228 return result;
229}
230
231static LLVMValueRef find_or_create_string(CodeGen *g, Buf *str) {
232 auto entry = g->str_table.maybe_get(str);
233 if (entry) {
234 return entry->value;
235 }
236 LLVMValueRef text = LLVMConstString(buf_ptr(str), buf_len(str), false);
237 LLVMValueRef global_value = LLVMAddGlobal(g->mod, LLVMTypeOf(text), "");
238 LLVMSetLinkage(global_value, LLVMInternalLinkage);
239 LLVMSetInitializer(global_value, text);
240 LLVMSetGlobalConstant(global_value, true);
241 g->str_table.put(str, global_value);
242
243 return global_value;
164244}
165245
166246static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node) {
167247 assert(expr_node->type == NodeTypeExpression);
168248 switch (expr_node->data.expression.type) {
169249 case AstNodeExpressionTypeNumber:
170 zig_panic("TODO number expr");
171 break;
250 {
251 Buf *number_str = &expr_node->data.expression.data.number;
252 LLVMTypeRef number_type = LLVMInt32Type();
253 LLVMValueRef number_val = LLVMConstIntOfStringAndSize(number_type,
254 buf_ptr(number_str), buf_len(number_str), 10);
255 return number_val;
256 }
172257 case AstNodeExpressionTypeString:
173 zig_panic("TODO string expr");
174 break;
258 {
259 Buf *str = &expr_node->data.expression.data.string;
260 fprintf(stderr, "str = '%s'\n", buf_ptr(str));
261 return find_or_create_string(g, str);
262 }
175263 case AstNodeExpressionTypeFnCall:
176264 return gen_fn_call(g, expr_node->data.expression.data.fn_call);
177265 }
......@@ -203,32 +291,37 @@ static void gen_block(CodeGen *g, AstNode *block_node) {
203291}
204292
205293void code_gen(CodeGen *g) {
206 LLVMModuleRef mod = LLVMModuleCreateWithName("ZigModule");
207294 g->builder = LLVMCreateBuilder();
208295
296 auto it = g->fn_defs.entry_iterator();
297 for (;;) {
298 auto *entry = it.next();
299 if (!entry)
300 break;
209301
210 for (int fn_decl_i = 0; fn_decl_i < g->root->data.root.fn_decls.length; fn_decl_i += 1) {
211 AstNode *fn_decl_node = g->root->data.root.fn_decls.at(fn_decl_i);
212 AstNodeFnDecl *fn_decl = &fn_decl_node->data.fn_decl;
302 AstNode *fn_def_node = entry->value;
303 AstNodeFnDef *fn_def = &fn_def_node->data.fn_def;
304 assert(fn_def->fn_proto->type == NodeTypeFnProto);
305 AstNodeFnProto *fn_proto = &fn_def->fn_proto->data.fn_proto;
213306
214 LLVMTypeRef ret_type = to_llvm_type(fn_decl->return_type);
215 LLVMTypeRef *param_types = allocate<LLVMTypeRef>(fn_decl->params.length);
216 for (int param_decl_i = 0; param_decl_i < fn_decl->params.length; param_decl_i += 1) {
217 AstNode *param_node = fn_decl->params.at(param_decl_i);
307 LLVMTypeRef ret_type = to_llvm_type(fn_proto->return_type);
308 LLVMTypeRef *param_types = allocate<LLVMTypeRef>(fn_proto->params.length);
309 for (int param_decl_i = 0; param_decl_i < fn_proto->params.length; param_decl_i += 1) {
310 AstNode *param_node = fn_proto->params.at(param_decl_i);
218311 assert(param_node->type == NodeTypeParamDecl);
219312 AstNode *type_node = param_node->data.param_decl.type;
220313 param_types[param_decl_i] = to_llvm_type(type_node);
221314 }
222 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, fn_decl->params.length, 0);
223 LLVMValueRef fn = LLVMAddFunction(mod, buf_ptr(&fn_decl->name), function_type);
315 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, fn_proto->params.length, 0);
316 LLVMValueRef fn = LLVMAddFunction(g->mod, buf_ptr(&fn_proto->name), function_type);
224317
225 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(fn, "entry");
226 LLVMPositionBuilderAtEnd(g->builder, entry);
318 LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn, "entry");
319 LLVMPositionBuilderAtEnd(g->builder, entry_block);
227320
228 gen_block(g, fn_decl->body);
321 gen_block(g, fn_def->body);
229322 }
230323
231 LLVMDumpModule(mod);
324 LLVMDumpModule(g->mod);
232325}
233326
234327ZigList<ErrorMsg> *codegen_error_messages(CodeGen *g) {
src/parser.cpp+151-23
......@@ -20,8 +20,12 @@ const char *node_type_str(NodeType node_type) {
2020 switch (node_type) {
2121 case NodeTypeRoot:
2222 return "Root";
23 case NodeTypeFnDef:
24 return "FnDef";
2325 case NodeTypeFnDecl:
2426 return "FnDecl";
27 case NodeTypeFnProto:
28 return "FnProto";
2529 case NodeTypeParamDecl:
2630 return "ParamDecl";
2731 case NodeTypeType:
......@@ -34,6 +38,8 @@ const char *node_type_str(NodeType node_type) {
3438 return "Expression";
3539 case NodeTypeFnCall:
3640 return "FnCall";
41 case NodeTypeExternBlock:
42 return "ExternBlock";
3743 }
3844 zig_unreachable();
3945}
......@@ -46,24 +52,30 @@ void ast_print(AstNode *node, int indent) {
4652 switch (node->type) {
4753 case NodeTypeRoot:
4854 fprintf(stderr, "%s\n", node_type_str(node->type));
49 for (int i = 0; i < node->data.root.fn_decls.length; i += 1) {
50 AstNode *child = node->data.root.fn_decls.at(i);
55 for (int i = 0; i < node->data.root.top_level_decls.length; i += 1) {
56 AstNode *child = node->data.root.top_level_decls.at(i);
5157 ast_print(child, indent + 2);
5258 }
5359 break;
54 case NodeTypeFnDecl:
60 case NodeTypeFnDef:
61 {
62 fprintf(stderr, "%s\n", node_type_str(node->type));
63 AstNode *child = node->data.fn_def.fn_proto;
64 ast_print(child, indent + 2);
65 ast_print(node->data.fn_def.body, indent + 2);
66 break;
67 }
68 case NodeTypeFnProto:
5569 {
56 Buf *name_buf = &node->data.fn_decl.name;
70 Buf *name_buf = &node->data.fn_proto.name;
5771 fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(name_buf));
5872
59 for (int i = 0; i < node->data.fn_decl.params.length; i += 1) {
60 AstNode *child = node->data.fn_decl.params.at(i);
73 for (int i = 0; i < node->data.fn_proto.params.length; i += 1) {
74 AstNode *child = node->data.fn_proto.params.at(i);
6175 ast_print(child, indent + 2);
6276 }
6377
64 ast_print(node->data.fn_decl.return_type, indent + 2);
65
66 ast_print(node->data.fn_decl.body, indent + 2);
78 ast_print(node->data.fn_proto.return_type, indent + 2);
6779
6880 break;
6981 }
......@@ -115,6 +127,19 @@ void ast_print(AstNode *node, int indent) {
115127 break;
116128 }
117129 break;
130 case NodeTypeExternBlock:
131 {
132 fprintf(stderr, "%s\n", node_type_str(node->type));
133 for (int i = 0; i < node->data.extern_block.fn_decls.length; i += 1) {
134 AstNode *child = node->data.extern_block.fn_decls.at(i);
135 ast_print(child, indent + 2);
136 }
137 break;
138 }
139 case NodeTypeFnDecl:
140 fprintf(stderr, "%s\n", node_type_str(node->type));
141 ast_print(node->data.fn_decl.fn_proto, indent + 2);
142 break;
118143 default:
119144 fprintf(stderr, "%s\n", node_type_str(node->type));
120145 break;
......@@ -135,10 +160,52 @@ static AstNode *ast_create_node(NodeType type, Token *first_token) {
135160 return node;
136161}
137162
163static AstNode *ast_create_node_with_node(NodeType type, AstNode *other_node) {
164 AstNode *node = allocate<AstNode>(1);
165 node->type = type;
166 node->line = other_node->line;
167 node->column = other_node->column;
168 return node;
169}
170
138171static void ast_buf_from_token(ParseContext *pc, Token *token, Buf *buf) {
139172 buf_init_from_mem(buf, buf_ptr(pc->buf) + token->start_pos, token->end_pos - token->start_pos);
140173}
141174
175static void parse_string_literal(ParseContext *pc, Token *token, Buf *buf) {
176 // skip the double quotes at beginning and end
177 // convert escape sequences
178 bool escape = false;
179 for (int i = token->start_pos; i < token->end_pos - 1; i += 1) {
180 uint8_t c = *((uint8_t*)buf_ptr(pc->buf) + i);
181 if (escape) {
182 switch (c) {
183 case '\\':
184 buf_append_char(buf, '\\');
185 break;
186 case 'r':
187 buf_append_char(buf, '\r');
188 break;
189 case 'n':
190 buf_append_char(buf, '\n');
191 break;
192 case 't':
193 buf_append_char(buf, '\t');
194 break;
195 case '"':
196 buf_append_char(buf, '"');
197 break;
198 }
199 escape = false;
200 } else if (c == '\\') {
201 escape = true;
202 } else {
203 buf_append_char(buf, c);
204 }
205 }
206 assert(!escape);
207}
208
142209static void ast_invalid_token_error(ParseContext *pc, Token *token) {
143210 Buf token_value = {0};
144211 ast_buf_from_token(pc, token, &token_value);
......@@ -304,7 +371,7 @@ static AstNode *ast_parse_expression(ParseContext *pc, int token_index, int *new
304371 token_index += 1;
305372 } else if (token->id == TokenIdStringLiteral) {
306373 node->data.expression.type = AstNodeExpressionTypeString;
307 ast_buf_from_token(pc, token, &node->data.expression.data.string);
374 parse_string_literal(pc, token, &node->data.expression.data.string);
308375 token_index += 1;
309376 } else {
310377 ast_invalid_token_error(pc, token);
......@@ -381,50 +448,111 @@ static AstNode *ast_parse_block(ParseContext *pc, int token_index, int *new_toke
381448}
382449
383450/*
384FnDecl : token(Fn) token(Symbol) ParamDeclList option(token(Arrow) Type) Block;
451FnProto : token(Fn) token(Symbol) ParamDeclList option(token(Arrow) Type)
385452*/
386static AstNode *ast_parse_fn_decl(ParseContext *pc, int token_index, int *new_token_index) {
453static AstNode *ast_parse_fn_proto(ParseContext *pc, int token_index, int *new_token_index) {
387454 Token *fn_token = &pc->tokens->at(token_index);
388455 token_index += 1;
389456 ast_expect_token(pc, fn_token, TokenIdKeywordFn);
390457
391 AstNode *node = ast_create_node(NodeTypeFnDecl, fn_token);
458 AstNode *node = ast_create_node(NodeTypeFnProto, fn_token);
392459
393460
394461 Token *fn_name = &pc->tokens->at(token_index);
395462 token_index += 1;
396463 ast_expect_token(pc, fn_name, TokenIdSymbol);
397464
398 ast_buf_from_token(pc, fn_name, &node->data.fn_decl.name);
465 ast_buf_from_token(pc, fn_name, &node->data.fn_proto.name);
399466
400467
401 ast_parse_param_decl_list(pc, token_index, &token_index, &node->data.fn_decl.params);
468 ast_parse_param_decl_list(pc, token_index, &token_index, &node->data.fn_proto.params);
402469
403470 Token *arrow = &pc->tokens->at(token_index);
404471 token_index += 1;
405472 if (arrow->id == TokenIdArrow) {
406 node->data.fn_decl.return_type = ast_parse_type(pc, token_index, &token_index);
473 node->data.fn_proto.return_type = ast_parse_type(pc, token_index, &token_index);
407474 } else if (arrow->id == TokenIdLBrace) {
408 node->data.fn_decl.return_type = nullptr;
475 node->data.fn_proto.return_type = nullptr;
409476 } else {
410477 ast_invalid_token_error(pc, arrow);
411478 }
412479
413 node->data.fn_decl.body = ast_parse_block(pc, token_index, &token_index);
480 *new_token_index = token_index;
481 return node;
482}
483
484/*
485FnDef : FnProto Block
486*/
487static AstNode *ast_parse_fn_def(ParseContext *pc, int token_index, int *new_token_index) {
488 AstNode *fn_proto = ast_parse_fn_proto(pc, token_index, &token_index);
489 AstNode *node = ast_create_node_with_node(NodeTypeFnDef, fn_proto);
490
491 node->data.fn_def.fn_proto = fn_proto;
492 node->data.fn_def.body = ast_parse_block(pc, token_index, &token_index);
493
494 *new_token_index = token_index;
495 return node;
496}
497
498/*
499FnDecl : FnProto token(Semicolon)
500*/
501static AstNode *ast_parse_fn_decl(ParseContext *pc, int token_index, int *new_token_index) {
502 AstNode *fn_proto = ast_parse_fn_proto(pc, token_index, &token_index);
503 AstNode *node = ast_create_node_with_node(NodeTypeFnDecl, fn_proto);
504
505 node->data.fn_decl.fn_proto = fn_proto;
506
507 Token *semicolon = &pc->tokens->at(token_index);
508 token_index += 1;
509 ast_expect_token(pc, semicolon, TokenIdSemicolon);
414510
415511 *new_token_index = token_index;
416512 return node;
417513}
418514
515/*
516ExternBlock : token(Extern) token(LBrace) many(FnProtoDecl) token(RBrace)
517*/
518static AstNode *ast_parse_extern_block(ParseContext *pc, int token_index, int *new_token_index) {
519 Token *extern_kw = &pc->tokens->at(token_index);
520 token_index += 1;
521 ast_expect_token(pc, extern_kw, TokenIdKeywordExtern);
522
523 AstNode *node = ast_create_node(NodeTypeExternBlock, extern_kw);
524
525 Token *l_brace = &pc->tokens->at(token_index);
526 token_index += 1;
527 ast_expect_token(pc, l_brace, TokenIdLBrace);
528
529 for (;;) {
530 Token *token = &pc->tokens->at(token_index);
531 if (token->id == TokenIdRBrace) {
532 token_index += 1;
533 *new_token_index = token_index;
534 return node;
535 } else {
536 AstNode *child = ast_parse_fn_decl(pc, token_index, &token_index);
537 node->data.extern_block.fn_decls.append(child);
538 }
539 }
540
541
542 zig_unreachable();
543}
419544
420static void ast_parse_fn_decl_list(ParseContext *pc, int token_index, ZigList<AstNode *> *fn_decls,
421 int *new_token_index)
545static void ast_parse_top_level_decls(ParseContext *pc, int token_index, int *new_token_index,
546 ZigList<AstNode *> *top_level_decls)
422547{
423548 for (;;) {
424549 Token *token = &pc->tokens->at(token_index);
425550 if (token->id == TokenIdKeywordFn) {
426 AstNode *fn_decl_node = ast_parse_fn_decl(pc, token_index, &token_index);
427 fn_decls->append(fn_decl_node);
551 AstNode *fn_decl_node = ast_parse_fn_def(pc, token_index, &token_index);
552 top_level_decls->append(fn_decl_node);
553 } else if (token->id == TokenIdKeywordExtern) {
554 AstNode *extern_node = ast_parse_extern_block(pc, token_index, &token_index);
555 top_level_decls->append(extern_node);
428556 } else {
429557 *new_token_index = token_index;
430558 return;
......@@ -440,7 +568,7 @@ AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens) {
440568 pc.tokens = tokens;
441569
442570 int new_token_index;
443 ast_parse_fn_decl_list(&pc, 0, &pc.root->data.root.fn_decls, &new_token_index);
571 ast_parse_top_level_decls(&pc, 0, &new_token_index, &pc.root->data.root.top_level_decls);
444572
445573 if (new_token_index != tokens->length - 1) {
446574 ast_invalid_token_error(&pc, &tokens->at(new_token_index));
src/parser.hpp+20-2
......@@ -10,6 +10,8 @@ struct CodeGenNode;
1010
1111enum NodeType {
1212 NodeTypeRoot,
13 NodeTypeFnProto,
14 NodeTypeFnDef,
1315 NodeTypeFnDecl,
1416 NodeTypeParamDecl,
1517 NodeTypeType,
......@@ -17,19 +19,28 @@ enum NodeType {
1719 NodeTypeStatement,
1820 NodeTypeExpression,
1921 NodeTypeFnCall,
22 NodeTypeExternBlock,
2023};
2124
2225struct AstNodeRoot {
23 ZigList<AstNode *> fn_decls;
26 ZigList<AstNode *> top_level_decls;
2427};
2528
26struct AstNodeFnDecl {
29struct AstNodeFnProto {
2730 Buf name;
2831 ZigList<AstNode *> params;
2932 AstNode *return_type;
33};
34
35struct AstNodeFnDef {
36 AstNode *fn_proto;
3037 AstNode *body;
3138};
3239
40struct AstNodeFnDecl {
41 AstNode *fn_proto;
42};
43
3344struct AstNodeParamDecl {
3445 Buf name;
3546 AstNode *type;
......@@ -92,6 +103,10 @@ struct AstNodeFnCall {
92103 ZigList<AstNode *> params;
93104};
94105
106struct AstNodeExternBlock {
107 ZigList<AstNode *> fn_decls;
108};
109
95110struct AstNode {
96111 enum NodeType type;
97112 AstNode *parent;
......@@ -100,13 +115,16 @@ struct AstNode {
100115 CodeGenNode *codegen_node;
101116 union {
102117 AstNodeRoot root;
118 AstNodeFnDef fn_def;
103119 AstNodeFnDecl fn_decl;
120 AstNodeFnProto fn_proto;
104121 AstNodeType type;
105122 AstNodeParamDecl param_decl;
106123 AstNodeBlock block;
107124 AstNodeStatement statement;
108125 AstNodeExpression expression;
109126 AstNodeFnCall fn_call;
127 AstNodeExternBlock extern_block;
110128 } data;
111129};
112130
src/tokenizer.cpp+3
......@@ -150,6 +150,8 @@ static void end_token(Tokenize *t) {
150150 t->cur_tok->id = TokenIdKeywordMut;
151151 } else if (mem_eql_str(token_mem, token_len, "const")) {
152152 t->cur_tok->id = TokenIdKeywordConst;
153 } else if (mem_eql_str(token_mem, token_len, "extern")) {
154 t->cur_tok->id = TokenIdKeywordExtern;
153155 }
154156
155157 t->cur_tok = nullptr;
......@@ -307,6 +309,7 @@ static const char * token_name(Token *token) {
307309 case TokenIdKeywordConst: return "Const";
308310 case TokenIdKeywordMut: return "Mut";
309311 case TokenIdKeywordReturn: return "Return";
312 case TokenIdKeywordExtern: return "Extern";
310313 case TokenIdLParen: return "LParen";
311314 case TokenIdRParen: return "RParen";
312315 case TokenIdComma: return "Comma";
src/tokenizer.hpp+1
......@@ -17,6 +17,7 @@ enum TokenId {
1717 TokenIdKeywordReturn,
1818 TokenIdKeywordMut,
1919 TokenIdKeywordConst,
20 TokenIdKeywordExtern,
2021 TokenIdLParen,
2122 TokenIdRParen,
2223 TokenIdComma,
test/hello.zig+4
......@@ -1,3 +1,7 @@
1extern {
2 fn puts(s: *mut u8) -> i32;
3}
4
15fn main(argc: i32, argv: *mut *mut u8) -> i32 {
26 puts("Hello, world!\n");
37 return 0;