{"id":9243927,"url":"https://github.com/pseudo-lang/pseudo-python","last_synced_at":"2025-08-17T08:33:45.251Z","repository":{"id":49982434,"uuid":"53290260","full_name":"pseudo-lang/pseudo-python","owner":"pseudo-lang","description":"a restricted python to javascript / c# / go / ruby  compiler","archived":false,"fork":false,"pushed_at":"2021-03-25T22:52:17.000Z","size":119,"stargazers_count":117,"open_issues_count":5,"forks_count":14,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-11-12T20:50:05.377Z","etag":null,"topics":["experiment","prototype","python-compiler","transpiler"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pseudo-lang.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-07T02:38:59.000Z","updated_at":"2024-05-28T16:18:55.000Z","dependencies_parsed_at":"2022-08-31T03:11:06.987Z","dependency_job_id":null,"html_url":"https://github.com/pseudo-lang/pseudo-python","commit_stats":null,"previous_names":["alehander42/pseudo-python"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pseudo-lang%2Fpseudo-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pseudo-lang%2Fpseudo-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pseudo-lang%2Fpseudo-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pseudo-lang%2Fpseudo-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pseudo-lang","download_url":"https://codeload.github.com/pseudo-lang/pseudo-python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230108747,"owners_count":18174540,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["experiment","prototype","python-compiler","transpiler"],"created_at":"2024-05-08T00:12:00.838Z","updated_at":"2024-12-17T11:30:35.560Z","avatar_url":"https://github.com/pseudo-lang.png","language":"Python","readme":"[![Build Status](https://travis-ci.org/alehander42/pseudo-python.svg?branch=master)](https://travis-ci.org/alehander42/pseudo-python)\n[![MIT License](http://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)\n\n# pseudo-python\n\nA restricted Python to idiomatic JavaScript / Ruby / Go / C# translator\n\n[Pseudo](https://github.com/alehander42/pseudo) is a framework for high level code generation: it is used by this compiler to translate a subset of Python to all Pseudo-supported languages\n\n**If you are using Python3.5 and you experience problems with an already installed version of pseudo-python, please upgrade it to `0.2.34` (`pip3 install pseudo-python --upgrade`)**\n\n\n# git history\n\nSorry: I lied, manipulating the git history in a way that it appears most of the work is after 1 march: I was finishing my work in a company in february and I wanted to hide the fact i've worked on pseudo in that time: I actually started more active work somewhere around 20-th february if I remember correctly, somewhere around the second half of february. I am not sure if i have that original git history anymore: sorry to Clevertech(the company)/any observers, this was a really ugly thing to do.\n\n\n## Supported subset\n\nPseudo-Python compiles to `pseudo ast`. \n\n[Pseudo](https://github.com/alehander42/pseudo) defines a language-independent AST model and an unified standard library.\nIt can map its own standard library to target language libraries and concepts automatically and it tries to generate readable and idiomatic code.\n\nPseudo-Python translates a subset of Python to Pseudo AST and then it receives the JS/Ruby/C#/Go backends for free. (+ at least 4-5 backends in the future)\n\nPseudo was inspired by the need to generate algorithms/code in different languages or portint tools/libraries to a new environment\n\nThat's why it can be mapped to a well defined subset of a language\n\nIt is meant as a framework consuming ast from parser generators / compilers / various tools and generating snippets / codebases in different target languages\n\n# Plan\n\n![a diagram illustrating the pseudo framework: compilers -\u003e ast -\u003e api translation -\u003e target code](http://i.imgur.com/7ySfy5j.jpg?2)\n\n# Pseudo supports\n\n  * basic types and collections and standard library methods for them\n  \n  * integer, float, string, boolean\n  * lists\n  * dicts\n  * sets\n  * tuples/structs(fixed length heterogeneous lists)\n  * fixed size arrays\n  * regular expressions\n\n  * functions with normal parameters (no default/keyword/vararg parameters)\n  * classes \n    * single inheritance\n    * polymorphism\n    * no dynamic instance variables\n    * basically a constructor + a collection of instance methods, no fancy metaprogramming etc supported\n\n  * exception-based error handling with support for custom exceptions\n  (target languages support return-based error handling too)\n  \n  * io: print/input, file read/write, system and subprocess commands\n\n  * iteration (for-in-range / for-each / iterating over several collections / while)\n  * conditionals (if / else if / else)\n  * standard math/logical operations\n\n## Installation\n\n\n```bash\npip install pseudo-python\n```\n\n## Usage\n\n```bash\npseudo-python \u003cfilename.py\u003e ruby\npseudo-python \u003cfilename.py\u003e csharp\n``` \netc for all the supported pseudo targets (javascript, c#, go, ruby and python)\n\n## examples\n\nEach example contains a detailed README and working translations to Python, JS, Ruby, Go and C#, generated by Pseudo\n\n[fibonacci](https://github.com/alehander42/pseudo/tree/master/examples/fib)\n\n[a football results processing command line tool](https://github.com/alehander42/pseudo/tree/master/examples/football)\n\n[a verbal expressions-like library ported to all the target languages](https://github.com/alehander42/pseudo/tree/master/examples/verbal_expressions)\n\n\n## Error messages\n\nA lot of work has been put into making pseudo-python error messages as clear and helpful as possible: they show the offending snippet of code and \noften they offer suggestions, list possible fixes or right/wrong ways to write something\n\n![Screenshot of error messages](http://i.imgur.com/Et3X9W1.png)\n\nBeware, pseudo and especially pseudo-python are still in early stage, so if there is anything weird going on, don't hesitate to submit an issue\n\n## Type inference\n\npseudo-python checks if your program is using a valid pseudo-translatable subset of Python, type checks it according to pseudo type rules and then generates a pseudo ast and passes it to pseudo for code generation.\n\n\nThe rules are relatively simple: currently pseudo-python infers everything\nfrom the usage of functions/classes, so has sufficient information when the program is calling/initializing all\nof its functions/classes (except for no-arg functions)\n\nOften you don't really need to do that for **all** of them, you just need to do it in a way that can create call graphs covering all of them  (e.g. often you'll have `a` calling `b` calling `x` and you only need to have an `a` invocation in your source)\n\nYou can also use type annotations. We are trying to respect existing Python3 type annotation conventions and currently pseudo-python recognizes `int`, `float`, `str`, `bool`, `List[\u003ctype\u003e]`, \n`Dict[\u003ckey-type\u003e, \u003cvalue-type\u003e]`, `Tuple[\u003ctype\u003e..]`, `Set[\u003ctype\u003e]` and `Callable[[\u003ctype\u003e..], \u003ctype\u003e]`\n\nBeware, you can't just annotate one param, if you provide type annotations for a function/method, pseudo-python expects type hints for all params and a return type\n\nVariables can't change their types, the equivalents for builtin types are\n```python\nlist :  List[@element_type] # generic\ndict:   Dictionary[@key_type @value_type] # generic\nset:    Set[@element_type] # generic\ntuple:  Array[@element_type] # for homogeneous tuples\n        Tuple[@element0_type, @element1_type..] # for heterogeneous tuples\nint:    Int\nfloat:  Float\nint/float: Number\nstr:    String\nbool:   Boolean\n```\n\nThere are several limitations which will probably be fixed in v0.3\n\nIf you initialize a variable/do first call to a function with a collection literal, it should have at least one element(that limitation will be until v0.3)\n\nAll attributes used in a class should be initialized in its `__init__`\n\nOther pseudo-tips:\n\n* Homogeneous tuples are converted to `pseudo` fixed length arrays and heterogeneous to `pseudo` tuples. [Pseudo](https://github.com/alehander42/pseudo) analyzes the tuples usage in the code and sometimes it translates them to classes/structs with meaningful names if the target language is `C#` `C++` or `Go` \n\n* Attributes that aren't called from other classes are translated as `private`, the other ones as `public`. The rule for methods is different:\n`_name` ones are only translated as `private`. That can be added as\nconfig option in the future\n\n* Multiple returns values are supported, but they are converted to `array`/`tuple`\n\n* Single inheritance is supported, `pseudo-python` supports polymorphism\nbut methods in children should accept the same types as their equivalents in the hierarchy (except `__init__`)\n\nThe easiest way to play with the type system is to just try several programs: `pseudo-python` errors should be enough to guide you, if not, \nyou can always open an issue\n\n## How does Pseudo work?\n\n\nThe implementation goal is to make the definitions of new supported languages  really clear and simple. \n\nIf you dive in, you'll find out\na lot of the code/api transformations are defined using a declarative dsl with rare ocassions \nof edge case handling helpers. \n\nThat has a lot of advantages:\n\n* Less bugs: the core transformation code is really generalized, it's reused as a dsl and its results are well tested\n\n* Easy to comprehend: it almost looks like a config file\n\n* Easy to add support for other languages: I was planning to support just python and c# in the initial version but it is so easy to add support for a language similar to the current supported ones, that I\nadded support for 4 more.\n\n* Easy to test: there is a simple test dsl too which helps all \nlanguage tests to share input examples [like that](pseudo/tests/test_ruby.py)\n\nHowever language translation is related to a lot of details and\na lot of little gotchas, tuning and refining some of them took days. Pseudo uses different abstractions to streamline the process and to reuse logic across languages.\n\n```ruby\nPSEUDO AST:\n   NORMAL CODE     PSEUDO STANDARD LIBRARY INVOCATIONS     \n      ||                    ||\n      ||                    ||\n      ||              API TRANSLATOR\n      ||                    ||\n      ||                    ||\n      ||                    \\/\n      ||              IDIOMATIC TARGET LANGUAGE \n      ||              STANDARD LIBRARY INVOCATIONS        \n      ||                    ||     \n      \\/                    \\/\n  STANDARD OR LANGUAGE-SPECIFIC MIDDLEWARES\n              e.g.\n    name camel_case/snake_case middleware\n    convert-tuples-to-classes middleware\n    convert-exception-based errors handling\n    to return-based error handling middleware\n              etc\n\n              ||\n              ||\n              ||\n              ||\n  TARGET LANGUAGE CODE GENERATOR\n\n      defined with a dsl aware\n      that handles formatting\n         automatically\n              ||\n              ||\n              ||\n              \\/\n\n            OUTPUT\n```\n\n\n## What's the difference between Pseudo and Haxe?\n\nThey might seem comparable at a first glance, but they have completely different goals.\n\nPseudo wants to generate readable code, ideally something that looks like a human wrote it/ported it\n\nPseudo doesn't use a target language runtime, it uses the target language standard library for everything (except for JS, but even there is uses `lodash` which is pretty popular and standard)\n\nPseudo's goal is to help with automated translation for cases\nlike algorithm generation, parser generation, refactoring, porting codebases etc. The fact that you can write compilers targetting Pseudo and receiver translation to many languages for free is just a happy accident\n\n\n## License\n\nCopyright © 2015 2016 [Alexander Ivanov](https://twitter.com/alehander42)\n\nDistributed under the MIT License.\n","funding_links":[],"categories":["Table of Contents"],"sub_categories":["Languages"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpseudo-lang%2Fpseudo-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpseudo-lang%2Fpseudo-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpseudo-lang%2Fpseudo-python/lists"}