News for hackers. Stuff that matters.
Re:Parsing S-expressions in C (2019) by outworlder on Thursday August 20, 2020 @04:20PM (#24228397)
I am scratching my head.

If we are parsing S-expressions... why not embed Scheme already? This way, not only you get an S-expression parser for 'free', you can easily manipulate S-expressions, you can add macros and so on. And you also get your scripting language out of this deal!

Something like Racket or Chicken (and possibly others) would work nicely. Chicken specifically has a very nice FFI - which you don't even have to use. As an experiment, I have asked Chicken's compiler to stop at the C code generation, added the generated code to my XCode project, and deployed the whole package to iPhone. Not even cross compilation shenanigans were required, since this was handled by XCode.

As a bonus*2, with a few lines of code, I opened a TCP listener, and fed straight to the REPL. Which meant that I could telnet and have a REPL, which could be used to replace functions on the fly and observe the results immediately, while the app was running.

They would have to rename the project at this point, though :)

Re:Parsing S-expressions in C (2019) by lurquer on Thursday August 20, 2020 @06:11PM (#24229621)
All you professional programmer-types seem to have forgotten that solving problems (even if they’ve been solved a thousand times before) is FUN.

I wrote a custom-made script parser... knowing full-well there were many libraries available. (For a goofy program I wrote available at cycell2d.com) Why? Because I enjoyed it!

Re:Parsing S-expressions in C (2019) by rumanator on Thursday August 20, 2020 @06:43PM (#24229878)
> If we are parsing S-expressions... why not embed Scheme already?

This sort of nonsense is a good example of how some projects die due to absurd feature creep that no one at all can possibly justify. Who in their right mind would ever want to embed a full blown scripting language if your usecase is... Open a file and input data?

If you want to input data following a document format, you parse the document format. That's it.

Re:Parsing S-expressions in C (2019) by Reelin on Thursday August 20, 2020 @09:41PM (#24231087)
> Who in their right mind would ever want to embed a full blown scripting language if your usecase is... Open a file and input data?

Because embedding a mature, well written dependency that fully satisfies all your requirements might well be easier from a technical standpoint. It's certainly likely to reduce your comparative future maintenance burden given that the upstream community is healthy and functional.

Re:Parsing S-expressions in C (2019) by rumanator on Friday August 21, 2020 @01:41AM (#24232319)
> Because embedding a mature, well written dependency

What? Are there no mature well written parsers?

Don't be ridiculous.

Re:Parsing S-expressions in C (2019) by Reelin on Friday August 21, 2020 @02:33AM (#24232592)
I thought the comparison was between embedding a Scheme or implementing your own sexp parser from scratch. Of course embedding a good sexp parser is also a valid option to consider, I'm not that dense.

Granted that in this particular case the point seems to have been the exercise of implementing the parser itself so embedding a dependency wasn't a valid approach to begin with.

Re:Parsing S-expressions in C (2019) by DonaldFisk on Thursday August 20, 2020 @08:52PM (#24230758)
At the top of the page, Ben writes that he's a "Functional Programming Enthusiast", so he'll at least have a vague familiarity with Scheme and similar languages, even if he doesn't use them.

S-expressions are a widely used standard data format and are not exclusive to Lisps. If this is just to read in configuration files, and the resulting data structures never change after they're read in, he can get away with using calloc instead of a garbage collector, and null-terminated char * C-style strings.

Lisps (including Scheme) have more heavyweight implementations of strings and symbols than Ben has here, with an extra level of indirection: a symbol's name slot points to a string struct, which contains a pointer into an array of chars and a length. He can get away with a null-terminated char * and a struct containing an enum to distinguish them. For Lisp programming, the extra complexity is essential but for reading a config file into a game written in C, it's accidental. I don't know what kind of game he's writing but presumably it isn't anything like Abuse or Crash Bandicoot.

In the first paragraph, he writes, "I turned away from XML because I'd rather be as library independent as possible." This implies that he might have considered embedding a Lisp but decided against it for the same reason.

Re:Parsing S-expressions in C (2019) by svvcfb1212 on Thursday August 20, 2020 @04:24PM (#24228454)
...and this is how we get unnecessarily bloated software with ungodly dependency chains and complex build systems.

Sometimes a purpose-built module that addresses the need and maintains simplicity is preferable over complexity in the interest of some future potential feature that nobody has asked for yet, and may never ask for.

Re:Parsing S-expressions in C (2019) by zem on Thursday August 20, 2020 @05:25PM (#24229192)
s7 scheme is pretty trivial to embed in a c project, it consists of an s7.c and s7.h file that you just add to your source tree, no other dependencies. https://ccrma.stanford.edu/software/snd/snd/s7.html
Re:Parsing S-expressions in C (2019) by Turing_Machine on Thursday August 20, 2020 @09:37PM (#24231065)
S7 is definitely the way to go if you want to embed Scheme with the absolute minimum of hassle.
Re:Parsing S-expressions in C (2019) by outworlder on Thursday August 20, 2020 @04:31PM (#24228555)
Those Scheme interpreters are tiny!

I can guarantee you that a game will need a scripting language. Better get that done already.

That's if you prefer Scheme. You could use Lua too.

Re:Parsing S-expressions in C (2019) by learc83 on Thursday August 20, 2020 @04:48PM (#24228762)
>I can guarantee you that a game will need a scripting language. Better get that done already.

Myself and thousands of people before and after me have written games without a scripting language.

Re:Parsing S-expressions in C (2019) by outworlder on Thursday August 20, 2020 @05:04PM (#24228951)
You _can_ write a game without any scripting language support whatsoever. You could also write the whole game in assembly if you so desired.

However, if every single time you want to change any piece of logic in your game, you have to do the edit/compile cycle, your development will be extremely slow. Especially if you compare with the ability to change code while the game is running.

Not having all the game logic in C or what have you makes the game easier to mod too.

Re:Parsing S-expressions in C (2019) by sarchertech on Thursday August 20, 2020 @05:37PM (#24229318)
>However, if every single time you want to change any piece of logic in your game, you have to do the edit/compile cycle, your development will be extremely slow. Especially if you compare with the ability to change code while the game is running.

1. I'm working on a game in Unity right now that takes about 10 seconds to compile and reload the assemblies for the scripting language (only about 2 seconds of that is actual compilation, the rest is time that would be applicable to an interpreted scripting language).

2. I've built games in C where the entire project took 4 seconds to compile. Incremental compilation and modular architecture can get that down to near instantaneous for most changes.

>Especially if you compare with the ability to change code while the game is running.

Take a look at handmade hero for an example of an architecture that allows you to hot-reload C code while the game is running.

>Not having all the game logic in C or what have you makes the game easier to mod too.

That's completely architecturally dependent. You can make a game easy to mod for non programmers by storing data in a human readable/editable format. If you want to make it easy for players to write code there's a lot more work that needs to be done than just using a scripting language. For most games worrying about allowing modders to easily edit code is premature optimization.

Re:Parsing S-expressions in C (2019) by svvcfb1212 on Thursday August 20, 2020 @06:15PM (#24229657)
> For most games worrying about allowing modders to easily edit code is premature optimization.

I was involved in a game project that died extremely quickly a few years ago. The project lead made every decision re: architecture, tooling, etc. somehow involve worrying about modding. As a result, we ended up with nothing to show since everything got derailed by a bizarre obsession by one person with modding before we even had anything to mod. I believe that person doesn't take lead roles on projects any longer...

Re:Parsing S-expressions in C (2019) by pengaru on Thursday August 20, 2020 @05:26PM (#24229203)
Fast iteration can be achieved with compiled game logic as well, it all comes down to how things are factored in your project.

An excellent example most with OpenGL experience may be familiar with is supporting hot reloading of GLSL for fast vfx development iteration. It's a compiled language, it gets compiled at runtime without significant delay, and most of us making games have created simple tools to support live editing of GLSL, maybe even supporting it in-game for dev builds.

The demoscene has even turned it into a form of competitive performance art:

https://github.com/Gargaj/Bonzomatic

Edit:

Forgot to mention Casey also goes through support of hot-reloading game logic using C in the Handmade Hero series on YouTube.

Re:Parsing S-expressions in C (2019) by heavenlyblue on Friday August 21, 2020 @02:12PM (#24237392)
You could just expose the logic as DLLs though
Re:Parsing S-expressions in C (2019) by pengaru on Thursday August 20, 2020 @04:52PM (#24228804)
> I can guarantee you that a game will need a scripting language. Better get that done already.

Is that really so certain?

http://www.youtube.com/watch?v=y2Wmz15aXk0

Re:Parsing S-expressions in C (2019) by pansa2 on Thursday August 20, 2020 @07:04PM (#24230040)
> I can guarantee you that a game will need a scripting language.

Embedding a scripting language (e.g. Lua) seemed to be very common around 2010, but I get the feeling that it's much less common today.

Has the use of embedded scripting languages in games declined over the last 10 years? Any particular reasons?

Re:Parsing S-expressions in C (2019) by dfox on Thursday August 20, 2020 @05:29PM (#24229234)
The issue with scripting engine for game is that for game your almost certainly need scripting engine with completely different semantics than whatever existing implementation will provide. Ie. you either need to save the state or replicate it over network, both with reasonable performance. And looking at various AAA games (from modding standpoint) there is embedded Lua or Python that mostly only changes state of some lower level VM, which more often that not is itself turing-complete.
Re:Parsing S-expressions in C (2019) by samatman on Thursday August 20, 2020 @05:00PM (#24228910)
It wouldn't be a C project if it didn't embody Greenspun's Tenth Rule.
Re:Parsing S-expressions in C (2019) by vrnvu on Friday August 21, 2020 @01:59AM (#24232410)
Let me google the rule for those of you reading.

"Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp."

Re:Parsing S-expressions in C (2019) by platinumrad on Thursday August 20, 2020 @05:17PM (#24229099)
Because he needs to parse some config files, not embed a second language.
Re:Parsing S-expressions in C (2019) by grumbel on Friday August 21, 2020 @05:34AM (#24233431)
Turing-completeness is not just a benefit, it also puts a huge maintenance cost on you. An S-expression is something you can edit in your level editor, process with a script or do a lot of fun stuff with. It's just data. You can even convert it to JSON, XML or binary if you ever get tired of S-expressions.

Once you are no longer dealing with data, but with actual Scheme code that is no longer the case and it becomes virtually impossible to do anything with the code other than directly editing it in a text editor. It's extremely inflexible and every tiny API change will break everything with no automatic way to fix it.

Re:Parsing S-expressions in C (2019) by tgb on Thursday August 20, 2020 @05:38PM (#24229324)
The author does say the following:

> I'd rather be as library independent as possible, ideally with my SDL being my only cross-platform dependency to worry about.

Re:Parsing S-expressions in C (2019) by unoti on Friday August 21, 2020 @10:24AM (#24235117)
> If we are parsing S-expressions... why not embed Scheme already?

I agree. I just thought the goal of the author was to produce a game, not to parse S expressions. It’s possible that parsing S expressions is the most direct route to get there. But there may be more direct and simple ways to get there.

Re:Parsing S-expressions in C (2019) by lc9er on Thursday August 20, 2020 @08:17PM (#24230535)
> I could telnet and have a REPL, which could be used to replace functions on the fly

I thought you had to restart a scheme’s REPL. Is that just a limitation of Racket?

Re:Parsing S-expressions in C (2019) by codemac on Thursday August 20, 2020 @04:23PM (#24228433)
guile for embedding works really nicely.
Re:Parsing S-expressions in C (2019) by outworlder on Thursday August 20, 2020 @04:35PM (#24228605)
Sorry, I keep forgetting about Guile. That thing needs more love indeed.
Re:Parsing S-expressions in C (2019) by grugagag on Thursday August 20, 2020 @04:41PM (#24228682)
As a newbie im getting a bit confused. What is the difference between guile and chicken scheme? There's also chez. Im even more confused
Re:Parsing S-expressions in C (2019) by Jtsummers on Thursday August 20, 2020 @05:21PM (#24229136)
They're each different implementations of Scheme, sometimes with different design goals (like embedding or scripting or for standalone programs). Scheme is a pretty simple language (in comparison to many others) and relatively small. There are also several versions (not just implementations). Common ones out there now are R5RS, R6RS (it was considered "big" and was somewhat unpopular), and R7RS. Consequently, different implementations are also conforming to different versions of the spec.

Really, if you're wanting to learn Scheme, just pick one and run with it. For learning purposes they're mostly equivalent if you stick to the more popular ones, but be aware of your learning material and the version of Scheme that they're using.

If you're familiar with other programming languages, it's somewhat like the relationship between different compilers. Though usually they adhere a bit more strictly to the same standards. Since Scheme is such a small language, there's more room for differences between implementations. This mostly doesn't affect you as a learner (most learning materials focus on the core parts of the language which all implementations should be consistent on).

Re:Parsing S-expressions in C (2019) by grugagag on Saturday August 22, 2020 @11:42AM (#24244835)
Thank a lot, I decided to give Racket a go. It has a fancy IDE and is battery included
Re:Parsing S-expressions in C (2019) by Jtsummers on Saturday August 22, 2020 @07:51PM (#24248273)
Cool. Racket's a great option, and it's really a multi-lingual environment. Technically not Scheme anymore, but compatible with it in most ways. So any learning material for Scheme will still apply, and you can select different Scheme versions within Racket if the Racket language itself is incompatible with whatever book or similar you're using.
Re:Parsing S-expressions in C (2019) by cycloptic on Thursday August 20, 2020 @06:09PM (#24229604)
There is a basic explanation that was written by the guile maintainer in 2013: https://www.wingolog.org/archives/2013/01/07/an-opinionated-...
Re:Parsing S-expressions in C (2019) by outworlder on Thursday August 20, 2020 @05:08PM (#24229001)
These are different implementations. The language spec they follow is slightly different so they are not completely compatible (although I've been able to use some OpenGL-ES Racket code with no modifications in Chicken by using macros).

Where they will differ the most is things like the packaging system (if they have packages, not all Scheme implementations will), as well as their main use-case.

If you are interested, just pick one and start to learn Scheme, using any implementation. Their differences will become apparent quickly.

Re:Parsing S-expressions in C (2019) by grugagag on Thursday August 20, 2020 @05:21PM (#24229145)
Thank you. Quite a few options out there in terms of LISPS and a beginner like me can be quite confused. I started looking at SBCL, Chicken Scheme and Racket and since I'm a newbie I'll stick with racket for the learning phase. I seem to like more the uniformity of Scheme over the cl for example. Do people usually specialize in one or the other or use them interchangeably depending on libraries and such? Another slight barrier has been learning Emacs and that's another reason I chose racket. I like what Emacs is capable but I most likely revisit later.
Re:Parsing S-expressions in C (2019) by minerjoe on Thursday August 20, 2020 @09:39PM (#24231078)
Racket can be a good first choice. I would nudge you, if I may, and if the lisp glove fits, into taking the deep dive into

  emacs + evil (optional) + paredit + SLIME/SLY + SBCL + Quicklisp
There are fully configured starting setups out there for a quick start. That's basically the closest thing nowadays you'll get to experience the interactive power of the ol' Lisp Machines.

That said, you might not always need such large runtimes and other lisps can fit the bill. For example, I usually program in CL (with C FFI) for most projects, but for embedding lisps into existing C programs, Guile can be a joy. Once I was programming servo motor controllers that offered C as one of the programming languages, and I embedded a single C file that gave me a tiny little scheme interpreter and I was then able to prototype motor control algorithms and setting in way less time than before. It's just a game changer for learning about a system when you can get a REPL on it and start rapidly digging and experimenting.

Re:Parsing S-expressions in C (2019) by grugagag on Saturday August 22, 2020 @11:45AM (#24244860)
Thanks. I tried emacs+slime+quicklisp on SBCL and it looks interesting but the learning curve is insane, for now I just want to learn some scheme and take it from there. Racket has some interesting libraries to tot with so Im really excited.
Re:Parsing S-expressions in C (2019) by grugagag on Thursday August 20, 2020 @04:39PM (#24228652)
Yes, preferably chicken scheme as the compiled c code is snappier. I was just looking at chicken scheme on a pocketchip, admittedly I am just just scratching the surface with lisps but this is so cool. For learning I find racket to be quite good but chicken seems a lot better at performance. How long have you been a lisper? Newbie here