Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Creating a programming language

Creating a programming language

Programming is hard. Making programming languages is harder. Or so people think. The truth is that making a programming language is just a series of small steps, using regular language constructs. It's so easy, we'll make one in this talk.

Christopher Pitt

October 05, 2014
Tweet

More Decks by Christopher Pitt

Other Decks in Programming

Transcript

  1. Grammar → Rules which define structure/syntax sentence : subject verb

    object | subject verb → Each rule is called a lexeme sentence is a lexeme, subject is a lexeme etc. definition : type identifier = value | type identifier
  2. Lexer → Breaks a code string into tokens /(integer)/ =

    ["type", "integer"] → Ignores code heirarchy ["if"], ["("], ["identifier", "x"], ["=="], ["number", "5"]... ["type", "integer"], ["identifier", "minutes"], ["assign"], ...
  3. Parser → Arranges tokens into a heirarchy ["if", ["equals", [["identifier",

    "x"], ["number", "5"]]]]... → Checks tokens for syntax errors Parse error: parse error, expecting T_PAAMAYIM_NEKUDOTAYIM ["definition", [["type", "number"], ["identifier", "minutes"], ["number", "45"]]]
  4. Abstract Syntax Tree → Represents constructs in the compiler's language

    new While($conditionTokens, $bodyTokens) new Definition($typeToken, $identifierToken, $valueToken)
  5. Interpreter → Calculates an immediate effect $foreach->affect($context) → Always accepts

    and returns a context $definition->applyToContext($context)
  6. Compiler → Writes the Abstract Syntax Tree into a new

    language $compiler->render($switch) → Also checks nodes for syntax errors in target language $definition->renderWithContext($context)
  7. All together! → integer minutes = 45 → ["type", "integer"],

    ["identifier", "minutes"], ["assign"], ["number", "45"] → ["definition", [["type", "number"], ["identifier", "minutes"], ["number", "45"]]] → new Definition($typeToken, $identifierToken, $valueToken) → $definition->renderWithContext($context) $minutes = 45;