Author Topic: Nimrod  (Read 44689 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod
« on: September 22, 2013, 08:02:34 PM »
Quote
Nimrod is a statically typed, imperative programming language that tries to give the programmer ultimate power without compromises on runtime efficiency. This means it focuses on compile-time mechanisms in all their various forms.

---
Nimrod borrows features from lots of languages. Major influences are Pascal/Delphi, Modula 3, Ada, C++, Lisp, Python, Oberon.

Nimrod also has many unique and novel features that set it apart from the crowd:

  • An effect system in addition to a type system.
  • Part of the effect system is "exception tracking" which is Java's checked exceptions done right.
  • Overloading based on ASTs in addition to overloading based on types.
  • Term rewriting macros for user definable optimizations that can be based on side effect and alias analysis.
  • And lots more ...

Nimrod is a language that scales: You can use it for a whole software stack; both low level and high level code are right at home in Nimrod.

What is Nimrod?

Nimrod is a general purpose, statically-typed, imperative programming language that supports procedural, object-oriented, functional and generic programming styles while remaining simple and efficient.  Nimrod runs on Windows, Linux, BSD and MacOS X.

Nimrod Design

The Nimrod compiler generates optimized C code and defers compilation to a wide range of external compilers.  Nimrod supports objects with inheritance, overloading, polymorphism and multiple dispatch.  Nimrod treats procedures as first-class entities, meaning it can also be used for functional programming.  Nimrod also supports metaprogramming by a combination of generics, templates, macros, conditional compilation with compile-time function execution, and user-defined operators.

Nimrod has high-level data types, including strings, arrays, sequences, sets, tuples, enumerations, etc.  Most objects created on the heap are managed with garbage collection. Nimrod also supports a module mechanism to create independent libraries.  The Nimrod standard library has I/O and OS operations, string utilities, Unicode support, regular expressions, and various parsers such as command line options, XML, CSV, and SQL.

Nimrod History

Nimrod was created in 2004 by Andreas Rumpf.  It was originally coded in Object Pascal (FreePascal) and Python.  However, the first version that bootstrapped (was able to compile itself) was released in 2008.



Kent from the OxygenBasic forum (and now an advocate here) found a really nice BASIC like front end language to C. I think the author did an outstanding job and it seems to have a loyal following. The language is a conglomeration of many existing languages including SB. It has a strong Python and Pascal flavour to it but still has many of the BASIC constructs and high level language features. (garbage collection, OOP, truples, sequences and bit shifting if needed) The best part is it has bindings to most of the main stream libraries and embeddable scripting engines. A C header file converter is included if need to create your own custom binding.
 
Nimrod has an IDE (Aporia) that is written in Nimrod. It uses Gtk and the gtksourceview widget for the editor. The IDE is also a good resource to get a feel how Nimrod as a language flows.



Nimrod Project Site

Nimrod Manual

Nimrod is also a supported language for the CompileOnline.com site. (see attached)

« Last Edit: September 27, 2013, 09:51:48 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - Tuples
« Reply #1 on: September 22, 2013, 08:06:35 PM »
Code: [Select]
type
    TPerson = tuple [name: string, age:int]

proc result(): TPerson = ("John",60)

var r = result()
echo(r)             # default stringification
echo (r.name)       # access by field name
var (name,age) = r  # tuple unpacking
echo (name,"|",age)

jrs@laptop:~/nimrod/examples$ nimrod c -d:release tuple.nim
config/nimrod.cfg(36, 11) Hint: added path: '/home/jrs/.babel/libs/' [Path]
Hint: used config file '/home/jrs/nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: tuple [Processing]
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/tuple.o examples/nimcache/tuple.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/system.o examples/nimcache/system.c
gcc   -o /home/jrs/nimrod/examples/tuple  examples/nimcache/system.o examples/nimcache/tuple.o  -ldl
Hint: operation successful (7450 lines compiled; 3.350 sec total; 8.870MB) [SuccessX]
jrs@laptop:~/nimrod/examples$ ./tuple
(name: John, age: 60)
John
John|60
jrs@laptop:~/nimrod/examples$
« Last Edit: September 22, 2013, 11:53:24 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - head replacement
« Reply #2 on: September 22, 2013, 08:08:09 PM »
A Nimrod version of the Linux head console command.

Code: [Select]
import parseopt
from strutils import parseInt
from os import existsFile

const usage = """
head [flags] filename
  -n: number of lines (default 10)
  -h,--help: this help
  -v,--version: version
"""

proc showUsage(msg: string) =
  if msg != nil: echo("head: ",msg)
  quit(usage,1)
 
proc head(file: string, n: int) =
  if not existsFile(file):  quit("file " & file & " does not exist")
  var
    f = open(file)
    i = 0
  for line in f.lines:
    echo(line)
    i += 1
    if i == n: break
 
proc parseCommands() =
  var
    files: seq[string]
    n = 10
  newSeq(files,0)
  for kind, key, val in getopt():
    case kind
    of cmdArgument:
      files.add(key)
    of cmdLongOption, cmdShortOption:
      case key
      of "help", "h": showUsage(nil)
      of "n":
        n = parseInt(val)
      of "version", "v":
          echo("1.0")
          return
    of cmdEnd: assert(false) # cannot happen
  if len(files) == 0:
    # no filename has been given, so we show the help:
    showUsage("please supply filename")
  if len(files) == 1:
    head(files[0],n)
  else:
    for f in files:
      echo("----- ",f)
      head(f,n)
 
parseCommands()

jrs@laptop:~/nimrod/examples$ nimrod c -d:release nhead.nim
config/nimrod.cfg(36, 11) Hint: added path: '/home/jrs/.babel/libs/' [Path]
Hint: used config file '/home/jrs/nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: nhead [Processing]
Hint: parseopt [Processing]
Hint: os [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: times [Processing]
Hint: posix [Processing]
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/nhead.o examples/nimcache/nhead.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/parseopt.o examples/nimcache/parseopt.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/os.o examples/nimcache/os.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/strutils.o examples/nimcache/strutils.c
gcc   -o /home/jrs/nimrod/examples/nhead  examples/nimcache/posix.o examples/nimcache/times.o examples/nimcache/parseutils.o examples/nimcache/strutils.o examples/nimcache/os.o examples/nimcache/parseopt.o examples/nimcache/system.o examples/nimcache/nhead.o  -ldl
Hint: operation successful (14375 lines compiled; 0.569 sec total; 15.158MB) [SuccessX]
jrs@laptop:~/nimrod/examples$ ./nhead -n=5 nhead.nim
import parseopt
from strutils import parseInt
from os import existsFile

const usage = """
jrs@laptop:~/nimrod/examples$
jrs@laptop:~/nimrod/examples$ ls -l nhead
-rwxrwxr-x 1 jrs jrs 67253 Sep 21 23:34 nhead
jrs@laptop:~/nimrod/examples$

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - polymorphic OOP example
« Reply #3 on: September 22, 2013, 08:10:55 PM »
Quote
Here we are making objects which are references (by default they are value types, like tuples, unlike java), initialized with the standard procedure new. Note the Pascal-like special variable result in procedures!

As expected, you may pass Germans to sayit, because a German is a person, but greeting has to be declared as a method for this to work; if it were a proc, we would get a warning about the second greeting being unused, and Germans are then addressed in English.

The cool thing about multi-methods is that they restore symmetry; a traditional polymorphic call a.foo(b) is only polymorphic in a. This makes sense in a language where dot method notation is just sugar for procedure calls where the first argument matches the type.

Code: [Select]
type
    TPerson = object of TObject
        name: string
        age: int

proc setPerson(p: ref TPerson, name: string, age:int) =
    p.name = name
    p.age = age

proc newPerson(name: string, age:int): ref TPerson =
    new(result)
    result.setPerson(name,age)

method greeting(p: ref TPerson):string = "Hello " & p.name & ", age " & $p.age

type
    TGerman = object of TPerson

proc newGerman(name: string, age:int): ref TGerman =
    new(result)
    result.setPerson(name,age)

method greeting(p: ref TGerman):string = "Hallo " & p.name & ", " & $p.age & " Jahre alt"

var john = newPerson("John",60)
var rene = newGerman("Rene",43)

proc sayit(p: ref TPerson) = echo p.greeting

sayit(john)
sayit(rene)

jrs@laptop:~/nimrod/examples$ nimrod c -d:release class.nim
config/nimrod.cfg(36, 11) Hint: added path: '/home/jrs/.babel/libs/' [Path]
Hint: used config file '/home/jrs/nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: class [Processing]
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/class.o examples/nimcache/class.c
gcc   -o /home/jrs/nimrod/examples/class  examples/nimcache/system.o examples/nimcache/class.o  -ldl
Hint: operation successful (7468 lines compiled; 0.297 sec total; 8.870MB) [SuccessX]
jrs@laptop:~/nimrod/examples$ ./class
Hello John, age 60
Hallo Rene, 43 Jahre alt
jrs@laptop:~/nimrod/examples$

kryton9

  • Guest
Re: Nimrod
« Reply #4 on: September 22, 2013, 08:18:41 PM »
Thanks John, I got no where with Nimrod. Your encouraging work and wonderful detailed posts of your progress are really motivating. I hope to catch up soon and contribute also.

I am glad you got Aporia working, as having a nice IDE is something I have been spoiled by in recent years with Code::Blocks, Lazarus and Visual Studio.

Do you do all of your development in Ubuntu and use Wine for Windows testing John?

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Re: Nimrod
« Reply #5 on: September 22, 2013, 08:31:21 PM »
My main development environment is Ubuntu 12.04 LTS 64 bit. I have a Win7 64 bit partition (always create your Windows partition first before Linux - less headaches)

I have a XP VirtualBox and Wine 1.6 (32/64 bit) I use for Windows related stuff.

Don't be shy. If you're having issues getting something going, that's what we're here for.

Our investment as developers in advocates is time well spent.

« Last Edit: September 22, 2013, 08:44:09 PM by John »

kryton9

  • Guest
Re: Nimrod
« Reply #6 on: September 22, 2013, 08:48:20 PM »
I got an online linux book to study, I had read one in the late 90's but forgot all I read.

Thanks, I always try things on my own and only ask for help when truly stumped so don't worry won't flood you with things I should learn on my own.

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Re: Nimrod
« Reply #7 on: September 22, 2013, 08:53:48 PM »
I think it's more fun than playing games.

Learning new languages and OSs only enriches your skill set and makes you more worthy to those knowing less.  8)


Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - OpenGL
« Reply #8 on: September 23, 2013, 10:52:20 PM »
Quote
GLFW is a lightweight utility library for use with OpenGL. It provides programmers with the ability to open windows, create and manage OpenGL contexts, as well as receive input from joystick, keyboard and mouse. The current release of GLFW supports OpenGL 3.0 and higher.

The box moves around the screen bouncing off its edges.



Code: [Select]
import glfw
import opengl
import strutils


## -------------------------------------------------------------------------------

var
    running : bool = true
    frameCount: int = 0
    lastTime: float = 0.0
    lastFPSTime: float = 0.0
    currentTime: float = 0.0
    frameRate: int = 0
    frameDelta: float = 0.0
    x: float = 0.0
    y: float = 0.0
    vx: float = 200.0
    vy: float = 200.0
    windowW: cint = 640
    windowH: cint = 480

## -------------------------------------------------------------------------------

proc Initialize() =
   
    if glfwInit() == 0:
        write(stdout, "Could not initialize GLFW! \n")

    if glfwOpenWindow(windowW.cint, windowH.cint, 0, 0, 0, 0, 0, 0, GLFW_WINDOW) == 0:
        glfwTerminate()

    opengl.loadExtensions()

    glfwSwapInterval(0)

    glClearColor(0.1,0.1,0.1,1.0)
    glClearDepth(1.0)

    glEnable(GL_BLEND)
    glDisable(GL_LIGHTING)
    glCullFace(GL_BACK)
    glDisable(GL_DEPTH_TEST)

    glViewport(0,0,windowW,windowH)

    glMatrixMode(GL_PROJECTION)

    glOrtho(0.0, float(windowW), float(windowH), 0.0, 0.0, 1.0)

    lastTime = glfwGetTime()
    lastFPSTime = lastTime

## -------------------------------------------------------------------------------

proc Update() =
   
    currentTime = glfwGetTime()

    frameDelta = currentTime - lastTime

    lastTime = currentTime

    if currentTime - lastFPSTime > 1.0:
        frameRate = int(float(frameCount) / (currentTime - lastFPSTime))
        glfwSetWindowTitle("FPS: $1" % intToStr(frameRate))
       
        lastFPSTime = currentTime
        frameCount = 0

    frameCount += 1

    x += vx * frameDelta
    y += vy * frameDelta

    var w = float(windowW)
    var h = float(windowH)

    if x > w - 100.0:

        x = w - 100.0
        vx *= -1.0

    elif x < 0.0:

        x = 0.0
        vx *= -1.0

    if y > h - 100.0:

        y = h - 100.0
        vy *= -1.0

    elif y < 0.0:

        y = 0.0
        vy *= -1.0


## --------------------------------------------------------------------------------

proc Render() =
   
    glClear(GL_COLOR_BUFFER_BIT)

    glMatrixMode(GL_MODELVIEW)

    glLoadIdentity()

    glBegin(GL_QUADS)

    glColor3f(0.9,0.2,0.49)

    glVertex3f(x, y, 0.0)

    glVertex3f(x + 100.0, y, 0.0)

    glVertex3f(x + 100.0, y + 100.0, 0.0)

    glVertex3f(x, y + 100.0, 0.0)

    glEnd()

    glfwSwapBuffers()



## --------------------------------------------------------------------------------

proc Run() =
   
    while running:

        Update()

        Render()

        running = glfwGetKey(GLFW_KEY_ESC) == GLFW_RELEASE and
                  glfwGetWindowParam(GLFW_OPENED) == GL_TRUE


## ==============================================================================

Initialize()

Run()

glfwTerminate()

jrs@laptop:~/nimrod/examples$ nimrod c -d:release glfwtest.nim
config/nimrod.cfg(36, 11) Hint: added path: '/home/jrs/.babel/libs/' [Path]
Hint: used config file '/home/jrs/nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: glfwtest [Processing]
Hint: glfw [Processing]
Hint: opengl [Processing]
Hint: x [Processing]
Hint: xlib [Processing]
Hint: xutil [Processing]
Hint: keysym [Processing]
Hint: dynlib [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/glfwtest.o examples/nimcache/glfwtest.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/system.o examples/nimcache/system.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/glfw.o examples/nimcache/glfw.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/opengl.o examples/nimcache/opengl.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/xlib.o examples/nimcache/xlib.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/xutil.o examples/nimcache/xutil.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/dynlib.o examples/nimcache/dynlib.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/strutils.o examples/nimcache/strutils.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/parseutils.o examples/nimcache/parseutils.c
gcc   -o /home/jrs/nimrod/examples/glfwtest  examples/nimcache/parseutils.o examples/nimcache/strutils.o examples/nimcache/dynlib.o examples/nimcache/keysym.o examples/nimcache/xutil.o examples/nimcache/xlib.o examples/nimcache/x.o examples/nimcache/opengl.o examples/nimcache/glfw.o examples/nimcache/system.o examples/nimcache/glfwtest.o  -ldl
Hint: operation successful (24526 lines compiled; 3.610 sec total; 26.266MB) [SuccessX]
jrs@laptop:~/nimrod/examples$ ./glfwtest
jrs@laptop:~/nimrod/examples$ ls -l glfwtest
-rwxrwxr-x 1 jrs jrs 67981 Sep 23 22:44 glfwtest
jrs@laptop:~/nimrod/examples$
« Last Edit: September 24, 2013, 10:25:10 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - Gtk Cairo
« Reply #9 on: September 24, 2013, 10:23:26 AM »
Quote
Gtk Cairo is a software library used to provide a vector graphics-based, device-independent API for software developers. It is designed to provide primitives for 2-dimensional drawing across a number of different backends. Cairo is designed to use hardware acceleration when available.

Code: [Select]
import cairo

var surface = image_surface_create(FORMAT_ARGB32, 240, 80)
var cr = create(surface)

select_font_face(cr, "serif", FONT_SLANT_NORMAL,
                              FONT_WEIGHT_BOLD)
set_font_size(cr, 32.0)
set_source_rgb(cr, 0.0, 0.0, 1.0)
move_to(cr, 10.0, 50.0)
show_text(cr, "Hello, world")
destroy(cr)
discard write_to_png(surface, "hello.png")
destroy(surface)

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - (lib)cURL
« Reply #10 on: September 24, 2013, 10:50:08 AM »
Quote
libcurl is a free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos), file transfer resume, http proxy tunneling and more!

A wget like web page download example in Nimrod using libcurl.

Code: [Select]
import
  libcurl

var hCurl = easy_init()
if hCurl != nil:
  discard easy_setopt(hCurl, OPT_VERBOSE, True)
  discard easy_setopt(hCurl, OPT_URL, "http://www.scriptbasic.info/index.html")
  discard easy_perform(hCurl)
  easy_cleanup(hCurl)

jrs@laptop:~/nimrod/examples$ nimrod c -d:release curlex.nim
config/nimrod.cfg(36, 11) Hint: added path: '/home/jrs/.babel/libs/' [Path]
Hint: used config file '/home/jrs/nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: curlex [Processing]
Hint: libcurl [Processing]
Hint: times [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/curlex.o examples/nimcache/curlex.c
gcc   -o /home/jrs/nimrod/examples/curlex  examples/nimcache/parseutils.o examples/nimcache/strutils.o examples/nimcache/times.o examples/nimcache/libcurl.o examples/nimcache/system.o examples/nimcache/curlex.o  -ldl
Hint: operation successful (10438 lines compiled; 0.263 sec total; 11.112MB) [SuccessX]
jrs@laptop:~/nimrod/examples$ ./curlex
* About to connect() to www.scriptbasic.info port 80 (#0)
*   Trying 184.169.134.178... * connected
> GET /index.html HTTP/1.1
Host: www.scriptbasic.info
Accept: */*

< HTTP/1.1 200 OK
< Date: Sun, 15 Sep 2013 04:37:36 GMT
< Server: Apache/2.2.22 (Ubuntu)
< Last-Modified: Thu, 07 Jun 2012 05:37:46 GMT
< ETag: "2329b-41-4c1db48920d04"
< Accept-Ranges: bytes
< Content-Length: 65
< Vary: Accept-Encoding
< Content-Type: text/html
<
<html><body><h1>ScriptBasic Development Site</h1>
</body></html>
* Connection #0 to host www.scriptbasic.info left intact
* Closing connection #0
jrs@laptop:~/nimrod/examples$

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - IUP
« Reply #11 on: September 24, 2013, 01:48:03 PM »
Quote
The IUP (Portable User Interface) is a computer software development kit that provides a portable, scriptable toolkit for GUI building using C and Lua. This allows rapid, zero-compile prototyping and refinement of deployable GUI applications.



Code: [Select]
# IupTabs: Creates a IupTabs control.

import iup

discard iup.Open(nil, nil)

var vbox1 = Iup.Vbox(Iup.Label("Inside Tab A"), Iup.Button("Button A", ""), nil)
var vbox2 = Iup.Vbox(Iup.Label("Inside Tab B"), Iup.Button("Button B", ""), nil)

Iup.SetAttribute(vbox1, "TABTITLE", "Tab A")
Iup.SetAttribute(vbox2, "TABTITLE", "Tab B")

var tabs1 = Iup.Tabs(vbox1, vbox2, nil)

vbox1 = Iup.Vbox(Iup.Label("Inside Tab C"), Iup.Button("Button C", ""), nil)
vbox2 = Iup.Vbox(Iup.Label("Inside Tab D"), Iup.Button("Button D", ""), nil)

Iup.SetAttribute(vbox1, "TABTITLE", "Tab C")
Iup.SetAttribute(vbox2, "TABTITLE", "Tab D")

var tabs2 = Iup.Tabs(vbox1, vbox2, nil)
Iup.SetAttribute(tabs2, "TABTYPE", "LEFT")

var box = Iup.Hbox(tabs1, tabs2, nil)
Iup.SetAttribute(box, "MARGIN", "10x10")
Iup.SetAttribute(box, "GAP", "10")

var dlg = Iup.Dialog(box)
Iup.SetAttribute(dlg, "TITLE", "IupTabs")
Iup.SetAttribute(dlg, "SIZE", "200x100")

discard Iup.ShowXY(dlg, IUP_CENTER, IUP_CENTER)
discard Iup.MainLoop()
Iup.Close()

jrs@laptop:~/nimrod/examples$ nimrod c -d:release iupex1.nim
config/nimrod.cfg(36, 11) Hint: added path: '/home/jrs/.babel/libs/' [Path]
Hint: used config file '/home/jrs/nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: iupex1 [Processing]
Hint: iup [Processing]
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/iupex1.o examples/nimcache/iupex1.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/system.o examples/nimcache/system.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/iup.o examples/nimcache/iup.c
gcc   -o /home/jrs/nimrod/examples/iupex1  examples/nimcache/iup.o examples/nimcache/system.o examples/nimcache/iupex1.o  -ldl
Hint: operation successful (8425 lines compiled; 0.498 sec total; 9.922MB) [SuccessX]
jrs@laptop:~/nimrod/examples$ ./iupex1
jrs@laptop:~/nimrod/examples$ ls -l iupex1
-rwxrwxr-x 1 jrs jrs 24456 Sep 14 19:22 iupex1
jrs@laptop:~/nimrod/examples$

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - Gtk
« Reply #12 on: September 24, 2013, 02:18:28 PM »
Quote
GTK+ (GIMP Toolkit) is a cross-platform widget toolkit for creating graphical user interfaces. It is licensed under the terms of the GNU LGPL.

Note: The Nimrod Aporia IDE is written in Nimrod and uses Gtk as its framework. It's a great resource if Gtk native is your GUI of choice.



ex5.nim
Code: [Select]
import
  glib2, gtk2

proc destroy(widget: pWidget, data: pgpointer){.cdecl.} =
  main_quit()

proc widgetDestroy(w: PWidget) {.cdecl.} =
  destroy(w)

nimrod_init()
var window = window_new(WINDOW_TOPLEVEL)
var button = button_new("Click me")
set_border_width(Window, 5)
add(window, button)
discard signal_connect(window, "destroy",
                       SIGNAL_FUNC(ex5.destroy), nil)
discard signal_connect_object(button, "clicked",
                              SIGNAL_FUNC(widgetDestroy),
                              window)
show(button)
show(window)
main()


Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - Zero MQ
« Reply #13 on: September 24, 2013, 02:55:15 PM »
Quote
Zero MQ (0mq) is a lightweight messaging kernel library which extends the standard socket interfaces with features traditionally provided by specialised messaging middleware products. 0MQ sockets provide an abstraction of asynchronous message queues, multiple messaging patterns, message filtering (subscriptions), seamless access to multiple transport protocols and more.

Server
Code: [Select]
import zmq

var connection = zmq.open("tcp://*:5555", server=true)

while True:
  var request = receive(connection)
  echo("Received: ", request)
  send(connection, "World")
 
close(connection)

jrs@laptop:~/nimrod/examples/0mq$ nimrod c -d:release server.nim
config/nimrod.cfg(36, 11) Hint: added path: '/home/jrs/.babel/libs/' [Path]
Hint: used config file '/home/jrs/nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: server [Processing]
Hint: zmq [Processing]
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/0mq/nimcache/system.o examples/0mq/nimcache/system.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/0mq/nimcache/zmq.o examples/0mq/nimcache/zmq.c
gcc   -o /home/jrs/nimrod/examples/0mq/server  examples/0mq/nimcache/zmq.o examples/0mq/nimcache/system.o examples/0mq/nimcache/server.o  -ldl
Hint: operation successful (7773 lines compiled; 1.396 sec total; 9.922MB) [SuccessX]

jrs@laptop:~/nimrod/examples/0mq$ ./server
Received: Hello
Received: Hello
Received: Hello
Received: Hello
Received: Hello
Received: Hello
Received: Hello
Received: Hello
Received: Hello
Received: Hello
Received: Hello

Client
Code: [Select]
import zmq

var connection = zmq.open("tcp://localhost:5555", server=false)

echo("Connecting...")

for i in 0..10:
  echo("Sending hello...", i)
  send(connection, "Hello")
 
  var reply = receive(connection)
  echo("Received ...", reply)

close(connection)

jrs@laptop:~/nimrod/examples/0mq$ nimrod c -d:release client.nim
config/nimrod.cfg(36, 11) Hint: added path: '/home/jrs/.babel/libs/' [Path]
Hint: used config file '/home/jrs/nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: client [Processing]
Hint: zmq [Processing]
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/0mq/nimcache/client.o examples/0mq/nimcache/client.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/0mq/nimcache/system.o examples/0mq/nimcache/system.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/0mq/nimcache/zmq.o examples/0mq/nimcache/zmq.c
gcc   -o /home/jrs/nimrod/examples/0mq/client  examples/0mq/nimcache/zmq.o examples/0mq/nimcache/system.o examples/0mq/nimcache/client.o  -ldl
Hint: operation successful (7776 lines compiled; 1.494 sec total; 9.922MB) [SuccessX]

jrs@laptop:~/nimrod/examples/0mq$ ./client
Connecting...
Sending hello...0
Received ...World
Sending hello...1
Received ...World
Sending hello...2
Received ...World
Sending hello...3
Received ...World
Sending hello...4
Received ...World
Sending hello...5
Received ...World
Sending hello...6
Received ...World
Sending hello...7
Received ...World
Sending hello...8
Received ...World
Sending hello...9
Received ...World
Sending hello...10
Received ...World
jrs@laptop:~/nimrod/examples/0mq$


Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Nimrod - parsexml
« Reply #14 on: September 24, 2013, 04:49:35 PM »
The parsexml module is being used to extract the links out of a HTML document. I use the www.oxygenbasic.org page for this test.

Code: [Select]
# Example program to show the new parsexml module
# This program reads an HTML file and writes all its used links to stdout.
# Errors and whitespace are ignored.

import os, streams, parsexml, strutils

proc `=?=` (a, b: string): bool =
  # little trick: define our own comparator that ignores case
  return cmpIgnoreCase(a, b) == 0

if paramCount() < 1:
  quit("Usage: htmlrefs filename[.html]")

var links = 0 # count the number of links
var filename = addFileExt(ParamStr(1), "html")
var s = newFileStream(filename, fmRead)
if s == nil: quit("cannot open the file " & filename)
var x: TXmlParser
open(x, s, filename)
next(x) # get first event
block mainLoop:
  while true:
    case x.kind
    of xmlElementOpen:
      # the <a href = "xyz"> tag we are interested in always has an attribute,
      # thus we search for ``xmlElementOpen`` and not for ``xmlElementStart``
      if x.elementName =?= "a":
        x.next()
        if x.kind == xmlAttribute:
          if x.attrKey =?= "href":
            var link = x.attrValue
            inc(links)
            # skip until we have an ``xmlElementClose`` event
            while true:
              x.next()
              case x.kind
              of xmlEof: break mainLoop
              of xmlElementClose: break
              else: nil
            x.next() # skip ``xmlElementClose``
            # now we have the description for the ``a`` element
            var desc = ""
            while x.kind == xmlCharData:
              desc.add(x.charData)
              x.next()
            Echo(desc & ": " & link)
      else:
        x.next()     
    of xmlEof: break # end of file reached
    of xmlError:
      Echo(errorMsg(x))
      x.next()
    else: x.next() # skip other events

echo($links & " link(s) found!")
x.close()

jrs@laptop:~/nimrod/examples$ nimrod c -d:release htmlrefs.nim
config/nimrod.cfg(36, 11) Hint: added path: '/home/jrs/.babel/libs/' [Path]
Hint: used config file '/home/jrs/nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: htmlrefs [Processing]
Hint: os [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: times [Processing]
Hint: posix [Processing]
Hint: streams [Processing]
Hint: parsexml [Processing]
Hint: hashes [Processing]
Hint: lexbase [Processing]
Hint: unicode [Processing]
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/htmlrefs.o examples/nimcache/htmlrefs.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/system.o examples/nimcache/system.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/os.o examples/nimcache/os.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/strutils.o examples/nimcache/strutils.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/streams.o examples/nimcache/streams.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/parsexml.o examples/nimcache/parsexml.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/hashes.o examples/nimcache/hashes.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/lexbase.o examples/nimcache/lexbase.c
gcc -c -w -O3 -fno-strict-aliasing -I/home/jrs/nimrod/lib -o examples/nimcache/unicode.o examples/nimcache/unicode.c
gcc   -o /home/jrs/nimrod/examples/htmlrefs  examples/nimcache/unicode.o examples/nimcache/lexbase.o examples/nimcache/hashes.o examples/nimcache/parsexml.o examples/nimcache/streams.o examples/nimcache/posix.o examples/nimcache/times.o examples/nimcache/parseutils.o examples/nimcache/strutils.o examples/nimcache/os.o examples/nimcache/system.o examples/nimcache/htmlrefs.o  -ldl
Hint: operation successful (16742 lines compiled; 2.079 sec total; 19.199MB) [SuccessX]

jrs@laptop:~/nimrod/examples$ ./htmlrefs o2.html
o2.html(17, 13) Error: '"' or "'" expected
o2.html(21, 11) Error: '"' or "'" expected
o2.html(24, 24) Error: '"' or "'" expected
o2.html(26, 35) Error: '"' or "'" expected
o2.html(30, 42) Error: '"' or "'" expected
Alpha Downloads: http://www.oxygenbasic.org/downloads.htm
Games: http://www.oxygenbasic.org/games.htm
Reference: http://www.oxygenbasic.org/reference.htm
Forum: http://www.oxygenbasic.org/forum/
Wiki: http://www.oxygenbasic.org/wiki/index.php/Main_Page
5 link(s) found!
jrs@laptop:~/nimrod/examples$