authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-19 00:02:03-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-19 19:31:50-04:00
log4cb203db9242cb477027b92d866c4dccece64d38
tree6c9753cf8495bb9cdaa5f978408b5704f5f7a7d4
parent7127c07f688ca80af78847de647cdd2499ea02f4

ir: parsing integer literals


2 files changed, 38 insertions(+), 10 deletions(-)

src-self-hosted/ir.zig+37-10
......@@ -219,8 +219,8 @@ fn parseOptionalType(ctx: *ParseContext) !?Type {
219219
220220fn parseInstruction(ctx: *ParseContext, opt_type: ?Type) error{ OutOfMemory, ParseFailure }!*Inst {
221221 switch (ctx.source[ctx.i]) {
222 '"' => return parseStringLiteralConst(ctx),
223 '0'...'9' => return parseIntegerLiteralConst(ctx),
222 '"' => return parseStringLiteralConst(ctx, opt_type),
223 '0'...'9' => return parseIntegerLiteralConst(ctx, opt_type),
224224 else => {},
225225 }
226226 const fn_name = try skipToAndOver(ctx, '(');
......@@ -327,7 +327,7 @@ fn parseBody(ctx: *ParseContext) !Inst.Fn.Body {
327327 };
328328}
329329
330fn parseStringLiteralConst(ctx: *ParseContext) !*Inst {
330fn parseStringLiteralConst(ctx: *ParseContext, opt_type: ?Type) !*Inst {
331331 const start = ctx.i;
332332 ctx.i += 1; // skip over '"'
333333
......@@ -344,17 +344,21 @@ fn parseStringLiteralConst(ctx: *ParseContext) !*Inst {
344344 },
345345 else => |e| return e,
346346 };
347 const const_inst = try ctx.allocator.create(Inst.Constant);
348 errdefer ctx.allocator.destroy(const_inst);
349
347350 const bytes_payload = try ctx.allocator.create(Value.Payload.Bytes);
348351 errdefer ctx.allocator.destroy(bytes_payload);
349352 bytes_payload.* = .{ .data = parsed };
350353
351 const ty_payload = try ctx.allocator.create(Type.Payload.Array_u8_Sentinel0);
352 errdefer ctx.allocator.destroy(ty_payload);
353 ty_payload.* = .{ .len = parsed.len };
354 const ty = opt_type orelse blk: {
355 const ty_payload = try ctx.allocator.create(Type.Payload.Array_u8_Sentinel0);
356 ty_payload.* = .{ .len = parsed.len };
357 break :blk Type.initPayload(&ty_payload.base);
358 };
354359
355 const const_inst = try ctx.allocator.create(Inst.Constant);
356360 const_inst.* = .{
357 .ty = Type.initPayload(&ty_payload.base),
361 .ty = ty,
358362 .positionals = .{ .value = Value.initPayload(&bytes_payload.base) },
359363 .kw_args = .{},
360364 };
......@@ -370,8 +374,31 @@ fn parseStringLiteralConst(ctx: *ParseContext) !*Inst {
370374 return parseError(ctx, "unexpected EOF in string literal", .{});
371375}
372376
373fn parseIntegerLiteralConst(ctx: *ParseContext) !*Inst {
374 return parseError(ctx, "TODO parse integer literal", .{});
377fn parseIntegerLiteralConst(ctx: *ParseContext, opt_type: ?Type) !*Inst {
378 const start = ctx.i;
379 while (ctx.i < ctx.source.len) : (ctx.i += 1) switch (ctx.source[ctx.i]) {
380 '0'...'9' => continue,
381 else => break,
382 };
383 const number_text = ctx.source[start..ctx.i];
384 const number = std.fmt.parseInt(u64, number_text, 10) catch |err| switch (err) {
385 error.Overflow => return parseError(ctx, "TODO handle big integers", .{}),
386 error.InvalidCharacter => return parseError(ctx, "invalid integer literal", .{}),
387 };
388
389 const int_payload = try ctx.allocator.create(Value.Payload.Int_u64);
390 errdefer ctx.allocator.destroy(int_payload);
391 int_payload.* = .{ .int = number };
392
393 const const_inst = try ctx.allocator.create(Inst.Constant);
394 errdefer ctx.allocator.destroy(const_inst);
395
396 const_inst.* = .{
397 .ty = opt_type orelse Type.initTag(.int_comptime),
398 .positionals = .{ .value = Value.initPayload(&int_payload.base) },
399 .kw_args = .{},
400 };
401 return &const_inst.base;
375402}
376403
377404pub fn main() anyerror!void {
src-self-hosted/type.zig+1
......@@ -75,6 +75,7 @@ pub const Type = extern union {
7575 /// See `zigTypeTag` for the function that corresponds to `std.builtin.TypeId`.
7676 pub const Tag = enum {
7777 // The first section of this enum are tags that require no payload.
78 int_comptime,
7879 int_u8,
7980 int_usize,
8081 // Bump this when adding items above.