Git Product home page Git Product logo

jonasmuehlmann / mvpl Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 487 KB

The minimum viable programming language

License: MIT License

CMake 4.45% C++ 63.57% Shell 0.27% Makefile 29.86% TypeScript 0.02% GAP 0.10% ANTLR 1.73%
lexer parser parser-combinators abstract-syntax-tree tokenizer interpreter bytecode bytecode-interpreter virtual-machine programming-language optimizer from-scratch bytecode-generation register-machine

mvpl's Introduction

MVPL

The Minimum Viable Programming Language (MVPL) planned to be implemented through a custom lexer, parser, bytecode generator, bytecode interpreter and simple optimizer.

architecture

Components

Lexer

The lexer turns a program like this:

function main()
{
    return 0;
}

int a token stream like this:

{
  "token_stream": [
    {
      "value": "function",
      "type": "FUNCTION",
      "source_location_": {
        "line_start": 0,
        "col_start": 0,
        "line_end": 0,
        "col_end": 8
      }
    },
    {
      "value": "main",
      "type": "IDENTIFIER",
      "source_location_": {
        "line_start": 0,
        "col_start": 9,
        "line_end": 0,
        "col_end": 13
      }
    },
    {
      "value": "(",
      "type": "LPAREN",
      "source_location_": {
        "line_start": 0,
        "col_start": 13,
        "line_end": 0,
        "col_end": 14
      }
    },
    {
      "value": ")",
      "type": "RPAREN",
      "source_location_": {
        "line_start": 0,
        "col_start": 14,
        "line_end": 0,
        "col_end": 15
      }
    },
    {
      "value": "{",
      "type": "LBRACE",
      "source_location_": {
        "line_start": 1,
        "col_start": 0,
        "line_end": 1,
        "col_end": 1
      }
    },
    {
      "value": "return",
      "type": "RETURN",
      "source_location_": {
        "line_start": 2,
        "col_start": 4,
        "line_end": 2,
        "col_end": 10
      }
    },
    {
      "value": "0",
      "type": "LITERAL",
      "source_location_": {
        "line_start": 2,
        "col_start": 11,
        "line_end": 2,
        "col_end": 12
      }
    },
    {
      "value": ";",
      "type": "SEMICOLON",
      "source_location_": {
        "line_start": 2,
        "col_start": 12,
        "line_end": 2,
        "col_end": 13
      }
    },
    {
      "value": "}",
      "type": "RBRACE",
      "source_location_": {
        "line_start": 3,
        "col_start": 0,
        "line_end": 3,
        "col_end": 1
      }
    }
  ]
}

Parser

The parser is a recursive descent parser and uses parser combinators with backtracking, and a pratt parser for dealing with operator precedence.

The parser turns a token stream like this:

{
  "token_stream": [
    {
      "value": "function",
      "type": "FUNCTION",
      "source_location_": {
        "line_start": 0,
        "col_start": 0,
        "line_end": 0,
        "col_end": 8
      }
    },
    {
      "value": "main",
      "type": "IDENTIFIER",
      "source_location_": {
        "line_start": 0,
        "col_start": 9,
        "line_end": 0,
        "col_end": 13
      }
    },
    {
      "value": "(",
      "type": "LPAREN",
      "source_location_": {
        "line_start": 0,
        "col_start": 13,
        "line_end": 0,
        "col_end": 14
      }
    },
    {
      "value": ")",
      "type": "RPAREN",
      "source_location_": {
        "line_start": 0,
        "col_start": 14,
        "line_end": 0,
        "col_end": 15
      }
    },
    {
      "value": "{",
      "type": "LBRACE",
      "source_location_": {
        "line_start": 1,
        "col_start": 0,
        "line_end": 1,
        "col_end": 1
      }
    },
    {
      "value": "return",
      "type": "RETURN",
      "source_location_": {
        "line_start": 2,
        "col_start": 4,
        "line_end": 2,
        "col_end": 10
      }
    },
    {
      "value": "0",
      "type": "LITERAL",
      "source_location_": {
        "line_start": 2,
        "col_start": 11,
        "line_end": 2,
        "col_end": 12
      }
    },
    {
      "value": ";",
      "type": "SEMICOLON",
      "source_location_": {
        "line_start": 2,
        "col_start": 12,
        "line_end": 2,
        "col_end": 13
      }
    },
    {
      "value": "}",
      "type": "RBRACE",
      "source_location_": {
        "line_start": 3,
        "col_start": 0,
        "line_end": 3,
        "col_end": 1
      }
    }
  ]
}

into an absract syntax tree (AST) like this:

{
    "ast": {
        "type": "PROGRAM",
        "source_location_": {
            "line_start": 0,
            "col_start": 0,
            "line_end": 3,
            "col_end": 1
        },
        "globals": [
            {
                "type": "FUNC_DEF",
                "source_location_": {
                    "line_start": 0,
                    "col_start": 0,
                    "line_end": 3,
                    "col_end": 1
                },
                "signature": {
                    "type": "FUNC_SIGNATURE",
                    "source_location_": {
                        "line_start": 0,
                        "col_start": 9,
                        "line_end": 0,
                        "col_end": 15
                    },
                    "identifier": "main",
                    "parameter_list": {
                        "type": "PARAMETER_DEF",
                        "source_location_": {
                            "line_start": 0,
                            "col_start": 13,
                            "line_end": 0,
                            "col_end": 15
                        },
                        "parameter_list": []
                    }
                },
                "body": {
                    "type": "BLOCK",
                    "source_location_": {
                        "line_start": 1,
                        "col_start": 0,
                        "line_end": 3,
                        "col_end": 1
                    },
                    "statements": [
                        {
                            "type": "RETURN_STMT",
                            "source_location_": {
                                "line_start": 2,
                                "col_start": 4,
                                "line_end": 2,
                                "col_end": 13
                            },
                            "value": {
                                "token": "LITERAL",
                                "value": "0"
                            }
                        }
                    ]
                }
            }
        ]
    }
}

Roadmap

License

Copyright (C) 2021-2022 Jonas Muehlmann

The project is licensed under the terms of the MIT license, you can view it here.

mvpl's People

Contributors

jonasmuehlmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mvpl's Issues

Implement semantic analysis stage

  • Detect shadowing variables
  • Detect assignment to function/procedure
  • Detect call to variable
  • Detect evaluation of procedure
  • Detect use-before-definition
  • Detect use in wrong scope
  • Detect redifinitions
  • Detect missing or double main function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.