📜 ⬆️ ⬇️

“OOC for C is like Scala for Java”

Today on Hacker News I came across a post about a (it seems very) new language " ooc ". He threw all the things for a day, started picking up - it looks painfully interesting.

So, " ooc is a modern, object-oriented, functionally (cotton) , high-level, low-level, sexy programming language. " ( This is what sexy developers represent it, well, I would add that it is also " compiled ")

Essentially, ooc is a high-level language translator in C with a garbage collector.
')
As one of the site visitors put it: " ooc for C is like Scala for Java " (jimbokun @ HN ).

Mandatory "Hello, World" on ooc:

hello.ooc :
"Hi, softer world =)" println()

output hello.c and hello.exe .

The ooc compiler translates this into c-code and uses the available c compiler (gcc, mingw, icc, tinycc) to make this hello.exe (or ./hello). Support for tinycc is claimed, but in reality we are waiting for support for C99 at tinycc itself (the author promised to make workarounds, but it is not working yet), so we are still rather weak in size .exe's (gcc from mingw does not do others).

So, what we already have in the language : classes, objects, strong typing, guessing types ( n := "Beer" length() ), chaining ( .dothis().dothat() ), compatibility with c-libraries, abstract classes, templates from C ++, lists, sparse lists, hashes (ArrayList, SparseList, HashMap), for (i in list) { ... } , wrappers for c-shnyh functions and classes, operator overloading, polymorphism, collector garbage (switchable), import file / name (not needed .h, but .h can be used as extern), closures, eiffel's contracts, exceptions, reflections, pattern matching, ranges, ternary operator, bindings, generics, coroutines, GTK, SDL , OpenGL, GLU, GLUT bindings ...

As comrade varjag @ HN said: “I think this is a joke made by Ruby users, but I don’t see it anywhere in the text.” No, this language actually exists. Examples, links, installation, underwater snags - under the cut ...

More examples

Functions :
add: func(a, b : Int) -> Int {
a + b
}

Pleasant for :
for (i in 0..10) {
printf( "%d\n" , i)
}

for (i in list) {
printf( "%d\n" , i)
}

Lists :
ints := SparseList<Int> new ()
ints add(13)
lucky := ints get(0)

OOP :
Vector3f: class {
x, y, z : Float
init: func(=x, =y, =z)
}

