State machines example : echo-server

2022-10-15 Thread dee0xeed
> But what about Rust, Odin, Crystal, Val, C++, Carbon, Swift, Go, Julia? It was a joke, wasn't it?

State machines example : echo-server

2022-10-15 Thread dee0xeed
> if you want feedback on your code you're probably better off sharing it as a > GitHub repository Sorry for long delay, [now it's on github](https://github.com/dee0xeed/edsm-in-nim-demo). See also implementations in [C](https://github.com/dee0xeed/edsm-in-c-demo), [D](ht

Stack/heap addresses, confusion

2022-06-29 Thread dee0xeed
> I'm too lazy to really bother how you use addr Just print them, nothing more. BTW, is there some less fancy way to do that? > but your understanding is correct, things are on the stack unless it's ref. > Yes, really. In C example addresses of variables on the stack are **greater** then heap

Stack/heap addresses, confusion

2022-06-29 Thread dee0xeed
Here is simple C program which prints some addresses #include #include struct thing { int a; int b; }; int main(void) { struct thing t1, t2, *t3, *t4; printf(" t1 @ %p\n", &t1); printf(" t2 @ %p\n", &t2);

State machines example : echo-server

2022-06-29 Thread dee0xeed
oops... corrected picture

State machines example : echo-server

2022-06-29 Thread dee0xeed
Hello, I'd like to bring to the public my exercises with event driven states machines in nim-lang. General architecture of an application looks like this: SM == "State Machine" MD == "Message Dispatcher" MQ == "Message Queue" EC == "Event Capture" OS == "Operating Sy

Unexpected empty sequence

2022-06-25 Thread dee0xeed
> According to the style guide, yes. " _When naming types that come in value, pointer, and reference varieties, use a regular name for the variety that is to be used the most, and add a "Obj", "Ref", or "Ptr" suffix for the other varieties. If there is no single variety that will be used the mo

Unexpected empty sequence

2022-06-25 Thread dee0xeed
> In the 2nd case, obj is the name of a variable on the stack, with an address > (of an object allocated on > the heap) stored in it. You can view ref as > auto-managed pointers in other language. Ok. Then there is a question. Is doing like this type TheObject = object

Unexpected empty sequence

2022-06-25 Thread dee0xeed
> With Stage as a ref type instead, the seq is an array of pointers under the > hood So, if we have something like type SomeObj = object ... var someSeq = seq[SomeObj] Run then var obj = SomeObj() someSeq.add(obj) Run

Unexpected empty sequence

2022-06-25 Thread dee0xeed
> As you discovered, a pointer to a member of a seq can become invalid when the > seq is resized This remembered me one case that happened a lng time ago. I reallocated some array of some structs, but forgot to change some pointers, which pointed to the elements of that array. But! In case

Unexpected empty sequence

2022-06-25 Thread dee0xeed
> Just using a pointer that you get before changing seq length is unsafe Yes, yes, yes, now it's absolutely obvious for me, and I just take addresses of sequence elements after the sequence is filled completely and (in case of my code) will never change anymore.

Unexpected empty sequence

2022-06-24 Thread dee0xeed
> I was trying to implement epoll based event driven state machines engine in > nim-lang and Well... for the moment I have working example with timers, signals and reading from stdin. See [here](http://zed.karelia.ru/0/nim)

Unexpected empty sequence

2022-06-24 Thread dee0xeed
> See my edit for an answer without the continued ptr footguns. Wow. You wrote almost exactly the same code as I have, see: type Action = proc(me: ptr StageMachine, src: ptr StageMachine, data: pointer) EnterFunc = proc(me: ptr StageMachine) LeaveFunc = proc(me: ptr

Unexpected empty sequence

2022-06-24 Thread dee0xeed
> See my edit for an answer without the continued ptr footguns. Initially I tried to use refs everywhere, but... I've been studying nim for a couple of weeks only and after some struggling with compiler decided to try raw pointers > Nim has safer reference types for a reason, you don't need to

Unexpected empty sequence

2022-06-24 Thread dee0xeed
> I don't know how addStage is implemented so cannot be sure. It was: proc addStage*(me: var StageMachine, sn: string, ef: EnterFunc, lf: LeaveFunc): ptr Stage = let s = Stage(name: sn, enter: ef, leave: lf) me.stages.add(s) return me.stages[me.stages.high].addr

Unexpected empty sequence

2022-06-24 Thread dee0xeed
Seems like this. I changed addStage so that it returns nothing and then: sm.addStage("INIT", sm_init_enter, nil) sm.addStage("WORK", sm_work_enter, sm_work_leave) let init = me.stages[0].addr let work = me.stages[1].addr Run After this change everything is o

Unexpected empty sequence

2022-06-24 Thread dee0xeed
Hi, everybody! I was trying to implement epoll based event driven state machines engine in nim-lang and encountered somewhat strange problem with adding objects to a sequence. See comments in the code below: import edsm proc sm_init_enter(me: ptr StageMachine) = Mess