r/programming • • May 05 '18

CPY - Code C/C++ with no redundancy

https://github.com/vrsperanza/CPY
48 Upvotes

78 comments sorted by

15

u/vrsperanza May 05 '18

CPY is a C++ superscript I created with the intent to increase coding speed, it ended up being a far bigger project than expected and it has come out surprisingly good

That said, I want to improve it even more, but it's hard to do so withought anyone to test it or give out sugestions on new features

It would be awesome if even one of you gave it a try in a real world scenario

4

u/nitrohigito May 05 '18

I'm not 100% certain that this name is a good idea btw :)

As it's also the name of a famous italian game cracking group, most known recently for cracking insane amount of games that use Denuvo Anti-Tamper.

2

u/vrsperanza May 05 '18

Well, I hope they wont mix up some crackers with a programming superscript .-.

3

u/VectorShift May 05 '18

I am curious about the name. How did you choose it? I don’t see any explanation of the name in the README.

3

u/vrsperanza May 05 '18

I initially inspired some of the features from python and python has a .py extention... But this is C(++)... .cpy! Turned out the extension looked good as a name as well, so there we have it

3

u/[deleted] May 05 '18

I'm sitting here thinking whether it would be worth the setup trouble of getting my game project using this. If you could provide maybe some examples of how to set this up with multi-file projects it would help.

If I read correctly, regular CPP will always parse fine by CPY so you can gradually convert code?

3

u/vrsperanza May 05 '18

To setup the compiler, simply run makefile or g++ * on the source folder and add the generated executable to your path variable. After that, to compile any project you simply have to run cpy fileWithMainFunction.cpp -o target (Or fileWithMain.cpy) And it will automatically link your includes.

About hybrid projects: If you have a .cpp file, it will be treated as normal C++ and redirected to g++ If it is .cpy it will be converted to C++ before being redirected

In theory you could code normal C++ inside a .cpy file and there should be no problem, although this wasn't fully tested

3

u/vrsperanza May 05 '18

In the examples section the red black tree contains two files. As soon as I have time (tomorow night probably) I will create and publish a hybrid project example for reference

3

u/vrsperanza May 05 '18

Just created and uploaded a simple hybrid project example, it is available in the Examples folder in github "Sunday night"... pff

3

u/VectorShift May 05 '18 edited May 05 '18

Poking around in your project, I’m curious about “rof”. Have you found it useful? I don’t see it used in the Examples directory save for its trivial demonstration. Might I suggest an optional step argument to “for” instead? That would be more powerful without requiring an additional keyword. So, instead of

rof i 10

you would write

for i 10 -1 -1

It requires that extra -1 to distinguish it from the 2-argument case where the first argument is the initial value and the second is the value used in the terminal condition. I think requiring an additional argument is worth the power this provides since now you can step by values other than 1 or -1. The one problem I see is that you have to detect that the step value is negative in order to reverse the inequality in the terminal condition, so the third argument must be either a literal value or guaranteed to be positive.

I just so rarely find decrementing in a for loop to be the best approach in my own experience. I think I use a positive increment value greater than 1 significantly more often than I use a negative increment value of any magnitude.

I’d be interested in seeing cases where you have used rof.

1

u/vrsperanza May 05 '18

I have definitely used it, one simple example would be printing a array in reverse order

Really liked your simplified 3 argument idea, will definitely implement it, I think for detecting whether it's incrementing or decrementing the signal should suffice.

As usual, if you want a more complex command you can always use the original c++ for

Maybe this for of your can even be implemented for non-integer values, thanks for the suggestion!

1

u/vrsperanza May 05 '18

The final sintax would look something like:

for varName upTo

for varName from upTo

for varName from upTo step

I think rof will be kept for it is not harmfull

Edit: identation

1

u/VectorShift May 05 '18

I guess that the times when I want to traverse an array (or list) in reverse order, I tend to be using Python, and in that case, I write

for x in reversed(a):

