This one is an oldie both in terms of its age and the programming language it uses.
ChaoSnake is a “yet another Snake game” written using nothing but GLFW and OpenGL in the Pascal programming language.
Monthly Archives: August 2011
Properties in the D programming language
Just to evangelize D a little and increase my code/crap ratio, let’s pretend we develop a library in C++ that contains this class:
class SomeMetaVariables { public: std::string foo; bool bar; }; // ... Somewhere in the client code: SomeMetaVariables baz; baz.foo = "foo"; baz.bar = true;
Our library is quite successful and many people are using SomeMetaVariables despite its obvious flaws.
Now, say we get many requests for additional functionality, for example:
“Make bar true only when foo is set to “foo” and other way arround.”
“Well, ok.” – we say and commit this new version of SomeMetaVariables:
class SomeMetaVariables { std::string foo; bool bar; public: std::string getFoo() { return foo; } std::string setFoo(std::string newFoo) { foo = newFoo; bar = (foo == "foo"); return foo; } bool getBar() { return bar; } bool setBar(bool newBar) { bar = newBar; foo = bar ? "foo" : ""; return bar; } };
We implemented the requested feature, but SomeMetaVariables‘ interface has changed…
“But why are you mad clients? You asked for it!” – cries the C++ developer.
Continue reading
ASM development log 0: The Game
It’s high time for more Another Syntactic Monstrosity examples, but first let’s take a look at its development.
devLog
I spent the last few days on redesigning the parsing strategy, ATM the interpreter reads the raw input, lexes it using static, immutable grammar specs and then parses the token stream into an AST, all this hardcoded in D.
This proves to be fast, but it’s nearly non-tweakable. That’s not what I intended ASM to be and it makes some neat features, such as overriding Tuple evaluator, impossible.
The new parsing strategy reminds of CommonLisp reader and reader-macros:
- The raw input is passed to the lexer (written in D) that uses dynamic syntax table – a set of regular expressions describing the syntax (defined in ASM) to tokenize the input into a token stream.
- Next, the token stream is passed to the parser (written in ASM with default implementation in D), that uses ASM functions, correlated to the syntax table, to dispatch the token stream and translate it to basic, lispy S-expressions (see LR parser for details on how it’s done).
This will allow ASM to act like any other language, given there’s the syntax table available, making it perfect for DSL programming.
For example:
(lambda (x y) (pow (+ x y) 2))
…might be written as:
[x y => (x + y)^2]
…and ASM will happily accept it. Awesome!
Continue reading
Accessing private class members in C++
I’ve been called Kajtek “MOTHERFU*KINGWALLOFTEXT” Rzepecki lately, so let’s make this post short.
We’ve got this code:
#include <iostream> class A { private: int bar; void foo() { std::cout << "In A::foo() " << bar << "\n"; } public: //... }; int main() { A* a = new A(); //... delete a; return 0; }
And we feel a sudden urge to call foo() or access bar. How do we do that?
Continue reading
