Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use terralang to realize terrapp

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use terralang to achieve terrapp", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use terralang to achieve terrapp" bar!

Keywords in this paper: invent your own language, lua extensible language system, use library invention language vs to invent language, is toolchain language and app production language, all ecological language

Throughout the xaas series, we compiled and assembled our own dbcolinux, and now we are going to invent our own language. if the linux ecology allows us to customize a perfect OS, then terralang is similarly a tool for customizing our language system, and one of its design purposes is this, and it has been specially designed to de-complicate it. We can invent our own simple language system in copy-paste style with a small amount of knowledge and practice.

The language system we are going to customize is cpp, and in the previous article on terralang, "terracling" and "terra++", we have mentioned this purpose many times, and now we are going to use the minimum core of terra to implement a really usable cpp-- in fact, terra is already a cpp, but it is still a bit different from the real available cpp, because it only defines the core required for the minimum language extension (we'll talk about its orthogonal types and statement design later). So strictly speaking, it is just a cplus, and now we have to build a website close to the standard CPP dsl for terralang,terralang.org on this basis, there are also a lot of related materials specifically mentioned this special topic, which has become the starting point of all our just enough.

Connect with lua, more like c, the language of design language

Before we do that, we need to understand the underlying reasons for this ability of terralang as a meta-language tool. As we did in the previous article, we refer to terralang as the whole terralangsys, while terra is a single language:

First of all, terra should be called terrac (Terra is c-series, terra is c extender towards Lua), but it is more suitable to be called terra/lua, which is why, because in terralangsys, terra is designed to be mixed with lua, so whether terra is the language for inventing the language or for everyday use. Both, and even terra can certainly be used as an independent language (A scripting-language with high-performance extensions.An embedded JIT-compiler for building languages.A stand-alone low-level language....), but for the most part, it is used in terra/lua. The under-input terra---terralang in lua is mainly used to metaprogramming or write dsl. Strictly speaking, there are only two languages in terralang: terra,lua.

There are two independent languages in terralang. Their runtime coexists and can be used offline. Terra compiler is part of the Lua runtime,Terra executes in a separate environment: Terra code runs independently of the Lua runtime. The run time of lua corresponds to the compile time of terra, while the run time of terra is OS, and the whole terralangsys is not bound.

But in terms of syntax and use, terra/lua is one. Even closely related, like a language, that is, terralang.

Terra is used to replace and improve C, it has its own type system (Terra's types are similar to C's), code system, and all that a language should have. Originally, if C had everything that terra needed to interact with lua, then C would be terra directly, but because it didn't, the enhancement to C evolved into terra,--. We call it cplus, and in terra, everyday programming is mixed with lua/terra. So in fact, it can be regarded as lua/terra+cplus mixed compilation, on the other hand, terra is also an extension of lua, Terra expressions are an extension of the Lua language. The Terra C API extends Lua's API with a set of Terra-specific functions, in a word, terra makes lua like c, c like lua

So why are you doing this? Because terralang is designed to interact well with C and lua, inter-operate, it must be very simple, from the very beginning, consider binding-free interaction, luajit has ffi can interact with C in a simple way, in fact, luajit ffi for C encapsulation has been close to terra, terra is also using its approach, since Lua/c can already be, why to give a terra/lua, this is because it is far from enough. For example, it lacks metaprogramming and multiple stage programming, so terra enhances and adds these. What makes it look like C on the one hand and connect to lua on the other?

What are the principles and principles of its implementation? It is true that a multilingual system is likely to be a much better solution, but terralang has only chosen what is right for it.

It implements a preprocessor, which separates the design period from the execution period, Separating compile-time and runtime-time environments. In these two phases, the units that interact with each other are very small-- a terra function or type, which makes the language system efficient, but both phases have a complete language system, but can undertake and serve the integrated results. The process goes like this:

In the design and compilation period, The preprocessor parses the text, building an AST for each Terra function. It then replaces the Terra function text with a call to specialize the Terra function in the local environment. This constructor takes as arguments the parsed AST, as well as a Lua closure that captures the local lexical environment. So that syntactically, terra/lua is integrated. The written .t program will contain multilingual source code, but at compile time shared lexical scope and different execution environments because of lexical scope, the types of both sides is visible to each other. Transform each other at any time. This simplification is deliberate: based on lua ffi, it makes all language elements from terra exist as values of lua, simplifying all conversation in each phase of the two languages, which we'll talk about later.

During the run time, it will call into an internal library that actually constructs and returns the specialized Terra function. The preprocessed code is then passed to the Lua interpreter to load. Terra code is compiled when a Terra function is typechecked the first time it is run. We use LLVM to compile Terra code since it can JIT-compile its intermediate representation directly to machine code. Clang is used to compile the C code into LLVM and generate Terra function wrappers that will invoke the C code when called.-- ends up at the bottom, and combined terra/lua will be a mixture of lua and native code, without any components of terra. Compiler, generated code, and runtime of a DSL. In the end, all three are connected to form an integrated language.

The effect of this effort is that, compared with other multilingual systems, as a model of multilingual binding-free interaction, Terra and Lua represent one possible design for a two-language system. Other meta-languages, such as py's meta object, do not use a language control language, but integrate it all into a single language, which is not flexible or powerful enough-only a single compiled language and a scripting language can naturally have the advantages of multiple languages. For example, llvm can generate performance critical parts into machine code to guarantee performance. There are also .net clr encapsulated unmanaged modules and so on. All without natural defects.

Here are some of the things that happen to be interoperable type orthogonal systems, metaprogramming capabilities, and multiple stage- > DSL are causal paths. The first is its type system.

Type and sentence conversion, orthonormal type conversion design, type-directed staging approach, and shared lexical domain

The interaction between two languages depends on whether the specific language type is typechecking attribute, scope, lifetime, memory layout and so on.

In the ffi era of luajit, there is this idea, which is also the original intention of lua design-lua is designed to be orthogonal to c, user defined struct can be a piece of C, since there is table, why not directly use it to express objects, there are functions, why can not use it to express other, this is orthogonal. Terra is also orthogonal for c-type design, so it is a language that happens to be a design language. No more, no less, just orthogonal. For example, its type corresponds to those of C #, for example, it also has pointer,struct- and it also adds more metaprogramming stage programming-oriented aspects. For example, built-in tables that make it easy to manage structures like ASTs and graphs, which are frequently used in DSL transformations.

This type is used for metaprogramming (as for whether or not to write DSL is another matter), Terra's type system is a form of meta-object protocol, instead of directly designing types for app production, terralang types are designed to be used naturally in a staged compilation process, which is ecotype, which allows programmers-- mainly metaprogrammers or DSL inventors-- before the type is materialized. Interfere with the type of memory layout behavior and other operations

First of all, let's take a look at the two languages and each issue of typechecking. We know that terra is needed, lua is not needed, in order to ensure language integration. You need to coordinate-between compiler, generated code, and and runtime of a DSL about the problems that types may bring. In Terra (and reflected in Terra Core), we perform specialization eagerly (as soon as a Terra function or quotation is defined), while we perform typechecking and linking lazily (only when a function is called, or is referred to by another function being called). In this way, conflicts can be avoided.

Therefore, the compilation and run-time separation of terra and the type system of orthogonal design are all for simplification and abnormal simplification, only to make the invention of DSL a little easier. The last big thing is that this is the shared word domain, which is another aspect of the simplification of getting terra to write dsl perverts.

Let's talk about the shared lexical scope, which eventually causes the separated things to form a terralang at the grammatical level. Removes the need to place operators (quotation,escape,etc..) in traditional stage programming. Here, we need to combine terralang's code generation mechanism with metaprogramming mechanism. We haven't talked about terralang's code generation mechanism. The above is only a rough mention. All in all, from syntax to the final language form, the two coexisting languages have to deal with problems that can not be bypassed (between compiler, generated code, and runtime of a DSL) scope on the one hand. The shared lexical environment makes it possible to organize Terra functions in the Lua environment, and refer to them directly from Terra code without explicit escape expressions. It even removes the need for namespace calls. To further reduce the need for escape expressions, we also treat lookups into nested Lua tables of the form x.id1.id2...idn (where id1...idn are valid entries in nested Lua tables) as if they were escaped. This syntactic sugar allows Terra code to refer to functions organized into Lua tables (e.g.std.malloc), removing the need for an explicit namespace mechanism in Terra.

For scopes, orthogonality is maintained at all stages and shapes of the staging and transparently across the lua,terra at the same time. In this way, all the contradictions about types mentioned at the beginning of this section are resolved. And we should always keep in mind that abnormal simplification is the principle of maintain from the beginning of design.

Here is a comprehensive description of the principles of metaprogramming to DSL.

Generative metaprogramming, and DSL invention

In fact, metaprogramming is not necessarily DSL, we can only use terralang as meta programming. This is also the scenario where we use terralang in most cases. The metaprogramming in the title of this section can be put in the first mark, but the meta-programmer of terralang is more suitable for writing DSL, and it is more beneficial to combine with the latter.

As mentioned earlier, in fact, no matter which language, writing code is an extension language to write "DSL", and the library is also an extension of the language. -either problem-oriented or extension language itself or APP-oriented, but terralang makes this kind of DSL realistic.

Multiple languages have a variety of meta-code generation methods, CPP is template-based-it is a compile-time type specialization process, and cross-language meta-programming, this is stage programming, in the stage programming model, there are some generation operators, which are also mentioned in the meta-programming techniques that describe terralang.

The ability to generate code for dynamic runtimes. This is equivalent to a multilingual version of cpp template programming. Although terralang actually uses llvm, rather than the same Turing complete CPP implementation based on template technology. But the two are very similar, and the two processes can be compared.

The previous type orthogonal design and shared lexical domains have done most of the work for terra. Ease the complexity here may only be about DSL itself. We know that when it comes to the principle of compilation, it is very complicated, and the complexity here is almost irreducible, because it involves system programming such as data structure, lexical analysis, submission, etc., but terralang also has its own scheme, which uses a method called Pratt Parsers, What makes this parsing technique interesting is that the syntax is defined in terms of tokens instead of grammar rules, plus terralang will also provide some language-making API. It greatly simplifies the difficulty of inventing DSL. These inventions of dsl exist as modules of terra. Not into terra, but into lua.

That is to say, extending DSL for terralang is also simple, completely eliminating the need for specialized language facilities such as lexical parsers. Although it uses compilation principles, it uses a more limited and simpler limited set, because the only difference between all languages is the entry table for terralang.

Terralang's DSL capability is particularly valuable because it allows anyone to invent a language, and the terra extension language is designed to be open to anyone to use.

-

Terra is a best practice for adding a scripting to the c-series. It transforms c into Lua like and lua/terra into c like scripting. Of course, its main function is that after the invention of DSL,--, we can develop cpp on this c/terra. In addition to having a full-state and all-inclusive language system, we can even have a high-performance library Terra's type reflection allows the creation of class-systems as libraries.

Terra,the programmable cp with Lua,just like programmable nginx with Lua, with terralang, there has been any language ever since. Terralang is both an app production language and a toolchain language. Or shell language, makefile language, etc.

Thank you for your reading, the above is the content of "how to use terralang to achieve terrapp". After the study of this article, I believe you have a deeper understanding of how to use terralang to achieve terrapp, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report