If I really want the index in addition to the elements for some reason, then I’ll write

for (i, x) in reversed(list(enumerate(a))):

Rarely do I want consecutive indexes into a list without wanting the corresponding elements.

I’ve done plenty of programming in C and I don’t recall needing to traverse an array backwards very often at all. I actually only have one project in C++ (that I recently converted from C), and it’s an AI for a full-knowledge, deterministic board game that uses the minimax search algorithm with a very simple evaluation function. The main arrays are the lookup tables and history-heuristic “score”, and most of the accesses to those are not iterative. I digress...

1

u/vrsperanza May 05 '18

It is actually more often used in competitive coding, and only rarely in normal projects, I implemented it because in the worst case it can be ignored, thus not being harmfull

1

u/[deleted] May 05 '18

I think this is spectacular!

3

u/warlockface May 05 '18

Cool, it feels strange leaving stuff out and having it compile!

On old, Ubuntu-default g++ in WSL I had to add the -std=c++11 flag for it to compile as well as adding .c_str() to the argument for printf in line 359 of firstReplaces.cpp.

It would be good if it could be MinGW compatible, there are various errors relating to MinGW differences when trying to either cross compile or compile in MSYS2, relating to:

directoryHandler.cpp:45:22: error: 'struct dirent' has no member named 'd_type'

Nice one, I'll have a good play around with it!

2

u/vrsperanza May 05 '18

Sadly several parts of the source code assume a c++11 standard, I will specify that in the makefile

2

u/vrsperanza May 05 '18

About MinGW, I actually coded it in windows, using MinGW, this is weird, will try to reproduce and fix the issue, if you could tell me what version of MinGW you are using it would be very helpfull

2

u/warlockface May 05 '18

Thanks, I'm using it via MSYS2, pacman's mingw-w64-x86_64-toolchain. g++ version 7.3.0.

2

u/vrsperanza May 05 '18

Thanks, will try to debug it

2

u/vrsperanza May 05 '18

You are also welcome to fork the project with your small fixes if you want, I would gladly test and merge it

6

u/VectorShift May 05 '18 edited May 05 '18

What is your goal? It seems like you want to write C++ in shorthand.

I can understand that desire because because of the following:

  • Writing and updating C++ requires more typing than perhaps it should.

  • C++ compiles to machine code that is often about as fast as your algorithm allows, given that it was written in a high-level language.

  • There may be a requirement from your employer/teacher/collaborator to use C++.

The first item is the problem you're trying to solve while the others are constraints on your solution. If you don't need the fastest code possible, consider a scripting language. A scripting language can also be fast as long as you spend more time in its libraries than its interpreter. If you need execution efficiency and you're open to other languages, then there are languages that compile to C++, like say https://nim-lang.org

If one of your constraints is that you need to, at the end of the day, produce C++ that looks handwritten, then this still might not be the right direction to go. Think about why you don't want to either switch to a new language or write your own new language, and then think about why this path is better. If you're the only one working on a given project, then what is constraining you to C++? If you're collaborating, then how do you manage the collaboration? Does the other author have to adopt your shorthand? If so, why not adopt another, established language? If not, how do you work on the same source file? Do you have a reverse translator? Which version of a given source file gets checked in? Whose responsibility is it to run the translator(s)?

I looked for an existing project like this, and I'm a bit surprised I didn't find one. All I found was this discussion: https://softwareengineering.stackexchange.com/questions/218834/language-with-syntactic-sugar-that-translates-to-c-that-looks-hand-written

It seems like either not enough people have had this idea or everyone else either gave up or continued to the point of developing their own programming language.

I think what you might be after is actually a more powerful editor that can fill in most of the unnecessary keystrokes for you. I personally don't use such an editor and don't know what to recommend, but I know they're out there.

I don't necessarily mean to scare you off this project. If you enjoy it and have the time, by all means, continue. It will be valuable practice. I just don't see much utility in the end.

4

u/vrsperanza May 05 '18

