| ... | @@ -38,6 +38,8 @@ pub fn expr(mod: *Module, scope: *Scope, node: *ast.Node) InnerError!*zir.Inst { | ... | @@ -38,6 +38,8 @@ pub fn expr(mod: *Module, scope: *Scope, node: *ast.Node) InnerError!*zir.Inst { |
| 38 | .Period => return field(mod, scope, node.castTag(.Period).?), | 38 | .Period => return field(mod, scope, node.castTag(.Period).?), |
| 39 | .Deref => return deref(mod, scope, node.castTag(.Deref).?), | 39 | .Deref => return deref(mod, scope, node.castTag(.Deref).?), |
| 40 | .BoolNot => return boolNot(mod, scope, node.castTag(.BoolNot).?), | 40 | .BoolNot => return boolNot(mod, scope, node.castTag(.BoolNot).?), |
| | 41 | .FloatLiteral => return floatLiteral(mod, scope, node.castTag(.FloatLiteral).?), |
| | 42 | .UndefinedLiteral, .BoolLiteral, .NullLiteral => return primitiveLiteral(mod, scope, node), |
| 41 | else => return mod.failNode(scope, node, "TODO implement astgen.Expr for {}", .{@tagName(node.tag)}), | 43 | else => return mod.failNode(scope, node, "TODO implement astgen.Expr for {}", .{@tagName(node.tag)}), |
| 42 | } | 44 | } |
| 43 | } | 45 | } |
| ... | @@ -405,6 +407,52 @@ fn integerLiteral(mod: *Module, scope: *Scope, int_lit: *ast.Node.IntegerLiteral | ... | @@ -405,6 +407,52 @@ fn integerLiteral(mod: *Module, scope: *Scope, int_lit: *ast.Node.IntegerLiteral |
| 405 | } | 407 | } |
| 406 | } | 408 | } |
| 407 | | 409 | |
| | 410 | fn floatLiteral(mod: *Module, scope: *Scope, float_lit: *ast.Node.FloatLiteral) InnerError!*zir.Inst { |
| | 411 | const arena = scope.arena(); |
| | 412 | const tree = scope.tree(); |
| | 413 | const bytes = tree.tokenSlice(float_lit.token); |
| | 414 | if (bytes.len > 2 and bytes[1] == 'x') { |
| | 415 | return mod.failTok(scope, float_lit.token, "TODO hex floats", .{}); |
| | 416 | } |
| | 417 | |
| | 418 | const val = std.fmt.parseFloat(f128, bytes) catch |e| switch (e) { |
| | 419 | error.InvalidCharacter => unreachable, // validated by tokenizer |
| | 420 | }; |
| | 421 | const float_payload = try arena.create(Value.Payload.Float_128); |
| | 422 | float_payload.* = .{ .val = val }; |
| | 423 | const src = tree.token_locs[float_lit.token].start; |
| | 424 | return mod.addZIRInstConst(scope, src, .{ |
| | 425 | .ty = Type.initTag(.comptime_float), |
| | 426 | .val = Value.initPayload(&float_payload.base), |
| | 427 | }); |
| | 428 | } |
| | 429 | |
| | 430 | fn primitiveLiteral(mod: *Module, scope: *Scope, node: *ast.Node) InnerError!*zir.Inst { |
| | 431 | const arena = scope.arena(); |
| | 432 | const tree = scope.tree(); |
| | 433 | const src = tree.token_locs[node.firstToken()].start; |
| | 434 | |
| | 435 | if (node.cast(ast.Node.BoolLiteral)) |bool_node| { |
| | 436 | return mod.addZIRInstConst(scope, src, .{ |
| | 437 | .ty = Type.initTag(.bool), |
| | 438 | .val = if (tree.token_ids[bool_node.token] == .Keyword_true) |
| | 439 | Value.initTag(.bool_true) |
| | 440 | else |
| | 441 | Value.initTag(.bool_false), |
| | 442 | }); |
| | 443 | } else if (node.tag == .UndefinedLiteral) { |
| | 444 | return mod.addZIRInstConst(scope, src, .{ |
| | 445 | .ty = Type.initTag(.@"undefined"), |
| | 446 | .val = Value.initTag(.undef), |
| | 447 | }); |
| | 448 | } else if (node.tag == .NullLiteral) { |
| | 449 | return mod.addZIRInstConst(scope, src, .{ |
| | 450 | .ty = Type.initTag(.@"null"), |
| | 451 | .val = Value.initTag(.null_value), |
| | 452 | }); |
| | 453 | } else unreachable; |
| | 454 | } |
| | 455 | |
| 408 | fn assembly(mod: *Module, scope: *Scope, asm_node: *ast.Node.Asm) InnerError!*zir.Inst { | 456 | fn assembly(mod: *Module, scope: *Scope, asm_node: *ast.Node.Asm) InnerError!*zir.Inst { |
| 409 | if (asm_node.outputs.len != 0) { | 457 | if (asm_node.outputs.len != 0) { |
| 410 | return mod.failNode(scope, &asm_node.base, "TODO implement asm with an output", .{}); | 458 | return mod.failNode(scope, &asm_node.base, "TODO implement asm with an output", .{}); |