Created
May 27, 2020 08:34
-
-
Save hyrious/1ffc87ce3dde56875b47958c855cb405 to your computer and use it in GitHub Desktop.
tio.run api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| module Tio | |
| def self.languages | |
| require 'json' | |
| JSON.load(Net::HTTP.get(URI('https://tio.run/languages.json'))).keys | |
| end | |
| def self.run_url | |
| URI('https://tio.run/cgi-bin/run/api/') | |
| end | |
| def self.deflate_raw string | |
| require 'zlib' | |
| Zlib.deflate(string).slice(2..-5) | |
| end | |
| def self.run(code, lang, input: nil, argv: [], flags: [], files: {}, variables: {}) | |
| require 'net/http' | |
| require 'stringio' | |
| files = files.merge({ ".code.tio": code }) | |
| files = files.merge({ ".input.tio": input }) if input | |
| variables = variables.merge({ lang: lang }) | |
| variables = variables.merge({ args: argv }) unless argv.empty? | |
| variables = variables.merge({ TIO_CFLAGS: flags }) unless flags.empty? | |
| io = StringIO.new.binmode | |
| variables.each { |k, v| | |
| io.write "V#{k}", 0.chr | |
| v = [v] unless v.kind_of? Array | |
| io.write v.size, 0.chr | |
| v.each { |x| io.write x, 0.chr } | |
| } | |
| files.each { |k, v| | |
| io.write "F#{k}", 0.chr, v.bytesize, 0.chr, v | |
| } | |
| io.write 'R' | |
| payload = deflate_raw io.string | |
| body = Net::HTTP.post(run_url, payload).body | |
| splitter = body.slice!(0, 16) | |
| stdout, stderr = body.split(splitter) | |
| puts stdout | |
| # STDERR.puts stderr | |
| true | |
| end | |
| end | |
| Tio.run <<~C, 'c-tcc' | |
| #include <stdio.h> | |
| int main(void) { | |
| #ifdef __TINYC__ | |
| printf("hello from tcc!\\n"); | |
| #else | |
| printf("hello world!\\n"); | |
| #endif | |
| } | |
| C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The language id options arrive on the https://tio.run home page via
curl https://tio.run/static/3fbdee7a34cd8d340fe2dbd19acd2391-languages.json | jq keys, but the static file hash will change. There's a hardcoded list here.