Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); const builtin = @import("builtin"); const python27 = switch (builtin.os.tag) { .linux => "Python 2.7.16rc1 (default, Feb 18 2019, 11:05:09)\n[GCC 8.2.0] on linux2\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", .macosx => "Python 2.7.10 (default, Aug 17 2018, 19:45:58)\n[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", else => unreachable, }; const python37 = switch (builtin.os.tag) { .linux => "Python 3.7.2+ (default, Feb 27 2019, 15:41:59)\n[GCC 8.2.0] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", .macosx => "Python 3.7.2+ (default, Feb 27 2019, 15:41:59)\n[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", else => unreachable, }; const stdout = std.io.getStdOut().outStream(); const stderr = std.io.getStdErr().outStream(); const stdin = std.io.getStdIn().inStream(); fn printPrompt() !void { try stdout.print(">>> ", .{}); } fn printVersion(name: []const u8) !void { const py3k = std.mem.indexOf(u8, name, "python3") != null; const ver_string = if (py3k) python37 else python27; try stdout.print("{}", .{ver_string}); } fn notCommentOrBlank(line: []const u8) bool { for (line) |c| { const answer = switch (c) { ' ', '\t' => null, '#', '\n', 0 => false, else => true, }; if (answer) |ans| { return ans; } else { continue; } } return false; } /// Reads a line of code from the file into buf, returns a slice with /// the same .ptr address as buf, and updates lineno to be the line /// number of the in_stream. fn readLine(in_stream: var, lineno: *usize, buf: []u8) ![]u8 { lineno.* = 0; while (true) { const line = try in_stream.readUntilDelimiterOrEof(buf, '\n'); lineno.* += 1; if (line) |nnline| { if (notCommentOrBlank(nnline)) { return nnline; } } else { return error.EOFError; } } } fn printError(filename: []const u8, lineno: usize, errline: []const u8) !void { try stderr.print( \\ File "{}", line {} \\ {} \\ ^ \\SyntaxError: invalid syntax , .{ filename, lineno, errline }); try stderr.print("\n", .{}); } /// Returns the first non-empty line in filename, using the memory in buf. fn getLine( progname: []const u8, filename: []const u8, lineno: *usize, buf: []u8, ) ![]u8 { const dir = std.fs.cwd(); const fh = try dir.openFile(filename, .{ .read = true }); const line = try readLine(fh.inStream(), lineno, buf); return line; } pub fn main() anyerror!u8 { var args = std.process.args(); const cmd = args.nextPosix().?; const filename = args.nextPosix(); var lineno: usize = undefined; var buf: [1024]u8 = undefined; if (filename) |fname| { if (getLine(cmd, fname, &lineno, buf[0..])) |errline| { try printError(fname, lineno, errline); return 1; } else |err| { // This is wrong. The errno is wrong, the message is // wrong, everything is wrong. It wouldn't even be that // hard to do it right, but that would depend on libc and // I'm lazy. try stderr.print( "{}: can't open file '{}': [Errno {}] {}\n", .{ cmd, fname, @errorToInt(err), @errorName(err) }, ); return 2; } } else { lineno = 1; // Read lines from stdin. try printVersion(cmd); while (true) { try printPrompt(); if (readLine(stdin, &lineno, buf[0..])) |line| { try printError("<stdin>", lineno, line); } else |_| { return 130; } } } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree