diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2009-06-08 08:13:09 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2009-06-08 08:13:09 +0200 |
commit | 36818817bd61594ea6d106328bb8df0f5a25cfc4 (patch) | |
tree | 58180069c9a32400e7e11ba5bc5c85be2f11f0cc /examples | |
parent | 4d4b3b1c04d41868ebb58bd9ccba7b303007e900 (diff) | |
download | Nim-36818817bd61594ea6d106328bb8df0f5a25cfc4.tar.gz |
version0.7.10
Diffstat (limited to 'examples')
-rw-r--r-- | examples/keyval.nim | 9 | ||||
-rw-r--r-- | examples/luaex.nim | 15 | ||||
-rw-r--r-- | examples/maximum.nim | 6 | ||||
-rw-r--r-- | examples/myfile.txt | 11 | ||||
-rw-r--r-- | examples/pythonex.nim | 12 | ||||
-rw-r--r-- | examples/tclex.nim | 25 |
6 files changed, 78 insertions, 0 deletions
diff --git a/examples/keyval.nim b/examples/keyval.nim new file mode 100644 index 000000000..99a410e8c --- /dev/null +++ b/examples/keyval.nim @@ -0,0 +1,9 @@ +# Filter key=value pairs from "myfile.txt" +import regexprs + +for x in lines("myfile.txt"): + if x =~ r"(\w+)=(.*)": + echo "Key: ", matches[1], + " Value: ", matches[2] + + diff --git a/examples/luaex.nim b/examples/luaex.nim new file mode 100644 index 000000000..a71f0fb05 --- /dev/null +++ b/examples/luaex.nim @@ -0,0 +1,15 @@ +# Embedds Lua into a Nimrod application + +import + lua, lualib, lauxlib + +const + code = """ +print 'hi' +""" + +var L = luaL_newstate() +luaL_openlibs(L) +discard luaL_loadbuffer(L, code, code.len, "line") +discard lua_pcall(L, 0, 0, 0) + diff --git a/examples/maximum.nim b/examples/maximum.nim new file mode 100644 index 000000000..5e4a9cc5e --- /dev/null +++ b/examples/maximum.nim @@ -0,0 +1,6 @@ +# Test high level features + +import strutils + +echo "Give a list of numbers (separated by spaces): " +stdin.readLine.splitSeq.each(parseInt).max.`$`.echo(" is the maximum!") diff --git a/examples/myfile.txt b/examples/myfile.txt new file mode 100644 index 000000000..1df0d56f8 --- /dev/null +++ b/examples/myfile.txt @@ -0,0 +1,11 @@ +kladsfa + +asdflksadlfasf + + +adsfljksadfl + + +key=/usr/bin/value +key2=/ha/ha + diff --git a/examples/pythonex.nim b/examples/pythonex.nim new file mode 100644 index 000000000..e2664f350 --- /dev/null +++ b/examples/pythonex.nim @@ -0,0 +1,12 @@ +# Example to embed Python into your application + +import python + +# IMPORTANT: Python on Windows does not like CR characters, so +# we use only \L here. + +Py_Initialize() +discard PyRun_SimpleString("from time import time,ctime\L" & + "print 'Today is',ctime(time())\L") +Py_Finalize() + diff --git a/examples/tclex.nim b/examples/tclex.nim new file mode 100644 index 000000000..6d6d45b8f --- /dev/null +++ b/examples/tclex.nim @@ -0,0 +1,25 @@ +# Example to embed TCL in Nimrod + +import tcl, os + +const + myScript = """puts "Hello, World - In quotes" """ + myScript2 = """ +package require Tk +pack [entry .e -textvar e -width 50] +bind .e <Return> { + set e [regsub { *=.*} $e ""] ;# remove evaluation (Chris) + catch {expr [string map {/ *1./} $e]} res + append e " = $res" +} +""" + +Tcl_FindExecutable(getApplicationFilename()) +var interp = Tcl_CreateInterp() +if interp == nil: quit("cannot create TCL interpreter") +if Tcl_Init(interp) != TCL_OK: + quit("cannot init interpreter") +if Tcl_Eval(interp, myScript) != TCL_OK: + quit("cannot execute script.tcl") + + |