An online Zig compiler inspired by Go and Rust

Zig Playground

This is a rudimentary online compiler for the Zig programming language. It is inspired by the Go playground.

Setup

The main server is a Go binary that serves up a single HTML page that allows you to enter your Zig code and then run it. To run it yourself, you will need a Go tool chain which can be installed via brew on a Mac. If you wish to run it locally, you must compile it for your GOOS and GOARCH but I have included a small shell script to make a Linux binary that Docker can use as well. You should also have Zig installed and accessible from within your $PATH on the host.

Hosting

In theory this could be run anywhere that a Docker container can execute. Google's Cloud Run may be a cheap option considering their generous free tier.

FAQ

What can this playground do?

It is currently set up to simply run a single Zig source file. (i.e. zig run source.zig)

Are there any timeouts?

If your code doesn't build within 10 seconds, the server will quit your request.

Why does the page look so bad?

I'm clearly more of a server-side programmer. Any styling changes are very much appreciated!

Why am I getting rate-limited?

You're allowed five compilations per minute which I think is fairly generous.

Is it secure?

Go read the source. I do not collect logs of any kind and am not interested in your data. Unless it is causing issues to the service.

Will this always be available?

To the best of my ability, I will try and keep this online. I may even buy a domain for it.

Contact

Feel free to email me at my listed address with any other questions or comments.

License

MIT

