nim hello world

Moderator: BarryK

Post Reply
williams2
Posts: 1062
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 305 times

nim hello world

Post by williams2 »

Supposedly, you can trick nim to compile a 150 byte executable.

https://hookrace.net/blog/nim-binary-size/

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

That's a great link!
I will append it to the blog post.

For everyone reading this, I created a post with quick size comparisons of "Hello World", one-line in V, Nim and BaCon:

https://bkhome.org/news/202208/quick-he ... rison.html

Here are earlier blog posts about Nim:

https://bkhome.org/news/202208/nim-comp ... edded.html

https://bkhome.org/news/202208/consider ... guage.html

Nim is looking so good. Want to get some experience with coding in Nim, if still looks good, might adopt it as the "official language" for coding in EasyOS.

I have some utilities written in BaCon, and want to write some more to integrate apt/dpkg with the ppm, and want to settle on a language to code in, higher-level than C, and with C's very efficient executable file size. And an elegant and simple coding syntax.

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

User avatar
wiak
Posts: 4082
Joined: Tue Dec 03, 2019 6:10 am
Location: Packing - big job
Has thanked: 65 times
Been thanked: 1208 times
Contact:

Re: nim hello world

Post by wiak »

williams2 wrote: Sun Aug 14, 2022 8:37 pm

Supposedly, you can trick nim to compile a 150 byte executable.

https://hookrace.net/blog/nim-binary-size/

I was already interested in Rust (was looking at FLTK with it) and I see above link also points to this: https://mainisusuallyafunction.blogspot ... -rust.html

In the end, I thought this looked interesting for simple Rust GUI programming: https://github.com/emilk/egui

https://docs.rs/egui/latest/egui/

The installation size of Rust is pretty massive though, but... disc storage can handle it methinks.

EDIT: so I've just installed rust on my Zorin system, and now going to fetch egui, but just for fun and quick play - I have no real desire to do any more real programming of that sort but my kid probably will (in fact I think he already mentioned Rust to me - he stumbles upon everything).

https://www.tinylinux.info/
DOWNLOAD wd_multi for hundreds of 'distros' at your fingertips: viewtopic.php?p=99154#p99154
Αξίζει να μεταφραστεί;

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

As I'm learning Nim, posting progress reports to the blog:

"Associative arrays in Nim"
https://bkhome.org/news/202208/associat ... n-nim.html

"Debian to Puppy package db conversion in Nim"
https://bkhome.org/news/202208/debian-t ... n-nim.html

Quite a pleasant experience so far.

Burunduk
Posts: 252
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 7 times
Been thanked: 127 times

Re: nim hello world

Post by Burunduk »

Thanks to this williams2's post I'm trying to learn the language too. Never heard about nim before. The only "achievement" so far:

Code: Select all

#!/bin/bash
echo "Hello!"

It compiles and runs very well.

As for dictionaries, it seems they are called tables in nim. A small test:

Code: Select all

import std/table

var t: Table[string, string]
var t2 = { "key1": "value1", "key2": "value2" }.toTable
t["key1"] = "value 1"
t["key2"] = "value 2"

if t.hasKey("key1"):
  echo t["key1"]
t2.del("key2")
echo t2.hasKey("key2")

Or maybe an object (struct) can be used here instead:

Code: Select all

import std/with

type
  Package = object
    description: string
    fileName: string
    installedSize: int

var
  foo: Package
  bar = Package(description: "just a package", installedSize: 10000)
with foo:
  description = "dummy package"
  fileName = "pool/main/whatever"
foo.installedSize = 1000

var
  list = @[foo, bar]
  sum = 0

for pkg in list:
  sum.inc(pkg.installedSize)
echo sum
User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

I have been collecting documentation links here:

https://bkhome.org/news/202208/nim-lang ... ation.html

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

I have rewritten the 'debdb2pupdb' utility in Nim code, posted about it here:

https://bkhome.org/news/202208/debdb2pu ... in-oe.html

The program is 256 lines, so a non-trivial exercise. Took a couple of days intense coding, burning the midnight oil. Of course, learning as I went, which slowed things down.

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

Have written another Nim mini-tutorial:

"Commandline and file IO in Nim"
https://bkhome.org/news/202208/commandl ... n-nim.html

User avatar
wiak
Posts: 4082
Joined: Tue Dec 03, 2019 6:10 am
Location: Packing - big job
Has thanked: 65 times
Been thanked: 1208 times
Contact:

Re: nim hello world

Post by wiak »

So how big is 'Nim' to add to default EasyOS or Puppy or whatever? I'm forever wishing for a new dev language for a small distro. I have not myself checked out Nim, but I imagine gcc has to be already available, or some similar compiler, or not?
Associative arrays are something awk does very well so that much alone is not enough to convince me to try Nim. In what way is it particularly more useful. I could understand why Bacon with HUG could be attractive, but not sure where NIM fits into similar needs/desires/scenarios.