ooc knows that x is a float, so the type can not be specified, as well as the fact that it is stored in this x (in “ooc”, the points before the object's methods and properties are not needed). Analog to the above in C ++ :
class Vector3f {
float x,y,z;
Vector3f( float x_, float y_, float z_): x(x_), y(y_), z(z_) {};
};
// ( C++ - ).

Chaining :
me := RandomGuy new ()
me eatBreakfast() .drinkCoffee() .yawn() .goBackToBed()

but you do not need to return this in these methods.

Abstract classes :
Animal: abstract class {
shout: abstract func
}

Dog: class extends Animal {
shout: func { "Woof, woof!" println() }
}

Cat: class extends Animal {
shout: func { "Meoooww!" println() }
}

malloc with garbage collector :
myRawArray := gc_malloc(Int size * 100) as Int* // free .

GUI application :
use gtk
import gtk/[Gtk, Window] // acts like: import gtk/Gtk, gtk/Window
exit: extern func

main: func {
w := Window new ( "Hi, world" )
w setUSize(800, 600) .connect( "destroy" , exit) .showAll()
Gtk main()
}

Reading file
import io/FileReader

main: func {
fr := FileReader new ( "/etc/hosts" )

while (fr hasNext())
fr read() print()

}

coroutines (!) (transfer of code execution from one subroutine to another without stack overflow):
import coroutine/Coro
firstCoro, secondCoro : Coro

secondTask: func (context: Pointer) {
num := 0
printf( "secondTask created with value %d\n" , (context as Int*) @)
while ( true ) {
printf( "secondTask: %d %d\n" , secondCoro bytesLeftOnStack(), num += 1)
secondCoro switchTo(firstCoro)
}
}

firstTask: func (context: Pointer) {
value := 2
num := 0
printf( "firstTask created with value %d\n" , (context as Int*) @)
secondCoro = Coro new ()
firstCoro startCoro(secondCoro, value &, secondTask)
while ( true ) {
printf( "firstTask: %d %d\n" , firstCoro bytesLeftOnStack(), num += 1)
firstCoro switchTo(secondCoro)
}
}

main: func {
mainCoro := Coro new ()
value := 1
mainCoro initializeMainCoro()
firstCoro = Coro new ()
mainCoro startCoro(firstCoro, value &, firstTask)
}


From what was found in the source code / on the blog: atexit, iniparser, filewriter, list, map, stack, os (launch, subprocess), regexp, threads, i / o, events .

GTK / SDL / OpenGL / GLU / GLUT bindings.

So, the language is really really sexy. Unfortunately, of course, it’s still raw. Many mistakes and problems, but if you like to live on the edge - welcome to the world of hacking.

Ooc installation

What is needed?

1. git (needed to download the distribution)
2. Java JDK 5 or higher
3. gcc under Linux or MingW under Windows (link does not lead to mingw.org , but to a more modern build, although the real mingw can now caught up with gcc 4)
4. Ant (build system, type "make")

JAVA ??? The compiled file does not use Java VM . Only the ooc-> c translator itself uses Java (transparently for you). The end user receives native code — in other words, .exe, which does not require Java in any way.

We put all of the above, under Windows we add the git , mingw / bin , java / bin , ant / bin folders in the PATH (in Linux after yum install / sudo apt-get, everything will be self-configured).

Check for correct PATH: run the commands “git”, “javac”, “ant”, “gcc” in the command line. If something is not found (“the path or file name is incorrectly specified”), then we are looking for where this particular was unpacked and add the path to it to the PATH.

KO: PATH is specified in Windows + Break, Advanced, Environment Variables, in the lower window, Change . After each change, you will need to click OK and restart the command line. Separators; (semicolon).

Go to the command line and continue to do:

git clone git: //github.com/nddrylliog/ooc.git
cd ooc/
ant

If everything is successful, we find where the ooc.jar file is ooc.jar - this is roughly the ooc/bin/ooc.jar .

Make (under Windows) the ooc.bat file and put it somewhere in the PATH:

java -jar d:\ooc\bin\ooc.jar %*
(here we write our way to our ooc.jar - see above)

under Linux we do:

sudo cp utils/ooc /usr/bin/
export OOC_DIST=/home/username/ooc/
if downloaded to / home / username / ooc /

Making hello.ooc :
main: func {
"Hello world!" println()
}

now we do:
ooc hello.ooc -r
and ideally we get hello.exe. ("-r" is "run", you can still "-noclean" - do not delete the generated .c sources).

Well, does it work?

... but in reality - the language is still raw. Seriously raw, so we probably get some kind of error and go to irc.freenode.com channel # ooc-lang. Comrade "nddryliog" - the author of the language. Ask questions, get answers. (I sometimes appear there as "slavav").

And by the way, the language is developing, the guys-authors are not averse to discussing even changes or innovations in the syntax of the language on the channel (they themselves write - such as " suggest innovations to be introduced into the language , we will think how to do"). Plus, there is no big organization or committee behind the language, so everything develops quickly and even your personal opinion can be taken into account.

In fact, I managed to get hello.exe, but with FileReader, for example, under Windows, the process deafly hung up. But developers are working, looking for, finish. Channel # ooc-lang is very friendly and people really help with problems in real time (especially if nddryliog is online).

On the other hand, there is a Rock - ooc compiler written on ooc itself. So I do not think that everything is so bad. It's just that Linux authors and they test more under it, so compatibility with Windows is still less than great.

Yes, if the compilation slows down - disable antiviruses. My hello.exe compiled for 30 seconds, with the antivirus turned off (Comodo) for 3 seconds.

If the channel helps you - you can update the language version with the help of: " git pull && ant clean && ant ".

More on aglitsky:
Homepage, examples
Ooc installation
Project blog (must read!)
Discussion on Hacker News (questions and answers)
docs.ooc-lang.org is useful documentation, although a little, for example, about how to make c code accessible from ooc .
Bindings for GTK, GL, etc. + Rock - The experimental ooc compiler written in ooc itself.

Bindings are downloaded and it is experimentally determined to unpack them in src/sdk relative to the ooc folder. (Through FileMon I looked where ooc is looking for them, not a word about it on the site).

License

By the way, the ooc code is distributed under the BSD license , which in Russian means: “you can download, use , you can sing blues and revenge the yard, just not dust! You can sell , buy, no need to open any source code . If you promise not to sue - you can print the code and put the sheet in the nose. Only indicate the author . ”(For clarity, the author of the language is not me, I have nothing to do with the language, I just share a promising finding with the community).

Ooc. We'll run out of slogans

The slogans on the main project are funny - they change when you refresh. Among them, "Y2k compatible" :), "Your insurance against boredom", "Object-oriented, with a subtlety", "Handmade hacking", "Error perfected" and "We will run out of slogans, sooner or later" ...

Documentation

ooc-lang.org - Created On: 30-May-2009 , so disassemble while hot. (In general, the language - 5 months)

Here, yes, trouble. The authors themselves acknowledge this, so that everything you figure out as you go along - try to publish - help the following.

Another underwater snag is literally the other day, the git repository changed its location, so you can find links to github.com that don't exist, in this case replace “ github.com amoswenger / ...” in the URL with -> “ github .com nddrylliog / ... ".

Source code was highlighted with Source Code Highlighter .


Yoi Haji
view from Habra

Source: https://habr.com/ru/post/72538/


All Articles