How do I refer to the last evaluated expression in OCaml's toplevel repl?
I.e. JavaScript uses $_, python uses _, and haskell ghci uses it.
This is not implemented by the default ocaml REPL bundled with the compiler. With utop, there is an option to enable this behavior with the -implicit-bindings flag. With this settings, all anonymous bindings are named as _0,_1,_2,... .
Related
I was debugging a program compiled in Rust using GDB (arm-none-eabi-gdb). At one point, I wanted to write to a memory address as follow:
(gdb) set *((int *) 0x24040000) = 0x0000CAFE
syntax error in expression, near `) 0x24040000) = 0x0000CAFE'.
After multiple tentative, I found out that I was casting the C style and I had to cast it the Rust style as follow:
set *(0x24040000 as *mut i32) = 0x0000CAFE
My question is how GDB is interpreting the different commands and why I get this error. Is it because the symbol (int) is not recognized, but in this case, how gdb load the symbols? Does gdb need to compile the instruction to the correct language of the binary running on the target?
Yes, it depends on the language, and the language is deduced from the filename of the loaded source file.
Quoting the manual:
print and many other GDB commands accept an expression and compute its value. Any kind of constant, variable or operator defined by the programming language you are using is valid in an expression in GDB. This includes conditional expressions, function calls, casts, and string constants.
And:
If you are not interested in seeing the value of the assignment, use the set command instead of the print command. set is really the same as print except that the expression’s value is not printed and is not put in the value history (see Value History). The expression is evaluated only for its effects.
And:
Language-specific information is built into GDB for some languages, allowing you to express operations like the above in your program’s native language, and allowing GDB to output values in a manner consistent with the syntax of your program’s native language. The language you use to build expressions is called the working language.
And:
There are two ways to control the working language—either have GDB set it automatically, or select it manually yourself. You can use the set language command for either purpose. On startup, GDB defaults to setting the language automatically.
[..] most of the time GDB infers the language from the name of the file.
In OCaml 4.08, a new warning about partial applications is emitted by default, as in:
let _ = (Format.printf "side-effect!#."; List.iter (fun () -> ()))
2 | (Format.printf "side-effect!#."; List.iter (fun () -> ()))
^^^^^^^^^^^^^^^^^^^^^^^^
Warning 5: this function application is partial,
maybe some arguments are missing.
Trying to disable it locally by adding annotations everywhere didn't seem to work:
let[#warning "-5"] _ [#warning "-5"] =
(Format.printf "side-effect!#."; List.iter (fun () -> ()))[##warning "-5"]
The only way which works is to use the [###warning "-5"] technique as mentioned here. However, that question mentions that
Local disabling of warnings with [#warning "…"] and [##warning "…"]is not well supported for OCaml versions anterior to 4.06.0
What should be the syntax in this case, for OCaml 4.08, to locally disable such warnings?
Edit: as suggested by glennsl, replacing let _ = ... with ignore (...) does offer an alternative (adding [##warning "-5"] after the ignore (...) does seem to work), but it is less uniform, since top-level declarations cannot be replaced this way, although they can safely be surrounded with [###warning "-5"]/[###warning "+5"]1. Still, it does not explain why my first attempt did not work: is it written incorrectly, is it by design, or possibly an oversight?
1 This 'hack' also has the disadvantage of possibly changing the previous state; for instance, if warning 5 had been previously disabled at the global level, this would re-enable it inadvertently.
I'd say that the following quote from the fine manual (section 8.13.1) indicates that the answer is "by (absence of) design":
Note that it is not well-defined which scope is used for a specific warning. This is implementation dependant and can change between versions.
Now, if we are trying to interpret why your attempts to silence the warning didn't work, we can check what the warning 5 is about:
expression whose result has function type and is ignored.
Thus, the warning is emitted when we have a functional expression e in a certain context. In the case of ignore, the context is simply an application, i.e. an englobing expression, an the scope in which we can ask to forget about warning 5 should be roughly the ignore expression itself (and its englobing expressions and item).
Now, for the let _ = ..., things are less clear-cut: e get ignored when it is bound to the catch-all pattern _. One might argue that putting [## warning "-5"] at the end of the let-binding should cover the whole definition, but as mentioned above, the compiler is perfectly free to disagree with this interpretation.
However, this gives an alternative solution that does not need to disable warning 5 at all: just bind the expression to a variable (starting with an _ to avoid warning 26, of course):
let _i_swear_to_hb_curry_i_know_what_i_m_doing =
Format.printf "side-effect!#."; List.iter (fun () -> ());;
does not trigger any warning: by being bound to such a variable, the expression might still feel a bit neglected, but it can't complain it is actively ignored.
I'm writing a Nim program using regexes, which works fine, except that when I compile, I get this error message:
Warning: re is deprecated [Deprecated]
I've looked in the documentation for the re module, but there's no mention of a new way to create regexes.
My question is, if the re"regex" constructor is deprecated, what should I use?
From the docs:
Consider using the nre or pegs modules instead.
pegs is supposed to be more powerful than regular expressions and as such uses a different syntax from most regular expression engines; by contrast, nre is just a better wrapper around the PCRE library than re.
I use vim+ctags with C++ and I'd like :tag to jump to class definition by default, rather than its constructor. In other words, I'd like to give the "c" (class) kind higher priority than the "f" one.
Is there a way to achieve this?
I use vim 7.1 and exuberant ctags 5.7
Does anyone have a regular expression for matching function calls in C programs ?
Since C isn't a regular language and C function calls can contain arbitrary argument expressions, I fear the answer to your question is “no.”
After a bit more searching I decided to let the compiler do the hard work.
Get the compiler to produce a Register Transfer Language (RTL) file using the -dr options of gcc.
The produced RTL file has the suffix .rtl or .expand.
This file is far easier to parse as the functions calls are already identified.
I doubt you can find a regex that matches all (and only) the function calls in some source code. But maybe you could use a tool like Understand, or your IDE, to browse your code.