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

More Decks by Christopher Pitt

Other Decks in Programming


  1. Creating A Programming Language integer minutes = 45;

  2. Grammar

  3. 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
  4. Lexer

  5. Lexer → Breaks a code string into tokens /(integer)/ =

    ["type", "integer"] → Ignores code heirarchy ["if"], ["("], ["identifier", "x"], ["=="], ["number", "5"]... ["type", "integer"], ["identifier", "minutes"], ["assign"], ...
  6. Parser

  7. 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"]]]
  8. Abstract Syntax Tree

  9. Abstract Syntax Tree → Represents constructs in the compiler's language

    new While($conditionTokens, $bodyTokens) new Definition($typeToken, $identifierToken, $valueToken)
  10. Interpreter

  11. Interpreter → Calculates an immediate effect $foreach->affect($context) → Always accepts

    and returns a context $definition->applyToContext($context)
  12. Compiler

  13. 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)
  14. All together!

  15. 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;