Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / Generated-code-optimizations-and-tricks.md
Last active September 4, 2015 21:01
Generated code optimizations and tricks

Generated code optimizations and tricks

I've been playing with generated (native) code in order to test how compilers (specific or in general) translate a = a + 1 and a++, and found some other interesting optimizations. I used gcc compiler for this simple program:

int main() {
  int a = 0;
  a = a + 1;

  int b = 0;
@DmitrySoshnikov
DmitrySoshnikov / es2015-callable.js
Last active August 29, 2015 14:26
es2015-callable.js
/**
* Callable objects in ES2015.
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*/
class Callable extends Function {
constructor() {
super('(' + Callable.prototype.__call__ + ')();');
return this.bind(this);
@DmitrySoshnikov
DmitrySoshnikov / AST-printer.js
Last active August 29, 2015 14:25
AST Printer / Code generator
/**
* = AST printer =
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*
* Once we have finished operating on our AST, we need to generate the
* code back from that (possibly transformed) AST.
*
* We choose a very simple AST format here, which we used in the
@DmitrySoshnikov
DmitrySoshnikov / Recursive-descent-backtracking.js
Last active January 3, 2024 17:15
Recursive descent parser with simple backtracking
/**
* = Recursive descent parser =
*
* MIT Style License
* By Dmitry Soshnikov <[email protected]>
*
* In this short lecture we'll cover the basic (non-predictive, backtracking)
* recursive descent parsing algorithm.
*
* Recursive descent is an LL parser: scan from left to right, doing
@DmitrySoshnikov
DmitrySoshnikov / LL-parser.js
Last active February 15, 2023 14:54
LL-parser
/**
* = LL parser =
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style license
*
* Often one can see manually written LL parsers implemented as
* recursive descent. Approach in this diff is a classical parse table
* state machine.
*

ES7 String trim functions

String.prototype.trim ( )

Return result of StringTrim abstract operation passing this value as thisArg, and TrimBoth as the type.

String.prototype.trimRight ( )

Return result of StringTrim abstract operation passing this value as thisArg, and TrimRight as the type.

@DmitrySoshnikov
DmitrySoshnikov / shadow-hoisting-tdz.md
Last active August 29, 2015 14:15
Shadow, hoisting, TDZ

1. A system without hoisting

An example of Scheme program without hoisting:

(define x 10)     ; global `x`
(print x)         ; 10

((lambda ()
    (print x)     ; still 10, not shadowed yet
@DmitrySoshnikov
DmitrySoshnikov / stack-vm-recursion.js
Last active March 23, 2022 07:09
Educational Stack-based VM with recursion example
/**
* Educational Stack-based VM.
*
* See also:
* - A simplified example without recursion: https://gist.github.com/DmitrySoshnikov/76e1cfb930df8d36145c
* - Register-based VM example: https://gist.github.com/DmitrySoshnikov/6407781
*
* by Dmitry Soshnikov <[email protected]>
* http://dmitrysoshnikov.com
* MIT Stye License (C) 2015
@DmitrySoshnikov
DmitrySoshnikov / stack-vm.js
Last active April 29, 2024 01:56
Educational Stack-based Virtual Machine
/**
* Educational Stack-based VM.
*
* See also:
* - More complex example with recursion: https://gist.github.com/DmitrySoshnikov/afda459222e96e6002ac
* - Register-based VM example: https://gist.github.com/DmitrySoshnikov/6407781
*
* by Dmitry Soshnikov <[email protected]>
* http://dmitrysoshnikov.com
* MIT Stye License (C) 2015
@DmitrySoshnikov
DmitrySoshnikov / es6-delegating-generator.js
Last active April 7, 2018 12:31
ES6 delegating generator
/**
* ES6 delegating generators.
*
* ES6 dramatically simplified calling of nested generators
* using "yield *" construct, which is called delegating
* generator.
*
* From history:
* Previously (in the original SpiderMonkey/Python) implementation
* we had to maintain a manual stack for it. The technique was