https://www.tinylinux.info/
DOWNLOAD wd_multi for hundreds of 'distros' at your fingertips: viewtopic.php?p=99154#p99154
Αξίζει να μεταφραστεί;

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

I haven't tried any of the GUI tools for nim, so far only wrote one CLI utility.

Needs gcc and libc, that's it.
Can also use tcc.

Very small binaries, but slightly bigger than those created by bacon, as has extra runtime safety checking, and an optional garbage collector, but only adds several KB.

My 256 line debdb2pupdb is about 60KB stripped.

Um, debdb2pupdb does link with a bit more than just libc:

Code: Select all

# ldd ./debdb2pupdb 
	linux-vdso.so.1 (0x00007fff84fbf000)
	libm.so.6 => /lib/libm.so.6 (0x00007f7ad4550000)
	librt.so.1 => /lib/librt.so.1 (0x00007f7ad4546000)
	libdl.so.2 => /lib/libdl.so.2 (0x00007f7ad4541000)
	libc.so.6 => /lib/libc.so.6 (0x00007f7ad4389000)
	/lib/ld-linux-x86-64.so.2 (0x00007f7ad46b0000)
	libpthread.so.0 => /lib/libpthread.so.0 (0x00007f7ad4369000)

That's interesting, because it has the "re" module and does some regular expression operations -- according to the docs, that requires linking with libpcre. That is what I read, but "re" must be using the regular expression functions in libc.

Here is the "hello world" in nim:

Code: Select all

# ldd ./helloworld
	linux-vdso.so.1 (0x00007ffdb3feb000)
	libdl.so.2 => /lib/libdl.so.2 (0x00007f29db9f6000)
	libc.so.6 => /lib/libc.so.6 (0x00007f29db83e000)
	/lib64/ld-linux-x86-64.so.2 => /lib/ld-linux-x86-64.so.2 (0x00007f29dba37000)
User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

Checking out the GUI toolkits.

First report, the "ui" toolkit:

"ui GUI toolkit for Nim"
https://bkhome.org/news/202208/ui-gui-t ... r-nim.html

I have tentatively narrowed it down to:
ui
Gintro
NiGui

Might go for Gintro, as binaries are very small and it has the most comprehensive support for gtk (gtk3).

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

Have started a new thread on GUI toolkits for Nim:

https://forum.puppylinux.com/viewtopic.php?t=6701

User avatar
puppy_apprentice
Posts: 691
Joined: Tue Oct 06, 2020 8:43 pm
Location: land of bigos and schabowy ;)
Has thanked: 5 times
Been thanked: 115 times

Re: nim hello world

Post by puppy_apprentice »

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

puppy-apprentice,
Thanks for those links!

I have written an intro page for Nim:

"The Nim language"
https://easyos.org/dev/nim/index.html

jeang3nie
Posts: 4
Joined: Mon Sep 26, 2022 3:47 pm

Re: nim hello world

Post by jeang3nie »

From Barry K's devlog:

At first glance, I could not see that capability in Nim, and a search came up with something about using tables -- and the example given was unclear.

The solution that you came up with using a CritBitTree can actually be interchanged easily with a std/table, as they have nearly the same interface. The only advantage that the CritBitTree has is that the entries are sorted, I presume by the key. Initialization is about the only difference. Nothing wrong with how the debdb2pubdb utility is as written, but here's a patch just to illustrate how close the interface actually is.

Code: Select all

--- debdb2pupdb.nim.orig	2022-09-04 22:06:33.000000000 +0800
+++ debdb2pupdb.nim	2022-09-27 20:57:54.201909793 +0800
@@ -12,13 +12,13 @@
 #need split(), removeSuffix()...
 import std/strutils
 #CritBitTree for associative array...
-import std/critbits
+import std/tables
 #regular expressions...
 import std/re
 #reverse()...
 #import std/algorithm
 
-var debarray: CritBitTree[string]
+var debarray = initTable[string, string]()
 
 #read the commandline...
 var args = commandLineParams() #in os module

Most of my coding recently is in Rust, but Nim is extremely interesting in a number of ways. One thing that I find really interesting for a compiled language is there is no predefined entry point. So I can write a small CLI utility which is also a library, depending on whether the file is the main module or not.

Code: Select all

proc main() =
    stdout.write "Hello World!"

when isMainModule:
    main()

This is really nice because you could easily write the backend of an application along with a CLI interface, and then add a GUI interface of your choosing. Or two different interfaces, for different platforms. Lots of flexibility.

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: nim hello world

Post by BarryK »

jeang3nie,
Thanks for that info about std/table!

Post Reply

Return to “EasyOS”