Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Thomas Zastrow, MPCDF Seminar 01-08-20
import nimx/window
import nimx/text_field
import nimx/button
proc startApp() =
var wnd = newWindow(newRect(40, 40, 800, 600))
let label = newLabel(newRect(20, 20, 300, 20))
label.text = "I'm a label with some umlauts: öüäß"
wnd.addSubview(label)
let tf = newTextField(newRect(20, 80, 180,30))
wnd.addSubview(tf)
let b = newButton(newRect(20,50,180,30))
b.title="Click me"
b.onAction do():
tf.text = "Awesome!!!"
wnd.addSubview(b)
let cb = newCheckbox(newRect(20, 110, 50, 16))
cb.title = "It is a checkbox"
wnd.addSubview(cb)
let rb = newRadiobox(newRect(20, 140, 50, 16))
rb.title = "And a radio button"
wnd.addSubview(rb)
runApplication:
startApp()
The NIMProgrammingLanguage
Thomas Zastrow, MPCDF Seminar 01-08-20
https://www.heise.de/developer/mel-
dung/Programmiersprachen-Nim-
liegt-als-stabile-Version-1-0-vor-4537
790.html
Thomas Zastrow, MPCDF Seminar 01-08-20
What is Nim?
„Nim is a statically typed compiled systems programminglanguage. It combines successful concepts from maturelanguages like Python, Ada and Modula“
nim-lang.org
„Today, Nim compiles to C, C++, JavaScript, and Objective-C. Thegoal for Nim is to be as fast as C, as expressive as Python, andas extensible as Lisp.“
https://en.wikipedia.org/wiki/Nim_(programming_language)
Thomas Zastrow, MPCDF Seminar 01-08-20
How it works
Nim Code Nim Compiler
C Compiler
C++ Compiler
Objective C
Compiler
JavaScript
Executable
Thomas Zastrow, MPCDF Seminar 01-08-20
First example
var x : intvar s = „Hello World!“
x = 2if x mod 2 == 0: echo "x is even!"else: echo "x is odd!"
nim compile demo01.nim
Creates executable demo01
C code is stored in ~/.cache/nim
Statically typed
Python like
syntax
Thomas Zastrow, MPCDF Seminar 01-08-20
Common Constructs
var ar : array[6, int]
for i in countup(0,5):
ar[i] = i
var se1 : seq[string] = @["a", "b", "c"]
type
Person = object
name : string
age : intproc add * (a,b:int) : int =
return a + b
import tables
import strtabs
var t = initTable[string, int]()
t["c"] = 10
t["b"] = 20
t["a"] = 30
import json
var jsonNode = parseJson("""{"value": 3,
"somestuff" : {"sport" : ["Soccer",
"Billard", "Running"], "nothing" : null}}""")
echo jsonNode["value"]
Thomas Zastrow, MPCDF Seminar 01-08-20
Further Stuff
• Macros: functions which are transforming aNim syntax tree into something else atcompile time
• Optional: Object Oriented Programming• Generics• Templates• ...
Thomas Zastrow, MPCDF Seminar 01-08-20
The Nimble Package Directory
• Central place for Nim packages• Access via „nimble“ command
https://nimble.directory/
nimble list
nimble search <keyword>
nimble install <package>
...
Thomas Zastrow, MPCDF Seminar 01-08-20
Some Packages
Jester: Web serverimport htmlgen
import jester
routes:
get "/":
resp h1("Hello world")
Nimx: GraphicalUser Interfaces
db: Unified db accessmoduleSupports MySQL,
Postgres and SQlite
... and many more!
Thomas Zastrow, MPCDF Seminar 01-08-20
IDE: Nim Plugin for MS Visual Code
Thomas Zastrow, MPCDF Seminar 01-08-20
Nim on a Raspberry Pi
• In Raspian, only an old Nim version (0.19.4-1)is available via the package management
• Let’s compile Nim itself:wget https://nim-lang.org/download/nim-1.0.4.tar.xz
unxz nim-1.0.4.tar.xz
cd nim-1.0.4/
sh build.sh
bin/nim c koch
./koch tools
export PATH="/home/pi/nimsource/nim-1.0.4/bin:$PATH"
export PATH="/home/pi/.nimble/bin:$PATH"
„The koch program is Nim's maintenance script. It is a replacement for
make and shell scripting with the advantage that it is much more
portable.“ (https://nim-lang.org/docs/koch.html)
Thomas Zastrow, MPCDF Seminar 01-08-20
Nim on a Raspberry Pi
Compiling Nim apps on a Raspberry Pi 4:
nim compile demo1.nim
file demo1demo1: ELF 32-bit LSB executable, ARM, EABI5
version 1 (SYSV), dynamically linked, interpreter
/lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, ...
(Executable runs also on Raspberry Pi Zero)
Thomas Zastrow, MPCDF Seminar 01-08-20
Conclusion
Nim feels like ... ... but runs like
*very comfortable* *very fast*
Thomas Zastrow, MPCDF Seminar 01-08-20
Conclusion
• If you are an experiencedC developer: No.
• In any other cases: Yes.
To Nim or not to Nim?
https://nim-lang.org/
Thomas Zastrow, MPCDF Seminar 01-08-20
import nimx/window
import nimx/text_field
import nimx/button
proc startApp() =
var wnd = newWindow(newRect(40, 40, 800, 600))
let label = newLabel(newRect(20, 20, 300, 20))
label.text = "I'm a label with some umlauts: öüäß"
wnd.addSubview(label)
let tf = newTextField(newRect(20, 80, 180,30))
wnd.addSubview(tf)
let b = newButton(newRect(20,50,180,30))
b.title="Click me"
b.onAction do():
tf.text = "Awesome!!!"
wnd.addSubview(b)
let cb = newCheckbox(newRect(20, 110, 50, 16))
cb.title = "It is a checkbox"
wnd.addSubview(cb)
let rb = newRadiobox(newRect(20, 140, 50, 16))
rb.title = "And a radio button"
wnd.addSubview(rb)
runApplication:
startApp()
Thank you ;-)