Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
cosinekitty / planet_data.csv
Last active January 20, 2020 22:09
Table of planet data: masses compared to Earth, distance from Sun in Sun radii, and barycenter in Sun radii.
body mass distance barycenter
Sun 333054.25 0.0
Mercury 0.06 133.8 0.000022
Venus 0.82 250.0 0.000612
Earth 1.00 345.7 0.001038
Mars 0.11 525.5 0.000170
Jupiter 317.85 1797.9 1.714159
Saturn 95.17 3312.2 0.946171
Uranus 14.54 6638.3 0.289716
Neptune 17.15 10389.6 0.534930
@cosinekitty
cosinekitty / tokenizer.js
Created January 31, 2020 00:48
The Parser constructor does all the tokenizing
class Parser {
constructor(text) {
this.nextTokenIndex = 0;
this.tokenList = [];
const reToken = /[0-9]+(\.[0-9]*)?([eE][\+\-]?[0-9]+)?|[A-Za-z_][A-Za-z_0-9]*|\S/g;
for(;;) {
const match = reToken.exec(text);
if (match === null) {
break;
}
@cosinekitty
cosinekitty / token.js
Created January 31, 2020 00:50
The Token class represents each token in the input.
class Token {
constructor(text, index) {
this.text = text;
this.index = index;
// Classify the token.
if (/^[A-Za-z_]/.test(text)) {
this.kind = 'identifier';
} else if (/^[0-9]/.test(text)) {
this.kind = 'number';
@cosinekitty
cosinekitty / parse_expr.js
Created January 31, 2020 01:43
Parses the 'expr' rule in the BNF grammar.
ParseExpr() {
// expr ::= mulexpr { addop mulexpr }
let expr = this.ParseMulExpr();
let optoken;
while (optoken = this.NextTokenIs(['+', '-'])) {
const right = this.ParseMulExpr();
if (optoken.text === '+') {
expr = new Expression_Add(optoken, expr, right);
} else {
expr = new Expression_Subtract(optoken, expr, right);
@cosinekitty
cosinekitty / expression.js
Created January 31, 2020 01:54
Expressions all have an operator precedence, a token, and a list of arguments.
class Expression {
constructor(precedence, optoken, arglist) {
this.precedence = precedence;
this.optoken = optoken;
this.arglist = arglist;
}
// ...
}
@cosinekitty
cosinekitty / expression_function.js
Created January 31, 2020 02:12
Derived class Expression_Function handles function nodes in the parse tree.
class Expression_Function extends Expression {
constructor(token, arglist) {
super(9, token, arglist);
}
PrettyMath() {
switch (this.optoken.text) {
case 'sqrt':
return '\\sqrt{' + this.PrettyMath_SingleArg() + '}';
@cosinekitty
cosinekitty / pretty_binary.js
Created January 31, 2020 03:05
Shows optimizing parentheses in TeX output for a left-associative operator.
PrettyMath_Binary_LeftAssoc(opsymbol) {
let left = this.arglist[0].PrettyMath();
let right = this.arglist[1].PrettyMath();
// Use parentheses around the left child expression
// if its operator precedence is less than this node's precedence.
// If it is equal, assume left-associativity means parentheses are not needed.
if (this.arglist[0].precedence < this.precedence) {
left = '\\left(' + left + '\\right)';
}
@cosinekitty
cosinekitty / mandel_func.cpp
Created February 25, 2020 01:22
C++ implementation of the Mandelbrot Set function
static int Mandelbrot(double cr, double ci, int limit)
{
int count = 0;
double zr = 0.0;
double zi = 0.0;
double zr2 = 0.0;
double zi2 = 0.0;
while ((count < limit) && (zr2 + zi2 < 4.001))
{
double tzi = 2.0*zr*zi + ci;
@cosinekitty
cosinekitty / mandel_zoomframe.cpp
Created February 25, 2020 01:29
C++ function for generating a series of Mandelbrot zoom PNG files.
static int GenerateZoomFrames(const char *outdir, int numframes, double xcenter, double ycenter, double zoom)
{
try
{
// Create a video frame buffer with 720p resolution (1280x720).
const int width = 1280;
const int height = 720;
VideoFrame frame(width, height);
const int limit = 16000;
@cosinekitty
cosinekitty / VideoFrame.cpp
Created February 25, 2020 01:34
A reusable C++ class for generating an image and saving it as a PNG file via lodepng.
class VideoFrame
{
private:
int width;
int height;
std::vector<unsigned char> buffer;
public:
VideoFrame(int _width, int _height)
: width(_width)