Ruby 语言工具包
RLTK is a collection of classes and methods designed to help programmers work with languages in an easy to use and straightforward manner. This toolkit provides the following features:
In addition, RLTK includes several ready-made lexers and parsers and a Turing-complete language called Kazoo for use in your code and as examples for how to use the toolkit.
Here are some reasons to use RLTK to build your lexers, parsers, and abstract syntax trees, as well as generating LLVM IR and native object files:
Lexer and Parser Definitions in Ruby - Many tools require you to write your lexer/parser definitions in their own format, which is then processed and used to generate Ruby code. RLTK lexers/parsers are written entirely in Ruby and use syntax you are already familiar with.
Re-entrant Code - The lexers and parsers generated by RLTK are fully re-entrant.
Multiple Lexers and Parsers - You can define as many lexers and parses as you want, and instantiate as many of them as you need.
Token Positions - Detailed information about a token's position is available in the parser.
Feature Rich Lexing and Parsing - Often, lexer and parser generators will try and force you to do everything their way. RLTK gives you more flexibility with features such as states and flags for lexers, and argument arrays for parsers. What's more, these features actually work (I'm looking at you REX).
LALR(1)/GLR Parsing - RLTK parsers use the LALR(1)/GLR parsing algorithms, which means you get both speed and the ability to handle any context-free grammar.
Parser Serialization - RLTK parsers can be serialized and saved after they are generated for faster loading the next time they are required.
Error Productions - RLTK parsers can use error productions to recover from, and report on, errors.
Fast Prototyping - If you need to change your lexer/parser you don't have to re-run the lexer and parser generation tools, simply make the changes and be on your way.
Parse Tree Graphs - RLTK parsers can print parse trees (in the DOT language) of accepted strings.
LLVM Bindings - RLTK provides wrappers for most of the C LLVM bindings.
The Contractor - LLVM's method of building instructions is a bit cumbersome, and is very imperative in style. RLTK provides the Contractor class to make things easier.
Documentation - We have it!
I Eat My Own Dog Food - I'm using RLTK for my own projects so if there is a bug I'll most likely be the first one to know.
To create your own lexer using RLTK you simply need to subclass the {RLTK::Lexer} class and define the rules that will be used for matching text and generating tokens. Here we see a simple lexer for a calculator:
…Ruby class PrefixCalc then_val, new_else_bb => else_val}, 'iftmp' }
build(start_bb) { cond cond_val, then_bb, else_bb }
build(new_then_bb) { br merge_bb } build(new_else_bb) { br merge_bb }
returning(phi_inst) { target merge_bb } end
More extensive examples of how to use the Contractor class can be found in the Kazoo tutorial chapters.
### Execution Engines
Once you have generated your code you may want to run it. RLTK provides bindings to both the LLVM interpreter and JIT compiler to help you do just that. Creating a JIT compiler is pretty simple.
```Ruby
mod = RLTK::CG::Module.new('my module')
jit = RLTK::CG::JITCompiler(mod)
mod.functions.add('add', RLTK:CG::NativeIntType, [RLTK::CG::NativeIntType, RLTK::CG::NativeIntType]) do
blocks.append('entry', nil, nil, self) do |fun|
ret add(fun.params[0], fun.params[1])
end
endNow you can run your 'add' function like this:
jit.run(fun, 1, 2)
…
def fib(x)
if x < 3 then
1
else
fib(x-1) + fib(x-2)The tutorial is organized as follows:
Before starting this tutorial you should know about regular expressions, the basic ideas behind lexing and parsing, and be able to read context-free grammar (CFG) definitions. By the end of this tutorial we will have written 372 lines of source code and have a JIT compiler for a Turing complete language.
The following lexer and parser classes are included as part of RLTK:
If you are interested in contributing to RLTK there are many aspects of the library that you can work on. A detailed TODO list can be found here. If you are looking for smaller units of work feel free to:
Lastly, I love hearing back from users. If you find any part of the documentation unclear or incomplete let me know. It is also helpful to me to know how people are using the library, so if you are using RLTK in your project send me an email. This lets me know what features are being used and where I should focus my development efforts.
Aaaaand we're back. Development of RLTK has been on hold for a while as I worked on other projects. If you want to see what I've been up to, you can check out Clang's new -Wconsumed flag and the Filigree gem.
The next version of RLTK is going to be updated to require Ruby 2.0 as well as LLVM 3.4. Previous versions of RLTK required my LLVM-ECB libarary to expose extra LLVM features through the C bindings; this is no longer necessary as this functionality has been moved into LLVM proper. If anyone has any requests for new or improved features for RLTK version 3.0, let me know.
暂无开放 Issues,或尚未同步最近议题。