-
Notifications
You must be signed in to change notification settings - Fork 348
Run hs2hs on .hsc-files before loading #1238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
f57ce5a
d6a8eea
de7dd07
91800f0
31bc866
4dc2902
914175e
15a0471
68a70c6
44dac28
3489200
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
;; haskell-hsc2hs-tests.el --- -*- lexical-binding: t; -*- | ||
|
||
(require 'ert) | ||
(require 'haskell) | ||
(require 'haskell-test-utils) | ||
|
||
|
||
(defvar default-hsc "{-# LANGUAGE CPP #-} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great test! |
||
{-# LANGUAGE CApiFFI #-} | ||
{-# LANGUAGE ForeignFunctionInterface #-} | ||
|
||
module Hsc2hsTest where | ||
|
||
import Foreign | ||
import Foreign.C.String | ||
import Foreign.C.Types | ||
|
||
#include <stdlib.h> | ||
|
||
newtype NUMBERS = NUMBERS { unNUMBERS :: CInt } | ||
deriving (Eq,Show) | ||
|
||
#{enum NUMBERS, NUMBERS | ||
, rand_max = RAND_MAX | ||
} | ||
") | ||
|
||
(defmacro with-hsc2hs (contents &rest body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should always use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"Load CONTENTS as a .hsc, then run BODY after it's loaded into REPL. | ||
Uses `haskell-process-path-hsc2hs' if executable exists, | ||
otherwise fake script hsc2hs.sh from this directory." | ||
(declare (debug t) (indent 1)) | ||
`(with-temp-switch-to-buffer | ||
(let ((f (make-temp-file "haskell-hsc2hs-tests.el" nil ".hsc"))) | ||
(insert ,contents) | ||
(write-file f) | ||
(haskell-mode) | ||
(let* ((dir (file-name-directory | ||
(find-lisp-object-file-name 'with-hsc2hs nil))) | ||
(existing-hsc2hs (executable-find haskell-process-path-hsc2hs)) | ||
(haskell-process-path-hsc2hs | ||
(if (and existing-hsc2hs (file-executable-p existing-hsc2hs)) | ||
haskell-process-path-hsc2hs | ||
(format "%s/%s" dir "hsc2hs.sh")))) | ||
(haskell-process-load-file)) | ||
(let ((proc (get-buffer-process "*hsc2hs*"))) | ||
(while (eq (process-status proc) 'run) ; TODO: is there no built-in way to block-wait on a process? | ||
(sit-for 0.5)) | ||
,@body | ||
(delete-file f))))) | ||
|
||
(ert-deftest hsc2hs-errors () | ||
(let ((error-hsc (concat default-hsc | ||
"newtype FOO = FOO { unFOO :: CInt } deriving (Eq,Show)\n" | ||
"#{enum FOO, FOO , a_typo = A_TYPO }\n"))) | ||
(with-hsc2hs error-hsc | ||
(with-current-buffer "*hsc2hs*" | ||
(goto-char (point-min)) | ||
(when (re-search-forward "A_TYPO" nil 'noerror) | ||
(goto-char (match-beginning 0))) | ||
(should (looking-at-p "A_TYPO. undeclared")))))) | ||
|
||
(ert-deftest hsc2hs-compile-and-load () | ||
(with-hsc2hs default-hsc | ||
(with-current-buffer "*haskell*" ; TODO: Where is this defined? | ||
(goto-char (point-max)) | ||
(insert ":t unNUMBERS rand_max") | ||
(goto-char (point-max)) | ||
(haskell-interactive-handle-expr) | ||
(sit-for 1.0) ; TODO: can we wait until the prompt appears, with a timeout? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There used to be an idea to have a callback from |
||
(forward-line -1) | ||
(should (looking-at-p "unNUMBERS rand_max :: CInt"))))) | ||
|
||
;; haskell-hsc2hs-tests.el ends here | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/bin/sh | ||
|
||
# Very stupid fake hsc2hs specific to our tests | ||
|
||
awk -v hs="${1%c}" ' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Is there a way to have this as pure awk script, i.e. not going through |
||
/^#{/ { | ||
skip = 1 | ||
} | ||
!skip && !/^#include/ { | ||
lines = lines $0"\n" | ||
} | ||
/}/ { | ||
skip = 0 | ||
} | ||
|
||
/A_TYPO/ { | ||
print FILENAME":"NR":58: error: ‘A_TYPO’ undeclared (first use in this function)" >"/dev/stderr" | ||
lines="" | ||
exit(1) | ||
} | ||
|
||
END { | ||
if(lines) { | ||
lines = lines "rand_max :: NUMBERS\n" | ||
lines = lines "rand_max = NUMBERS 2147483647\n" | ||
print lines > hs | ||
} | ||
} | ||
' "$1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
defcustom
withstring
type.