lrnjrnl

self-study notebook

Read this first

Objects from Functions: When “return” Overrides “new”

In my write-up on basic Javascript prototypal inheritance, I demonstrated this weird JavaScript overriding feature I noticed between new and return when using functions that can be both factory functions and constructors. My understanding was that the return keyword automatically overrides a new operator. Today I discovered that it’s only true if the return value is an object. If the return value is a primitive (string, boolean, number, null), the new operator overrides the return statement and the function becomes a constructor!

This script I wrote helped me figure it out on my own, but I just found the documentation on MDN right near the top of the new keyword page. It seems to be a weird language feature. Why didn’t JavaScript’s language designers choose to make return automatically override new regardless of whether or not the return value is primitive? Here’s the excerpt from the...

Continue reading →


Javascript’s Prototypal Inheritance: Level Zero

 What is prototypal inheritance?

In this post, I attempt to describe the components needed to understand the concept of prototypal inheritance in JavaScript and then proceed to ask deeper questions about their application and semantics. The deeper questions are answered with code examples.


This blog is not meant to be authoritative in any way. These are simply my notes distilled and organized. If you find errors or if you find the language ambiguous, I’m always looking for feedback and corrections. You can reach me via Twitter: @lrnjrnl


I like to learn abstract concepts by following a simple method:

  1. ask a basic question
  2. read about the topic
  3. verify reading with experiments
  4. repeat the process with deeper questions until there is enough information to encapsulate the concept in a project or document.

These are the five questions that help me understand the prototypal...

Continue reading →


Emulating Python’s Lexical Scope Semantics

 KA-BOOOOOOOOOM!

 That’s the sound of my head exploding.

“I slept on it. I won’t make any more changes to ex35. It was a learning exercise and I’m still learning from it, but the existing interpreter is hairy enough that I’d need to rewrite the entire thing from scratch. Instead, I’m going to start a new language, a baby BASIC, with the knowledge I gained from ex35.

Thanks for all the memories.”

That was earlier this morning. Draft saved. Then came the pivot. I dove back into LMPTHW ex35 for a quick hack. And hack I did. Problem solved. Surprisingly, it was easy. Only a few lines of the existing interpreter needed a rewrite.

In my previous solution to emulating Python’s lexical scope semantics, I believed that each function definition was bundled into a tuple with a copy of the environment’s symbol table. This meant that every time the function was called, I was in fact calling a...

Continue reading →


Designing an Interpreter

 The Concept

I’m building a toy version of Python based on PunyPy from Learn More Python The Hard Way by Zed Shaw. Mine shall be named QuasiPy. It’s barely a language at all; it has no control flow!

Despite its pitiful capabilities, QuasiPy has a few components in common with some real programming languages.

  • scanner (aka lexical analyzer) - splits code into a sequence of syntax tokens
  • parser (aka syntax analyzer) - identifies and nests language structures according to grammar rules
  • analyzer (aka semantic analyzer) - traverses the nested structure via ‘visitor’ functions to verify semantic rules (ie scope)
  • interpreter - traverses the nested structure to perform assignment, evaluate expressions, perform i/o etc.

The scanner scans the source code for lexemes (ordered groups of characters) which represent keywords, variables, operators, values and other elements of syntax...

Continue reading →


Why lrnjrnl

lrnjrnl is a study journal for my personal interests.

I’m writing an interpreter for a toy version of the Python programming language. It’s a parsing exercise from Learn More Python The Hard Way by Zed Shaw. It has kept me busy for the last few months because I am determined to implement a rudimentary form of lexical scope.

My primary resource has been the web. I have been recording my progress and my resources in a thread at forum.learncodethehardway.com. That thread reached 100 posts yesterday. That’s why I’m moving my journal to this web log.

Studying concepts by asking questions and performing tests often corrects or clarifies my misunderstandings, but sometimes the opposite will result! Asking too many questions and doing too much reading can confuse me and lead me to erroneous conclusions. Nevertheless, when it comes to learning, I believe the failures are equal in value to the...

Continue reading →