Comments
  • Improve UI slightly

    Improve UI slightly

    A few things:

    • Display result of compiling on the same page
    • Improve the look slightly
    • Add a favicon
    • Have a default example in the code box

    To do the first, it uses a little JavaScript to POST the text to the server, instead of submitting it as a form.

    I removed bootstrap and added a few of my own styles; let me know if there's parts you don't like. (As a bonus, removing bootstrap cuts out 98% of the page size! :) )

  • Compiler version is outdated

    Compiler version is outdated

    I tried running this program in the Zig playground, but it doesn't compile successfully. This might be due to an outdated version of the Zig compiler:

    const std = @import("std");
    
    // we make the interface very explicit here.
    // Might also be possible with less comptime parameters, but could be way more ugly then
    fn map(comptime Src: type, comptime Dst: type, comptime len: usize, array: [len]Src, function: fn(Src) Dst) [len]Dst {
        var result: [len]Dst = undefined;
        for (result) |*res ,index| {
            res.* = function(array[index]);
        }
        return result;
    }
    
    fn add(a: f64) f64 {
        return a + 1.0;
    }
    
    pub fn main() void {
        var array_thing = map(
            f64, // src type
            f64, // dst type
            3,   // array length
            .{1.0, 2.0,3.0},  // anonymous literal, can be coerced to array
            add, // the function
        );
        std.debug.print("Hello, {d}!", .{array_thing}); // prints "Hello, { 2, 3, 4 }!"
    }
    

    Will the Zig playground be automatically updated to use the latest compiler version, or does it still need to be updated manually?

  • Enhancement: Support zig fmt

    Enhancement: Support zig fmt

    Similar to Go Playground, I was wondering if we could possibly add an fmt option too in the zig-play. To format the user's source zig code in the textarea.

    Wanted to run it by you if it was feasible to handle sending the source code as a payload, formatting it, and then updating the source text area with the formatted code or displaying fmt warnings (if any) in the stdout box.

    Something along these lines:

    $ echo 'const std = @import("std"); pub fn main() void { std.debug.print("Hello, {s}!", .{"world"}); }' | zig fmt --stdin
    const std = @import("std");
    pub fn main() void {
        std.debug.print("Hello, {s}!", .{"world"});
    }
    
  • Added Zig Fmt Support

    Added Zig Fmt Support

    Fixes: #10

    Now the playground includes a "Format" button to fun zig fmt on the user's source code similar to how we compile it. If the formatted code is different from the original code, it gets updated in the textarea. Likewise, If the code gets an error in fmt, the error message gets updated in stdout.

    A new route /server/fmt is added which is similar to /server/run takes in the source as payload converts it to a file and now runs zig fmt on it with the same constraints as zig run with size & time.

    It still has a few hiccups, like inadequate line number detection in the user's text area. Will try to improve on it in later PRs.

  • Fix Placeholder Program Compile Error

    Fix Placeholder Program Compile Error

    Hi,

    Really nice work with https://zig-play.dev/ I visited the page and found out the default placeholder in the Source: text box does not compile. The following PR fixes this issue.

    Issue:

    For the current placeholder

    const std = @import("std"); pub fn main() void { std.debug.print("Hello, {}!", .{"world"}); }
    

    It fails to compile with the error in stdout

    An error occurred:
    /usr/local/bin/lib/std/fmt.zig:519:25: error: cannot format array ref without a specifier (i.e. {s} or {*})
                            @compileError("cannot format array ref without a specifier (i.e. {s} or {*})");
                            ^
    /usr/local/bin/lib/std/fmt.zig:352:23: note: called from here
            try formatType(
                          ^
    /usr/local/bin/lib/std/io/writer.zig:34:34: note: called from here
                return std.fmt.format(self, format, args);
                                     ^
    /usr/local/bin/lib/std/debug.zig:68:27: note: called from here
        nosuspend stderr.print(fmt, args) catch return;
                              ^
    /tmp/playground092885291/play.zig:1:65: note: called from here
    const std = @import("std"); pub fn main() void { std.debug.print("Hello, {}!", .{"world"}); }
                                                                    ^
    /tmp/playground092885291/play.zig:1:48: note: called from here
    const std = @import("std"); pub fn main() void { std.debug.print("Hello, {}!", .{"world"}); }
                                                   ^
    

    Adding the specifier now fixes this

    const std = @import("std"); pub fn main() void { std.debug.print("Hello, {s}!", .{"world"}); }
    
  • Add hosting url to readme

    Add hosting url to readme

    Saw this on lobster, but it took me a minute to locate the link because I seldom look at the sidebar. Throw a link into the README so it is in the body.

  • Loading and saving programs from the playground

    Loading and saving programs from the playground

    There's currently no way to share links to programs in the Zig playground. But there should be a way to save a link to a program in a query string, like this:

    https://zig-play.dev/?source_code_goes_here

    I would use encodeURIComponent to convert the source code to a query string, and then use decodeURIcomponent to convert it back to source code.

  • The program isn't sufficiently isolated thus users can access files they probably shouldn't

    The program isn't sufficiently isolated thus users can access files they probably shouldn't

    If a user enters similar to the following they're capable of querying the system for

    const std = @import("std");
    
    pub fn main() void {
        std.debug.print("Hello, {}!", .{@embedFile("../../etc/profile")});
    }
    

    Along with being root

    const std = @import("std");
    
    pub fn main() !void {
        const result = try std.ChildProcess.exec(.{
            .allocator = std.heap.page_allocator,
            .argv = &[_][]const u8{ "sh", "-c", "whoami" },
        });
    
        std.log.err("{s}", .{result.stdout});
    }
    

    The program should probably be isolated using namespaces and under a different user such that it's not possible for it to see anything other than the stdlib files and dependencies.

Related tags
CodePlayground is a playground tool for go and rust language.

CodePlayground CodePlayground is a playground tool for go and rust language. Installation Use homebrews to install code-playground. brew tap trendyol/

Mar 5, 2022
Go implementation of the Rust `dbg` macro

godbg ?? godbg is an implementation of the Rust2018 builtin debugging macro dbg. The purpose of this package is to provide a better and more effective

Dec 14, 2022
Go specs implemented as a scripting language in Rust.

Goscript A script language like Python or Lua written in Rust, with exactly the same syntax as Go's. The Goal Runs most pure Go code, probably add som

Jan 8, 2023
A Go implementation of Rust's evmap

A Go implementation of Rust's evmap which optimizes for high-read, low-write workloads and uses eventual consistency to ensure that readers and writers never block each other.

Sep 3, 2022
Slabmap - Ported from Rust library slabmap

slabmap Ported from Rust library slabmap Examples import "github.com/pourplusquo

Jul 30, 2022
Golang CS:GO external base. Development currently halted due to compiler/runtime Golang bugs.

gogo Golang CS:GO External cheat/base. Also, my first Golang project. Wait! Development momentarily halted due to compiler/runtime bugs. Disclaimer Th

Jun 25, 2022
🚀 Backend for Online Courses Builder | SaaS Product

Backend app for Online Course Constructor Platform Build & Run (Locally) Prerequisites go 1.15 docker golangci-lint (optional, used to run code checks

Jan 3, 2023
Instant online preview of HTML files or websites.

Instant online preview of HTML files or websites.

Apr 19, 2022
Beecrowd is a Jugde Online Plataform to problems submissions. This repo contains all my submissions to Beecrowd.

Beecrowd Solutions Keywords: Beecrowd, judge, problems, competitive, programming, solutions, ad-hoc, sql, strings, algorithms, math, graphs Table of C

Jan 5, 2022
A stack oriented esoteric programming language inspired by poetry and forth

paperStack A stack oriented esoteric programming language inspired by poetry and forth What is paperStack A stack oriented language An esoteric progra

Nov 14, 2021
🧀 Formaggo is a simple model checker inspired by TLA+, The checker and the models are written in Go

?? Formaggo. A cheesy exhaustive state checker in Go. Formaggo is a simple model checker inspired by TLA+. The checker and the models are written in G

Jan 23, 2022
README snippets for Visual Code inspired by readme.so

vscode-readme This was inspired by @katherinepeterson who made the wonderful readme.so! Huge thanks. Configuration Make sure you have quickSuggestions

Feb 7, 2022
An easy-to-use Map Reduce Go parallel-computing framework inspired by 2021 6.824 lab1. It supports multiple workers on a single machine right now.

MapReduce This is an easy-to-use Map Reduce Go framework inspired by 2021 6.824 lab1. Feature Multiple workers on single machine right now. Easy to pa

Dec 5, 2022
Directed Acyclic Graph (DAG) inspired tasks queuing API.

flow Directed Acyclic Graph (DAG) inspired tasks queuing API. The examples directory contains samples. Status This project's API will not have any met

Jul 29, 2022
A Go library inspired by chalk.js

colors A Go library inspired by chalk.js Print in these pretty colors Black DarkRed DarkGreen DarkYellow DarkBlue DarkMagenta DarkCyan LightGray DarkG

Jan 28, 2022
DuckChat - Stallman Inspired
DuckChat - Stallman Inspired

DuckChat - Stallman Inspired Demo of current release. This application runs comp

Feb 13, 2022
[TOOL, CLI] - Filter and examine Go type structures, interfaces and their transitive dependencies and relationships. Export structural types as TypeScript value object or bare type representations.

typex Examine Go types and their transitive dependencies. Export results as TypeScript value objects (or types) declaration. Installation go get -u gi

Dec 6, 2022
:chart_with_upwards_trend: Monitors Go MemStats + System stats such as Memory, Swap and CPU and sends via UDP anywhere you want for logging etc...

Package stats Package stats allows for gathering of statistics regarding your Go application and system it is running on and sent them via UDP to a se

Nov 10, 2022
James is your butler and helps you to create, build, debug, test and run your Go projects
James is your butler and helps you to create, build, debug, test and run your Go projects

go-james James is your butler and helps you to create, build, debug, test and run your Go projects. When you often create new apps using Go, it quickl

Oct 8, 2022