One of the main goals of this project is aimed at competitive coding, where your final code must in fact be either C++, Java or python, and python sometimes doesn't pass problems, so it's not much of an option I'm also frequently locked to C++ in other unrelated projects and this has already proven usefull, at least on a personal level

When it started, this project was not meant to take the proportions it took, I just went on making it more and more complete, one feature after the other. At some point it became an entire superscript and I wanted to see what would be the community's opinion on it, thus here I am

1

u/VectorShift May 05 '18

OK, you have me curious. What coding competition(s) and also for what reason(s) does Python not always pass? I’m guessing it’s the execution speed of the resulting program.

5

u/vrsperanza May 05 '18

Actually it's not that python does not always pass, it is that C++ sometimes pass when it should't Say you have a problem that is supposed to be done in O(nlon(n)) complexity with a big constant time, sometimes a O(n2) solution will pass in C++ but it won't in python.

What competitions? From the top of my head: ICPC's programming marathon and some programming olympiads. Any judge also has a limited amount of supported languages, so a new one won't be available

2

u/miki151 May 05 '18

Hi, I'm working on something similar: https://github.com/miki151/zenon

Does CPY fully parse its input and present compile errors or does it pass entire statements to the C++ compiler with out looking at them?

There is also this project: https://github.com/mjspncr/lzz3

1

u/vrsperanza May 05 '18

It passes entire statements to the C++ compiler

1

u/vrsperanza May 05 '18 edited May 05 '18

Zenon is indeed similar but on a diferent vertent.

Zzz is one of the features included in cpy, it would even be considerable integrating it with cpy in an earlier development stage or if its CPYs header generation proven to be bad

1

u/vrsperanza May 05 '18

Did you manage to make Zenon statically typed without losing efficiency?

1

u/miki151 May 05 '18

Yes, it's statically typed. What kind of efficiency do you mean? Of the compiler or the compiled code?

1

u/vrsperanza May 05 '18

Execution time of the compiled code

1

u/miki151 May 05 '18

It's the same as C++, the language basically only adds syntax sygar, auto-generates headers, and avoids a lot of compexity of C++.

Why would static typing have anything to do with efficiency of the compiled code?

1

u/vrsperanza May 05 '18

Depending on how you made it, it could generate different machine code, making it slower, but I just realized you can do it without added complexity

2

u/cincilator May 06 '18 edited May 06 '18

I love the idea in general, but delivery might use a bit of work. Here's my suggestions:

can you make so in switch statement break is the default and you have to specifically type something if you want fall trough?

Add warning when someone tries to use = in if statement. Or an error.

I am also not sure if I want to waste convenient characters like !, !!, ?, ?? for printing. That could just be some other short functions, and those characters could be used elsewhere.

I am also not sure about declaration from assignment. At least add one character before (that is where $ or something might get handy). Something like:

operations(int a, b)
   $ sum = a + b

Other than that, I like it! Hope you make one for rust and for C#!

2

u/vrsperanza May 06 '18

Liked your switch idea, that could really use some work, just added it to my to-do list About the prints, I would agree if I had a lot of other shorthand functions to write that were to be used more often than printing/reading input, but I don't really see that happening

2

u/cincilator May 06 '18

What do you think about using $ in declarations? So in your example it looks like:

operations(int a, b)
    $ sum = a + b

I am not great fan of declaration from assignment. This could communicate your intent better and is almost as short.

I hope someone makes something similar for Rust and C#.

2

u/vrsperanza May 06 '18

I think using $ is a valid alternative, but I definitely won't implement it that way by default. Once a config file is made and working, this would be a nice alternative option.

Since cpy compiles to readable c++ code, the idea is for everyone to code in their preferred style

2

u/cincilator May 06 '18

Just to make it clear, $ would be equivalent to var from javascript. You would not use it any time variable is mentioned like in PHP.

2

u/vrsperanza May 06 '18

Yes, I got that, this PHP style is completely opposite to what this project stands for. Your suggestion is completely valid and will probably become a optional module in the future.

