I have translated several programs from Python to Nim. Some of them where quite
simple and the translation was easy… for a human who knows what the program is
intended for. Other were a bit more complicated, but as these are programs I
wrote, there is little usage of dynamic features.
In any
Fully automatic translation seems (almost?) feasible if you map every value to
a JSON-like universal type, say PyObject. Then you would have
super-non-idiomatic Nim code but you can gradually rewrite it to use more of
Nim's static typing and native types.
Just FYI, PyPy itself is written in RPython - statically typed subset of Python
(which of course can be run by normal interpreters) and then that RPython is
compiled to C and then into the binary
Ive read that parsing the .PYC is even easier, because some constructs are
"expanded" there, like `:=` is a normal assign, `@decorator` is a normal
function, and so on, I dont know if true tho.
Well we can try to learn the lessons from
[https://github.com/metacraft-labs/py2nim_deprecated](https://github.com/metacraft-labs/py2nim_deprecated)
* Python code is more dynamic than it looks and even your code doesn't use
much of Python's dynamic nature, some of your dependencies do.
*
The examples I saw for sorting OrderedTable were for the key, not sorting by
the value, so I'm not sure if what I need can be done with an OrderedTable.
Thanks, I'll probably just a seq of tuples.
A Table has no ordering, so you’d have to first copy it to some form that does,
like a seq of (int, string) tuples, and then sort that (e.g. using a comparator
proc that returns the result of comparing the two strings.)
FYI there's an OrderedTable and it has a .sort proc. So you can convert your
table to an OrderedTable and then sort it :) Or just use OrderedTable right
from the start
I have a 200K-line Python app that uses 5-10 C extensions plus a few Cython
modules I wrote for content-defined file chunking and a dedup hash table.
Performance in general is good, but Python has its limits in several ways:
* threading allows sharing data but the GIL limits performance
*
> So it's a pain to rebase... Anyone know what's required to get the base PR
> merged?
Where is your PR?
> Is there a wish to build a compiler's internals documentation or is it a
> knowledge that lives in the compiler hackers' minds?
Well we have the internal documentation and we're always accepting PRs but I
generally assume that compiler contributors have read some book about compiler
I was able to auto-merge your branch with upstream devel with no immediate
problems, but only tested a hello world, didn't test all the POSIX stuff.
The question of how to separate the different options is fraught... It would be
great to be able to specify --os:esp-idf and have it work as
That's a good list! But the LwIP and FreeRTOS <=> POSIX brigde requires support
in the Nim standard library to make using Nim sockets/selects/etc libraries
possible.
The port on my branch with networking supports the LwIP, and some of the VFS
via the normal POSIX API's (uart, select, etc). The
The other option is that I may re-apply my changes to 1.2.4 branch and just
develop from there as the "stable" version.
I think part of the "sales pitch" for Nim out in the world, for good or ill, is
"sorta like Python but compiled with static types". It isn't so strange that
people show up asking how to automate "sorta like" into "more so". ;-)
The vast majority of Python code that I have seen uses precious
I created the repo because people were asking for **an Example** on how to do
that, it is an example it was not meant to be complete.
...but if people are interested on making a real working one, we can team-up or
whatever.
:)
If you mean compiling _from_ python, there's
[https://github.com/metacraft-labs/py2nim_deprecated](https://github.com/metacraft-labs/py2nim_deprecated)/
which has gained "deprecated" in its name since I last looked at it.
It's not very complete though.
If you compiling _to_ python, there's
I wrote how to debug Nim program and Nim compiler with GDB long time ago.
[https://internet-of-tomohiro.netlify.app/nim/gdb.en.html](https://internet-of-tomohiro.netlify.app/nim/gdb.en.html)
It might contain outdated content.
@ShalokShalom can perhaps clarify what he meant, but I took him to be asking
for a `c2nim`-like program `python2nim` to help facilitate porting pure Python
code to pure Nim code (as opposed to using already written Nim code from within
Python or already written Python code from within Nim).
In my opinion, it really depends on the the size of your file and the
complexity of your procedure. For me, if it's one simple procedure, I guess
having it in the file works, and I'd do it the way @shirleyquirk does. But if
it's multiple procedures or a complex one, I'd probably separate it
Thanks. For this particular example, I've started looking at how static generic
expressions are handled by procs and templates into `semtypes.nim`.
But the question wanted to be more general. How a new comer can understand how
the Nim compiler works? What tools/options are available to have a
> Case 2. Overloading for optimization, to avoid copy/reuse buffers we can have
> 2 versions of a procedure, one with sink and one without, for example
That's not supported by Nim and so far I haven't seen convincing examples. If
you want to take over ownership, you always want to leaving you
Thank you very much @mratsim! :-D This has cleared up a lot of my questions.
It's always fascination how smart compilers can be about reading code and
optimizing it. A huge amount of work has obviously been put into them.
One more question about sink inference: Is it infered once per proc or is
Dear imgui not have any love?
It's nice, but I think it's not the best choice if you actually want to make a
user-oriented desktop application. It's very useful for developers (debugging,
creating game editors, etc)
Have you had a look at nimpy? Lets you compile Nim code to a python module, and
lets you call python modules from Nim.
That's about as good as anything is going to get, prove me wrong!
I mean, Haxe transpiles to python, but it's about as readable as Nim-compiled c.
Do you mean from Nim to Python or from Python to Nim? Which use cases do you
have in mind?
Good point. This comes up from time to time in the forum. It would be really
nice if there was some documentation on `testament` (or if there's some, a link
to it). Also, I vaguely remember that `testament` also has disadvantages over
`unittest`, so the tradeoff when to use one or the other
I think for small simple modules it's ok to have the tests (with doAssert, not
assert) in the same file. Apart from that, I prefer a test module using the
[unittest](https://nim-lang.org/docs/unittest.html) module for each module to
test.
In my experience, putting the tests in the same file as
You can use macros to provide class-like capabilities to object variants (i.e.
constructing the object variant from multiple modules)
See:
### Case 1
`var y = x` would be elided and all further reference of y should use x instead
in the generated code.
For stack objects this shouldn't really matter because GCC/LLVM should optimize
those variable renaming anyway. This is because during code generation, they
transform the code in
testament lets you separate tests from code but yes, less well documented.
[https://nim-lang.org/docs/contributing.html](https://nim-lang.org/docs/contributing.html)
has some info
At the topo of the unittest module documentation it says:
> Note: Instead of unittest.nim, please consider to use the testament tool
> which offers process isolation for your tests. Also when isMainModule:
> doAssert conditionHere is usually a much simpler solution for testing
> purposes.
For simple stuff, especially for unit testing small modules
when isMainModule:
doAssert #condition#
Run
is fine or for a larger testsuite you can use
[https://nim-lang.org/docs/unittest.html](https://nim-lang.org/docs/unittest.html)
which also runs in the
`import system,rdstdin,strutils,sequtils proc
execvp*(file:cstring,args:cstringArray):cint{.header: "", importc.}
proc fork*():cint{.header: "", importc.} let input =
readLinefromstdin("enter command:>").split(' ').allocCStringArray()#does not do
quote handling! let pid = fork() if pid==0:
i think you might want to execlp() it's an os system call: your program gets
replaced by whatever other program you just called. not sure how cross-platform
it is, or whether it's in os.nim
Thanks for your answer!
Yes, I know that it's proprietary unless you pay, but you can freely use the
binaries in your applications (even in commercial ones). And no one stops you
from making a program with _good_ UX since it's HTML + extensions/CSS2.1 + some
CSS3 + custom extensions/custom TiScript (JavaScript-like
Do you really?
[https://github.com/c-smile/sciter-sdk/blob/master/license.htm](https://github.com/c-smile/sciter-sdk/blob/master/license.htm)
At least two of the programs given as the examples on their GH page are
terrible in terms of UX.
Replying to myself and for future use: at least, there's an undocumented `scan`
compiler command to dump the result of the lexer/scanner.
Try `nim scan foo.nim` to see the tokens identified in the source file.
There's also a `parse` command but it discards its result. So apart from
checking
Well if you want to know why something works for procs and not for templates,
look at the different handling of `skTemplate` inside `sigmatch.nim` and
elsewhere. Looking at the AST is pointless, no matter how much you look at it
and under which views and wether it's the AST for the proc or for
Please report issues on github so that we track their progress.
@dponyatov suggests a list of features for embedded
[https://github.com/nim-lang/needed-libraries/issues/81](https://github.com/nim-lang/needed-libraries/issues/81)
suggests it support newlib+FreeRTOS+FAT32+lwip esp-idf specifically provides
newlib,freertos,lwip and 'vfs' which is a filesystem
relevant:
[https://github.com/nim-lang/Nim/issues/12036](https://github.com/nim-lang/Nim/issues/12036)
Unfortunately I have no time to test this pull request,but I can give one more
example which may help to pinpoint the issue:
# test350.nim
import os,times
echo now().utc
let filename = extractFileName(getAppFilename())
let fi = getFileInfo(filename)
> Have you given the selectors module a try? It should provide an even nicer
> API on top of the raw POSIX select.
Oh actually that is what I'm using, not the raw posix.select. I meant to say
that wrapping the esp32 libraries in Nim was far enough along to provide access
to the posix.select,
Excellent! It'd be great to get some help. I've got a number of changes for
wrapping a few ESP libraries that could probably be brought out into another
package. Also the branch is pretty out of date and needs some rebasing.
on windows. I have tested the following:
wNim, which is an alive one among what I have tested and is updated now and
then. it supplies many common controls. it can produce tiny size application,
but only for windows. I like its autolayout DSL very much, which lets us do the
layout in a very
Can you please try with
[https://github.com/nim-lang/Nim/pull/14964](https://github.com/nim-lang/Nim/pull/14964)
? It fixes the issue which is a bit similar to yours
Yeah if it's going to be added to the compiler then that's great. Those
workarounds don't work in my use case but I've just simplified things to remove
the case statement
When compilation fails, `./koch temp c /tmp/foo.nim` displays the stack trace
down to the compilation error message, so we can have a _snapshot_
understanding of the parsing process. But when the compilation succeeds,
there's no way to understand what were the compilation phases.
For instance,
> Except you don't, you can just do
if I want to add a variant to a variant object I need to edit the object
itself, while I can also inherit from e.g. a type defined in a library.
I actually remember one case where I used ref arrays. While implementing a
[slot maps
container](https://seanmiddleditch.com/2013-01-05-data-structures-for-game-developers-the-slot-map).
I imagine there are other cases where you store data "chunked" like here and
ref arrays come handy.
What I meant here is that in some cases you can use object variants where you
would otherwise use inheritance in a more "traditional" design. That doesn't
mean that object variants can do everything that inheritance can do. Both
object variants and inheritance are different design tools and
> Well technically you can replace all uses of inheritance with object variants
> (I think). But in some cases it will get too verbose :)
In the sense that all turing-complete languages are equivalent...
With variants, you have to know and handle all the possible classes at every
"method"
"With variants, you have to know and handle all the possible classes at every
"method" implementation." except you don't, you can just do
case myobj.kind
of MyKind:
# do stuff...
else:
discard
Run
Well yeah, the wiki page is the best way to debug the compiler currently (as
far as I know, you can also ask @Araq :P) :) You can also read
[https://nim-lang.org/docs/intern.html](https://nim-lang.org/docs/intern.html)
> I would like to see this: "Object variants as lightweight alternative to
> inheritance" explained. :)
Object variants are not really a lightweight alternative to inheritance.
Both concepts are explained in various tutorials, I tried
Well technically you can replace all uses of inheritance with object variants
(I think). But in some cases it will get too verbose :)
I don't expect that you can replace _all_ uses of inheritance with object
variants. But some. :-)
Thank you for you answer.
I just want using `nimble` as a classic package manager. But may be i don't
have understand its goal (i begin to wonder, seeing your post and i'm serious).
A classic package manager can:
* update its database of packets (ok : `nimble refresh` )
* do a full upgrade
> not quite sure what you are asking?
It would be nice to get informed when a new version is available, and it would
be nice to be able to update all packages, or a subset when updates are
available.
Generally I do: nimble refresh nimble unistall gintro nimble install gintro
I do not want to
> not quite sure what you are asking?
It would be nice to get informed when a new version is available, and it would
be nice to be able to update all packages, or a subset when updates are
available.
Generally I do: nimble refresh nimble unistall gintro nimble install gintro
I do not want to
Nimble is not like a traditional Linux package manager, it is a **programming
language** package manager. Programs or libraries might use different versions
of the other libraries, so your 3rd question doesn't make sense. 2nd question -
well, _maybe_ , but it is not recommended to depend
I am not quite sure what you are asking?
* If you have a package that you are developing and making local updates use
nimble develop then you can make edits, use git pull etc... and package stays
in sync.
* If you want to update a package that you don't own, increment its version
in your
I agree, the contents on the page are nice but I didn't like the design :) cc
@juancarlospaco
I would like to see this: "Object variants as lightweight alternative to
inheritance" explained. :)
Wow, Sciter seems to be really amazing! I must try it
Thanks for that hints. I have recently listed available Nim GUI libs, I will
add these 3 to the list when we can provide working bindings and some examples.
Do you know which on my list are currently working? The list is just above
Chapter 1 headline in
thank you for pointing out sciter. although it looks nice, I found the document
or tutorial is lack, at least for python/nim language.
Thank you. I've posted an [issue](https://github.com/nim-lang/Nim/issues/14942)
My suggestion is [Sciter](http://sciter.com)
* It's [free](https://sciter.com/prices/)
*
[Lightweight](https://terrainformatica.com/2018/12/23/sciternode-versus-electron/)
* [Cross-platform (win, linux, osx, arm)](https://quark.sciter.com/)
* Single dll 5-7mb (depending on renderer)
*
> It looks like this isn't supposed to work and should be flagged by the
> compiler.
It's more like it was a use-case that was missed and should be added to the
compiler.
Current workarounds:
* Use `let` instead of const:
Thanks @mratsim for pointing me to those issues! I did not find them before. It
looks like this isn't supposed to work and should be flagged by the compiler.
@shirleyquirk thank you for also looking into the issue. I had tried that but
with my object init proc it only works if the procedure is
Thank you. That wasn't quite it, but got me in the right direction.
Karax was actually triggering updates correctly, but as my try/except is inside
createApp it would just reset the notes to whatever is in localStorage at the
time. So obviously the note added in onclick would be lost if not
try this.
try:
let x = window.localStorage.getItem("notes")
# notes = ($x).parseJson.to(seq[Note])
notes.add(($x).parseJson.to(seq[Note]))
except:
window.localStorage.setItem("notes", $(%*(notes)))
Run
I do find this syntax nice but the problem is it doesn't work
I'm not sure why but dup had trouble working inside macros. Maybe because it's
a macro itself and the macro resolution algorithm works in such a way that by
the time outer macro is resolved the inner one is not, so we're getting a
> My question is why linear search of seq is faster than constant search of
> tables?
Sorry to pile on four days later, but the right question would be "why is my
benchmark code producing crazy results?" since it's much more likely there's
something wrong with your quick benchmark than with
> Currently I'm using Nim on an ESP32 with posix.select for a custom TCP
> protocol.
Have you given the selectors module a try? It should provide an even nicer API
on top of the raw POSIX select.
this is fabulous, thanks for your hard work! I work a lot with the esp, and it
would be such a dream to have first class nim support. I'll check this out and
contribute as much as i'm able
@miran Thanks for the info. Will install it next week.
Just wanted to report back here: -d:nimEmulateOverflowChecks solved the issue.
:-)
In any case next week I will do more with nim. (But I guess I'll install 1.2.4
first!)
What I'm missing in the docs:
* Syntax of pragma content
* allowed locations
in your original post you were talking about syntax for dup.
i think nesting the blocks:
echo:
%newUser().dup:
db.insert x
db.insert y
Run
you might find pleasing
chaining ...It macros doesn't work
[https://github.com/nim-lang/Nim/issues/12928](https://github.com/nim-lang/Nim/issues/12928)/
do have a look at
[zero_functional](https://nimble.directory/pkg/zerofunctional) it does chaining
well, and inlines each link into the same loop
like this:
Awesome thank you that cleared things up quite a bit.
I fixed the mistake, for some reason the system assumed the path was leading to
my desktop, somehow? I still have no idea about the trailing slash but another
reinstallation with the folder on the desktop worked for me
Perfect! thank you! Synthesis was on my radar, but I hadn't delved yet. will
certainly do so now.
looking at the generated C code this is definitely a bug
when root is instantiated
_[both](https://forum.nim-lang.org/postActivity.xml#both) root.value and
root.children are initialized if root.value is set to 0 after root.children has
been set, it overwrites the seq pointer
which isn't
> I would expect that this function below would be naked and just does a add
> and jmp call
Unless you compile with optimizations on, all C function push the stack base
pointer on the stack on function entry (ebp), then save the stack pointer (esp)
in ebp then do the function. Compile with
Known issue:
[https://github.com/nim-lang/Nim/issues/13081](https://github.com/nim-lang/Nim/issues/13081)
And linked:
*
[https://github.com/nim-lang/Nim/issues/8015](https://github.com/nim-lang/Nim/issues/8015)
*
untyped are automatically converted to NimNode.
For your final use-case, look into the code of Synthesis:
[https://github.com/mratsim/Synthesis/blob/09bef6df/synthesis/factory.nim#L79-L111](https://github.com/mratsim/Synthesis/blob/09bef6df/synthesis/factory.nim#L79-L111)
i don't know what's going on either, but swapping A and B:
Node = object
case kind: NodeKind
of B:
value: int
of A:
children: seq[Node]
Run
works
Run
is deprecated and everything was supposed to be moved to
_t
Run
but it looks like that particular call in syslocks.nim hasn't been.
I think the diff could be as simple as
154 - L = cast[SysLock](c_malloc(sizeof(SysLockObj)))
* L=
Interesting. Thanks for the workaround.
Pragmas are just a way to attach more info to the functions.
The compiler takes the data in the pragmas and does some thing special with it.
Take the {.inline.} pragma it just marks the functions with inline=true and
then when compiler sees the function it inlines it.
You can also create your
For debug re wrote the code, then this got compiled successfully. Wired.
> Nim Compiler Version 1.2.0 [Windows: amd64]
import algorithm, sequtils, strutils, tables
proc disorganize(books: seq[string]): seq[string] =
let step1 = books.mapIt((b :it, t: it.toCountTa
If the env | grep gcc command returned anything, can you post it here? If not,
it's probably not the problem
Both dont work haha. The system / mingw compiler looks for ...gcc.exeinstead of
the location of gcc.exe so it wont find the necessary file. Idk how to fix it
because the Path variable is correct
1 - 100 of 18992 matches
Mail list logo