Skip to content

๐Ÿš‚ An adventure with hand crafted parsing in an attempt to create a tiny, Turing incomplete toy language. Status: Work in progress ๐Ÿง

Notifications You must be signed in to change notification settings

maciejsmolinski/toy-lang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

49 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Toy Lang

An adventure with hand crafted parsing in an attempt to create a tiny, Turing incomplete toy language.

Syntax

Function definitions and function calls

fun average(a, b) {
  return (a + b) / 2
}

average(3.14, 2)

and that's it for now!

Run the project directly in your browser

Open and run the project via gitpod.io:

https://gitpod.io/#https://github.com/maciejsmolinski/toy-lang/blob/main/index.ts

Run the project locally

Clone the repository, check out the directory and install dependencies

git clone [email protected]:maciejsmolinski/toy-lang.git
cd toy-lang && npm install

Run the code and watch for file changes

npm run watch

The output printed on the screen

[INPUT]


  fun average(a, b) {
    return (a + b) / 2
  }

  average(3.14, 2)



[TOKENIZE:OUTPUT]

{ type: 'identifier', value: 'fun' }
{ type: 'identifier', value: 'average' }
{ type: 'leftParen' }
{ type: 'identifier', value: 'a' }
{ type: 'comma' }
{ type: 'identifier', value: 'b' }
{ type: 'rightParen' }
{ type: 'leftBrace' }
{ type: 'return' }
{ type: 'leftParen' }
{ type: 'identifier', value: 'a' }
{ type: 'operator', value: '+' }
{ type: 'identifier', value: 'b' }
{ type: 'rightParen' }
{ type: 'operator', value: '/' }
{ type: 'number', value: 2 }
{ type: 'rightBrace' }
{ type: 'identifier', value: 'average' }
{ type: 'leftParen' }
{ type: 'number', value: 3.14 }
{ type: 'comma' }
{ type: 'number', value: 2 }
{ type: 'rightParen' }


[PARSE:OUTPUT]

{
  "type": "Program",
  "body": [
    {
      "type": "FunctionDeclaration",
      "name": {
        "type": "Identifier",
        "name": "average"
      },
      "params": [
        {
          "type": "Identifier",
          "name": "a"
        },
        {
          "type": "Identifier",
          "name": "b"
        }
      ],
      "body": {
        "type": "BlockStatement",
        "body": [
          {
            "type": "ReturnStatement",
            "expr": {
              "type": "BinaryExpression",
              "left": {
                "type": "BinaryExpression",
                "left": {
                  "type": "Identifier",
                  "name": "a"
                },
                "operator": "+",
                "right": {
                  "type": "Identifier",
                  "name": "b"
                }
              },
              "operator": "/",
              "right": {
                "type": "NumericLiteral",
                "value": 2
              }
            }
          }
        ]
      }
    },
    {
      "type": "FunctionCall",
      "name": {
        "type": "Identifier",
        "name": "average"
      },
      "args": [
        {
          "type": "NumericLiteral",
          "value": 3.14
        },
        {
          "type": "NumericLiteral",
          "value": 2
        }
      ]
    }
  ]
}

About

๐Ÿš‚ An adventure with hand crafted parsing in an attempt to create a tiny, Turing incomplete toy language. Status: Work in progress ๐Ÿง

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published