Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dustinlacewell
Last active September 10, 2020 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinlacewell/33c002ae42f853ac1d9534d560cc6d8f to your computer and use it in GitHub Desktop.
Save dustinlacewell/33c002ae42f853ac1d9534d560cc6d8f to your computer and use it in GitHub Desktop.

Babel Evaluation Test

Execute snippets of arbitrary languages!

Use C-c C-c on any #+begin_src or #+end_src line.

Awk

BEGIN { print "Hello from Awk."; }

Bash

echo "Hello from Bash."

C

int main() {
   printf("Hello from C.");
   return 0;
}

C++

#include<iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}

C#

using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("Hello from C#.");
    }
}

D

import std.stdio;

void main()
{
    writeln("Hello from D!");
}

Elisp

"Hello from Elisp."

F#

"Hello from F#."

pp

;; (s-chop-prefix ": val it : string =" (car (split-string out "\n")))
(nth 1 (split-string (car (split-string out "\n")) " = "))

Fortran

program hello
  print *, "Hello from Fortran."
end program

Go

fmt.Println("Hello from Go.")

Javascript

function foo() {
    return "Hello from JS.";
}

console.log(foo());

Lua

print("Hello from Lua.")

NASM

global _start

section .text

_start:
  mov rax, 1        ; write(
  mov rdi, 1        ;   STDOUT_FILENO,
  mov rsi, msg      ;   "Hello from NASM.",
  mov rdx, msglen   ;   sizeof("Hello from NASM.")
  syscall           ; );

  mov rax, 60       ; exit(
  mov rdi, 0        ;   EXIT_SUCCESS
  syscall           ; );

section .rodata
  msg: db "Hello from NASM.", 13
  msglen: equ $ - msg

OCaml

print_string "Hello from OCaml.\n";;

Perl

print("Hello from Perl.\n");

Python

return "Hello from Python."

R

print("Hello from R.")

pp

(s-chop-prefix "[1] " out)

Ruby

puts "Hello from Ruby."

Rust

println!("Hello from Rust.");

Scheme

(begin (display "Hello from Scheme.")
       (newline))
(require 'ob-nasm "./ob-nasm.el")
(use-package csharp-mode)
(use-package ess) ;; R
(use-package fsharp-mode)
(use-package geiser) ;; Scheme
(use-package go-mode)
(use-package haskell-mode)
(use-package lua-mode)
(use-package nasm-mode)
(use-package perl-mode)
(use-package ruby-mode)
(use-package rust-mode)
(use-package tuareg) ;; OCaml
(use-package ob-csharp
:straight (ob-csharp :type git
:host github
:repo "thomas-villagers/ob-csharp"
:files ("src/ob-csharp.el")))
(use-package ob-fsharp
:straight (ob-fsharp :type git
:host github
:repo "zweifisch/ob-fsharp"
:files ("ob-fsharp.el")))
(use-package ob-rust
:straight (ob-rust :type git
:host github
:repo "zweifisch/ob-rust"
:files ("ob-rust.el")))
(use-package ob-go
:straight (ob-go :type git
:host github
:repo "pope/ob-go"
:files ("ob-go.el")))
(setq org-babel-load-languages
'((C . t)
(R . t)
(awk . t)
(emacs-lisp . t)
(fortran . t)
(go . t)
(js . t)
(lua . t)
(nasm . t)
(ocaml . t)
(perl . t)
(python . t)
(ruby . t)
(rust . t)
(scheme . t)
(shell . t)))
(org-babel-do-load-languages 'org-babel-load-languages
org-babel-load-languages)
;;; ob-nasm.el --- Execute Nasm code within org-mode blocks.
;; Copyright 2020 ldlework
;; Author: ldlework <dlacewell@gmail.com>
;; Maintainer: ldlework <dlacewell@gmail.com>
;; Keywords: org babel nasm
;; URL: https://github.com/dustinlacewell/ob-nasm
;; Version: 0.0.1
;; Package-Requires: ((org "9"))
;;; Commentary:
;;
;; Execute Nasm code within org-mode blocks.
;;; Code:
(require 'org)
(require 'ob)
(defgroup ob-nasm nil
"org-mode blocks for Nasm."
:group 'org)
;;;###autoload
(defun org-babel-execute:nasm (body params)
"org-babel nasm hook."
(let ((source-file (concat temporary-file-directory "babel.s"))
(object-file (concat temporary-file-directory "babel.o"))
(binary-file (concat temporary-file-directory "babel")))
(write-region body nil source-file)
(shell-command (format "nasm -f elf64 -o %s %s" object-file source-file))
(shell-command (format "ld -o %s %s" binary-file object-file))
(shell-command (format "chmod +x %s" binary-file))
(s-trim (shell-command-to-string binary-file))))
;;;###autoload
(eval-after-load "org"
'(add-to-list 'org-src-lang-modes '("nasm" . nasm)))
(provide 'ob-nasm)
;;; ob-nasm.el ends here
with import <nixpkgs> {};
mkShell rec {
name = "babel-tour";
buildInputs = [
dmd
dotnet-sdk omnisharp-roslyn mono6
dtools
fsharp41
gfortran
go
guile
lua
nasm
nodejs-14_x yarn
ocaml
perl
python37
R
ruby
rustc
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment