Is it possible to use function pointers in Nimrod?
What I've tried is:
type fptr = (proc(int):int)
proc f(x:int): int =
result = x+1
var myf : fptr = f
echo myf(0)
but when I try to compile I get:
Hint: added path: 'C:\Users\Peter\.babel\pkgs\' [Path]
Hint: used config file 'C:\Program Files (x86)\Nimrod\config\nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: hello3 [Processing]
Error: internal error: GetUniqueType
Traceback (most recent call last)
nimrod.nim nimrod
nimrod.nim handleCmdLine
main.nim mainCommand
main.nim commandCompileToC
modules.nim compileProject
modules.nim compileModule
passes.nim processModule
passes.nim processTopLevelStmt
cgen.nim myProcess
ccgstmts.nim genStmts
ccgexprs.nim expr
ccgstmts.nim genStmts
ccgexprs.nim expr
ccgstmts.nim genVarStmt
ccgstmts.nim genSingleVar
cgen.nim assignGlobalVar
ccgtypes.nim getTypeDesc
ccgtypes.nim getTypeDescAux
ccgtypes.nim genProcParams
cgen.nim fillLoc
ccgutils.nim getUniqueType
msgs.nim internalError
msgs.nim rawMessage
msgs.nim rawMessage
msgs.nim handleError
Sure you can use pointers, the only problem is that you forgot to define the name of the first parameter and unfortunately this crashes the compiler. The following example works:
type fptr = (proc(x: int):int)
proc f(x:int): int =
result = x+1
var myf : fptr = f
echo myf(0)
Note that you can omit the type in the myf variable declaration. You can also omit the brackets around the proc type definition. I've reported the error you found to the developers at https://github.com/Araq/Nimrod/issues/1183.
Related
I am not sure if this is currently possible (and maybe it is not even advisable), but I would like to be able to catch the output of a compiler error and reuse it in code. An example would be:
type IntOrString = int | string
var s: seq[IntOrString]
this code would not compile with error:
/usercode/in.nim(2, 5) Error: invalid type: 'IntOrString' in this context: 'seq[IntOrString]' for var
I am interested in a way to be able to use the message of this error in the code.
My use case is being able to easily document and discuss compiler errors in nimib. If I were to write a document that shows and discuss the different type of compiler errors catching the message automatically that could be useful (a workaround right now would be to write the code to file and compile with verbosity 0).
It is possible to use the compiler api to catch errors:
import ../compiler/[nimeval, llstream, ast, lineinfos, options], os, strformat
let std = findNimStdLibCompileTime()
let modules = [std, std / "pure", std / "std", std / "core"]
var intr = createInterpreter("script", modules)
intr.registerErrorHook proc(config:ConfigRef,info:TLineInfo,msg:string,severity:Severity) =
raise newException(CatchableError,&"{severity}: {(info.line,info.col)} {msg}")
)
try:
intr.evalScript(llStreamOpen("""type IntOrString = int | string
var s: seq[IntOrString]"""))
except CatchableError as e:
echo e.msg
echo "done"
destroyInterpreter(intr)
outputs:
Error: (2, 4) invalid type: 'IntOrString' in this context: 'seq[IntOrS
tring]' for var
done
Caveat: you can't run runtime code at compile time, for example, trying to run
type Dog = object of RootObj
method speak(i:Dog):string = "woof"
echo Dog().speak()
in the interpreter will give you errors, instead you would have to do something like:
type Dog = object of RootObj
method speak*(i:Dog):string = "woof"
proc newDog*():Dog = discard
let
dog = intr.callRoutine(intr.selectRoutine("newDog"),[])
speech = intr.callRoutine(intr.selectRoutine("speak"),[dog])
if speech.kind == nkStrLit:
echo speech.strval
I have this call to the czmq api:
int rc = zsock_connect(updates, ("inproc://" + uuidStr).c_str());
(Note: uuidStr is of type std::string and zsock_connect expects a const char* as its second argument)
Which gives compiler error:
error: format not a string literal and no format arguments [-Werror=format-security]
int rc = zsock_connect(updates, ("inproc://" + uuidStr).c_str());
^
I've tried:
const char* connectTo = ("inproc://" + uuidStr).c_str();
int rc = zsock_connect(updates, connectTo);
and also
int rc = zsock_connect(updates, (const char*)("inproc://" +
uuidStr).c_str());
But the error persists.
How do I correct this?
Context; I'm trying to compile this code as a Python extension on Linux using pip install. On Windows it compiles with pip install and runs just fine, presumably that compiler is more permissive.
This function acts like printf() and friends, right? If so, you have the same problem with that that exists with printf(some_var) - if the string you're passing has format sequences in it, you get undefined behavior and bad stuff happening because there aren't the arguments present that you're telling the function to expect. The fix is to do something like:
int rc = zsock_connnect(updates, "inproc://%s", uuidStr.c_str());
Basically, give it a format that takes your string as an argument.
I'm using the GHC API to parse a module. If the module contains syntax errors the GHC API writes them to stdout. This interferes with my program, which has another way to report errors. Example session:
$ prog ../stack/src/Stack/Package.hs
../stack/src/Stack/Package.hs:669:0:
error: missing binary operator before token "("
#if MIN_VERSION_Cabal(1, 22, 0)
^
../stack/src/Stack/Package.hs:783:0:
error: missing binary operator before token "("
#if MIN_VERSION_Cabal(1, 22, 0)
^
../stack/src/Stack/Package.hs
error: 1:1 argon: phase `C pre-processor' failed (exitcode = 1)
Only the last one should be outputted. How can I make sure the GHC API does not output anything? I'd like to avoid libraries like silently which solve the problem by redirecting stdout to a temporary file.
I already tried to use GHC.defaultErrorHandler, but while I can catch the exception, GHC API still writes to stdout. Relevant code:
-- | Parse a module with specific instructions for the C pre-processor.
parseModuleWithCpp :: CppOptions
-> FilePath
-> IO (Either (Span, String) LModule)
parseModuleWithCpp cppOptions file =
GHC.defaultErrorHandler GHC.defaultFatalMessager (GHC.FlushOut $ return ()) $
GHC.runGhc (Just libdir) $ do
dflags <- initDynFlags file
let useCpp = GHC.xopt GHC.Opt_Cpp dflags
fileContents <-
if useCpp
then getPreprocessedSrcDirect cppOptions file
else GHC.liftIO $ readFile file
return $
case parseFile dflags file fileContents of
GHC.PFailed ss m -> Left (srcSpanToSpan ss, GHC.showSDoc dflags m)
GHC.POk _ pmod -> Right pmod
Moreover, with this approach I cannot catch the error message (I just get ExitFailure). Removing the line with GHC.defaultErrorHandler gives me the output shown above.
Many thanks to #adamse for pointing me in the right direction! I have found the answer in Hint's code.
It suffices to override logging in the dynamic flags:
initDynFlags :: GHC.GhcMonad m => FilePath -> m GHC.DynFlags
initDynFlags file = do
dflags0 <- GHC.getSessionDynFlags
src_opts <- GHC.liftIO $ GHC.getOptionsFromFile dflags0 file
(dflags1, _, _) <- GHC.parseDynamicFilePragma dflags0 src_opts
let dflags2 = dflags1 { GHC.log_action = customLogAction }
void $ GHC.setSessionDynFlags dflags2
return dflags2
customLogAction :: GHC.LogAction
customLogAction dflags severity _ _ msg =
case severity of
GHC.SevFatal -> fail $ GHC.showSDoc dflags msg
_ -> return () -- do nothing in the other cases (debug, info, etc.)
The default implementation of GHC.log_action can be found here:
http://haddock.stackage.org/lts-3.10/ghc-7.10.2/src/DynFlags.html#defaultLogAction
The code for parsing remains the same in my question, after having removed the line about GHC.defaultErrorHandler, which is no longer needed, assuming one catches exceptions himself.
I have seen this question before and then the answer was to temporarily redirect stdout and stderr.
To redirect stdout to a file as an example:
import GHC.IO.Handle
import System.IO
main = do file <- openFile "stdout" WriteMode
stdout' <- hDuplicate stdout -- you might want to keep track
-- of the original stdout
hDuplicateTo file stdout -- makes the second Handle a
-- duplicate of the first
putStrLn "hi"
hClose file
# lines 11-12:
proc last[T](ll: seq[T]): var T =
return ll[high(ll)]
# line 118:
if last(formula)["state"] == c_empty:
Errors:
main.nim(118, 12) Info: template/generic instantiation from here
main.nim(12, 12) Error: expression has no address
What the compiler wants?
[] does not return a var.
I don't think you need to annotate anything in this snippet with var though, since nothing is being mutated. Specify var at the call site if needed.
Try to submit compilable examples in the future if you can.
I am trying to cach exception caused by read function:
run :: CurrentData -> IO ()
run current = do
{
x <- (getCommandFromUser) `E.catch` handler;
updated <- executeCommand x current;
run updated;
} where handler :: E.IOException -> IO Command
handler e = do putStrLn "wrong command format" >> return (DoNothing);
In this code function getCommandfrom user gets some string from user and then tries to read some data from this string using "read" function
If read fails there is exception thrown:
*** Exception : prelude.read : no parse
and program exits...
I can't catch this exception - what is type of this exception???
I tried also E.SomeException instead of E.IOException...
E is from import Control.Exception As E
"what is type of this exception?" The type is ErrorCall, also available from Control.Exception. An ErrorCall is what is thrown when the error function is called.
Just change the type of handler and it will work. A last resort to get things working is to catch E.SomeException, but that's almost always the wrong thing to do.