Skip to content

Instantly share code, notes, and snippets.

@dram
Last active October 21, 2015 13:30
Show Gist options
  • Select an option

  • Save dram/a91e2911b6a0ec3d9238 to your computer and use it in GitHub Desktop.

Select an option

Save dram/a91e2911b6a0ec3d9238 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import sys
import subprocess
import tempfile
def dylan_compile(library, headers, sources, path=None):
prefix = os.path.basename(path) if path else library + "."
with tempfile.NamedTemporaryFile(prefix=prefix) as f:
f.writelines(headers)
for i in range(len(headers), max(sources.keys()) + 1):
f.write(sources.get(i, "\n"))
f.flush()
try:
return subprocess.check_output(["mindycomp",
f.name, "-l", library, "-o", "-"])
except subprocess.CalledProcessError as exc:
sys.exit(exc.returncode)
def extract_file(lines):
library = None
headers = []
sources = {}
for i in sorted(lines.keys()):
tokens = lines[i].split()
if tokens[0] == "#library":
if library:
break
else:
library = tokens[1]
elif tokens[0].endswith(":"):
headers.append(lines[i])
else:
sources[i] = lines[i]
del lines[i]
return (library, headers, sources)
def run(path):
lines = {}
for i, line in enumerate(open(path).readlines()):
if line.isspace() or line.startswith("#!"):
continue
lines[i] = line
p = subprocess.Popen(["mindy", "-0", path, "-x", "-"],
stdin=subprocess.PIPE)
while lines:
p.stdin.write(dylan_compile(*extract_file(lines), path=path))
p.stdin.close()
p.wait()
sys.exit(p.returncode)
if __name__ == "__main__":
run(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment