Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / using-calc-parser.php
Last active October 18, 2016 10:14
using-calc-parser.php
<?php
require('CalcParser.php');
var_dump(CalcParser::parse('2 + 2 * 2'));
/*
Result:
@DmitrySoshnikov
DmitrySoshnikov / calculator.ast.php.js
Created October 18, 2016 10:12
calculator.ast.php.js
/**
* Calculator grammar for a parser in Python.
*
* syntax-cli -g calculator.ast.php.js -m LALR1 -o CalcParser.php
*
* <?php
*
* require('CalcParser.php');
* var_dump(CalcParser::parse('2 + 2 * 2'));
*/
@DmitrySoshnikov
DmitrySoshnikov / using-calc-parser.js
Created October 18, 2016 10:05
using-calc-parser.js
const CalcParser = require('./CalcParser.js');
console.log(CalcParser.parse('2 + 2 * 2')); // AST
/*
Result:
{
type: "Binary",
left: {
@DmitrySoshnikov
DmitrySoshnikov / calculator.ast.js
Last active May 14, 2021 08:36
calculator.ast.js
/**
* Calculator grammar for a parser in JavaScript.
*
* syntax-cli -g calculator.ast.js -m LALR1 -o CalcParser.js
*
* const CalcParser = require('./CalcParser.js');
* console.log(CalcParser.parse('2 + 2 * 2')); // AST
*/
{
import calcparser
print(calcparser.parse('2 + 2 * 2')) # int(6)
@DmitrySoshnikov
DmitrySoshnikov / calculator.grammar.js
Last active October 23, 2016 04:33
Calculator grammar
/**
* Calculator grammar for a parser in Python.
*
* syntax-cli -g calculator.grammar.js -m LALR1 -o calcparser.py
*
* >>> import calcparser
* >>> calcparser.parse('2 + 2 * 2')
* >>> 6
*/
class Foo {
get x() {}
set x(v) {}
}
class Bar extends Foo {
x = 42;
}
console.log(new Bar().x); // undefined, should be 42
/**
* JS scoping, and this property resolutions rules in ES7/ES8.
*
* Quiz: x is gone, and x is everywhere!
*
* Help find Xs! What's the output?
*/
let x = 1;
@DmitrySoshnikov
DmitrySoshnikov / es6-list-negative-indices.js
Last active August 17, 2016 05:19
ES6 List negative indices
/**
* Reverse indices of Lists/Arrays in ES6.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*/
'use strict';
class List extends Array {
@DmitrySoshnikov
DmitrySoshnikov / expression-only.md
Last active April 16, 2016 12:00
ES: Expression only

"Everything is an expression"... since ES1?

Many languages support "expression-only" semantics, allowing to use any construct in an expression position.

NOTE: the difference between expressions and statements is that the former produce a value, while the later don't.

For example, Ruby's if-expression:

x = 10