Skip to content

Instantly share code, notes, and snippets.

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 / LL-parser.js
Last active February 15, 2023 14:54
LL-parser
/**
* = LL parser =
*
* by Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
* 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.
*
@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 / 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 / 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 / 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 / LL1-parser-first-follow-sets.js
Last active March 27, 2024 07:24
LL(1) Parser. Parsing table, part 1: First and Follow sets.
/**
* LL(1) parser. Building parsing table, part 1: First and Follow sets.
*
* NOTICE: see full implementation in the Syntax tool, here:
* https://github.com/DmitrySoshnikov/syntax/blob/master/src/sets-generator.js
*
* by Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
* MIT Style License
*
* An LL(1)-parser is a top-down, fast predictive non-recursive parser,
@DmitrySoshnikov
DmitrySoshnikov / LL1-parsing-table.js
Last active February 15, 2023 14:55
LL(1) Parser. Parsing table, part 2: building the table from First and Follow sets.
/**
* Building LL(1) parsing table from First and Follow sets.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*
* This diff is a continuation of what we started in the previous two diffs:
*
* Dependencies:
*
@DmitrySoshnikov
DmitrySoshnikov / 1-python-defaults.py
Last active October 2, 2015 21:30
Python vs ECMAScript default params semantics
# Python
#
# Closures when they are used as default params, capture bindings
# from the environment one level above the class.
#
x = 1 # global env
def wrap():
@DmitrySoshnikov
DmitrySoshnikov / python-code-sharing.py
Created October 5, 2015 21:27
Python: compiled code sharing between several functions
# Compiled code sharing between
# several functions.
arr = []
for k in [1, 2, 3]:
arr.append(lambda: k)
print(arr[0].__code__ == arr[1].__code__) # True
print(arr[0].__code__.co_code) # bytecode