Skip to content
Li, Xizhi edited this page Aug 6, 2017 · 19 revisions

What is NPL?

NPL is short for neural parallel language.

  • NPL is a general-purpose open-source language. Its syntax is 100% compatible with Lua.
  • NPLRuntime includes a built-in full-fledged 3D computer game engine, called ParaEngine. It provides essential functionality for building 3D/2D/Web/Server applications.
  • NPL provides rich C/C++ API and large collections of libraries written in NPL.
  • NPL is a single language solution for advanced and interactive GUI, complex opengl/DirectX 3D graphics, scalable webserver, fast database and distributed software frameworks. It is cross-platform, high performance, extensible and debuggable.
  • NPL is a language initially designed in 2004 to simulate the brain. File nodes and connections are ubiquitous; programmers no longer needs to touch threading or low-level networking logics to write complex networked applications.
  • NPL can mix user-mode preemptive and non-preemptive code, a single thread can simulate tens of thousands of virtual concurrent code, like concurrency of Erlang.
  • NPL is a weakly-typed scripting language, yet with approaching C/C++ speed thanks to luajit, and can call C/C++ functions with almost zero cost with ffi or file activation.

See here for a list of projects written in NPL.

Facts You Should Know

  • NPL runtime is written in C/C++. It can utilize the luajit compiler, which dynamically compiles script/bytecode into native machine code, thus it can approach native C/C++ performance with some caveats.
  • NPL runtime native API is a rich collection of C/C++ functions that is callable from NPL script. It includes networking, graphics, io, audio, assets management, and core NPL/ParaEngine infrastructure. NPL Reference contains its full documentation. However, 99% of the time, we never use native API directly in NPL script.
  • Instead, we use NPL libraries, which are pure NPL/Lua script files, distributed open sourced or in a zip/pkg file. Over 1 million lines of fully-documented NPL script code are available for use. Try to search in the repository of NPL source code, before writing your own.
  • NPL libraries are all written in object oriented fashion, code is organized in folders and table namespaces.
  • Because NPL is written in C/C++, it is cross-platform, extensible and easy to integrate with other thirty-party tools. For examples, NPLRuntime is distributed with following built-in plugins: bullet(a robust 3d physics engine), mono(C# scripting module with NPL API), mysql/sqlite(database engine), libcurl(a robust http/ssh client).
  • There are two ways to use Lua, one is embedding, the other is extending it. NPL takes the former approach. For example, true preemptive programming would NOT be possible with the second approach. The embedding approach also allows NPL to expose all of its API and language features to other compilers, for example, each NPL runtime states can host Lua/Mono C#/C++ compiler states altogether.

Hello World in NPL

Run in command line npl helloworld.lua. The content of helloworld.lua is like this:

print("hello world");

Now, there comes a more complicated helloworld. It turns an ordinary helloworld.lua file into a neuron file, by associating an activate function with it. The file is then callable from any NPL thread or remote computer by its NPL address(url).

NPL.activate("(gl)helloworld.lua", {data="hello world!"})
local function activate()
   local msg = msg;
   if(msg) then
      print(msg.data or "");
   end
end
NPL.this(activate);

If your code has *.npl file extension, you can use or extend NPL syntax via meta programming. For example, above code can be written with NPL syntax in helloworld.npl as below.

NPL.activate("(gl)helloworld.npl", {data="hello world!"})
this(msg){
   if(msg) then
      print(msg.data or "");
   end
}

Why Should I Use NPL?

You may write your next big project in NPL if you meet any following condition, if not all:

  • If you like truly dynamic language like lua.
  • If you care about C/C++ compatibility and performance.
  • Coming from the C/C++ world, yet looking for a replacement language for Java/C#, that can handle heavy loads of 2d/3d graphical client or server applications, easier than C/C++.
  • Looking for a scripting language for both client and server development that is small and easy to deploy on Windows and other platforms.
  • Looking for a dynamic language that can be just-in-time compiled and modified at runtime.
  • Mix user-mode preemptive and non-preemptive code for massive concurrent computation.

Note: calling API between C++/scripting runtime, usually requires a context switch which is computationally expensive most language also involves type translation (such as string parameter), managed/unmanaged environment transition (garbage collection), etc. NPL/LUA has the smallest cost among all languages, and with luajit, type translation is even unnecessary. Thus making it ideal to choose NPL when some part of your responsive app needs to be developed in C/C++.

Background

NPL prototype was designed in 2004, which was then called 'parallel oriented language'. In 2005, it was implemented together with ParaEngine, a distributed 3d computer game engine.

Why a New Programming Language?

NPL is initially designed to write flexible algorithms that work in a multi-threaded and distributed environment with many computers across the network. More specifically, I want to have a language that is suitable for writing neural network algorithms, 3d simulation, and visualization. Lua and C/C++ affinity was chosen from the beginning.

Communicate Like The Brain

Although we are still unclear about our brain's exact computational model, however, following fact seems ubiquitous in our brain.

  • The brain consists of neurons and connections.
  • Data flows from one neuron to another: it is asynchronous, single-direction and without callback. Communication in NPL is the same as above. Each file can become a neuron file to receive messages from other neuron files. They communicate asynchronously without callback. As a result, no lock is required because there is no shared data; making it both simple and fast to write and test distributed algorithms and deploy software in a heterogeneous environment.

Compare NPL With Other Languages

  • C/C++:
    • Pros: They are actually the only popular cross-platform language. NPL Runtime is also written in C++.
    • Cons: But you really need a more flexible scripting language which is dynamically compiled and easier to use.
  • Mono C#, Java:
    • Pros: Suitable for server side or heavy client. Rich libraries.
    • Cons: Deployment on Windows platform is big. Writing C++ plugins is hard, calling into C++ functions is usually slow. Language is strong-typed, not really dynamic.
  • Node.js, Electron:
    • Pros: Suitable for server side or heavy client. HTML5/javascript is popular.
    • Cons: Deployment on the client-side is very big. writing C++ plugins is very hard.
  • Python, Ruby, ActionScript and many others:
    • Pros: Popular and modular
    • Cons: Performance is not comparable to C/C++; standard-alone client-side deployment is very big; syntax too strict for a scripting language. Some of them look alien for C/C++ developers.
  • Lua:
    • Pros: NPL is 100% compatible with it. Really dynamic language, highly extensible by C/C++, and syntax is clear for C/C++ developers.
    • Cons: Born as an embedded language, it does not have a standalone general-purpose runtime with native API support and rich libraries for complex client/server side programming. This is where NPL fits in.
  • Concurrency Model: in-depth compare with Erlang, GO, scala

References

Click the links, if you like to read the old history:

Clone this wiki locally