Forward declaration of object name
I need to forward declare of object name. I use c2nim and it generates code similar to below: #First file connection {.bycopy.} = object #New line #Second file connection* {.bycopy.} = object id*: cint sock*: cint Run
Re: Recommended GUI library?
Python have TCL in standard library (in reference implementation).
Template retrieving variading type of argument (compatible with C vaarg)
I have problem with this code type va_list *{.pure.} = ptr object template va_arg*[T](list: var va_list): T = result = (cast[ptr T](va_list))[] inc[ptr T](list) var args: va_list var c = va_arg[cf_sequence](args) Run Error is: /home/postep2/src/posteppkg/common/includes/slANDothers_nim_utils.nim(96, 13) Error: expression cannot be cast to ptr cf_sequence| ---|---
Re: Like C extern pragma?
I use this hack: when not defined IS_SUPPORT_IMPORTED: var IS_SUPPORT_IMPORTED {.compileTime.} : bool = true Run But nim (I use nimble package/project manager) complain about IS_SUPPORT_IMPORTED is defined twice. > /home/postep2/src/posteppkg/utility/includes/support.nim(20, 7) Error: > redefinition of 'IS_SUPPORT_IMPORTED'; previous declaration here: > /home/postep2/src/posteppkg/utility/includes/support.nim(20, 7)| > ---|---
Re: Like C extern pragma?
Ok. I found the problem. It was I use c2nim and it puts asterisk after procedure name. That was obvious. Procedure must been defined in the same file, because it was marked as to be exported. Because It was C header file (and after use of c2nim it was nim file contains only signature/prototypes/etc.), I revert state of file and removes asterisk.
Like C extern pragma?
I found information about extern pragma, but It don't describe what it does. I need C-like extern pragma or way to use only function prototype (and include file with these prototypes). That's because I have problem with circual import of modules. Nim compiler complain about missing function implementation, when I tries to do that (only declaration of function in file, implementation in another).
Re: Cannot import module.
Thanks. Currently I have following error: /tmp/nimble_15/githubcom_Parashuramanimi18n/i18n.nim(198, 41) Error: usage of 'isNil' is a user-defined error Run
Re: Cannot import module.
./nim.cfg #--gc="refc"\--debugger:"native"| ---|--- ./src/posteppkg/common/nim.cfg \--cincludes="../../c-files/common"| ---|--- \--clibdir="../../c-files/common"| \--app="lib"| \--out="./libcommon.so"| \--noMain:on| \--undef=useNimRtl| ./postep.nimble Package version = "0.0.1" author= "Sławomir Lach" description = "Nimble version of postęp - game based on Freeciv" license = "GPL-2.0" srcDir= "src" installExt= @["nim"] bin = @["posteppkg/common/libcommon.so", "executables/client/progressclient", "executables/server/progressserver"] # Dependencies requires "nim >= 1.0.0" Run In ./ I type nimble build . I thnik I must add dependency. I don't remember what install ext is.
Re: Cannot import module.
Not worked.
Cannot import module.
Hello. I have problem. I installed i18n module by nimble install, but > import i18n Causes problem.
Re: Current status of Nim for Webassembly?
I think using pointers instead of ref will work. About other way I known nothing. I also don't understand this.
Re: Getting memory address of object into string
echo obj.addr.repr Or repr(obj.addr) Or repr(addr(obj)) Or similar
Re: Problem with macro and identation ...
I think this source is outdated: [https://flenniken.net/blog/nim-macros](https://flenniken.net/blog/nim-macros)/ Compiler complains there's no children field in nim node. When look at official documentation, I found sons field. Should I use sons? Also, I look inside some system file of nim and there's NimNode declared as an reference to Node. Should I use rectangle operator to access NimNode fields? My code now looks like this: macro get_decl_name(decl: untyped): untyped = for a in decl.children: if a is nnkTypeDef: for c in a.children: if c is IdentNode: return c type progress_specenum = object map: seq[string] var generated: string macro generate_specenum*(names_of_values: static[seq[string]]; decl: untyped): untyped = var name = get_decl_name(decl) var code = fmt"var\n progress_specenum_{name}: progress_specenum = progress_specenum(map: name_of_values)\n" code &= fmt"proc get_str_repr_of_specenum_{name}(val: {name}): string =\n " code &= fmt"for a in progress_specenum_{name}.map:\n" code &= fmt"if a == val: return a\n " code &= fmt"return nil" code &= fmt"proc get_val_of_specenum_{name}_from_str(val: string): {name} =\n " code &= fmt"var curr: {name}\n " code &= fmt"for a in progress_specenum_{name}.map:\n" code &= fmt"if a == val: return curr\n" code &= fmt"succ(curr)\n " code &= fmt"return BAD" generated &= code macro flush_ruleset_rel_code*() = result = parseStmt(generated) generated = "" Run But compiler complains there's no field children inside NimNode.
Re: Problem with macro and identation ...
I think about using inheritance to achieve this. Nim, as pascal, delivers many method to use countable types, like splices, etc. I will write type, which contains seq of strings and I will implement method to get string from this sequence by index. Other method will get index of string. One question is: can I cast enum to integer and make integer to be automatically casted to some enum (compiler should cast this automatically). Is there any generic enum type? Exists there some method like get next value of enum?
Re: Problem with macro and identation ...
Thanks a lot. I was trying to write macro that generates: enum type and procedure to map value of this enum to related string. I decided to port game based on Freeciv. I'm only developer of this game and introducing a new features takes too long. Freeciv (and my game) is write in C and uses x-includes to generate code on compile time by macros. Given x-includes header takes as „argument” definitions of enum values and related strings by checking in the chain next enum value and related string is defined. It request to name definition in strict way, so this is good example: #define SPECENUM_VAL0 PLAYER_GREEN #define SPECENUM_STR0 "Green" #define SPECENUM_VAL1 PLAYER_RED #define SPECENUM_STR1 "Red" . . . #define SPECENUM_VALN PLAYER_COLOR_n #define SPECENUM_STRN "Some color" #include "genereate_specenum.h" Run I need to understood how macros in nim works to do similar thinks (generate_specenum.h generates enum type and functions translates enum value to related string and vice-versa). I found nim function, which converts enum value to string, but it do similar thing to nim's quote (or quoteStr - I don't remember) or hash/double hash (I don't remember) operator in C preprocesor.
Problem with macro and identation ...
Sorry for that (probably) simple question, but I have indentation problems (compiler complains about it): First file: type progress_specenum_el = object name: cstring value: string macro generate_specenum(name: string; elements: openarray[progress_specenum_el]): untyped = echo """{name} = enumn " echo "BAD," for a in elements: echo "{a.value}," echo "n" echo "proc string_to_{name}(rname: string): {name} =n const map: seq[progress_specenum_el] = @[repr({elements})]n var b: {name} = low(b)n for a in map:n if a.name == rname:n return bn b=b.succ()n return BAD" Second file: include includes/progress_utils type generate_specenum("test", [progress_specenum_el("LOL", "LOL"), progress_specenum_el("LOL2", "LOL3")])
Re: Crashed when compiled as dynamic library for C
Also I thought new callable accept variable not a type. I remember it works like in Pascal - allocates memory and write pointer to this memory into given (as parameter) variable. In nim it also should initializes memory. Does your library compile?
Passing more than one parenthesis block to macro
Is it possible to pass more block to macro? One will be normal (indent) block and others could be parenthesis or something similar.
Re: Safety of staticRead and StaticExec?
I don't known much about nim and compilation process. I known compilation same C code on similar machine with similar system can produce different executable, so there's reproductive compilation Debian project. But if nimble could install dependencies, is there possible to write nim compilation sandbox. It could use bumblewrap under GNU/Linux.
Safety of staticRead and StaticExec?
What can do staticRead and staticExec? Are there exist any restrictions? Is there a way to restrict behavior, by (for example) passing command line argument to compiler? I mean passing list of regexp. If programmer pass list of regexp and staticRead (or staticExec) parameter don't match to any given regexp, compilation fails before invoking one of these function. Is good idea to allow to put possible commands/files to .nimble files, so user/programmer could read, which commands and which file access are needed to be able to compile a program. Maybe someone could write GUI tool to read these regexp, search filesystem for files and present it (regexp for files/commands and file names matching to files regexps) + manual page for matching items.
Re: beginners tutorial
I porting now my program from C to nim. I plan to describe my journey with separated article. Currently I have worried about calculating offset/index of item in sequence (sequence is something rather similar to array or an list?). I have found an solution on this forum, but I doubt it works any time. It is truth item could be placed descending or ascending, but with the same first element array? I mean a[0].addr = 2, a[1].addr = 1 or a[0].addr = 1, a[1].addr = 2?
Re: Integrate NIM with C based project.
I build Progress: Call to Power with nimble. Currently only change is use some pragmas, change main function name in C files to something else and add code, that calls this function. I will wrote articles how to convert such projects. I only partially test. I must start rewriting some part of code to nim.
Re: Integrate NIM with C based project.
I just discovered NIM doesn't append a file extension to my library. -l linker argument gives linker name by omitting lib prefix and so extension. These are append automatically and linker will search lib names by matching it to result of mentioned operations. How to force nimc to generate file with .so extension?
Re: Translate syntax to some of natural language?
I don't think about something similar to symbolic execution. We don't check any possibly value, etc. We only show diagram to user (with descriptions).
Re: link with dynlib contains main
First problem are solved, but... main function of libraries written in NIM wasn't called. I use -l="-Lpath_relative_to_main_project_dir -llib_name" (Of course, I skip extension and lib prefix). What I must said: When using --clib, NIM append directory path of file currently compiled, so in result I've got /home/slawomir/postep/src/executables/path_realtive_to_main_dir/lib_name I decided to create function called a just like this: proc a {.exportc, cdecl.} = echo "a" And import it just like this: proc a {.importc, cdecl.} But have this error, when calling /home/postep/src/executables/postep.nim(8, 12) Error: expression 'a()' has no type (or is ambiguous)| ---|--- I call this function in this way: discard a()
link with dynlib contains main
Dynamic Load Libraries/Shared Objects under GNU/Linux and Windows can contain something like main procedure from C. I will call it start point. I compile shared object and now try to link it with my program. This shared object only contains echo instruction for testing purpose. How to link it? I tries to use --clib="libcommon.so", but it searches it in current directory - placement, where executable file exist. When I modify path to point to library (path is correct), linker told me file not exists.
Re: Integrate NIM with C based project.
I got first step forward. I will update this thread for any other nim/nimble beginners. What I doing wrong? First impression I must put different configuration of compiler with relating submodules to separate directories was good, but I don't known how to order nimble to build more modules. My mind just skip bin property in .nimble file was array. I added path to other modules relative to srcDir. Also I must repair syntax of nimble file. Missfortunately, path was os-dependent. I attach files here: 1\. $PROJECTDIR/postep.nimble > # Package > > version = "0.0.1" author = "Sławomir Lach" description = "Nimble version of > postęp - game based on Freeciv" license = "GPL-2.0" srcDir = "src" installExt > = @["nim"] bin = @["postep", "posteppkg/common/libcommon"] > > # Dependencies > > requires "nim >= 1.0.0" 2\. App compiler config file ($PROJECTDIR/nim.cfg) > \--gc="refc"| > ---|--- 3\. Library compiler config file ($PROJECTDIR/src/posteppkg/common/nim.cfg) > \--cincludes="../../c-files/common"| > ---|--- > \--clibdir="../../c-files/common"| > \--app="lib"| > \--out="./libcommon"|
Re: Integrate NIM with C based project.
Sorry I'm answering in two post. My game is opensource. Sources are accessible on sourceforge.net. Game is called progress call to power. There is one problem. I must check I upload newest sources. Not everything working, so I won't mess up in repository.
Re: Integrate NIM with C based project.
Re: up. Thanks for your response, but this doesn't solve my problem. Your told how to debug memory problems with nim app. You don't tell me how to connect C code with nim or how to solve memory problem in C code.
Integrate NIM with C based project.
I tries to rewrite my project, called progress: call to power (game based on Freeciv) to nim, because I have memory problems (heap overlaps stack, so one function jump outside one's bound). I tries different approach. First was adding nim.cfg given bellow: cincludes:../c-files/common clibdir:../c-files/common app:lib output:./libcommon But it doesn't work. Next try was by write {.compiletime.} procedure. I use walkDir from us module with pattern *.c, but I cannot use compile with variable, because this variable doesn't been known at compile time (I called my function from normal code, so probably there's a problem - is there any way to call it at compile time?)
Rewrite daemonic CMS to NIM?
I asks is there any reason to rewrite my project, called daemonic cms to NIM. Daemonic CMS is very simple framework to create web pages. In future I will add simple CMS to it. Daemonic CMS uses PIB (PHP In Browser - project of somebody else, which is an compiled version of PHP to webassembly). The reason to rewrite is Apache Server with PHP works different than NodeJS. Php script starts and better if it stops execution. Node is started and do everything in on process, as far as I known. I need to send data to browser in series, because I thought about new possibilities, like web game programming. Exists some NIM web server, which works as NodeJS? Because NIM is compiled language, maybe in future, instead provide compiled to webassembly nim interpreter, I will compile and cache page code, which should been interpret in browser and send it as webassembly? It is crazy and allows, of course, use cryptocurrency mining on visitor browser. It's bad, but it's only an side effect.
Re: Translate syntax to some of natural language?
Ok. I will went this direction. Many years ago I started partnership project, which is a computer program and command database with commands description and it's insecurity level. Also I have similar database for files. The project was abandoned, meanwhile explain shell appear. I now thinking about write similar to progress/explain shell reverse engineering tool to create graph of bash script. It will translate bash script to graph with human readable description of this script. It will save current directory and when it find ls * (for example), it will put rectangle and text "ls *; will list each file in /etc/" (for example). For pipe it will create tree. For example ls * | grep *.png gives "ls * | grep .png: it filter each element containing .png string for: list of each file in /home/I" .
[Maybe a new feature] Hook free function pointer.
Nim, as far as I known, uses GC, but can use C headers to generate bindings. C and C++ doesn't use GC, so there could be some weird situations, when C/C++ library free an memory and our program store pointer to this memory somewhere. Few years ago I had similar problem and solve it. I use free wrapper, iterate through list stores real memory pointers and replace pointer in this list to 0, where it was matched. When GC ran, it iterate on this list and free elements with NULL pointer. Solution for Nim could been similar. Just add new pointer type. It will be wrapper of normal pointer. To create my pointer type, programmer might specify free function and it will be injected or something at runtime (program start). When program tries to assign normal pointer to my pointer type, we iterate on wrappers list and check it exist currently. If not, we wrap it and create a new list element. When assigning my pointer type var to variable of the same type, we copy address to a wrapper. When program uses my pointer type var, we check there exist NULL value. If it's true we will throw an exception.
Re: One of my old programming language
I never used TCL, sorry. It works with template system, when in GUI/CUI mode. I use language like XSLT, but better fits with my needs. Application request to activate action /action/ok and it activate templates /action/ _. Inside one of /action/_ (or /action/ok) template we generate button, attaching callback given by app to button in this template and set text to ok (last-part-of-path). When app request to change text of ok button, it can do this by special API of course. Very positive aspect is, libgreattao can generate gui in many ways. Another style (style could be selected by user), we generate dropdown list with options like /actions/ok, /action/cancel, etc. and two buttons: (1) process button to process event related to selected option and (2) cancel, with will be generated only if /actions/cancel is requested. We could also set template priority, so /actions/cancel wouldn't be provided in dropdown list, but only near process button.
One of my old programming language
What do you think about one of my, but very old programming language. It's a scripting language, but invoked by special library. I called this library kind: interaction library, because it's automatically detects best way to interact with user and handle all interaction manner. Program (for example) telling: i need view class /desktop/dialogs/message_dialog and I support /message and actions /action/ok, /action/cancel. Program also told: set message to "what would you do?" and my library handles everything. I telling you this, because one of mode of running programs use my library is shell mode, where user/programmer could interact with view by writing commands or text script is started based on parameters given to program, but my library contains builtin web server and can work with user in cui/gtk+2.0 or QT mode. Description of shell language supports by my library: [https://www.dobreprogramy.pl/Libgreattao-i-jego-tekstowa-powloka,Blog,63527.html](https://www.dobreprogramy.pl/Libgreattao-i-jego-tekstowa-powloka,Blog,63527.html) (in Polish) [https://www.dobreprogramy.pl/Libgreattao-i-jego-tekstowa-powloka,Blog,63527.html](https://www.dobreprogramy.pl/Libgreattao-i-jego-tekstowa-powloka,Blog,63527.html) (in English) The language looks like an assembler, but it is very flexible. For example, you could define block of code, using block statement, put compare instruction at beginning of block, put other instruction and .false label at the end of block. If condition in compare instruction wasn't true, shell will jump to .false label. You think, so how to handle or? There's solution: you can jump to label in block up on stack, event it's placed in different procedure body, but that's not advised. How it work? [code] function OR =text i 0 block test_first_true ifintless $i $.unnamed_param_count =add i $i 1 =fromdict command $.unnamed_params $i block test execute $command scopebyname OR return : .false endblock test continue : .false scopebyname OR scopelevel -1 goto .false endblock test_first_true endblock [/code] I known it looking bad, but I understood this code in one minute and I wrote it long time ago. Magic. And of course - I think core of language should be as small as possible. Everything else we could write in my language itself! What do you think?
Translate syntax to some of natural language?
I decided to learn nim and do funny things. As I remembered, long time ago, programming language (scientist?) had to translate syntax of some languages to use some other natural language than English. This would be especially useful for children (not each child must known English, but learning computer programming is like learning math - you must think). I decided to start with Polish, because I live in Poland. I known nim have great macro system, which allows to near define own syntax. I need some help: 1. Some advice/template macro, which could I extend? 2. How to change statement to not have special meaning, so nim treads it as (for example) variable name or how to disable nim statements? 3. How to pass command line argument to include file with given path - I need this, because include/etc. is an English word and I think we shouldn't force user to use it I known people currently doesn't translate syntax (decided it's stupid think), but I think it's best way to learn nim possibilities, especially to generate config files from source for example and vice-versa ;-) .