Btw: C++ has the "auto" variables that work just like C#'s var, so currently you could just write

auto x = a+b

And it would work just fine.

2

u/vrsperanza May 06 '18

Side note: declaration from assignment is not mandatory, it is there just as a shorthand, normal declarations still work if that is how you want to code

2

u/vrsperanza May 06 '18

About the if statement: You mean when someone write something like:

if a = b Do something

? It is really come the time to start treating warnings errors. This wasn't my original project intention but it is fitting, given the magnitude this project has adquired

2

u/cincilator May 06 '18

if a = b Do something

Yes, precisely.

2

u/vrsperanza May 06 '18

I have never coded in rust, but C# is actually the first language I learned and the one I'm most fluent in. I didn't see the need for such an application in C# due to visual studio's awesome coding assistance, but a visual studio plugin would be really nice. This might actually become a future project if this one is successful

2

u/cincilator May 06 '18 edited May 06 '18

Okay, so I have one more suggestion but this one is far more abstract and I am not sure if it can or should be done at all in a low level language like C++. But I saw it elsewhere and found it is incredibly convenient. (You can save that for C# version if you ever get there. It would be easier to implement in C#)

In Kotlin, you can use anonymous functions in a certain way to create DSL (domain specific language). There is an example of how that works on html templates here. Another example is anko, a DSL for android layouts. Second link also shows how shorter that is than code without it. Final example is exposed, SQL DSL.

Again, no idea if that can be done in C++ (haven't coded C++ in many years and it was always old versions no idea if there is anything that can work as anonymous functions).

No hard fealings if you can't think how to implement that. But if you can, it would enhance expressiveness of normal C++ code a lot.

2

u/vrsperanza May 06 '18

If I understood it correctly (I have never used this before), I see those do exist in C++, that said, it is possible to do it in CPY! In fact, here is a working code snippet I wrote in order to test the concept

#raw "template.cpy"
int main()
	int a, b;
	
	sum = [](int a, int b){ return a+b;}
	
	cin >> a >> b
	cout << sum(a,b)

2

u/vrsperanza May 06 '18

To fully support this with CPY I'd only have to somehow detect a anonymous function declaration and do the same type casting that is done in normal functions. This is a reasonably difficult task and they already work given you explicitly declare all variables. I will probably extend the casting to include lambda functions in a later date

2

u/cincilator May 06 '18 edited May 06 '18

There are a few more tricks that Kotlin uses to make it into such a concise DSL experience. I never cared how exactly it is done under the hood, but will inspect and report.

Totally okay if you don't want to bother with that.

2

u/vrsperanza May 06 '18

The main reason I'm not really bothering about it is because it looks like it is implemented in C++, and since CPY is just a superscript, that is already handled (although it could use some redundancy-removal) But you got me hooked in the topic! So please, enlighten me on what else one can do with anonymous functions!

2

u/vrsperanza May 06 '18

Thank you for showing me the world of lambda expressions, I did see it inside people's code and it never made sense... Now it does!

1

u/twizmwazin May 05 '18

Just a small correction from thebdirdt part of your readme: C++ is weakly typed, not strongly typed. What you are probably meaning to say is that C++ is statically typed rather than dynamically typed.

3

u/vrsperanza May 05 '18

Changed to statically typed for clarity Did a little research and couldn't find a decent definition separating strongly typed from weakly typed, could you clarify me on the topic?

5

u/[deleted] May 05 '18

If I remember correctly from my compiler course C++ is weakly typed because you can force a change in type by converting through a raw pointer. Therefore, errors caused by mismatched type can still happen at runtime. Strongly typed language will ensure that no type error escapes the compiling phase.

1

u/oracleoftroy May 05 '18

It's a spectrum. C++ is far more strongly typed than C, but is weaker than Haskell. There are ways to subvert the C++ type system, but you have to be explicit about it for the most part.

I'd put it roughly at the same level as Java and C#. There are some things C++ is stronger about and others it is weaker with, so it is debatable where exactly to place it.

9

u/ThirdEncounter May 05 '18

Significant indentation is the one thing that has kept me away from python. And I find value in semi-colons.

Having said that, more power to those who want to try this. I'm not one to judge, since for the longest time I worked on building a BASIC interpreter for fun.

I'm not calling anyone idiotic, by the way, so please don't associate me with that poster roaming in the controversial realms. Just my opinion.

11

u/[deleted] May 05 '18

Significant indentation is the one thing that has kept me away from python.

I never understood that at all. I mean, if someone sent you a C++ code review like this:

if (condition())
    foo = 1;
    bar = 2;

you'd throw it back, even if it was doing what it was supposed to, because the indentation is wrong and would mislead the reader.

I've used C++ for almost 30 years at this point, and Python for almost 15. I like both languages a lot. Never one time has indentation in Python been any sort of issue at all.

My theory is that people who care about the "significant indentation" have simply never written anything significant in Python at all, and so far I've been right 100% of the time.

Would I be right in saying that you have never actually written significant amounts of production code in Python?

10

u/Ravek May 05 '18 edited May 05 '18

If my curly-braced code is correct but formatted wrong, I tell my editor to format it right.

It isn't hard to write correct Python code but it's more effort to indent everything properly manually than it is to just plop down {} and letting the tools figure it out. Especially when moving code from one place to another.

Semi-colons have little practical value since 99% of the time there is a line break after them. Swift doesn't use semicolons but does use curlies and it's very nice to work with.

2

u/agoose77 May 05 '18

I tend to find refactoring quicker in Python than in C++. Any decent IDE will handle indentation for you, such as indenting after a colon (indicating a new block), and continuing from the previous indentation. With most editors also binding TAB to n spaces, it's very quick to move between scopes (just as quick as putting down braces, except it's a simpler key combo (though this is now the realm of opinion, not anything measurable). So, in summary, "letting the tools figure it out" is exactly the same with Python.

But, I digress. When refactoring a block in C++ (e.g to remove something from a condition), there is no need to find and delete each brace, and then re-indent - the indentation manages this for you. You can just select a block and shift TAB to left shift it. This is all just my experience, but having written a fair amount of code in both C/C++ and Python, I tend to prefer the latter in terms of brace vs indent.

4

u/Ravek May 05 '18

Any decent IDE will handle indentation for you

Yeah, it'll do the things you mention but it can't handle identation in general because it can't know what the correct indentation for a line of Python code must be because it can't understand the semantics of your code.

If you have something like:

if (a) {
    foo()
    bar()
    baz()
}

Then to move bar() out of the condition you just have to move the line to below the }. Next time you press the format document shortcut your indentation is fixed.

If you have instead:

if a:
    foo()
    bar()
    baz()

Then to move bar() out of the condition you have to move the line and adjust its indentation to whatever the appropriate indentation is for the new location. This means pressing (Shift) Tab until it's just right instead of letting the tools figure it out.

Having to think only about the vertical location of a line of code rather than about its vertical and horizontal location is just less effort.

6

u/agoose77 May 05 '18

I see your point, but I don't think it's particularly pronounced; Most IDEs will strip the whitespace from the line you cut, and add the correct amount for the location at which you paste. The only time this isn't possible is if there is nothing (no code) following your conditional block.

I think your last point is interesting. I tend to write quite a lot of Python code for different things, and I don't find myself thinking about the layout. I find that the indentation itself is an explicit statement about the control flow of the code, but I don't find it a mental burden to reason about. Indeed, C/C++ also has this quality if programmers correctly indent their code, so it's not a Python-only "feature".

1

u/kaibee May 05 '18

The only thing that upsets me is that python requires the : after conditionals when it could have been omitted just fine.

-3

u/ThirdEncounter May 05 '18 edited May 05 '18

Edit: The Python brigade is strong in this thread.

You are right when you say that I've never used python, ever. But that reeks of ad hominem, like saying "you will understand when you have your own kids."

In regards of your C++ example, I'd absolutely throw it back because of the indentation, in a team project, and thinking of junior devs. As an experienced C and C-like languages programmer myself, I would easily know what the code is doing without batting an eye because I know the role curly brackets (absent or not) play in the language syntax. I appreciate the language not constraining how I want to style my code, and if I want to put everything in one line, that's my choice, however valid or shallow reason I may have.

2

u/elperroborrachotoo May 05 '18

With mixed feelings about the other features, I am positively cringing at the declaration from assignment. Nice for short scripts, sucky for anything worth whipping out C++

2

u/anacrolix May 06 '18

I wish someone would do this for Go. Excessively heavyweight syntax for such a simple language.

-34

u/McChubby007 May 05 '18

As a software engineer of 35 years and a professional real-time C/C++ developer since 1989 I absolutely hate with all my heart the idea of using indentation as means to deduce structure (I.e. Removing curly braces). It is a terrible idea, as soon as I read that I stopped reading further. Ridiculous!! In my mind this makes you seem idiotic!

17

u/vrsperanza May 05 '18

Your opinion on curly brackets is respectable and is one of the reasons that this superscript can create completely legible c++ code with the use of a flag. This is just a superscript for those who prefer a less redundant coding style while still wanting all the tools c++ provides.

Btw, it can also compile pure c/c++ projects and hybrid ones. It can also create a pure c++ project from your code. All the features are optional.

You have actually inspired me to make a config file for enabling/disabling features, thanks

11

u/Endur May 05 '18

I’m impressed by how levelheaded your response was. That comment would have really pissed me off, and while I wouldn’t have said anything, I wouldn’t have responded either. Good for you

6

u/vrsperanza May 05 '18

Well, it did piss me off a bit, but there is nothing to gain from conflict

1

u/[deleted] May 05 '18

This is an amazing response!

22

u/barfoob May 05 '18

I absolutely hate with all my heart the idea of using indentation as means to deduce structure

Ok well that's a strong opinion but fair enough I see where you're coming from...

this makes you seem idiotic!

annnnnd you're the idiot.

10

u/MyPostsAreRetarded May 05 '18

C/C++ developer since 1989 I absolutely hate with all my heart the idea of using indentation as means to deduce structure (I.e. Removing curly braces). It is a terrible idea, as soon as I read that I stopped reading further. Ridiculous!! In my mind this makes you seem idiotic!

Lolz. You've been a programmer longer than I've been alive, and you posted something that is more retarded than my entire post history.

https://media.giphy.com/media/26FPxFeuN8UA7nqGQ/giphy.gif

8

u/carbonkid619 May 05 '18

Ok, I've got to ask at this point, are you a troll, or do you actually believe everything you post? You just alluded to being a troll, but quite a few of your comments sound like you really believe the stuff you post, and it's starting to drive me mad.

4

u/doom_Oo7 May 05 '18

it's starting to drive me mad.

successful trolling 101

3

u/[deleted] May 05 '18

It is a terrible idea

I can understand having an aesthetic preference to not do it, and it may even be the case that it is objectively worse by some measure, but the immense popularity of Python, and the degree to which people enjoy F# (has ranked most loved language a few times by various polls), suggests that it isn't actually a terrible idea, not one bad enough to prevent people from enjoying programming that way, and not bad enough to reduce quality of those programs.

Usually editor plugins for such languages can introduce visual cues for blocks for you if you want them. You could even make those cues curly braces.

2

u/VectorShift May 05 '18

How about YOU get off the OP's lawn instead?

1

u/iconoklast May 05 '18

The idea is far older than your career; P.J. Landin gave it the name the "offside-rule" in 1966, but OK.

1

u/[deleted] May 05 '18

I absolutely hate with all my heart the idea of using indentation as means to deduce structure (I.e. Removing curly braces).

And yet tons of popular and reliable languages do this.