Git Product home page Git Product logo

rockstar-py's Introduction

rockstar-py

Python transpiler for the esoteric language Rockstar

Travis (.org) Codacy grade PyPI - Version PyPI - Status PyPI - Status PyPI - Python Version PyPI - Wheel GitHub top language Code Style - Black GitHub contributors GitHub stars GitHub issues GitHub code size in bytes GitHub Release Date GitHub commits since tagged version GitHub last commit License - MIT

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Installing

First, make sure you have installed a supported Python version (>= 3.6).

Now, the easiest way of installing rockstar-py is using pip:

pip install rockstar-py

(This may require sudo if you're installing globally on a *nix system.

You can also clone this project using git and install the package with setuptools:

git clone https://github.com/yyyyyyyan/rockstar-py.git
cd rockstar-py
python3 setup.py install

Usage

If you installed the package using pip or setuptools, you can simply run rockstar-py in the command line:

rockstar-py [-h] (-i INPUT | --stdin) [-o OUTPUT | --stdout | --exec] [-v]

Otherwise, you can run the transpiler from inside the rockstar-py folder by running Python with the rockstarpy package:

python3 rockstarpy [-h] (-i INPUT | --stdin) [-o OUTPUT | --stdout | --exec] [-v]

Call rockstar-py with the flag -h/--help to see a description of all options:

usage: rockstar-py [-h] (-i INPUT | --stdin) [-o OUTPUT | --stdout | --exec] [-v]

Python transpiler for the esoteric language Rockstar

optional arguments:
 -h, --help            show this help message and exit
 -i INPUT, --input INPUT
                       Input file (.rock)
 --stdin               Stream in stdin
 -o OUTPUT, --output OUTPUT
                       Output file (.py)
 --stdout              Stream to stdout
 --exec                Execute (without saving) the transpiled code
 -v, --version         Version

Examples

Just to make it more clear, some examples with the fizz.rock code.

Basic usage

> rockstar-py -i fizz.rock -o fizz.py
> ls
fizz.py  fizz.rock
> cat fizz.py
def Midnight(your_heart, your_soul):
   while your_heart >= your_soul: # this is a comment
       your_heart = your_heart - your_soul
   return your_heart
Desire = 100
my_world = False
Fire = 3 # i love comments
Hate = 5
while not my_world == Desire:
   my_world += 1
   if Midnight(my_world, Fire) == False and Midnight(my_world, Hate) == False:
       print("FizzBuzz!")
       continue
   if Midnight(my_world, Fire) == False:
       print("Fizz!")
       continue
   if Midnight(my_world, Hate) == False:
       print("Buzz!")
       continue
   print(my_world)

Using --stdout

> rockstar-py -i fizz.rock --stdout
def Midnight(your_heart, your_soul):
   while your_heart >= your_soul: # this is a comment
       your_heart = your_heart - your_soul
   return your_heart
Desire = 100
my_world = False
Fire = 3 # i love comments
Hate = 5
while not my_world == Desire:
   my_world += 1
   if Midnight(my_world, Fire) == False and Midnight(my_world, Hate) == False:
       print("FizzBuzz!")
       continue
   if Midnight(my_world, Fire) == False:
       print("Fizz!")
       continue
   if Midnight(my_world, Hate) == False:
       print("Buzz!")
       continue
   print(my_world)

Using --stdin

> rockstar-py --stdin -o fizz.py
Midnight takes your heart and your soul
While your heart is as high as your soul (this is a comment)
Put your heart without your soul into your heart

Give back your heart


Desire's a lovestruck ladykiller
My world is empty
Fire's ice (i love comments)
Hate is water
Until my world is Desire,
Build my world up
If Midnight taking my world, Fire is nothing and Midnight taking my world, Hate is nothing
Shout "FizzBuzz!"
Take it to the top

If Midnight taking my world, Fire is nothing
Shout "Fizz!"
Take it to the top

If Midnight taking my world, Hate is nothing
Say "Buzz!"
Take it to the top

Whisper my world
[Ctrl+D]
> ls
fizz.py  fizz.rock

Using --exec

> rockstar-py -i fizz.rock --exec
1
2
Fizz!
4
Buzz!
Fizz!
7
8
Fizz!
Buzz!
11
Fizz!
13
14
FizzBuzz!
16
17
Fizz!
19
Buzz!
Fizz!
22
23
Fizz!
Buzz!
26
Fizz!
28
29
FizzBuzz!
31
32
Fizz!
34
Buzz!
Fizz!
37
38
Fizz!
Buzz!
41
Fizz!
43
44
FizzBuzz!
46
47
Fizz!
49
Buzz!
Fizz!
52
53
Fizz!
Buzz!
56
Fizz!
58
59
FizzBuzz!
61
62
Fizz!
64
Buzz!
Fizz!
67
68
Fizz!
Buzz!
71
Fizz!
73
74
FizzBuzz!
76
77
Fizz!
79
Buzz!
Fizz!
82
83
Fizz!
Buzz!
86
Fizz!
88
89
FizzBuzz!
91
92
Fizz!
94
Buzz!
Fizz!
97
98
Fizz!
Buzz!

Contributing

The project has basically reached its end, but I'm still accepting pull requests that improve speed and legibility of the code.

Authors

Contributors

Huge thanks to everyone who is contribuing to this project. Check them out at Contributors!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Hat tip to dylanbeattie for creating Rockstar
  • The FizzBuzz example works well. If valid code doesn’t work, create an issue so I can get a look.
  • I’ll work on the readibility and organization of the code, would love suggestions on how/where to do that.
  • I'd also love help with the tests.

rockstar-py's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rockstar-py's Issues

Roll X into Y

the Statement "Roll X into Y" does not work.
Translates in python to either "Roll_X into Y" or "roll X into Y" (depending on capitalization of "Roll".

From the Rockstar docs: "Rockstar also supports a special roll x into y syntax for removing the first element from an array and assigning it to a variable"

Multi line Comments

due to the parenthetic nature of comments in rockstar comments can run across multiple lines such as

(
multi
line
comment
)

this can be fixed by replacing " ( " and " ) " with " """ "

Call to convert_code in main

Line 175, your call to convert_code is preceded by 'rockstar.', so it doesn't work.

origin:
rockstar.convert_code(rockstar_code, py_rockstar)

fix:
convert_code(rockstar_code, py_rockstar)

It's working !!

Can't import rockstar-py into other python code

Currently building some python code that uses rockstar transpiling (I actually have an almost practicable purpose for rockstar :) ).

I tried using the pip install but the "-" in rockstar-py is not valid in a python incude statement.

Copying the rockstar folder in to my project worked fine so, it is not the code, just the packaging that is the problem.

Blocks do not terminate

It seems that code blocks (function body, loop, condition body etc) are not terminated after a blank line.

Example:

While X is greater than 1
Say X

If X is greater than 1
Say X

Transpiles into:

while X > 1:
    print(X)
    
    if X > 1:
        print(X)

Correct output should be along the lines of:

while X > 1:
    print(X)
    
if X > 1:
    print(X)

See specification

Two number types

Right now the transpiler doesn't ensure correct number types.

Put 421 into my heart
Put 421.0 into my soul
Shout my soul over 2
Say my heart over 2

Results in

210.5
210

It should be either all int

210
210

or all float

210.5
210.5

rockstar-py: command not found

After pip3 install rockstar-py . I want to run this command rockstar-py --output rockstar.py -i lyrics.txt but I get this error
-bash: rockstar-py: command not found

Error upon using inputted value as number

After running a primality tester written in Rockstar, the generated file is erroring on this line (the_storm is in inputted value):
if the_storm <= a_pair * a_pair: TypeError: '<=' not supported between instances of 'str' and 'int'
This seems to be because Python is treating the_storm as a string instead of an int.

Python code insertion

It's currently extremely easy to insert arbitrary Python statements into the code, like x=1. For example, the code

for n in 1,2,3,4:Say n*5

Try it online!

Is parsed fine and is basically the same as the equivalent Python.

Python 3.7 support

@yanorestes I noticed you added 3.7 to the Travis config in 841b851 and then removed it after it failed in 935de25

A few weeks ago @alxwrd managed to get Python 3.7 working in Travis for ety-python here - we could use a similar solution for rockstar-py

use global keyword to allow assigning to a variable outside of function scope

Hello,

From rockstar spec:

If a variable is defined outside of a function, it is in global scope. Global scope variables are available everywhere below its first initialization. If a variable is defined inside of a function, it is in local scope. Local scope variables are available from their initialization until the end of the function they are defined in.

While within a function, if you write to a variable that has been defined in global scope, you write to that variable, you do not define a new local variable.

Currently with rockstar-py, we can read global variables but not reassign them from a function scope. For that, we need to add a global declaration: https://docs.python.org/3/reference/simple_stmts.html?highlight=global#the-global-statement

So the compiler has to list all variables declared as global (i.e. ident == 0), then add a global myVar line in each function body that refers to it.

I wish I could contribute to this but I'm still a beginner in Python. I picked rockstar-py because the repository looks active and I got good results so far with my rockstar project.

Functions not taking string literals

example:

Example takes test
Give back test

Say My Logf Ext taking "test"
File ".default.py", line 3
    print(My_Logf_Ext taking "test)
                           ^
SyntaxError: invalid syntax

transpiled code:

def Example(test):
    return test
print(My_Logf_Ext taking "test)

Accepting Invalid Variable Names

As you're currently the primary implementation of rockstar (good job!), I've noticed that the programs that are verified with rockstar-py often contain invalid variable names. For example:

Put "value" into value

shouldn't compile because "value" isn't a valid variable name (doesn't begin with 'a', 'an', etc. or a capital letter). If you change it to "value" it works fine:

Before
After

I also understand that if you want to accept invalid rockstar (something compilers do often) if you can make heads or tails of it that's perfectly fine too (I do it with variable name spacing), just wanted to make you aware of it.

Version 1.2.1 does not run - ImportError: cannot import name 'rockstar'

After a fresh install of Python 3.4 via package manager (onto CentOS 7), followed by pip3 and sudo pip3 install rockstar-py, running rockstar-py gives the following error:

[vagrant@adpc-com-039 Python-3.7.0]$ rockstar-py
Traceback (most recent call last):
  File "/usr/bin/rockstar-py", line 9, in <module>
    load_entry_point('rockstar-py==1.2.1', 'console_scripts', 'rockstar-py')()
  File "/usr/lib/python3.4/site-packages/pkg_resources/__init__.py", line 547, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2719, in load_entry_point
    return ep.load()
  File "/usr/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2379, in load
    return self.resolve()
  File "/usr/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2385, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/lib/python3.4/site-packages/rockstarpy/__init__.py", line 1, in <module>
    from . import rockstar
  File "/usr/lib/python3.4/site-packages/rockstarpy/rockstar.py", line 2, in <module>
    from . import command_line
  File "/usr/lib/python3.4/site-packages/rockstarpy/command_line.py", line 2, in <module>
    from . import rockstar
ImportError: cannot import name 'rockstar'

Package structure

Great work on the transpiler! 😄

This issue relates to the part of the README that says:

I'll work on the readibility and organization of the code, would love suggestions on how/where to do that.

Would rockstar-py be better structured as a package? E.g.:

  • Move rockstar.py to rockstar/__init__.py
  • Add a setup.py (setuptools) file, with console script entry point
  • Add rockstar/__main__.py for running as a module (e.g. python -m rockstar)

If you think this is a good idea, I'd be happy to make these changes and open a PR


I'm not sure if this is related to this repo not being structured as a package, but after pip installing rockstar-py and running rockstar-py, a ModuleNotFoundError is raised:

$ rockstar-py                   
Traceback (most recent call last):
  File "/home/james/.local/share/virtualenvs/test-wygyIHa6/bin/rockstar-py", line 7, in <module>
    from rockstarpy.command_line import main
  File "/home/james/.local/share/virtualenvs/test-wygyIHa6/lib/python3.6/site-packages/rockstarpy/__init__.py", line 1, in <module>
    from .rockstarpy import rockstar
ModuleNotFoundError: No module named 'rockstarpy.rockstarpy'

Rockstar-py fails concat number and string

Rockstar-py seems not to be able to compile 99-beer.rock (from rockstar/examples):

python3 -W ignore -m rockstarpy --output bottles.py --input $ROCKSTAR/examples/99_beers.rock

runs fine, but

python3 bottles.py 
Traceback (most recent call last):
  File "bottles.py", line 6, in <module>
    print(the_beers + your_heart)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Compound assignment support

Hi, there seems to be an issue with transpiling compound assignments:

X is 1
Let X be with 1
Say X

transpiles into

X = 1
X = + 1
print(X)

and prints 1.

Correct behaviour would be to transpile the second line into X += 1, making the final X value 2.

See specification

List arithmetic support

Hi, it would be awesome if rockstar-py supported list arithmetic (in other words, multiple expressions on the right side of the operator)

Example:

Let X be 1 with 2, 3, 4
Say X    (prints 10)

Could be transpiled to:

X = 10    # as result of 1 + 2 + 3 + 4
print(X)

See specification

Arithmetic expressions not working

For example

Tango takes Man and Woman
the music is a strife
My Life is Man with the music
My Life is My Life without Woman
Give back My Life

is evaluated to

def Tango(Man, Woman):
    the_music = 16
    My_Life = 3435
    My_Life = 2455
    return My_Life

Is this a bug or is it in accordance to Rockstar specs?

Also, comparison doesn't seem to be working fully as intended. I got If Your Heart is nothing, to transpile into if Your_Heart = 7:. It seems that in this case, the transpiler was confused by the comma, as If Your Heart is nothing transpiles correctly.
However, comparing two variables (eg. If Your Heart is Dying still doesn't work and transpiles as an assignment). For some reason, this works only if I use your heart instead of Your Heart (where this variable is an output of a function).

numeric literals

numeric literals are not supported as in the code

Limit is 100
Counter is 0
Fizz is 3
Buzz is 5

which generates the output

Limit = 0
Counter = 0
Fizz = 0
Buzz = 0

when it should generate the output

Limit = 100
Counter = 0
Fizz = 3
Buzz = 5

Aint doesn't work

This code should compile:

Listen to your heart

If your heart aint "rock"
Say "Pick rock, paper, or scissors"

But instead, I get this error:

File "output.py", line 2
    if your_heart aint "rock":
                    ^
SyntaxError: invalid syntax

How come aint doesn't work?

While Loops and If Statements

When compiled, while statements don't seem to terminate properly. Everything below the looped code just compiles inside the first while statement. Likewise with If statements; a subsequent If statement gets nested inside the first one. I tried leaving blank lines but all that seems to do is add blank lines to the compiled program.

Is this a bug or am I just doing something wrong?

transpiler transpiles true, right, yes, ok into true instead of True

I tried to build this package myself using this repo, I've wrote the following file:
rockstar111.rock:

My heart is ok (should be True)
Tommy says True that (intializes tommy to True that)
Shout My heart
Shout Tommy

I transpiled the file using the follwoing command:
rockstar-py --input rockstar-tut1.rock --stdout
output:

my_heart = true # should be True
Tommy = "True that " # intializes tommy to True that
print(my_heart)
print(Tommy)

Note: using pip install rocstar-py instead of this very repo I'm getting the right output.

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.